Browse Source

Update ModelLibrary for new mesh batch

TothBenoit 1 tuần trước cách đây
mục cha
commit
890cb93dda

+ 1 - 1
hrt/prefab/l3d/modellibrary/Batcher.hx

@@ -85,7 +85,7 @@ class Batcher extends h3d.scene.Object {
 				batch.material.mainPass.addShader(library.killAlpha);
 		}
 
-		batch.primitiveSubParts = [new h3d.scene.MeshBatch.MeshBatchPart()];
+		batch.primitiveSubMeshes = [];
 	}
 
 	override function onRemove() {

+ 4 - 2
hrt/prefab/l3d/modellibrary/GPUBatcher.hx

@@ -42,16 +42,18 @@ class GPUBatcher extends Batcher {
 		var batch : h3d.scene.MeshBatch;
 		if(currentBatchCount > GPUMeshBatchThreshold) {
 			var gpuBatch = new h3d.scene.GPUMeshBatch(getPrimitive(), null, this);
+			gpuBatch.primitiveSubMeshes = [];
 			gpuBatch.enableGpuCulling();
 			gpuBatch.enableGpuLod();
-			gpuBatch.calcBounds = true;
 			batch = gpuBatch;
-		} else {
+		}
+		else {
 			batch = new h3d.scene.MeshBatch(getPrimitive(), null, this);
 			batch.enableStorageBuffer();
 		}
 
 		batch.forceGpuUpdate();
+		batch.calcBounds = false;
 
 		setupMeshBatch(batch, props, material);
 

+ 22 - 7
hrt/prefab/l3d/modellibrary/ModelLibrary.hx

@@ -141,13 +141,28 @@ class ModelLibrary extends Prefab {
 	function emitInstance(bakedMaterial : BakedMaterialData, primitive : h3d.prim.HMDModel, batch : h3d.scene.MeshBatch, ?absPos : h3d.Matrix) {
 		cache.shader.uvTransform.set(bakedMaterial.uvX, bakedMaterial.uvY, bakedMaterial.uvSX, bakedMaterial.uvSY);
 		cache.shader.libraryParams.set(bakedMaterial.texId, 1.0 / atlasResolution / bakedMaterial.uvSX, 0.0, 0.0);
-		var primitiveSubPart = batch.primitiveSubParts[0];
-		primitiveSubPart.indexCount = bakedMaterial.indexCount;
-		primitiveSubPart.indexStart = bakedMaterial.indexStart;
-		primitiveSubPart.lodIndexCount = bakedMaterial.lodIndexCount;
-		primitiveSubPart.lodIndexStart = bakedMaterial.lodIndexStart;
-		primitiveSubPart.lodConfig = primitive.getLodConfig();
-		primitiveSubPart.bounds = cache.geomBounds[bakedMaterial.geomId];
+		var curSubMesh = -1;
+		for ( subMeshIndex => subMesh in batch.primitiveSubMeshes ) {
+			var subPart = subMesh.subParts[0];
+			if ( subPart.indexStart == bakedMaterial.indexStart )
+				curSubMesh = subMeshIndex;
+		}
+		if ( curSubMesh == -1 ) {
+			var subPart = new h3d.scene.MeshBatch.SubPart();
+			subPart.indexStart = bakedMaterial.indexStart;
+			subPart.indexCount = bakedMaterial.indexCount;
+			subPart.lodIndexStart = bakedMaterial.lodIndexStart;
+			subPart.lodIndexCount = bakedMaterial.lodIndexCount;
+			subPart.matIndex = 0;
+			var subMesh = new h3d.scene.MeshBatch.SubMesh();
+			subMesh.lodCount = bakedMaterial.lodIndexCount == null ? 1 : bakedMaterial.lodIndexCount.length + 1;
+			subMesh.lodConfig = primitive.getLodConfig();
+			subMesh.subParts = [subPart];
+			subMesh.bounds = cache.geomBounds[bakedMaterial.geomId];
+			batch.primitiveSubMeshes.push(subMesh);
+			curSubMesh = batch.primitiveSubMeshes.length - 1;
+		}
+		batch.curSubMesh = curSubMesh;
 		if ( absPos != null )
 			batch.worldPosition = absPos;
 		batch.emitInstance();