Преглед на файлове

Add support for instances with different CCW

David Catuhe преди 5 години
родител
ревизия
0e5389659a
променени са 1 файла, в които са добавени 18 реда и са изтрити 3 реда
  1. 18 3
      src/Meshes/mesh.ts

+ 18 - 3
src/Meshes/mesh.ts

@@ -1468,7 +1468,10 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
                 batchCache.visibleInstances[subMeshId] = visibleInstances[defaultRenderId];
             }
         }
-        batchCache.hardwareInstancedRendering[subMeshId] = this._instanceDataStorage.hardwareInstancedRendering && (batchCache.visibleInstances[subMeshId] !== null) && (batchCache.visibleInstances[subMeshId] !== undefined);
+        batchCache.hardwareInstancedRendering[subMeshId] = 
+                        this._instanceDataStorage.hardwareInstancedRendering 
+                        && (batchCache.visibleInstances[subMeshId] !== null) 
+                        && (batchCache.visibleInstances[subMeshId] !== undefined);
         this._instanceDataStorage.previousBatch = batchCache;
         return batchCache;
     }
@@ -1688,15 +1691,27 @@ export class Mesh extends AbstractMesh implements IGetSetVerticesData {
 
         var sideOrientation: Nullable<number>;
 
-        if (!instanceDataStorage.isFrozen) {
+        if (!instanceDataStorage.isFrozen && this._effectiveMaterial.backFaceCulling) {
+            let mainDeterminant = effectiveMesh._getWorldMatrixDeterminant();
             sideOrientation = this.overrideMaterialSideOrientation;
             if (sideOrientation == null) {
                 sideOrientation = this._effectiveMaterial.sideOrientation;
             }
-            if (effectiveMesh._getWorldMatrixDeterminant() < 0) {
+            if (mainDeterminant < 0) {
                 sideOrientation = (sideOrientation === Material.ClockWiseSideOrientation ? Material.CounterClockWiseSideOrientation : Material.ClockWiseSideOrientation);
             }
             instanceDataStorage.sideOrientation = sideOrientation!;
+
+            let visibleInstances = batch.visibleInstances[subMesh._id];
+            if (visibleInstances) {
+                for (var instance of visibleInstances) {
+                    if (mainDeterminant !== instance._getWorldMatrixDeterminant()) {
+                        this._effectiveMaterial.backFaceCulling = false; // Turn off back face culling as one of the instance is having an incompatible world matrix
+                        break;
+                    }
+                }
+            }
+
         } else {
             sideOrientation = instanceDataStorage.sideOrientation;
         }