|
|
@@ -1,18 +1,20 @@
|
|
|
import LightingNode from './LightingNode.js';
|
|
|
import { NodeUpdateType } from '../core/constants.js';
|
|
|
import { uniform } from '../core/UniformNode.js';
|
|
|
-import { float, vec2, vec3, vec4 } from '../tsl/TSLBase.js';
|
|
|
+import { float, vec2, vec3, vec4, If, int, Fn } from '../tsl/TSLBase.js';
|
|
|
import { reference } from '../accessors/ReferenceNode.js';
|
|
|
import { texture } from '../accessors/TextureNode.js';
|
|
|
import { positionWorld } from '../accessors/Position.js';
|
|
|
import { normalWorld } from '../accessors/Normal.js';
|
|
|
-import { mix, fract } from '../math/MathNode.js';
|
|
|
-import { add } from '../math/OperatorNode.js';
|
|
|
+import { mix, fract, step, max, clamp, sqrt } from '../math/MathNode.js';
|
|
|
+import { add, sub } from '../math/OperatorNode.js';
|
|
|
import { Color } from '../../math/Color.js';
|
|
|
import { DepthTexture } from '../../textures/DepthTexture.js';
|
|
|
-import { Fn } from '../tsl/TSLBase.js';
|
|
|
-import { LessCompare, WebGPUCoordinateSystem } from '../../constants.js';
|
|
|
import NodeMaterial from '../../materials/nodes/NodeMaterial.js';
|
|
|
+import QuadMesh from '../../renderers/common/QuadMesh.js';
|
|
|
+import { Loop } from '../utils/LoopNode.js';
|
|
|
+import { viewportCoordinate } from '../display/ViewportNode.js';
|
|
|
+import { HalfFloatType, LessCompare, RGFormat, VSMShadowMap, WebGPUCoordinateSystem } from '../../constants.js';
|
|
|
|
|
|
const BasicShadowMap = Fn( ( { depthTexture, shadowCoord } ) => {
|
|
|
|
|
|
@@ -115,11 +117,88 @@ const PCFSoftShadowMap = Fn( ( { depthTexture, shadowCoord, shadow } ) => {
|
|
|
|
|
|
} );
|
|
|
|
|
|
-const shadowFilterLib = [ BasicShadowMap, PCFShadowMap, PCFSoftShadowMap ];
|
|
|
+// VSM
|
|
|
+
|
|
|
+const VSMShadowMapNode = 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 = Fn( ( { samples, radius, resolution, 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( viewportCoordinate.xy, vec2( 0, uvOffset ).mul( radius ) ).div( resolution ) ).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 = Fn( ( { samples, radius, resolution, 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( viewportCoordinate.xy, vec2( uvOffset, 0 ).mul( radius ) ).div( resolution ) );
|
|
|
+ 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 = [ BasicShadowMap, PCFShadowMap, PCFSoftShadowMap, VSMShadowMapNode ];
|
|
|
|
|
|
//
|
|
|
|
|
|
-let overrideMaterial = null;
|
|
|
+let _overrideMaterial = null;
|
|
|
+const _quadMesh = /*@__PURE__*/ new QuadMesh();
|
|
|
|
|
|
class AnalyticLightNode extends LightingNode {
|
|
|
|
|
|
@@ -146,6 +225,12 @@ class AnalyticLightNode extends LightingNode {
|
|
|
this.shadowNode = null;
|
|
|
this.shadowColorNode = null;
|
|
|
|
|
|
+ this.vsmShadowMapVertical = null;
|
|
|
+ this.vsmShadowMapHorizontal = null;
|
|
|
+
|
|
|
+ this.vsmMaterialVertical = null;
|
|
|
+ this.vsmMaterialHorizontal = null;
|
|
|
+
|
|
|
this.isAnalyticLightNode = true;
|
|
|
|
|
|
}
|
|
|
@@ -170,24 +255,52 @@ class AnalyticLightNode extends LightingNode {
|
|
|
|
|
|
if ( shadowColorNode === null ) {
|
|
|
|
|
|
- if ( overrideMaterial === null ) {
|
|
|
+ if ( _overrideMaterial === null ) {
|
|
|
|
|
|
- overrideMaterial = new NodeMaterial();
|
|
|
- overrideMaterial.fragmentNode = vec4( 0, 0, 0, 1 );
|
|
|
- overrideMaterial.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.fragmentNode unintentionally when using material.shadowNode
|
|
|
- overrideMaterial.name = 'ShadowMaterial';
|
|
|
+ _overrideMaterial = new NodeMaterial();
|
|
|
+ _overrideMaterial.fragmentNode = vec4( 0, 0, 0, 1 );
|
|
|
+ _overrideMaterial.isShadowNodeMaterial = true; // Use to avoid other overrideMaterial override material.fragmentNode unintentionally when using material.shadowNode
|
|
|
+ _overrideMaterial.name = 'ShadowMaterial';
|
|
|
|
|
|
}
|
|
|
|
|
|
+ const shadowMapType = renderer.shadowMap.type;
|
|
|
+ const shadow = this.light.shadow;
|
|
|
+
|
|
|
const depthTexture = new DepthTexture();
|
|
|
depthTexture.compareFunction = LessCompare;
|
|
|
|
|
|
- const shadow = this.light.shadow;
|
|
|
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 );
|
|
|
+ const radius = reference( 'radius', 'float', shadow );
|
|
|
+ const resolution = reference( 'mapSize', 'vec2', shadow );
|
|
|
+
|
|
|
+ let material = this.vsmMaterialVertical || ( this.vsmMaterialVertical = new NodeMaterial() );
|
|
|
+ material.fragmentNode = VSMPassVertical( { samples, radius, resolution, shadowPass: shadowPassVertical } ).context( builder.getSharedContext() );
|
|
|
+ material.name = 'VSMVertical';
|
|
|
+
|
|
|
+ material = this.vsmMaterialHorizontal || ( this.vsmMaterialHorizontal = new NodeMaterial() );
|
|
|
+ material.fragmentNode = VSMPassHorizontal( { samples, radius, resolution, shadowPass: shadowPassHorizontal } ).context( builder.getSharedContext() );
|
|
|
+ material.name = 'VSMHorizontal';
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
//
|
|
|
|
|
|
const shadowIntensity = reference( 'intensity', 'float', shadow );
|
|
|
@@ -221,7 +334,7 @@ class AnalyticLightNode extends LightingNode {
|
|
|
|
|
|
//
|
|
|
|
|
|
- const filterFn = shadow.filterNode || shadowFilterLib[ renderer.shadowMap.type ] || null;
|
|
|
+ const filterFn = shadow.filterNode || _shadowFilterLib[ renderer.shadowMap.type ] || null;
|
|
|
|
|
|
if ( filterFn === null ) {
|
|
|
|
|
|
@@ -230,7 +343,7 @@ class AnalyticLightNode extends LightingNode {
|
|
|
}
|
|
|
|
|
|
const shadowColor = texture( shadowMap.texture, shadowCoord );
|
|
|
- const shadowNode = frustumTest.select( filterFn( { depthTexture, shadowCoord, shadow } ), float( 1 ) );
|
|
|
+ const shadowNode = frustumTest.select( filterFn( { depthTexture: ( shadowMapType === VSMShadowMap ) ? this.vsmShadowMapHorizontal.texture : depthTexture, shadowCoord, shadow } ), float( 1 ) );
|
|
|
|
|
|
this.shadowMap = shadowMap;
|
|
|
|
|
|
@@ -274,13 +387,14 @@ class AnalyticLightNode extends LightingNode {
|
|
|
const { shadowMap, light } = 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 = overrideMaterial;
|
|
|
+ scene.overrideMaterial = _overrideMaterial;
|
|
|
|
|
|
shadowMap.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
|
|
|
|
|
|
@@ -292,7 +406,7 @@ class AnalyticLightNode extends LightingNode {
|
|
|
|
|
|
renderer.setRenderObjectFunction( ( object, ...params ) => {
|
|
|
|
|
|
- if ( object.castShadow === true ) {
|
|
|
+ if ( object.castShadow === true || ( object.receiveShadow && shadowType === VSMShadowMap ) ) {
|
|
|
|
|
|
renderer.renderObject( object, ...params );
|
|
|
|
|
|
@@ -303,18 +417,64 @@ class AnalyticLightNode extends LightingNode {
|
|
|
renderer.setRenderTarget( shadowMap );
|
|
|
renderer.render( scene, light.shadow.camera );
|
|
|
|
|
|
- renderer.setRenderTarget( currentRenderTarget );
|
|
|
renderer.setRenderObjectFunction( currentRenderObjectFunction );
|
|
|
|
|
|
+ // vsm blur pass
|
|
|
+
|
|
|
+ if ( light.isPointLight !== true && shadowType === VSMShadowMap ) {
|
|
|
+
|
|
|
+ this.vsmPass( frame, light );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ renderer.setRenderTarget( currentRenderTarget );
|
|
|
+
|
|
|
scene.overrideMaterial = currentOverrideMaterial;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ vsmPass( frame, light ) {
|
|
|
+
|
|
|
+ const { renderer } = frame;
|
|
|
+
|
|
|
+ this.vsmShadowMapVertical.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
|
|
|
+ this.vsmShadowMapHorizontal.setSize( light.shadow.mapSize.width, light.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 );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
disposeShadow() {
|
|
|
|
|
|
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.shadowNode = null;
|
|
|
this.shadowColorNode = null;
|
|
|
|