Skip to content

Commit ae1f0c3

Browse files
committed
[Patch] Fixed static batch material issues
1 parent d43c77f commit ae1f0c3

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Sources/UntoldEngine/Renderer/RenderPasses.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,9 @@ public enum RenderPasses {
870870
let indexBuffer = batchGroup.indexBuffer
871871
else { continue }
872872

873-
// Get material from first entity in batch
874-
guard let firstEntityId = batchGroup.entityIds.first,
875-
let renderComponent = scene.get(component: RenderComponent.self, for: firstEntityId),
876-
let firstMesh = renderComponent.mesh.first,
877-
let firstSubmesh = firstMesh.submeshes.first,
878-
let material = firstSubmesh.material
879-
else { continue }
873+
// Use the material captured when this batch was built.
874+
// This guarantees batched shading matches the grouped submesh material.
875+
let material = batchGroup.material
880876

881877
// Set uniforms
882878
renderEncoder.setVertexBytes(

Sources/UntoldEngine/Systems/BatchingSystem.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import simd
1616
public struct BatchGroup {
1717
var id: UUID
1818
var materialHash: String // Identifier for material compatibility
19+
var material: Material // Representative material for this batch
1920

2021
// Separate buffers for each vertex attribute (simpler than interleaved)
2122
var positionBuffer: MTLBuffer?
@@ -202,7 +203,7 @@ public class BatchingSystem {
202203
Logger.log(message: "📋 Found \(entities.count) entities with StaticBatchComponent")
203204

204205
// Group meshes by material AND LOD level
205-
var materialGroups: [String: [(entityId: EntityID, mesh: Mesh, meshIndex: Int, transform: simd_float4x4, lodIndex: Int)]] = [:]
206+
var materialGroups: [String: [(entityId: EntityID, mesh: Mesh, meshIndex: Int, transform: simd_float4x4, lodIndex: Int, material: Material)]] = [:]
206207

207208
// Iterate through all entities with StaticBatchComponent
208209
for entityId in entities {
@@ -251,7 +252,8 @@ public class BatchingSystem {
251252
mesh: mesh,
252253
meshIndex: submeshIndex,
253254
transform: finalTransform,
254-
lodIndex: lodIndex
255+
lodIndex: lodIndex,
256+
material: material
255257
))
256258
}
257259
}
@@ -273,7 +275,9 @@ public class BatchingSystem {
273275
(entityId: item.entityId, mesh: item.mesh, meshIndex: item.meshIndex, transform: item.transform)
274276
}
275277

276-
if let batchGroup = createBatchGroup(from: convertedGroup, materialHash: batchKey) {
278+
guard let batchMaterial = meshGroup.first?.material else { continue }
279+
280+
if let batchGroup = createBatchGroup(from: convertedGroup, materialHash: batchKey, material: batchMaterial) {
277281
batchGroups.append(batchGroup)
278282

279283
// Track entity to batch mapping with LOD info
@@ -296,7 +300,8 @@ public class BatchingSystem {
296300

297301
private func createBatchGroup(
298302
from meshGroup: [(entityId: EntityID, mesh: Mesh, meshIndex: Int, transform: simd_float4x4)],
299-
materialHash: String
303+
materialHash: String,
304+
material: Material
300305
) -> BatchGroup? {
301306
var allPositions: [simd_float4] = [] // Changed to float4 to match vertex descriptor
302307
var allNormals: [simd_float4] = [] // Changed to float4 to match vertex descriptor
@@ -405,6 +410,7 @@ public class BatchingSystem {
405410
return BatchGroup(
406411
id: UUID(),
407412
materialHash: materialHash,
413+
material: material,
408414
positionBuffer: positionBuffer,
409415
normalBuffer: normalBuffer,
410416
uvBuffer: uvBuffer,

0 commit comments

Comments
 (0)