@@ -16,6 +16,7 @@ import simd
1616public 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