Skip to content

Commit d4af8b2

Browse files
sunagcmhhelgeson
authored andcommitted
WebGPURenderer: Shadow fixes (mrdoob#29991)
* shadow revision * cleanup * cleanup * rev
1 parent 4288854 commit d4af8b2

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/nodes/lighting/PointShadowNode.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ const pointShadowFilter = /*@__PURE__*/ Fn( ( { filterFn, depthTexture, shadowCo
127127
const lightToPosition = shadowCoord.xyz.toVar();
128128
const lightToPositionLength = lightToPosition.length();
129129

130-
const cameraNearLocal = uniform( 'float' ).onRenderUpdate( () => shadow.camera.near );
131-
const cameraFarLocal = uniform( 'float' ).onRenderUpdate( () => shadow.camera.far );
130+
const cameraNearLocal = uniform( 'float' ).setGroup( renderGroup ).onRenderUpdate( () => shadow.camera.near );
131+
const cameraFarLocal = uniform( 'float' ).setGroup( renderGroup ).onRenderUpdate( () => shadow.camera.far );
132132
const bias = reference( 'bias', 'float', shadow ).setGroup( renderGroup );
133133
const mapSize = uniform( shadow.mapSize ).setGroup( renderGroup );
134134

src/nodes/lighting/ShadowNode.js

+31-15
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
1212
import QuadMesh from '../../renderers/common/QuadMesh.js';
1313
import { Loop } from '../utils/LoopNode.js';
1414
import { screenCoordinate } from '../display/ScreenNode.js';
15-
import { HalfFloatType, LessCompare, RGFormat, VSMShadowMap, WebGPUCoordinateSystem } from '../../constants.js';
15+
import { HalfFloatType, LessCompare, NoBlending, RGFormat, VSMShadowMap, WebGPUCoordinateSystem } from '../../constants.js';
1616
import { renderGroup } from '../core/UniformGroupNode.js';
1717
import { viewZToLogarithmicDepth } from '../display/ViewportDepthNode.js';
1818
import { objectPosition } from '../accessors/Object3DNode.js';
1919
import { lightShadowMatrix } from '../accessors/Lights.js';
2020

21+
const shadowMaterialLib = /*@__PURE__*/ new WeakMap();
2122
const shadowWorldPosition = /*@__PURE__*/ vec3().toVar( 'shadowWorldPosition' );
2223

2324
const linearDistance = /*@__PURE__*/ Fn( ( [ position, cameraNear, cameraFar ] ) => {
@@ -43,6 +44,29 @@ const linearShadowDistance = ( light ) => {
4344

4445
};
4546

47+
const getShadowMaterial = ( light ) => {
48+
49+
let material = shadowMaterialLib.get( light );
50+
51+
if ( material === undefined ) {
52+
53+
const depthNode = light.isPointLight ? linearShadowDistance( light ) : null;
54+
55+
material = new NodeMaterial();
56+
material.colorNode = vec4( 0, 0, 0, 1 );
57+
material.depthNode = depthNode;
58+
material.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.colorNode unintentionally when using material.shadowNode
59+
material.blending = NoBlending;
60+
material.name = 'ShadowMaterial';
61+
62+
shadowMaterialLib.set( light, material );
63+
64+
}
65+
66+
return material;
67+
68+
};
69+
4670
export const BasicShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord } ) => {
4771

4872
return texture( depthTexture, shadowCoord.xy ).compare( shadowCoord.z );
@@ -224,7 +248,6 @@ const _shadowFilterLib = [ BasicShadowFilter, PCFShadowFilter, PCFSoftShadowFilt
224248

225249
//
226250

227-
let _overrideMaterial = null;
228251
const _quadMesh = /*@__PURE__*/ new QuadMesh();
229252

230253
class ShadowNode extends Node {
@@ -332,18 +355,6 @@ class ShadowNode extends Node {
332355

333356
const shadowMapType = renderer.shadowMap.type;
334357

335-
if ( _overrideMaterial === null ) {
336-
337-
const depthNode = light.isPointLight ? linearShadowDistance( light ) : null;
338-
339-
_overrideMaterial = new NodeMaterial();
340-
_overrideMaterial.fragmentNode = vec4( 0, 0, 0, 1 );
341-
_overrideMaterial.depthNode = depthNode;
342-
_overrideMaterial.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.fragmentNode unintentionally when using material.shadowNode
343-
_overrideMaterial.name = 'ShadowMaterial';
344-
345-
}
346-
347358
const depthTexture = new DepthTexture( shadow.mapSize.width, shadow.mapSize.height );
348359
depthTexture.compareFunction = LessCompare;
349360

@@ -467,12 +478,15 @@ class ShadowNode extends Node {
467478

468479
const currentOverrideMaterial = scene.overrideMaterial;
469480

470-
scene.overrideMaterial = _overrideMaterial;
481+
scene.overrideMaterial = getShadowMaterial( light );
471482

472483
shadow.camera.layers.mask = camera.layers.mask;
473484

474485
const currentRenderTarget = renderer.getRenderTarget();
475486
const currentRenderObjectFunction = renderer.getRenderObjectFunction();
487+
const currentMRT = renderer.getMRT();
488+
489+
renderer.setMRT( null );
476490

477491
renderer.setRenderObjectFunction( ( object, ...params ) => {
478492

@@ -500,6 +514,8 @@ class ShadowNode extends Node {
500514

501515
renderer.setRenderTarget( currentRenderTarget );
502516

517+
renderer.setMRT( currentMRT );
518+
503519
scene.overrideMaterial = currentOverrideMaterial;
504520

505521
}

src/renderers/common/Renderer.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ class Renderer {
16331633
renderObject( object, scene, camera, geometry, material, group, lightsNode, clippingContext = null, passId = null ) {
16341634

16351635
let overridePositionNode;
1636-
let overrideFragmentNode;
1636+
let overrideColorNode;
16371637
let overrideDepthNode;
16381638

16391639
//
@@ -1653,6 +1653,9 @@ class Renderer {
16531653

16541654
}
16551655

1656+
overrideMaterial.alphaTest = material.alphaTest;
1657+
overrideMaterial.alphaMap = material.alphaMap;
1658+
16561659
if ( overrideMaterial.isShadowNodeMaterial ) {
16571660

16581661
overrideMaterial.side = material.shadowSide === null ? material.side : material.shadowSide;
@@ -1664,11 +1667,10 @@ class Renderer {
16641667

16651668
}
16661669

1667-
16681670
if ( material.castShadowNode && material.castShadowNode.isNode ) {
16691671

1670-
overrideFragmentNode = overrideMaterial.fragmentNode;
1671-
overrideMaterial.fragmentNode = material.castShadowNode;
1672+
overrideColorNode = overrideMaterial.colorNode;
1673+
overrideMaterial.colorNode = material.castShadowNode;
16721674

16731675
}
16741676

@@ -1710,9 +1712,9 @@ class Renderer {
17101712

17111713
}
17121714

1713-
if ( overrideFragmentNode !== undefined ) {
1715+
if ( overrideColorNode !== undefined ) {
17141716

1715-
scene.overrideMaterial.fragmentNode = overrideFragmentNode;
1717+
scene.overrideMaterial.colorNode = overrideColorNode;
17161718

17171719
}
17181720

0 commit comments

Comments
 (0)