Skip to content

Commit c2c5685

Browse files
authored
WebGLRenderer: Use native depth texture for VSM shadow maps (#32443)
1 parent 9edec1c commit c2c5685

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

src/constants.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,14 +1254,6 @@ export const RGBDepthPacking = 3202;
12541254
*/
12551255
export const RGDepthPacking = 3203;
12561256

1257-
/**
1258-
* The depth value is not packed.
1259-
*
1260-
* @type {number}
1261-
* @constant
1262-
*/
1263-
export const IdentityDepthPacking = 3204;
1264-
12651257
/**
12661258
* Normal information is relative to the underlying surface.
12671259
*

src/renderers/shaders/ShaderLib/depth.glsl.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ void main() {
111111
// TODO Deprecate
112112
gl_FragColor = vec4( packDepthToRG( fragCoordZ ), 0.0, 1.0 );
113113
114-
#elif DEPTH_PACKING == 3204
115-
116-
gl_FragColor = vec4( vec3( fragCoordZ ), 1.0 );
117-
118114
#endif
119115
120116
}

src/renderers/shaders/ShaderLib/vsm.glsl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void main() {
4343
mean = mean / samples;
4444
squared_mean = squared_mean / samples;
4545
46-
float std_dev = sqrt( squared_mean - mean * mean );
46+
float std_dev = sqrt( max( 0.0, squared_mean - mean * mean ) );
4747
4848
gl_FragColor = vec4( mean, std_dev, 0.0, 1.0 );
4949

src/renderers/webgl/WebGLLights.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Matrix4 } from '../../math/Matrix4.js';
33
import { Vector2 } from '../../math/Vector2.js';
44
import { Vector3 } from '../../math/Vector3.js';
55
import { UniformsLib } from '../shaders/UniformsLib.js';
6+
import { RGFormat } from '../../constants.js';
67

78
function UniformsCache() {
89

@@ -239,7 +240,23 @@ function WebGLLights( extensions ) {
239240
const intensity = light.intensity;
240241
const distance = light.distance;
241242

242-
const shadowMap = ( light.shadow && light.shadow.map ) ? ( light.shadow.map.depthTexture || light.shadow.map.texture ) : null;
243+
let shadowMap = null;
244+
245+
if ( light.shadow && light.shadow.map ) {
246+
247+
if ( light.shadow.map.texture.format === RGFormat ) {
248+
249+
// VSM uses color texture with blurred mean/std_dev
250+
shadowMap = light.shadow.map.texture;
251+
252+
} else {
253+
254+
// Other types use depth texture
255+
shadowMap = light.shadow.map.depthTexture || light.shadow.map.texture;
256+
257+
}
258+
259+
}
243260

244261
if ( light.isAmbientLight ) {
245262

src/renderers/webgl/WebGLShadowMap.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FrontSide, BackSide, DoubleSide, NearestFilter, LinearFilter, PCFShadowMap, VSMShadowMap, NoBlending, LessEqualCompare, GreaterEqualCompare, DepthFormat, UnsignedIntType, RGFormat, HalfFloatType, PCFSoftShadowMap, IdentityDepthPacking } from '../../constants.js';
1+
import { FrontSide, BackSide, DoubleSide, NearestFilter, LinearFilter, PCFShadowMap, VSMShadowMap, NoBlending, LessEqualCompare, GreaterEqualCompare, DepthFormat, UnsignedIntType, RGFormat, HalfFloatType, FloatType, PCFSoftShadowMap } from '../../constants.js';
22
import { WebGLRenderTarget } from '../WebGLRenderTarget.js';
33
import { WebGLCubeRenderTarget } from '../WebGLCubeRenderTarget.js';
44
import { MeshDepthMaterial } from '../../materials/MeshDepthMaterial.js';
@@ -26,7 +26,6 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
2626
_viewport = new Vector4(),
2727

2828
_depthMaterial = new MeshDepthMaterial(),
29-
_depthMaterialVSM = new MeshDepthMaterial( { depthPacking: IdentityDepthPacking } ),
3029
_distanceMaterial = new MeshDistanceMaterial(),
3130

3231
_materialCache = {},
@@ -215,6 +214,14 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
215214
} );
216215
shadow.map.texture.name = light.name + '.shadowMap';
217216

217+
// Native depth texture for VSM - depth is captured here, then blurred into the color texture
218+
shadow.map.depthTexture = new DepthTexture( _shadowMapSize.x, _shadowMapSize.y, FloatType );
219+
shadow.map.depthTexture.name = light.name + '.shadowMapDepth';
220+
shadow.map.depthTexture.format = DepthFormat;
221+
shadow.map.depthTexture.compareFunction = null; // For regular sampling (not shadow comparison)
222+
shadow.map.depthTexture.minFilter = NearestFilter;
223+
shadow.map.depthTexture.magFilter = NearestFilter;
224+
218225
} else {
219226

220227
if ( light.isPointLight ) {
@@ -339,9 +346,9 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
339346

340347
}
341348

342-
// vertical pass
349+
// vertical pass - read from native depth texture
343350

344-
shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture;
351+
shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.depthTexture;
345352
shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize;
346353
shadowMaterialVertical.uniforms.radius.value = shadow.radius;
347354
renderer.setRenderTarget( shadow.mapPass );
@@ -371,15 +378,7 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
371378

372379
} else {
373380

374-
if ( type === VSMShadowMap ) {
375-
376-
result = _depthMaterialVSM;
377-
378-
} else {
379-
380-
result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial;
381-
382-
}
381+
result = ( light.isPointLight === true ) ? _distanceMaterial : _depthMaterial;
383382

384383
if ( ( renderer.localClippingEnabled && material.clipShadows === true && Array.isArray( material.clippingPlanes ) && material.clippingPlanes.length !== 0 ) ||
385384
( material.displacementMap && material.displacementScale !== 0 ) ||

0 commit comments

Comments
 (0)