entity renderer: use camera world offset

This commit is contained in:
Bixilon 2023-05-23 20:42:32 +02:00
parent 544b3a3407
commit cb121b115b
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 56 additions and 15 deletions

View File

@ -38,7 +38,7 @@ class WorldOffset(private val camera: Camera) : Drawable {
companion object {
const val MAX_DISTANCE = (World.MAX_RENDER_DISTANCE * 2) * ProtocolDefinition.SECTION_WIDTH_X // coordinates higher than that value are not allowed
const val MAX_DISTANCE = World.MAX_RENDER_DISTANCE * ProtocolDefinition.SECTION_WIDTH_X // coordinates higher than that value are not allowed
const val MIN_DISTANCE = MAX_DISTANCE - (World.MAX_RENDER_DISTANCE / 4) * ProtocolDefinition.SECTION_WIDTH_X // only if value is lower that that value coordinates will be back offset
}
}

View File

@ -61,6 +61,8 @@ class EntityRenderer(
var visibleCount: Int = 0
private set
private var reset = false
override fun init(latch: CountUpAndDownLatch) {
connection.events.listen<EntitySpawnEvent> { event ->
if (event.entity is LocalPlayerEntity) return@listen
@ -75,6 +77,7 @@ class EntityRenderer(
}
profile.hitbox::enabled.observe(this) { this.hitboxes = it }
context.camera.offset::offset.observe(this) { reset = true }
context.inputHandler.registerKeyCallback(
HITBOX_TOGGLE_KEY_COMBINATION,
@ -104,8 +107,12 @@ class EntityRenderer(
override fun prePrepareDraw() {
val count = AtomicInteger()
val reset = reset
runAsync {
it.entity.draw(millis())
if (reset) {
it.reset()
}
it.update = it.checkUpdate()
it.prepareAsync()
if (it.visible) {
@ -113,6 +120,9 @@ class EntityRenderer(
}
}
this.visibleCount = count.get()
if (reset) {
this.reset = false
}
}
override fun postPrepareDraw() {

View File

@ -23,4 +23,6 @@ interface ModelUpdater : Drawable {
fun prepare() = Unit
fun unload() = Unit
fun reset() = Unit
}

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.rendering.entity.hitbox
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kotlinglm.vec3.Vec3d
import de.bixilon.minosoft.data.entities.entities.LivingEntity
import de.bixilon.minosoft.data.registries.shapes.aabb.AABB
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
@ -31,7 +32,6 @@ class EntityHitbox(
private var aabb: AABB = model.aabb
private var data: HitboxData? = null
private var update = true
var enabled: Boolean = true
get() = field && model.renderer.hitboxes
@ -52,7 +52,6 @@ class EntityHitbox(
}
this.data = data
this.aabb = aabb
update = true
return true
}
@ -80,18 +79,21 @@ class EntityHitbox(
} else {
mesh.drawAABB(aabb = shrunk, color = data.color, margin = 0.1f)
}
val center = Vec3(shrunk.center)
val offset = model.context.camera.offset.offset
val center = Vec3(shrunk.center - offset)
data.velocity?.let { mesh.drawLine(center, center + Vec3(it) * 3, color = ChatColors.YELLOW) }
val eyeHeight = shrunk.min.y + model.entity.eyeHeight
val eyeAABB = AABB(Vec3(shrunk.min.x, eyeHeight, shrunk.min.z), Vec3(shrunk.max.x, eyeHeight, shrunk.max.z)).hShrink(RenderConstants.DEFAULT_LINE_WIDTH)
val eyeAABB = AABB(Vec3d(shrunk.min.x, eyeHeight, shrunk.min.z), Vec3d(shrunk.max.x, eyeHeight, shrunk.max.z)).hShrink(-RenderConstants.DEFAULT_LINE_WIDTH)
mesh.drawAABB(eyeAABB, RenderConstants.DEFAULT_LINE_WIDTH, ChatColors.DARK_RED)
val eyeStart = Vec3(center.x, eyeHeight, center.z)
val eyeStart = Vec3(center.x, eyeHeight - offset.y, center.z)
mesh.drawLine(eyeStart, eyeStart + Vec3(data.rotation.front) * 5.0f, color = ChatColors.BLUE)
mesh.drawLine(eyeStart, eyeStart + data.rotation.front * 5.0f, color = ChatColors.BLUE)
this.mesh = mesh
}

View File

@ -47,6 +47,11 @@ abstract class EntityModel<E : Entity>(
return update
}
override fun reset() {
update = true
hitbox.reset()
}
override fun prepareAsync() {
if (!update) {
return

View File

@ -77,6 +77,10 @@ class CloudLayer(
if (offset.y != 0) pushZ(offset.y == 1)
}
fun reset() {
}
private fun reset(cloudPosition: Vec2i) {
for (array in arrays.unsafeCast<Array<CloudArray?>>()) {
array?.unload()
@ -92,9 +96,13 @@ class CloudLayer(
return this shr 4
}
private fun updatePosition() {
private fun calculateCloudPosition(): Vec2i {
val offset = this.offset.toInt()
val position = clouds.connection.player.physics.positionInfo.chunkPosition + Vec2i(offset / CloudArray.CLOUD_SIZE, 0)
return clouds.connection.player.physics.positionInfo.chunkPosition + Vec2i(offset / CloudArray.CLOUD_SIZE, 0)
}
private fun updatePosition() {
val position = calculateCloudPosition()
if (position == this.position) {
return
}

View File

@ -63,12 +63,16 @@ class CloudRenderer(
var delta = 0.0f
private set
private var reset = false
override val skipOpaque: Boolean
get() = !sky.effects.clouds || !sky.profile.clouds.enabled || connection.profiles.block.viewDistance < 3 || layers.isEmpty()
override fun asyncInit(latch: CountUpAndDownLatch) {
matrix.load(connection.assetsManager)
context.camera.offset::offset.observe(this) { reset() }
}
private fun getCloudHeight(index: Int): IntRange {
@ -103,6 +107,10 @@ class CloudRenderer(
sky.profile.clouds::flat.observe(this, instant = true) { this.flat = it }
}
private fun reset() {
reset = true
}
private fun updateLayers(layers: Int) {
while (layers < this.layers.size) {
toUnload += this.layers.removeLast()
@ -117,6 +125,11 @@ class CloudRenderer(
if (!sky.effects.clouds) {
return
}
if (reset) {
updateLayers(0)
updateLayers(nextLayers)
reset = false
}
if (layers.size != nextLayers) {
updateLayers(nextLayers)
}
@ -236,8 +249,8 @@ class CloudRenderer(
setYOffset()
for (array in layers) {
array.draw()
for (layer in layers) {
layer.draw()
}
}

View File

@ -80,8 +80,9 @@ open class LineMesh(context: RenderContext) : GenericColorMesh(context) {
fun drawLazyAABB(aabb: AABB, color: RGBColor) {
data.ensureSize(6 * order.size * GenericColorMeshStruct.FLOATS_PER_VERTEX)
val offset = context.camera.offset.offset
for (direction in Directions.VALUES) {
val positions = direction.getPositions(Vec3(aabb.min), Vec3(aabb.max))
val positions = direction.getPositions(Vec3(aabb.min - offset), Vec3(aabb.max - offset))
for ((positionIndex, _) in order) {
addVertex(positions[positionIndex], color)
}
@ -90,8 +91,9 @@ open class LineMesh(context: RenderContext) : GenericColorMesh(context) {
fun drawAABB(aabb: AABB, lineWidth: Float = RenderConstants.DEFAULT_LINE_WIDTH, color: RGBColor, margin: Float = 0.0f, shape: AbstractVoxelShape? = null) {
data.ensureSize(12 * 4 * order.size * GenericColorMeshStruct.FLOATS_PER_VERTEX)
val min = aabb.min - margin
val max = aabb.max + margin
val offset = context.camera.offset.offset
val min = aabb.min - margin - offset
val max = aabb.max + margin - offset
fun tryDrawLine(start: Vec3, end: Vec3) {
tryDrawLine(start, end, lineWidth, color, shape)

View File

@ -63,7 +63,6 @@ class BlockOutlineRenderer(
override var unload: Boolean = false
override fun init(latch: CountUpAndDownLatch) {
val profile = connection.profiles.block
this.profile::enabled.observe(this) { reload = true }
this.profile::collisions.observe(this) { reload = true }
this.profile::outlineColor.observe(this) { reload = true }