forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShadowNode.js
593 lines (377 loc) · 17.8 KB
/
ShadowNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
import Node from '../core/Node.js';
import { NodeUpdateType } from '../core/constants.js';
import { float, vec2, vec3, vec4, If, int, Fn, nodeObject } from '../tsl/TSLBase.js';
import { reference } from '../accessors/ReferenceNode.js';
import { texture } from '../accessors/TextureNode.js';
import { positionWorld } from '../accessors/Position.js';
import { transformedNormalWorld } from '../accessors/Normal.js';
import { mix, fract, step, max, clamp, sqrt } from '../math/MathNode.js';
import { add, sub } from '../math/OperatorNode.js';
import { DepthTexture } from '../../textures/DepthTexture.js';
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
import QuadMesh from '../../renderers/common/QuadMesh.js';
import { Loop } from '../utils/LoopNode.js';
import { screenCoordinate } from '../display/ScreenNode.js';
import { HalfFloatType, LessCompare, NoBlending, RGFormat, VSMShadowMap, WebGPUCoordinateSystem } from '../../constants.js';
import { renderGroup } from '../core/UniformGroupNode.js';
import { viewZToLogarithmicDepth } from '../display/ViewportDepthNode.js';
import { objectPosition } from '../accessors/Object3DNode.js';
import { lightShadowMatrix } from '../accessors/Lights.js';
const shadowMaterialLib = /*@__PURE__*/ new WeakMap();
const shadowWorldPosition = /*@__PURE__*/ vec3().toVar( 'shadowWorldPosition' );
const linearDistance = /*@__PURE__*/ Fn( ( [ position, cameraNear, cameraFar ] ) => {
let dist = positionWorld.sub( position ).length();
dist = dist.sub( cameraNear ).div( cameraFar.sub( cameraNear ) );
dist = dist.saturate(); // clamp to [ 0, 1 ]
return dist;
} );
const linearShadowDistance = ( light ) => {
const camera = light.shadow.camera;
const nearDistance = reference( 'near', 'float', camera ).setGroup( renderGroup );
const farDistance = reference( 'far', 'float', camera ).setGroup( renderGroup );
const referencePosition = objectPosition( light );
return linearDistance( referencePosition, nearDistance, farDistance );
};
const getShadowMaterial = ( light ) => {
let material = shadowMaterialLib.get( light );
if ( material === undefined ) {
const depthNode = light.isPointLight ? linearShadowDistance( light ) : null;
material = new NodeMaterial();
material.colorNode = vec4( 0, 0, 0, 1 );
material.depthNode = depthNode;
material.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.colorNode unintentionally when using material.shadowNode
material.blending = NoBlending;
material.name = 'ShadowMaterial';
shadowMaterialLib.set( light, material );
}
return material;
};
export const BasicShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord } ) => {
return texture( depthTexture, shadowCoord.xy ).compare( shadowCoord.z );
} );
export const PCFShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord, shadow } ) => {
const depthCompare = ( uv, compare ) => texture( depthTexture, uv ).compare( compare );
const mapSize = reference( 'mapSize', 'vec2', shadow ).setGroup( renderGroup );
const radius = reference( 'radius', 'float', shadow ).setGroup( renderGroup );
const texelSize = vec2( 1 ).div( mapSize );
const dx0 = texelSize.x.negate().mul( radius );
const dy0 = texelSize.y.negate().mul( radius );
const dx1 = texelSize.x.mul( radius );
const dy1 = texelSize.y.mul( radius );
const dx2 = dx0.div( 2 );
const dy2 = dy0.div( 2 );
const dx3 = dx1.div( 2 );
const dy3 = dy1.div( 2 );
return add(
depthCompare( shadowCoord.xy.add( vec2( dx0, dy0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( 0, dy0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx1, dy0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx2, dy2 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( 0, dy2 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx3, dy2 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx0, 0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx2, 0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy, shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx3, 0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx1, 0 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx2, dy3 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( 0, dy3 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx3, dy3 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx0, dy1 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( 0, dy1 ) ), shadowCoord.z ),
depthCompare( shadowCoord.xy.add( vec2( dx1, dy1 ) ), shadowCoord.z )
).mul( 1 / 17 );
} );
export const PCFSoftShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord, shadow } ) => {
const depthCompare = ( uv, compare ) => texture( depthTexture, uv ).compare( compare );
const mapSize = reference( 'mapSize', 'vec2', shadow ).setGroup( renderGroup );
const texelSize = vec2( 1 ).div( mapSize );
const dx = texelSize.x;
const dy = texelSize.y;
const uv = shadowCoord.xy;
const f = fract( uv.mul( mapSize ).add( 0.5 ) );
uv.subAssign( f.mul( texelSize ) );
return add(
depthCompare( uv, shadowCoord.z ),
depthCompare( uv.add( vec2( dx, 0 ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( 0, dy ) ), shadowCoord.z ),
depthCompare( uv.add( texelSize ), shadowCoord.z ),
mix(
depthCompare( uv.add( vec2( dx.negate(), 0 ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( dx.mul( 2 ), 0 ) ), shadowCoord.z ),
f.x
),
mix(
depthCompare( uv.add( vec2( dx.negate(), dy ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( dx.mul( 2 ), dy ) ), shadowCoord.z ),
f.x
),
mix(
depthCompare( uv.add( vec2( 0, dy.negate() ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( 0, dy.mul( 2 ) ) ), shadowCoord.z ),
f.y
),
mix(
depthCompare( uv.add( vec2( dx, dy.negate() ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( dx, dy.mul( 2 ) ) ), shadowCoord.z ),
f.y
),
mix(
mix(
depthCompare( uv.add( vec2( dx.negate(), dy.negate() ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( dx.mul( 2 ), dy.negate() ) ), shadowCoord.z ),
f.x
),
mix(
depthCompare( uv.add( vec2( dx.negate(), dy.mul( 2 ) ) ), shadowCoord.z ),
depthCompare( uv.add( vec2( dx.mul( 2 ), dy.mul( 2 ) ) ), shadowCoord.z ),
f.x
),
f.y
)
).mul( 1 / 9 );
} );
// VSM
export const VSMShadowFilter = /*@__PURE__*/ Fn( ( { depthTexture, shadowCoord } ) => {
const occlusion = float( 1 ).toVar();
const distribution = texture( depthTexture ).uv( shadowCoord.xy ).rg;
const hardShadow = step( shadowCoord.z, distribution.x );
If( hardShadow.notEqual( float( 1.0 ) ), () => {
const distance = shadowCoord.z.sub( distribution.x );
const variance = max( 0, distribution.y.mul( distribution.y ) );
let softnessProbability = variance.div( variance.add( distance.mul( distance ) ) ); // Chebeyshevs inequality
softnessProbability = clamp( sub( softnessProbability, 0.3 ).div( 0.95 - 0.3 ) );
occlusion.assign( clamp( max( hardShadow, softnessProbability ) ) );
} );
return occlusion;
} );
const VSMPassVertical = /*@__PURE__*/ Fn( ( { samples, radius, size, shadowPass } ) => {
const mean = float( 0 ).toVar();
const squaredMean = float( 0 ).toVar();
const uvStride = samples.lessThanEqual( float( 1 ) ).select( float( 0 ), float( 2 ).div( samples.sub( 1 ) ) );
const uvStart = samples.lessThanEqual( float( 1 ) ).select( float( 0 ), float( - 1 ) );
Loop( { start: int( 0 ), end: int( samples ), type: 'int', condition: '<' }, ( { i } ) => {
const uvOffset = uvStart.add( float( i ).mul( uvStride ) );
const depth = shadowPass.uv( add( screenCoordinate.xy, vec2( 0, uvOffset ).mul( radius ) ).div( size ) ).x;
mean.addAssign( depth );
squaredMean.addAssign( depth.mul( depth ) );
} );
mean.divAssign( samples );
squaredMean.divAssign( samples );
const std_dev = sqrt( squaredMean.sub( mean.mul( mean ) ) );
return vec2( mean, std_dev );
} );
const VSMPassHorizontal = /*@__PURE__*/ Fn( ( { samples, radius, size, shadowPass } ) => {
const mean = float( 0 ).toVar();
const squaredMean = float( 0 ).toVar();
const uvStride = samples.lessThanEqual( float( 1 ) ).select( float( 0 ), float( 2 ).div( samples.sub( 1 ) ) );
const uvStart = samples.lessThanEqual( float( 1 ) ).select( float( 0 ), float( - 1 ) );
Loop( { start: int( 0 ), end: int( samples ), type: 'int', condition: '<' }, ( { i } ) => {
const uvOffset = uvStart.add( float( i ).mul( uvStride ) );
const distribution = shadowPass.uv( add( screenCoordinate.xy, vec2( uvOffset, 0 ).mul( radius ) ).div( size ) );
mean.addAssign( distribution.x );
squaredMean.addAssign( add( distribution.y.mul( distribution.y ), distribution.x.mul( distribution.x ) ) );
} );
mean.divAssign( samples );
squaredMean.divAssign( samples );
const std_dev = sqrt( squaredMean.sub( mean.mul( mean ) ) );
return vec2( mean, std_dev );
} );
const _shadowFilterLib = [ BasicShadowFilter, PCFShadowFilter, PCFSoftShadowFilter, VSMShadowFilter ];
//
const _quadMesh = /*@__PURE__*/ new QuadMesh();
class ShadowNode extends Node {
static get type() {
return 'ShadowNode';
}
constructor( light, shadow = null ) {
super();
this.light = light;
this.shadow = shadow || light.shadow;
this.shadowMap = null;
this.vsmShadowMapVertical = null;
this.vsmShadowMapHorizontal = null;
this.vsmMaterialVertical = null;
this.vsmMaterialHorizontal = null;
this.updateBeforeType = NodeUpdateType.RENDER;
this._node = null;
this.isShadowNode = true;
}
setupShadowFilter( builder, { filterFn, depthTexture, shadowCoord, shadow } ) {
const frustumTest = shadowCoord.x.greaterThanEqual( 0 )
.and( shadowCoord.x.lessThanEqual( 1 ) )
.and( shadowCoord.y.greaterThanEqual( 0 ) )
.and( shadowCoord.y.lessThanEqual( 1 ) )
.and( shadowCoord.z.lessThanEqual( 1 ) );
const shadowNode = filterFn( { depthTexture, shadowCoord, shadow } );
return frustumTest.select( shadowNode, float( 1 ) );
}
setupShadowCoord( builder, shadowPosition ) {
const { shadow } = this;
const { renderer } = builder;
const bias = reference( 'bias', 'float', shadow ).setGroup( renderGroup );
let shadowCoord = shadowPosition;
let coordZ;
if ( shadow.camera.isOrthographicCamera || renderer.logarithmicDepthBuffer !== true ) {
shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
coordZ = shadowCoord.z;
if ( renderer.coordinateSystem === WebGPUCoordinateSystem ) {
coordZ = coordZ.mul( 2 ).sub( 1 ); // WebGPU: Conversion [ 0, 1 ] to [ - 1, 1 ]
}
} else {
const w = shadowCoord.w;
shadowCoord = shadowCoord.xy.div( w ); // <-- Only divide X/Y coords since we don't need Z
// The normally available "cameraNear" and "cameraFar" nodes cannot be used here because they do not get
// updated to use the shadow camera. So, we have to declare our own "local" ones here.
// TODO: How do we get the cameraNear/cameraFar nodes to use the shadow camera so we don't have to declare local ones here?
const cameraNearLocal = reference( 'near', 'float', shadow.camera ).setGroup( renderGroup );
const cameraFarLocal = reference( 'far', 'float', shadow.camera ).setGroup( renderGroup );
coordZ = viewZToLogarithmicDepth( w.negate(), cameraNearLocal, cameraFarLocal );
}
shadowCoord = vec3(
shadowCoord.x,
shadowCoord.y.oneMinus(), // follow webgpu standards
coordZ.add( bias )
);
return shadowCoord;
}
getShadowFilterFn( type ) {
return _shadowFilterLib[ type ];
}
setupShadow( builder ) {
const { renderer } = builder;
const { light, shadow } = this;
const shadowMapType = renderer.shadowMap.type;
const depthTexture = new DepthTexture( shadow.mapSize.width, shadow.mapSize.height );
depthTexture.compareFunction = LessCompare;
const shadowMap = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
shadowMap.depthTexture = depthTexture;
shadow.camera.updateProjectionMatrix();
// VSM
if ( shadowMapType === VSMShadowMap ) {
depthTexture.compareFunction = null; // VSM does not use textureSampleCompare()/texture2DCompare()
this.vsmShadowMapVertical = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height, { format: RGFormat, type: HalfFloatType } );
this.vsmShadowMapHorizontal = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height, { format: RGFormat, type: HalfFloatType } );
const shadowPassVertical = texture( depthTexture );
const shadowPassHorizontal = texture( this.vsmShadowMapVertical.texture );
const samples = reference( 'blurSamples', 'float', shadow ).setGroup( renderGroup );
const radius = reference( 'radius', 'float', shadow ).setGroup( renderGroup );
const size = reference( 'mapSize', 'vec2', shadow ).setGroup( renderGroup );
let material = this.vsmMaterialVertical || ( this.vsmMaterialVertical = new NodeMaterial() );
material.fragmentNode = VSMPassVertical( { samples, radius, size, shadowPass: shadowPassVertical } ).context( builder.getSharedContext() );
material.name = 'VSMVertical';
material = this.vsmMaterialHorizontal || ( this.vsmMaterialHorizontal = new NodeMaterial() );
material.fragmentNode = VSMPassHorizontal( { samples, radius, size, shadowPass: shadowPassHorizontal } ).context( builder.getSharedContext() );
material.name = 'VSMHorizontal';
}
//
const shadowIntensity = reference( 'intensity', 'float', shadow ).setGroup( renderGroup );
const normalBias = reference( 'normalBias', 'float', shadow ).setGroup( renderGroup );
const shadowPosition = lightShadowMatrix( light ).mul( shadowWorldPosition.add( transformedNormalWorld.mul( normalBias ) ) );
const shadowCoord = this.setupShadowCoord( builder, shadowPosition );
//
const filterFn = shadow.filterNode || this.getShadowFilterFn( renderer.shadowMap.type ) || null;
if ( filterFn === null ) {
throw new Error( 'THREE.WebGPURenderer: Shadow map type not supported yet.' );
}
const shadowDepthTexture = ( shadowMapType === VSMShadowMap ) ? this.vsmShadowMapHorizontal.texture : depthTexture;
const shadowNode = this.setupShadowFilter( builder, { filterFn, shadowTexture: shadowMap.texture, depthTexture: shadowDepthTexture, shadowCoord, shadow } );
const shadowColor = texture( shadowMap.texture, shadowCoord );
const shadowOutput = mix( 1, shadowNode.rgb.mix( shadowColor, 1 ), shadowIntensity.mul( shadowColor.a ) ).toVar();
this.shadowMap = shadowMap;
this.shadow.map = shadowMap;
return shadowOutput;
}
setup( builder ) {
if ( builder.renderer.shadowMap.enabled === false ) return;
return Fn( ( { material } ) => {
shadowWorldPosition.assign( material.shadowPositionNode || positionWorld );
let node = this._node;
if ( node === null ) {
this._node = node = this.setupShadow( builder );
}
if ( builder.material.shadowNode ) { // @deprecated, r171
console.warn( 'THREE.NodeMaterial: ".shadowNode" is deprecated. Use ".castShadowNode" instead.' );
}
if ( builder.material.receivedShadowNode ) {
node = builder.material.receivedShadowNode( node );
}
return node;
} )();
}
renderShadow( frame ) {
const { shadow, shadowMap } = this;
const { renderer, scene } = frame;
shadowMap.setSize( shadow.mapSize.width, shadow.mapSize.height );
renderer.render( scene, shadow.camera );
}
updateShadow( frame ) {
const { shadowMap, light, shadow } = this;
const { renderer, scene, camera } = frame;
const shadowType = renderer.shadowMap.type;
const depthVersion = shadowMap.depthTexture.version;
this._depthVersionCached = depthVersion;
const currentOverrideMaterial = scene.overrideMaterial;
scene.overrideMaterial = getShadowMaterial( light );
shadow.camera.layers.mask = camera.layers.mask;
const currentRenderTarget = renderer.getRenderTarget();
const currentRenderObjectFunction = renderer.getRenderObjectFunction();
const currentMRT = renderer.getMRT();
renderer.setMRT( null );
renderer.setRenderObjectFunction( ( object, ...params ) => {
if ( object.castShadow === true || ( object.receiveShadow && shadowType === VSMShadowMap ) ) {
renderer.renderObject( object, ...params );
}
} );
renderer.setRenderTarget( shadowMap );
this.renderShadow( frame );
renderer.setRenderObjectFunction( currentRenderObjectFunction );
// vsm blur pass
if ( light.isPointLight !== true && shadowType === VSMShadowMap ) {
this.vsmPass( renderer );
}
renderer.setRenderTarget( currentRenderTarget );
renderer.setMRT( currentMRT );
scene.overrideMaterial = currentOverrideMaterial;
}
vsmPass( renderer ) {
const { shadow } = this;
this.vsmShadowMapVertical.setSize( shadow.mapSize.width, shadow.mapSize.height );
this.vsmShadowMapHorizontal.setSize( shadow.mapSize.width, shadow.mapSize.height );
renderer.setRenderTarget( this.vsmShadowMapVertical );
_quadMesh.material = this.vsmMaterialVertical;
_quadMesh.render( renderer );
renderer.setRenderTarget( this.vsmShadowMapHorizontal );
_quadMesh.material = this.vsmMaterialHorizontal;
_quadMesh.render( renderer );
}
dispose() {
this.shadowMap.dispose();
this.shadowMap = null;
if ( this.vsmShadowMapVertical !== null ) {
this.vsmShadowMapVertical.dispose();
this.vsmShadowMapVertical = null;
this.vsmMaterialVertical.dispose();
this.vsmMaterialVertical = null;
}
if ( this.vsmShadowMapHorizontal !== null ) {
this.vsmShadowMapHorizontal.dispose();
this.vsmShadowMapHorizontal = null;
this.vsmMaterialHorizontal.dispose();
this.vsmMaterialHorizontal = null;
}
this.updateBeforeType = NodeUpdateType.NONE;
}
updateBefore( frame ) {
const { shadow } = this;
const needsUpdate = shadow.needsUpdate || shadow.autoUpdate;
if ( needsUpdate ) {
this.updateShadow( frame );
if ( this.shadowMap.depthTexture.version === this._depthVersionCached ) {
shadow.needsUpdate = false;
}
}
}
}
export default ShadowNode;
export const shadow = ( light, shadow ) => nodeObject( new ShadowNode( light, shadow ) );