mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-19 12:25:12 -04:00
chest opening/closing animation
This commit is contained in:
parent
b773abbf61
commit
32f29325b8
@ -19,12 +19,15 @@ import de.bixilon.minosoft.gui.rendering.models.unbaked.ModelBakeUtil
|
||||
import de.bixilon.minosoft.gui.rendering.models.unbaked.element.UnbakedElement
|
||||
import de.bixilon.minosoft.gui.rendering.skeletal.SkeletalMesh
|
||||
import de.bixilon.minosoft.gui.rendering.skeletal.model.SkeletalModel
|
||||
import de.bixilon.minosoft.gui.rendering.skeletal.model.outliner.SkeletalOutliner
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
|
||||
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.rotateAssign
|
||||
import glm_.glm
|
||||
import glm_.vec2.Vec2
|
||||
import glm_.vec3.Vec3
|
||||
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
|
||||
import java.util.*
|
||||
|
||||
class BakedSkeletalModel(
|
||||
val model: SkeletalModel,
|
||||
@ -32,9 +35,40 @@ class BakedSkeletalModel(
|
||||
) {
|
||||
lateinit var mesh: SkeletalMesh
|
||||
|
||||
|
||||
private fun calculateOutlinerMapping(): Map<UUID, Int> {
|
||||
val mapping: Object2IntOpenHashMap<UUID> = Object2IntOpenHashMap()
|
||||
|
||||
fun addOutliner(child: Any, outlinerIdOffset: Int): Int {
|
||||
if (child is UUID) {
|
||||
mapping[child] = outlinerIdOffset
|
||||
return 0
|
||||
}
|
||||
if (child !is SkeletalOutliner) {
|
||||
throw IllegalArgumentException()
|
||||
}
|
||||
|
||||
var offset = 1
|
||||
|
||||
for (childChild in child.children) {
|
||||
offset += addOutliner(childChild, outlinerIdOffset + offset)
|
||||
}
|
||||
return offset
|
||||
}
|
||||
|
||||
for (outliner in this.model.outliner) {
|
||||
addOutliner(outliner, -1) // for the root 1 is always added
|
||||
}
|
||||
|
||||
return mapping
|
||||
}
|
||||
|
||||
fun loadMesh(renderWindow: RenderWindow) {
|
||||
val mesh = SkeletalMesh(renderWindow, 1000)
|
||||
|
||||
val outlinerMapping = calculateOutlinerMapping()
|
||||
|
||||
|
||||
for (element in model.elements) {
|
||||
for ((direction, face) in element.faces) {
|
||||
val positions = direction.getPositions(element.from.fromBlockCoordinates(), element.to.fromBlockCoordinates())
|
||||
@ -54,10 +88,11 @@ class BakedSkeletalModel(
|
||||
positions[index] = out
|
||||
}
|
||||
}
|
||||
val outlinerId = outlinerMapping[element.uuid] ?: 0
|
||||
|
||||
for ((index, textureIndex) in mesh.order) {
|
||||
val indexPosition = positions[index].array
|
||||
mesh.addVertex(indexPosition, texturePositions[textureIndex], 0, textures[face.texture]!!, 0xFFFFFF, 0xFF)
|
||||
mesh.addVertex(indexPosition, texturePositions[textureIndex], outlinerId, textures[face.texture]!!, 0xFFFFFF, 0xFF)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -65,7 +100,7 @@ class BakedSkeletalModel(
|
||||
this.mesh = mesh
|
||||
}
|
||||
|
||||
private fun Vec3.fromBlockCoordinates(): Vec3 {
|
||||
return (this / UnbakedElement.BLOCK_RESOLUTION) + Vec3(0.5f, 0.0f, 0.5f)
|
||||
}
|
||||
private fun Vec3.fromBlockCoordinates(): Vec3 {
|
||||
return (this / UnbakedElement.BLOCK_RESOLUTION) + Vec3(0.5f, 0.0f, 0.5f)
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,9 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow
|
||||
import de.bixilon.minosoft.gui.rendering.skeletal.baked.BakedSkeletalModel
|
||||
import de.bixilon.minosoft.gui.rendering.system.base.shader.Shader
|
||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.toVec3
|
||||
import glm_.func.rad
|
||||
import glm_.mat4x4.Mat4
|
||||
import glm_.vec3.Vec3
|
||||
import glm_.vec3.Vec3i
|
||||
|
||||
class SkeletalInstance(
|
||||
@ -26,9 +28,17 @@ class SkeletalInstance(
|
||||
val model: BakedSkeletalModel,
|
||||
) {
|
||||
private val blockPosition = blockPosition.toVec3
|
||||
private var openDelta = -90.0f
|
||||
private var closing: Boolean? = null
|
||||
|
||||
fun playAnimation(name: String) {
|
||||
|
||||
if (name.contains("closing")) {
|
||||
openDelta = -90.0f
|
||||
closing = true
|
||||
} else {
|
||||
openDelta = 0.0f
|
||||
closing = false
|
||||
}
|
||||
}
|
||||
|
||||
fun draw() {
|
||||
@ -43,7 +53,28 @@ class SkeletalInstance(
|
||||
private fun setTransforms(shader: Shader) {
|
||||
val base = Mat4().translateAssign(blockPosition.toVec3)
|
||||
|
||||
val transforms = arrayOf(base)
|
||||
val origin = Vec3(0 + 8, 10, 7 + 8) / Vec3(16, 16, 16)
|
||||
val rotationX = (openDelta).rad
|
||||
if (closing != null) {
|
||||
if (closing!!) {
|
||||
openDelta += 2f
|
||||
} else {
|
||||
openDelta -= 2f
|
||||
}
|
||||
if (openDelta <= -90.0f || openDelta >= 0.0f) {
|
||||
closing = null
|
||||
}
|
||||
}
|
||||
|
||||
val lid = Mat4()
|
||||
lid.translateAssign(origin)
|
||||
lid.rotateAssign(-rotationX, Vec3(1, 0, 0))
|
||||
lid.translateAssign(-origin)
|
||||
lid[3, 0] += blockPosition.x
|
||||
lid[3, 1] += blockPosition.y
|
||||
lid[3, 2] += blockPosition.z
|
||||
|
||||
val transforms = arrayOf(base, lid)
|
||||
|
||||
shader["uSkeletalTransforms"] = transforms
|
||||
}
|
||||
|
@ -31,7 +31,6 @@ abstract class StorageBlockEntityRenderer<E : StorageBlockEntity>(
|
||||
this.instance = SkeletalInstance(renderWindow, blockPosition, renderWindow.modelLoader.blockModels["minecraft:models/block/entities/single_chest.bbmodel".toResourceLocation()]!!)
|
||||
}
|
||||
|
||||
|
||||
override fun draw(renderWindow: RenderWindow) {
|
||||
instance?.draw()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user