@@ -12,12 +12,13 @@ import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
12
12
import QuadMesh from '../../renderers/common/QuadMesh.js' ;
13
13
import { Loop } from '../utils/LoopNode.js' ;
14
14
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' ;
16
16
import { renderGroup } from '../core/UniformGroupNode.js' ;
17
17
import { viewZToLogarithmicDepth } from '../display/ViewportDepthNode.js' ;
18
18
import { objectPosition } from '../accessors/Object3DNode.js' ;
19
19
import { lightShadowMatrix } from '../accessors/Lights.js' ;
20
20
21
+ const shadowMaterialLib = /*@__PURE__ */ new WeakMap ( ) ;
21
22
const shadowWorldPosition = /*@__PURE__ */ vec3 ( ) . toVar ( 'shadowWorldPosition' ) ;
22
23
23
24
const linearDistance = /*@__PURE__ */ Fn ( ( [ position , cameraNear , cameraFar ] ) => {
@@ -43,6 +44,29 @@ const linearShadowDistance = ( light ) => {
43
44
44
45
} ;
45
46
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
+
46
70
export const BasicShadowFilter = /*@__PURE__ */ Fn ( ( { depthTexture, shadowCoord } ) => {
47
71
48
72
return texture ( depthTexture , shadowCoord . xy ) . compare ( shadowCoord . z ) ;
@@ -224,7 +248,6 @@ const _shadowFilterLib = [ BasicShadowFilter, PCFShadowFilter, PCFSoftShadowFilt
224
248
225
249
//
226
250
227
- let _overrideMaterial = null ;
228
251
const _quadMesh = /*@__PURE__ */ new QuadMesh ( ) ;
229
252
230
253
class ShadowNode extends Node {
@@ -332,18 +355,6 @@ class ShadowNode extends Node {
332
355
333
356
const shadowMapType = renderer . shadowMap . type ;
334
357
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
-
347
358
const depthTexture = new DepthTexture ( shadow . mapSize . width , shadow . mapSize . height ) ;
348
359
depthTexture . compareFunction = LessCompare ;
349
360
@@ -467,12 +478,15 @@ class ShadowNode extends Node {
467
478
468
479
const currentOverrideMaterial = scene . overrideMaterial ;
469
480
470
- scene . overrideMaterial = _overrideMaterial ;
481
+ scene . overrideMaterial = getShadowMaterial ( light ) ;
471
482
472
483
shadow . camera . layers . mask = camera . layers . mask ;
473
484
474
485
const currentRenderTarget = renderer . getRenderTarget ( ) ;
475
486
const currentRenderObjectFunction = renderer . getRenderObjectFunction ( ) ;
487
+ const currentMRT = renderer . getMRT ( ) ;
488
+
489
+ renderer . setMRT ( null ) ;
476
490
477
491
renderer . setRenderObjectFunction ( ( object , ...params ) => {
478
492
@@ -500,6 +514,8 @@ class ShadowNode extends Node {
500
514
501
515
renderer . setRenderTarget ( currentRenderTarget ) ;
502
516
517
+ renderer . setMRT ( currentMRT ) ;
518
+
503
519
scene . overrideMaterial = currentOverrideMaterial ;
504
520
505
521
}
0 commit comments