浏览代码

FIxing Volumetric Lights with linear depth

Julien Moreau-Mathis 8 年之前
父节点
当前提交
1e795ae70d

+ 7 - 1
src/PostProcess/RenderPipeline/Pipelines/babylon.standardRenderingPipeline.ts

@@ -505,7 +505,9 @@
             var geometry = geometryRenderer.getGBuffer();
 
             // Base post-process
-            this.vlsPostProcess = new PostProcess("HDRVLS", "standard", ["shadowViewProjection", "cameraPosition", "sunDirection", "sunColor", "scatteringCoefficient", "scatteringPower"], ["shadowMapSampler", "positionSampler" ], ratio / 8, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define VLS");
+            this.vlsPostProcess = new PostProcess("HDRVLS", "standard", ["shadowViewProjection", "cameraPosition", "sunDirection", "sunColor", "scatteringCoefficient", "scatteringPower", "depthValues"], ["shadowMapSampler", "positionSampler" ], ratio, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, "#define VLS");
+
+            var depthValues = Vector2.Zero();
 
             this.vlsPostProcess.onApply = (effect: Effect) => {
                 if (this.sourceLight && this.sourceLight.getShadowGenerator()) {
@@ -522,6 +524,10 @@
 
                     effect.setFloat("scatteringCoefficient", this.volumetricLightCoefficient);
                     effect.setFloat("scatteringPower", this.volumetricLightPower);
+
+                    depthValues.x = generator.getLight().getDepthMinZ(this._scene.activeCamera);
+                    depthValues.y = generator.getLight().getDepthMaxZ(this._scene.activeCamera);
+                    effect.setVector2("depthValues", depthValues);
                 }
             };
 

+ 7 - 1
src/Shaders/standard.fragment.fx

@@ -117,11 +117,14 @@ void main(void)
 #define PI 3.1415926535897932384626433832795
 
 uniform mat4 shadowViewProjection;
+uniform mat4 lightWorld;
 
 uniform vec3 cameraPosition;
 uniform vec3 sunDirection;
 uniform vec3 sunColor;
 
+uniform vec2 depthValues;
+
 uniform float scatteringCoefficient;
 uniform float scatteringPower;
 
@@ -154,12 +157,15 @@ void main(void)
 	for (int i = 0; i < int(NB_STEPS); i++)
 	{
 		vec4 worldInShadowCameraSpace = shadowViewProjection * vec4(currentPosition, 1.0);
+		float depthMetric =  (worldInShadowCameraSpace.z + depthValues.x) / (depthValues.y);
+		float shadowPixelDepth = clamp(depthMetric, 0.0, 1.0);
+
 		worldInShadowCameraSpace.xyz /= worldInShadowCameraSpace.w;
 		worldInShadowCameraSpace.xyz = 0.5 * worldInShadowCameraSpace.xyz + vec3(0.5);
 
 		float shadowMapValue = texture2D(shadowMapSampler, worldInShadowCameraSpace.xy).r;
 		
-		if (shadowMapValue > worldInShadowCameraSpace.z)
+		if (shadowMapValue > shadowPixelDepth)
 			accumFog += sunColor * computeScattering(dot(rayDirection, sunDirection));
 		
 		currentPosition += stepL;