diff --git a/src/main/java/de/bixilon/minosoft/data/entities/EntityRenderInfo.kt b/src/main/java/de/bixilon/minosoft/data/entities/EntityRenderInfo.kt index 08a1d8dda..8732275ef 100644 --- a/src/main/java/de/bixilon/minosoft/data/entities/EntityRenderInfo.kt +++ b/src/main/java/de/bixilon/minosoft/data/entities/EntityRenderInfo.kt @@ -20,37 +20,56 @@ import de.bixilon.minosoft.data.entities.entities.Entity import de.bixilon.minosoft.data.registries.shapes.aabb.AABB import de.bixilon.minosoft.gui.rendering.renderer.drawable.Drawable import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil -import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.blockPosition +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.EMPTY +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.addedY import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition class EntityRenderInfo(private val entity: Entity) : Drawable, Tickable { - private var position0 = Vec3d(entity.physics.position) - private var position1 = position0 + private var position0 = Vec3d.EMPTY + private var position1 = Vec3d(entity.physics.position) + private var defaultAABB = entity.defaultAABB - var position: Vec3d = position0 + var position: Vec3d = position1 private set var eyePosition: Vec3d = position private set var cameraAABB: AABB = AABB.EMPTY private set - var eyeBlockPosition = position1.blockPosition - private set - private var rotation0 = entity.physics.rotation - private var rotation1 = rotation0 - var rotation: EntityRotation = rotation0 + private var rotation0 = EntityRotation.EMPTY + private var rotation1 = entity.physics.rotation + var rotation: EntityRotation = rotation1 private set private fun interpolatePosition(delta: Float) { - // TODO: Only interpolate if changed + val position1 = this.position1 + + if (position == position1) { + interpolateAABB(false) + return + } + position = Vec3dUtil.interpolateLinear(delta.toDouble(), position0, position1) - eyePosition = position + Vec3d(0.0, entity.eyeHeight.toDouble(), 0.0) - cameraAABB = entity.defaultAABB + position - eyeBlockPosition = position1.blockPosition + eyePosition = position.addedY(entity.eyeHeight.toDouble()) + + interpolateAABB(true) + } + + private fun interpolateAABB(force: Boolean) { + val defaultAABB = entity.defaultAABB + if (!force && this.defaultAABB == defaultAABB) { + return + } + cameraAABB = defaultAABB + position } private fun interpolateRotation(delta: Float) { + val rotation1 = this.rotation1 + if (rotation == rotation1) { + return + } + val rotation0 = this.rotation0 rotation = EntityRotation(interpolateLinear(delta, rotation0.yaw, rotation1.yaw), interpolateLinear(delta, rotation0.pitch, rotation1.pitch)) } @@ -60,11 +79,24 @@ class EntityRenderInfo(private val entity: Entity) : Drawable, Tickable { interpolateRotation(delta) } - override fun tick() { + private fun tickPosition() { + val entityPosition = entity.physics.position + if (position0 == entityPosition && position1 == entityPosition) return + position0 = position1 - position1 = Vec3d(entity.physics.position) + position1 = entityPosition + } + + private fun tickRotation() { + val entityRotation = entity.physics.rotation + if (rotation0 == entityRotation && rotation1 == entityRotation) return rotation0 = rotation1 - rotation1 = entity.physics.rotation + rotation1 = entityRotation + } + + override fun tick() { + tickPosition() + tickRotation() } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/other/debug/DebugHUDElement.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/other/debug/DebugHUDElement.kt index ef636715b..df1a86d8c 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/other/debug/DebugHUDElement.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/gui/hud/elements/other/debug/DebugHUDElement.kt @@ -49,6 +49,7 @@ import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions import de.bixilon.minosoft.gui.rendering.particle.ParticleRenderer import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.blockPosition import de.bixilon.minosoft.gui.rendering.world.WorldRenderer import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen import de.bixilon.minosoft.properties.MinosoftProperties @@ -291,7 +292,7 @@ class DebugHUDElement(guiRenderer: GUIRenderer) : Element(guiRenderer), Layouted this@DebugWorldInfo += AutoTextElement(guiRenderer, 1) { BaseComponent("Sky properties ", entity.connection.world.dimension.effects) } this@DebugWorldInfo += AutoTextElement(guiRenderer, 1) { BaseComponent("Biome ", biome) } - this@DebugWorldInfo += AutoTextElement(guiRenderer, 1) { with(entity.connection.world.getLight(entity.renderInfo.eyeBlockPosition)) { BaseComponent("Light block=", (this and SectionLight.BLOCK_LIGHT_MASK), ", sky=", ((this and SectionLight.SKY_LIGHT_MASK) shr 4)) } } + this@DebugWorldInfo += AutoTextElement(guiRenderer, 1) { with(entity.connection.world.getLight(entity.renderInfo.eyePosition.blockPosition)) { BaseComponent("Light block=", (this and SectionLight.BLOCK_LIGHT_MASK), ", sky=", ((this and SectionLight.SKY_LIGHT_MASK) shr 4)) } } this@DebugWorldInfo += AutoTextElement(guiRenderer, 1) { BaseComponent("Fully loaded: ", chunk.neighbours.complete) } lastChunk.value = chunk diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/util/vec/vec3/Vec3dUtil.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/util/vec/vec3/Vec3dUtil.kt index ccee36d48..3d5789d4a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/util/vec/vec3/Vec3dUtil.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/util/vec/vec3/Vec3dUtil.kt @@ -150,10 +150,16 @@ object Vec3dUtil { } } - fun Vec3d.addY(y: Double): Vec3d { + fun Vec3d.addedY(y: Double): Vec3d { val res = Vec3d(this) res.y += y return res } + + fun Vec3d.assign(other: Vec3d) { + this.x = other.x + this.y = other.y + this.z = other.z + } } diff --git a/src/main/java/de/bixilon/minosoft/physics/EntityPositionInfo.kt b/src/main/java/de/bixilon/minosoft/physics/EntityPositionInfo.kt index fde9954ad..6e8f8d72d 100644 --- a/src/main/java/de/bixilon/minosoft/physics/EntityPositionInfo.kt +++ b/src/main/java/de/bixilon/minosoft/physics/EntityPositionInfo.kt @@ -26,7 +26,7 @@ import de.bixilon.minosoft.data.world.positions.ChunkPositionUtil.sectionHeight import de.bixilon.minosoft.data.world.positions.InChunkPosition import de.bixilon.minosoft.data.world.positions.InChunkSectionPosition import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY -import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.addY +import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.addedY import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3dUtil.blockPosition import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.EMPTY import de.bixilon.minosoft.physics.entities.EntityPhysics @@ -54,7 +54,7 @@ class EntityPositionInfo( val blockPosition = position.blockPosition val chunkPosition = blockPosition.chunkPosition val sectionHeight = blockPosition.sectionHeight - val eyePosition = position.addY(physics.entity.eyeHeight.toDouble()).blockPosition + val eyePosition = position.addedY(physics.entity.eyeHeight.toDouble()).blockPosition val inChunkPosition = blockPosition.inChunkPosition val inSectionPosition = blockPosition.inChunkSectionPosition