|
@@ -2,14 +2,14 @@ import LightingNode from './LightingNode.js';
|
|
|
import { NodeUpdateType } from '../core/constants.js';
|
|
|
import { uniform } from '../core/UniformNode.js';
|
|
|
import { addNodeClass } from '../core/Node.js';
|
|
|
-import { vec3 } from '../shadernode/ShaderNode.js';
|
|
|
+import { /*vec2,*/ vec3 } from '../shadernode/ShaderNode.js';
|
|
|
import { reference } from '../accessors/ReferenceNode.js';
|
|
|
import { texture } from '../accessors/TextureNode.js';
|
|
|
import { positionWorld } from '../accessors/PositionNode.js';
|
|
|
-//import { step } from '../math/MathNode.js';
|
|
|
-import { cond } from '../math/CondNode.js';
|
|
|
+import { normalWorld } from '../accessors/NormalNode.js';
|
|
|
+//import { add } from '../math/OperatorNode.js';
|
|
|
|
|
|
-import { Color, DepthTexture, NearestFilter } from 'three';
|
|
|
+import { Color, DepthTexture, NearestFilter, LessCompare } from 'three';
|
|
|
|
|
|
let depthMaterial = null;
|
|
|
|
|
@@ -53,7 +53,7 @@ class AnalyticLightNode extends LightingNode {
|
|
|
depthTexture.magFilter = NearestFilter;
|
|
|
depthTexture.image.width = shadow.mapSize.width;
|
|
|
depthTexture.image.height = shadow.mapSize.height;
|
|
|
- //depthTexture.compareFunction = THREE.LessCompare;
|
|
|
+ depthTexture.compareFunction = LessCompare;
|
|
|
|
|
|
rtt.depthTexture = depthTexture;
|
|
|
|
|
@@ -62,31 +62,69 @@ class AnalyticLightNode extends LightingNode {
|
|
|
//
|
|
|
|
|
|
const bias = reference( 'bias', 'float', shadow );
|
|
|
+ const normalBias = reference( 'normalBias', 'float', shadow );
|
|
|
|
|
|
- //const diffuseFactor = normalView.dot( objectViewPosition( this.light ).sub( positionView ).normalize().negate() );
|
|
|
- //bias = mix( bias, 0, diffuseFactor );
|
|
|
-
|
|
|
- let shadowCoord = uniform( shadow.matrix ).mul( positionWorld );
|
|
|
+ let shadowCoord = uniform( shadow.matrix ).mul( positionWorld.add( normalWorld.mul( normalBias ) ) );
|
|
|
shadowCoord = shadowCoord.xyz.div( shadowCoord.w );
|
|
|
|
|
|
+ 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 ) );
|
|
|
+
|
|
|
shadowCoord = vec3(
|
|
|
shadowCoord.x,
|
|
|
- shadowCoord.y.oneMinus(),
|
|
|
- shadowCoord.z
|
|
|
+ shadowCoord.y.oneMinus(), // WebGPU: Flip Y
|
|
|
+ shadowCoord.z.add( bias ).mul( 2 ).sub( 1 ) // WebGPU: Convertion [ 0, 1 ] to [ - 1, 1 ]
|
|
|
);
|
|
|
|
|
|
- // @TODO: Optimize using WebGPU compare-sampler
|
|
|
-
|
|
|
- let depth = texture( depthTexture, shadowCoord.xy );
|
|
|
- depth = depth.mul( .5 ).add( .5 ).add( bias );
|
|
|
-
|
|
|
- shadowNode = cond( shadowCoord.z.lessThan( depth ).or( shadowCoord.y.lessThan( .000001 ) /*@TODO: find the cause and remove it soon */ ), 1, 0 );
|
|
|
- //shadowNode = step( shadowCoord.z, depth );
|
|
|
-
|
|
|
+ const textureCompare = ( depthTexture, shadowCoord, compare ) => texture( depthTexture, shadowCoord ).compare( compare );
|
|
|
+ //const textureCompare = ( depthTexture, shadowCoord, compare ) => compare.step( texture( depthTexture, shadowCoord ) );
|
|
|
+
|
|
|
+ // BasicShadowMap
|
|
|
+
|
|
|
+ shadowNode = textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z );
|
|
|
+
|
|
|
+ // PCFShadowMap
|
|
|
+ /*
|
|
|
+ const mapSize = reference( 'mapSize', 'vec2', shadow );
|
|
|
+ const radius = reference( 'radius', 'float', shadow );
|
|
|
+
|
|
|
+ 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.mul( 2 );
|
|
|
+ const dy2 = dy0.mul( 2 );
|
|
|
+ const dx3 = dx1.mul( 2 );
|
|
|
+ const dy3 = dy1.mul( 2 );
|
|
|
+
|
|
|
+ shadowNode = add(
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy2 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy2 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy2 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, 0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, 0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy, shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, 0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, 0 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx2, dy3 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy3 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx3, dy3 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx0, dy1 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( 0, dy1 ) ), shadowCoord.z ),
|
|
|
+ textureCompare( depthTexture, shadowCoord.xy.add( vec2( dx1, dy1 ) ), shadowCoord.z )
|
|
|
+ ).mul( 1 / 17 );
|
|
|
+ */
|
|
|
//
|
|
|
|
|
|
this.rtt = rtt;
|
|
|
- this.colorNode = this.colorNode.mul( shadowNode );
|
|
|
+ this.colorNode = this.colorNode.mul( frustumTest.mix( 1, shadowNode ) );
|
|
|
|
|
|
this.shadowNode = shadowNode;
|
|
|
|