Przeglądaj źródła

Merge pull request #7595 from Popov72/shadow-pointlight-bug

Fix: shadows not rendered correctly with point lights
mergify[bot] 5 lat temu
rodzic
commit
3f76581095

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

@@ -326,6 +326,7 @@
 - Fixed bug in the `Image` GUI class when rotating a SVG picture ([Popov72](https://github.com/Popov72))
 - Fix for bug where NME would crash if frames did not have comments ([Kyle Belfort](https://github.com/belfortk))
 - Fix wrong import of _TimeToken ([Sebavan](https://github.com/sebavan/)
+- Fix shadows not rendered correctly when using point lights ([Popov72](https://github.com/Popov72))
 
 ## Breaking changes
 

+ 5 - 0
src/Lights/Shadows/shadowGenerator.ts

@@ -1313,6 +1313,11 @@ export class ShadowGenerator implements IShadowGenerator {
             }
         }
 
+        // Point light
+        if (this._light.needCube()) {
+            defines.push("#define USEDISTANCE");
+        }
+
         this._isReadyCustomDefines(defines, subMesh, useInstances);
 
         // Get correct effect

+ 12 - 1
src/Shaders/shadowMap.fragment.fx

@@ -4,6 +4,11 @@
 
 varying float vDepthMetric;
 
+#ifdef USEDISTANCE
+uniform vec3 lightData;
+varying vec3 vPositionW;
+#endif
+
 #ifdef ALPHATEST
 varying vec2 vUV;
 uniform sampler2D diffuseSampler;
@@ -30,8 +35,14 @@ void main(void)
     float depth = vDepthMetric;
 
 #ifdef DEPTHCLAMP
-    depth = clamp(((z + depthValues.x) / (depthValues.y)) + biasAndScale.x, 0.0, 1.0);
+    #ifdef USEDISTANCE
+        depth = clamp(((length(vPositionW - lightData) + depthValues.x) / (depthValues.y)) + biasAndScale.x, 0.0, 1.0);
+    #else
+        depth = clamp(((z + depthValues.x) / (depthValues.y)) + biasAndScale.x, 0.0, 1.0);
+    #endif
     gl_FragDepth = depth;
+#elif defined(USEDISTANCE)
+    depth = (length(vPositionW - lightData) + depthValues.x) / (depthValues.y) + biasAndScale.x;
 #endif
 
 #ifdef ESM

+ 10 - 1
src/Shaders/shadowMap.vertex.fx

@@ -21,6 +21,10 @@ uniform vec2 depthValues;
 
 varying float vDepthMetric;
 
+#ifdef USEDISTANCE
+varying vec3 vPositionW;
+#endif
+
 #ifdef ALPHATEST
 varying vec2 vUV;
 uniform mat4 diffuseMatrix;
@@ -76,9 +80,14 @@ vec4 worldPos = finalWorld * vec4(positionUpdated, 1.0);
     worldPos.xyz -= worldNor * normalBias;
 #endif
 
+#ifdef USEDISTANCE
+vPositionW = worldPos.xyz;
+#endif
+
 // Projection.
 gl_Position = viewProjection * worldPos;
 
+
 #ifdef DEPTHTEXTURE
     // Depth texture Linear bias.
     gl_Position.z += biasAndScale.x * gl_Position.w;
@@ -87,7 +96,7 @@ gl_Position = viewProjection * worldPos;
 #ifdef DEPTHCLAMP
     z = gl_Position.z;
     gl_Position.z = 0.0;
-#else
+#elif !defined(USEDISTANCE)
     // Color Texture Linear bias.
     vDepthMetric = ((gl_Position.z + depthValues.x) / (depthValues.y)) + biasAndScale.x;
 #endif