Skip to content

Commit 4a8c7f2

Browse files
committed
Added IdentityDepthPacking constant to avoid unnecessary depth inversion (1.0 - z) when rendering VSM shadow maps.
1 parent 5048f72 commit 4a8c7f2

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/constants.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,37 +1191,45 @@ export const TriangleStripDrawMode = 1;
11911191
export const TriangleFanDrawMode = 2;
11921192

11931193
/**
1194-
* Basic depth packing.
1194+
* The depth value is inverted (1.0 - z) for visualization purposes.
11951195
*
11961196
* @type {number}
11971197
* @constant
11981198
*/
11991199
export const BasicDepthPacking = 3200;
12001200

12011201
/**
1202-
* A depth value is packed into 32 bit RGBA.
1202+
* The depth value is packed into 32 bit RGBA.
12031203
*
12041204
* @type {number}
12051205
* @constant
12061206
*/
12071207
export const RGBADepthPacking = 3201;
12081208

12091209
/**
1210-
* A depth value is packed into 24 bit RGB.
1210+
* The depth value is packed into 24 bit RGB.
12111211
*
12121212
* @type {number}
12131213
* @constant
12141214
*/
12151215
export const RGBDepthPacking = 3202;
12161216

12171217
/**
1218-
* A depth value is packed into 16 bit RG.
1218+
* The depth value is packed into 16 bit RG.
12191219
*
12201220
* @type {number}
12211221
* @constant
12221222
*/
12231223
export const RGDepthPacking = 3203;
12241224

1225+
/**
1226+
* The depth value is not packed.
1227+
*
1228+
* @type {number}
1229+
* @constant
1230+
*/
1231+
export const IdentityDepthPacking = 3204;
1232+
12251233
/**
12261234
* Normal information is relative to the underlying surface.
12271235
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ 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+
114118
#endif
115119
116120
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void main() {
3232
3333
#else
3434
35-
float depth = 1.0 - texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, uvOffset ) * radius ) / resolution ).r;
35+
float depth = texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, uvOffset ) * radius ) / resolution ).r;
3636
mean += depth;
3737
squared_mean += depth * depth;
3838

src/renderers/webgl/WebGLShadowMap.js

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

2828
_depthMaterial = new MeshDepthMaterial(),
29+
_depthMaterialVSM = new MeshDepthMaterial( { depthPacking: IdentityDepthPacking } ),
2930
_distanceMaterial = new MeshDistanceMaterial(),
3031

3132
_materialCache = {},
@@ -98,11 +99,11 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
9899

99100
if ( _state.buffers.depth.getReversed() === true ) {
100101

101-
_state.buffers.color.setClear( 1, 1, 1, 1 );
102+
_state.buffers.color.setClear( 0, 0, 0, 0 );
102103

103104
} else {
104105

105-
_state.buffers.color.setClear( 0, 0, 0, 0 );
106+
_state.buffers.color.setClear( 1, 1, 1, 1 );
106107

107108
}
108109

@@ -370,7 +371,15 @@ function WebGLShadowMap( renderer, objects, capabilities ) {
370371

371372
} else {
372373

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

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

src/renderers/webgl/WebGLUniforms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ class WebGLUniforms {
11291129

11301130
for ( const u of this.seq ) {
11311131

1132-
if ( u.type === gl.SAMPLER_2D_SHADOW || u.type === gl.SAMPLER_CUBE_SHADOW ) {
1132+
if ( u.type === gl.SAMPLER_2D_SHADOW || u.type === gl.SAMPLER_CUBE_SHADOW || u.type === gl.SAMPLER_2D_ARRAY_SHADOW ) {
11331133

11341134
shadowSamplers.push( u );
11351135

0 commit comments

Comments
 (0)