Skip to content

Commit

Permalink
Implement entity model part rotation (fixes rendering of pigs, villag…
Browse files Browse the repository at this point in the history
…ers, etc)
  • Loading branch information
stackotter committed Jun 5, 2024
1 parent a796dc8 commit 72bdb3f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Sources/Core/Renderer/Mesh/BlockMeshBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ struct BlockMeshBuilder {
indices.append(
contentsOf: geometry.indices.map { index in
return index + startingIndex
})
}
)
}

let geometry = Geometry(vertices: vertices, indices: indices)
Expand Down
25 changes: 21 additions & 4 deletions Sources/Core/Renderer/Mesh/EntityMeshBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,40 @@ public struct EntityMeshBuilder {
func buildSubmodel(
_ submodel: JSONEntityModel.Submodel,
index: Int,
transformation: Mat4x4f = MatrixUtil.identity,
into geometry: inout Geometry<EntityVertex>
) {
var transformation = transformation
if let rotation = submodel.rotate {
let translation = submodel.translate ?? .zero
transformation =
MatrixUtil.rotationMatrix(-MathUtil.radians(from: rotation))
* MatrixUtil.translationMatrix(translation)
}

for box in submodel.boxes ?? [] {
buildBox(
box,
color: index < Self.colors.count ? Self.colors[index] : [0.5, 0.5, 0.5],
transformation: transformation,
into: &geometry
)
}

for (nestedIndex, nestedSubmodel) in (submodel.submodels ?? []).enumerated() {
buildSubmodel(nestedSubmodel, index: nestedIndex, into: &geometry)
buildSubmodel(
nestedSubmodel,
index: nestedIndex,
transformation: transformation,
into: &geometry
)
}
}

func buildBox(
_ box: JSONEntityModel.Box,
color: Vec3f,
transformation: Mat4x4f,
into geometry: inout Geometry<EntityVertex>
) {
var boxPosition = Vec3f(
Expand All @@ -61,8 +77,6 @@ public struct EntityMeshBuilder {
boxSize += 2 * growth
}

boxPosition = boxPosition / 16 + position
boxSize /= 16
for direction in Direction.allDirections {
// The index of the first vertex of this face
let offset = UInt32(geometry.vertices.count)
Expand All @@ -72,7 +86,10 @@ public struct EntityMeshBuilder {

let faceVertexPositions = CubeGeometry.faceVertices[direction.rawValue]
for vertexPosition in faceVertexPositions {
let position = vertexPosition * boxSize + boxPosition
var position = vertexPosition * boxSize + boxPosition
position = (Vec4f(position, 1) * transformation).xyz
position /= 16
position += self.position
let vertex = EntityVertex(
x: position.x,
y: position.y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public struct JSONEntityModel: Codable {
public var id: String?
public var invertAxis: String?
public var mirrorTexture: String?
/// Translation to apply post-rotation (ignored if ``rotate`` is `nil`).
public var translate: Vec3f?
/// Rotation in degrees.
public var rotate: Vec3f?
public var boxes: [Box]?
public var submodels: [Submodel]?
Expand Down Expand Up @@ -42,7 +44,7 @@ public struct JSONEntityModel: Codable {

var models: [Identifier: JSONEntityModel] = [:]
for file in files where file.pathExtension == "jem" {
var identifier = Identifier(
let identifier = Identifier(
namespace: namespace,
name: file.deletingPathExtension().lastPathComponent
)
Expand Down

0 comments on commit 72bdb3f

Please sign in to comment.