Skip to content

Commit

Permalink
Fix model render
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxuanchiadm committed Feb 26, 2024
1 parent 2a8e0d9 commit 6047802
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package net.easecation.bedrockloader.entity
import net.easecation.bedrockloader.bedrock.entity.components.EntityComponents
import net.easecation.bedrockloader.loader.BedrockAddonsRegistry
import net.fabricmc.fabric.api.`object`.builder.v1.entity.FabricEntityTypeBuilder
import net.minecraft.entity.EntityDimensions
import net.minecraft.entity.EntityType
import net.minecraft.entity.EquipmentSlot
import net.minecraft.entity.SpawnGroup
Expand All @@ -29,6 +30,7 @@ class EntityDataDriven(
?: throw IllegalStateException("[EntityDataDriven] Entity $identifier has no components")
EntityDataDriven(identifier, components, type, world)
}
builder.dimensions(EntityDimensions.fixed(1f, 1f))
return builder.build()
}
fun buildEntityAttributes(components: EntityComponents): DefaultAttributeContainer.Builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,57 @@ object BedrockRenderUtil {
var boneCount = 0
fun addBoneToModelData(bone: GeometryDefinition.Bone, parentPartData: ModelPartData) {
val pivotTransform = ModelTransform.of(
bone.pivot?.get(0) ?: 0f,
bone.pivot?.get(1) ?: 0f,
bone.pivot?.get(2) ?: 0f,
bone.rotation?.get(0) ?: 0f,
bone.rotation?.get(1) ?: 0f,
bone.rotation?.get(2) ?: 0f
(bone.pivot?.get(0) ?: 0f),
-(bone.pivot?.get(1) ?: 0f),
(bone.pivot?.get(2) ?: 0f),
(bone.rotation?.get(0) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
(bone.rotation?.get(1) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
(bone.rotation?.get(2) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
detachPivot = true
)
val bonePartData = parentPartData.addChild(bone.name, ModelPartBuilder.create().mirrored(bone.mirror == true), pivotTransform)
val bonePartData = parentPartData.addChild(bone.name, ModelPartBuilder.create(), pivotTransform)

bone.cubes?.forEach { cube ->
val cubeBuilder = ModelPartBuilder.create().mirrored(cube.mirror == true)
val cubeBuilder = ModelPartBuilder.create()
cube.uv?.let {
when (it) {
is GeometryDefinition.Uv.UvBox -> cubeBuilder.uv(it.uv?.get(0) ?: 0, it.uv?.get(1) ?: 0)
is GeometryDefinition.Uv.UvPerFace -> {} // TODO 暂不支持逐面UV
is GeometryDefinition.Uv.UvPerFace -> cubeBuilder.uv(ModelPart.FaceUV(
north = ModelPart.UVMapping(uv = Pair(it.north?.uv?.get(0) ?: 0, it.north?.uv?.get(1) ?: 0), uvSize = Pair(it.north?.uv_size?.get(0) ?: 0, it.north?.uv_size?.get(1) ?: 0)),
east = ModelPart.UVMapping(uv = Pair(it.east?.uv?.get(0) ?: 0, it.east?.uv?.get(1) ?: 0), uvSize = Pair(it.east?.uv_size?.get(0) ?: 0, it.east?.uv_size?.get(1) ?: 0)),
south = ModelPart.UVMapping(uv = Pair(it.south?.uv?.get(0) ?: 0, it.south?.uv?.get(1) ?: 0), uvSize = Pair(it.south?.uv_size?.get(0) ?: 0, it.south?.uv_size?.get(1) ?: 0)),
west = ModelPart.UVMapping(uv = Pair(it.west?.uv?.get(0) ?: 0, it.west?.uv?.get(1) ?: 0), uvSize = Pair(it.west?.uv_size?.get(0) ?: 0, it.west?.uv_size?.get(1) ?: 0)),
up = ModelPart.UVMapping(uv = Pair(it.up?.uv?.get(0) ?: 0, it.up?.uv?.get(1) ?: 0), uvSize = Pair(it.up?.uv_size?.get(0) ?: 0, it.up?.uv_size?.get(1) ?: 0)),
down = ModelPart.UVMapping(uv = Pair(it.down?.uv?.get(0) ?: 0, it.down?.uv?.get(1) ?: 0), uvSize = Pair(it.down?.uv_size?.get(0) ?: 0, it.down?.uv_size?.get(1) ?: 0)),
))
}
}
cube.origin?.let { cubeBuilder
.cuboid(
it[0], it[1], it[2],
cube.size?.get(0) ?: 0f,
cube.size?.get(1) ?: 0f,
cube.size?.get(2) ?: 0f
cube.origin?.let {
val size = cube.size ?: listOf(0f, 0f, 0f)
val offset = listOf(
it[0] + size[0] / 2,
-it[1] - size[1],
it[2] + size[2] / 2
)
}
bonePartData.addChild("cube${boneCount++}", cubeBuilder,
ModelTransform.of(
cube.pivot?.get(0) ?: 0f, cube.pivot?.get(1) ?: 0f, cube.pivot?.get(2) ?: 0f,
cube.rotation?.get(0) ?: 0f, cube.rotation?.get(1) ?: 0f, cube.rotation?.get(2) ?: 0f
cubeBuilder.cuboid(
offset[0],
offset[1],
offset[2],
size[0],
size[1],
size[2]
)
}
val rotationData = ModelTransform.of(
(cube.pivot?.get(0) ?: 0f),
-(cube.pivot?.get(1) ?: 0f),
(cube.pivot?.get(2) ?: 0f),
(cube.rotation?.get(0) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
(cube.rotation?.get(1) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
(cube.rotation?.get(2) ?: 0f) * (Math.PI.toFloat() * 2 / 360F),
detachPivot = true
)
bonePartData.addChild("cube${boneCount++}", cubeBuilder, rotationData)
}

bones.filter { it.parent == bone.name }.forEach { childBone ->
Expand All @@ -50,7 +70,7 @@ object BedrockRenderUtil {
}

val modelData = ModelData()
val rootPartData = modelData.root
val rootPartData = modelData.root.addChild("offset", ModelPartBuilder.create(), ModelTransform.pivot(0f, 24f, 0f))
for (bone in bones) {
if (bone.parent == null) {
addBoneToModelData(bone, rootPartData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import net.minecraft.client.util.math.Vector2f
import net.minecraft.util.math.Vec3f

@Environment(value = EnvType.CLIENT)
class ModelCuboidData(private val name: String?, textureX: Float, textureY: Float, offsetX: Float, offsetY: Float, offsetZ: Float, sizeX: Float, sizeY: Float, sizeZ: Float, extra: Dilation, private val mirror: Boolean, textureScaleX: Float, textureScaleY: Float) {
class ModelCuboidData(private val name: String?, textureX: Float, textureY: Float, private val faceUV: ModelPart.FaceUV?, offsetX: Float, offsetY: Float, offsetZ: Float, sizeX: Float, sizeY: Float, sizeZ: Float, extra: Dilation, private val mirror: Boolean, textureScaleX: Float, textureScaleY: Float) {
private val offset = Vec3f(offsetX, offsetY, offsetZ)
private val dimensions = Vec3f(sizeX, sizeY, sizeZ)
private val extraSize = extra
private val textureUV = Vector2f(textureX, textureY)
private val textureScale = Vector2f(textureScaleX, textureScaleY)

fun createCuboid(textureWidth: Int, textureHeight: Int): ModelPart.Cuboid {
return ModelPart.Cuboid(textureUV.x.toInt(), textureUV.y.toInt(), offset.x, offset.y, offset.z, dimensions.x, dimensions.y, dimensions.z, extraSize.radiusX, extraSize.radiusY, extraSize.radiusZ, this.mirror, textureWidth.toFloat() * textureScale.x, textureHeight.toFloat() * textureScale.y)
return ModelPart.Cuboid(textureUV.x.toInt(), textureUV.y.toInt(), faceUV, offset.x, offset.y, offset.z, dimensions.x, dimensions.y, dimensions.z, extraSize.radiusX, extraSize.radiusY, extraSize.radiusZ, this.mirror, textureWidth.toFloat() * textureScale.x, textureHeight.toFloat() * textureScale.y)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ class ModelPart(private val cuboids: List<Cuboid>, private val children: Map<Str
var pitch: Float = 0f
var yaw: Float = 0f
var roll: Float = 0f
var detachPivot: Boolean = false
var visible: Boolean = true

var transform: ModelTransform
get() = ModelTransform.of(this.pivotX, this.pivotY, this.pivotZ, this.pitch, this.yaw, this.roll)
get() = ModelTransform.of(this.pivotX, this.pivotY, this.pivotZ, this.pitch, this.yaw, this.roll, this.detachPivot)
set(rotationData) {
this.pivotX = rotationData.pivotX
this.pivotY = rotationData.pivotY
this.pivotZ = rotationData.pivotZ
this.pitch = rotationData.pitch
this.yaw = rotationData.yaw
this.roll = rotationData.roll
this.detachPivot = rotationData.detachPivot
}

fun copyTransform(part: ModelPart) {
Expand All @@ -40,6 +42,7 @@ class ModelPart(private val cuboids: List<Cuboid>, private val children: Map<Str
this.pivotX = part.pivotX
this.pivotY = part.pivotY
this.pivotZ = part.pivotZ
this.detachPivot = part.detachPivot
}

fun getChild(name: String): ModelPart {
Expand Down Expand Up @@ -106,6 +109,9 @@ class ModelPart(private val cuboids: List<Cuboid>, private val children: Map<Str
if (this.pitch != 0.0f) {
matrices.multiply(Vec3f.POSITIVE_X.getRadialQuaternion(this.pitch))
}
if (detachPivot) {
matrices.translate(-(this.pivotX / 16.0f).toDouble(), -(this.pivotY / 16.0f).toDouble(), -(this.pivotZ / 16.0f).toDouble())
}
}

private fun renderCuboids(entry: MatrixStack.Entry, vertexConsumer: VertexConsumer, light: Int, overlay: Int, red: Float, green: Float, blue: Float, alpha: Float) {
Expand Down Expand Up @@ -148,12 +154,12 @@ class ModelPart(private val cuboids: List<Cuboid>, private val children: Map<Str

@Environment(value = EnvType.CLIENT)
data class UVMapping(
val uv: Pair<Float, Float>,
val uvSize: Pair<Float, Float>
val uv: Pair<Int, Int>,
val uvSize: Pair<Int, Int>
)

@Environment(value = EnvType.CLIENT)
class Cuboid(u: Int, v: Int, x: Float, y: Float, z: Float, sizeX: Float, sizeY: Float, sizeZ: Float, extraX: Float, extraY: Float, extraZ: Float, mirror: Boolean, textureWidth: Float, textureHeight: Float) {
class Cuboid(u: Int, v: Int, faceUV: FaceUV?, x: Float, y: Float, z: Float, sizeX: Float, sizeY: Float, sizeZ: Float, extraX: Float, extraY: Float, extraZ: Float, mirror: Boolean, textureWidth: Float, textureHeight: Float) {
private val sides: Array<Quad?>
val minX: Float
val minY: Float
Expand Down Expand Up @@ -200,12 +206,21 @@ class ModelPart(private val cuboids: List<Cuboid>, private val children: Map<Str
val textureEndVZ = textureOriginV + sizeZ
val textureEndVY = textureOriginV + sizeZ + sizeY

sides[2] = Quad(arrayOf(vertexBottomSE, vertexBottomSW, vertexBottomNW, vertexBottomNE), textureEndUZ, textureOriginV, textureEndUX, textureEndVZ, textureWidth, textureHeight, mirror, Direction.DOWN)
sides[3] = Quad(arrayOf(vertexTopNE, vertexTopNW, vertexTopSW, vertexTopSE), textureEndUX, textureEndVZ, textureDoubleEndUX, textureOriginV, textureWidth, textureHeight, mirror, Direction.UP)
sides[1] = Quad(arrayOf(vertexBottomNW, vertexBottomSW, vertexTopSW, vertexTopNW), textureOriginU, textureEndVZ, textureEndUZ, textureEndVY, textureWidth, textureHeight, mirror, Direction.WEST)
sides[4] = Quad(arrayOf(vertexBottomNE, vertexBottomNW, vertexTopNW, vertexTopNE), textureEndUZ, textureEndVZ, textureEndUX, textureEndVY, textureWidth, textureHeight, mirror, Direction.NORTH)
sides[0] = Quad(arrayOf(vertexBottomSE, vertexBottomNE, vertexTopNE, vertexTopSE), textureEndUX, textureEndVZ, textureWrapU, textureEndVY, textureWidth, textureHeight, mirror, Direction.EAST)
sides[5] = Quad(arrayOf(vertexBottomSW, vertexBottomSE, vertexTopSE, vertexTopSW), textureWrapU, textureEndVZ, textureFullWrapU, textureEndVY, textureWidth, textureHeight, mirror, Direction.SOUTH)
if (faceUV == null) {
sides[2] = Quad(arrayOf(vertexBottomSE, vertexBottomSW, vertexBottomNW, vertexBottomNE), textureEndUZ, textureOriginV, textureEndUX, textureEndVZ, textureWidth, textureHeight, mirror, Direction.DOWN)
sides[3] = Quad(arrayOf(vertexTopNE, vertexTopNW, vertexTopSW, vertexTopSE), textureEndUX, textureEndVZ, textureDoubleEndUX, textureOriginV, textureWidth, textureHeight, mirror, Direction.UP)
sides[1] = Quad(arrayOf(vertexBottomNW, vertexBottomSW, vertexTopSW, vertexTopNW), textureOriginU, textureEndVZ, textureEndUZ, textureEndVY, textureWidth, textureHeight, mirror, Direction.WEST)
sides[4] = Quad(arrayOf(vertexBottomNE, vertexBottomNW, vertexTopNW, vertexTopNE), textureEndUZ, textureEndVZ, textureEndUX, textureEndVY, textureWidth, textureHeight, mirror, Direction.NORTH)
sides[0] = Quad(arrayOf(vertexBottomSE, vertexBottomNE, vertexTopNE, vertexTopSE), textureEndUX, textureEndVZ, textureWrapU, textureEndVY, textureWidth, textureHeight, mirror, Direction.EAST)
sides[5] = Quad(arrayOf(vertexBottomSW, vertexBottomSE, vertexTopSE, vertexTopSW), textureWrapU, textureEndVZ, textureFullWrapU, textureEndVY, textureWidth, textureHeight, mirror, Direction.SOUTH)
} else {
sides[2] = Quad(arrayOf(vertexBottomSE, vertexBottomSW, vertexBottomNW, vertexBottomNE), faceUV.down.uv.first.toFloat(), faceUV.down.uv.second.toFloat(), faceUV.down.uv.first.toFloat() + faceUV.down.uvSize.first.toFloat(), faceUV.down.uv.second.toFloat() + faceUV.down.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.DOWN)
sides[3] = Quad(arrayOf(vertexTopNE, vertexTopNW, vertexTopSW, vertexTopSE), faceUV.up.uv.first.toFloat(), faceUV.up.uv.second.toFloat(), faceUV.up.uv.first.toFloat() + faceUV.up.uvSize.first.toFloat(), faceUV.up.uv.second.toFloat() + faceUV.up.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.UP)
sides[1] = Quad(arrayOf(vertexBottomNW, vertexBottomSW, vertexTopSW, vertexTopNW), faceUV.west.uv.first.toFloat(), faceUV.west.uv.second.toFloat(), faceUV.west.uv.first.toFloat() + faceUV.west.uvSize.first.toFloat(), faceUV.west.uv.second.toFloat() + faceUV.west.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.WEST)
sides[4] = Quad(arrayOf(vertexBottomNE, vertexBottomNW, vertexTopNW, vertexTopNE), faceUV.north.uv.first.toFloat(), faceUV.north.uv.second.toFloat(), faceUV.north.uv.first.toFloat() + faceUV.north.uvSize.first.toFloat(), faceUV.north.uv.second.toFloat() + faceUV.north.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.NORTH)
sides[0] = Quad(arrayOf(vertexBottomSE, vertexBottomNE, vertexTopNE, vertexTopSE), faceUV.east.uv.first.toFloat(), faceUV.east.uv.second.toFloat(), faceUV.east.uv.first.toFloat() + faceUV.east.uvSize.first.toFloat(), faceUV.east.uv.second.toFloat() + faceUV.east.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.EAST)
sides[5] = Quad(arrayOf(vertexBottomSW, vertexBottomSE, vertexTopSE, vertexTopSW), faceUV.south.uv.first.toFloat(), faceUV.south.uv.second.toFloat(), faceUV.south.uv.first.toFloat() + faceUV.south.uvSize.first.toFloat(), faceUV.south.uv.second.toFloat() + faceUV.south.uvSize.second.toFloat(), textureWidth, textureHeight, mirror, Direction.SOUTH)
}
}

fun renderCuboid(entry: MatrixStack.Entry, vertexConsumer: VertexConsumer, light: Int, overlay: Int, red: Float, green: Float, blue: Float, alpha: Float) {
Expand Down
Loading

0 comments on commit 6047802

Please sign in to comment.