Browse Source

Merge pull request #9096 from Popov72/fix-shadows

Shadows: Fix wrong shadows when using lod meshes + too many draw calls
David Catuhe 4 years ago
parent
commit
112b2e47cb

+ 2 - 0
.gitignore

@@ -157,6 +157,7 @@ node_modules
 # for VSCode
 .vs
 .tempChromeProfileForDebug
+.tempChromeCanaryProfileForDebug
 .temp
 *.js.map
 *.js.fx
@@ -200,5 +201,6 @@ gui/dist/
 # Local Netlify folder
 .netlify
 Playground/dist/
+Playground/temp/
 Sandbox/public/dist/
 ktx2Decoder/dist/

+ 2 - 0
dist/preview release/what's new.md

@@ -337,6 +337,8 @@
 - Changed use of mousemove to pointermove in freeCameraMouseInput and flyCameraMouseInput to fix issue with Firefox ([PolygonalSun](https://github.com/PolygonalSun))
 - Fixed `TriPlanarMaterial` to compute the right world normals ([julien-moreau](https://github.com/julien-moreau))
 - Fix `SkeletonViewer` to use utillity layer with custom lighting to improve debug mesh visibility ([Drigax](https://github.com/drigax))
+- Fix same sub mesh being rendered multiple times in the shadow map ([Popov72](https://github.com/Popov72))
+- Fix incorrect shadows on the master mesh when using a lod mesh ([Popov72](https://github.com/Popov72))
 
 ## Breaking changes
 

+ 11 - 1
src/Lights/Shadows/shadowGenerator.ts

@@ -1085,7 +1085,7 @@ export class ShadowGenerator implements IShadowGenerator {
 
         effectiveMesh._internalAbstractMeshDataInfo._isActiveIntermediate = false;
 
-        if (!material || subMesh.verticesCount === 0) {
+        if (!material || subMesh.verticesCount === 0 || subMesh._renderId === scene.getRenderId()) {
             return;
         }
 
@@ -1099,7 +1099,17 @@ export class ShadowGenerator implements IShadowGenerator {
         }
 
         var hardwareInstancedRendering = engine.getCaps().instancedArrays && (batch.visibleInstances[subMesh._id] !== null && batch.visibleInstances[subMesh._id] !== undefined || renderingMesh.hasThinInstances);
+        if (effectiveMesh._internalAbstractMeshDataInfo._currentLOD !== effectiveMesh) {
+            if (hardwareInstancedRendering) {
+                delete batch.renderSelf[subMesh._id];
+            } else {
+                return;
+            }
+        }
+
         if (this.isReady(subMesh, hardwareInstancedRendering, isTransparent)) {
+            subMesh._renderId = scene.getRenderId();
+
             const shadowDepthWrapper = renderingMesh.material?.shadowDepthWrapper;
 
             let effect = shadowDepthWrapper?.getEffect(subMesh, this) ?? this._effect;

+ 1 - 0
src/Meshes/abstractMesh.ts

@@ -86,6 +86,7 @@ class _InternalAbstractMeshDataInfo {
     public _isActiveIntermediate = false;
     public _onlyForInstancesIntermediate = false;
     public _actAsRegularMesh = false;
+    public _currentLOD: Nullable<AbstractMesh> = null;
 }
 
 /**

+ 1 - 0
src/scene.ts

@@ -3555,6 +3555,7 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
             if (meshToRender === undefined || meshToRender === null) {
                 continue;
             }
+            mesh._internalAbstractMeshDataInfo._currentLOD = meshToRender;
 
             // Compute world matrix if LOD is billboard
             if (meshToRender !== mesh && meshToRender.billboardMode !== TransformNode.BILLBOARDMODE_NONE) {