Просмотр исходного кода

WebGPURenderer: Add support for VSM. (#29225)

* WebGPURenderer: Add support for VSM.

* AnalyticLightNode: Clean up.

* AnalyticLightNode: Clean up.

* fix `viewportCoordinate` in WebGLBackend

* Create webgpu_shadowmap_vsm.jpg

---------

Co-authored-by: sunag <sunagbrasil@gmail.com>
Michael Herzog 1 год назад
Родитель
Сommit
b5209aa70b

+ 1 - 0
examples/files.json

@@ -399,6 +399,7 @@
 		"webgpu_shadertoy",
 		"webgpu_shadertoy",
 		"webgpu_shadowmap",
 		"webgpu_shadowmap",
 		"webgpu_shadowmap_opacity",
 		"webgpu_shadowmap_opacity",
+		"webgpu_shadowmap_vsm",
 		"webgpu_skinning",
 		"webgpu_skinning",
 		"webgpu_skinning_instancing",
 		"webgpu_skinning_instancing",
 		"webgpu_skinning_points",
 		"webgpu_skinning_points",

BIN
examples/screenshots/webgpu_shadowmap_vsm.jpg


+ 240 - 0
examples/webgpu_shadowmap_vsm.html

@@ -0,0 +1,240 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - VSM Shadows example</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<link type="text/css" rel="stylesheet" href="main.css">
+	</head>
+	<body>
+		<div id="info">
+		<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - VSM Shadows example by <a href="https://github.com/supereggbert">Paul Brunt</a>
+		</div>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.webgpu.js",
+					"three/tsl": "../build/three.webgpu.js",
+					"three/addons/": "./jsm/"
+				}
+			}
+		</script>
+
+		<script type="module">
+
+			import * as THREE from 'three';
+
+			import Stats from 'three/addons/libs/stats.module.js';
+			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+
+			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
+
+			let camera, scene, renderer, clock, stats;
+			let dirLight, spotLight;
+			let torusKnot, dirGroup;
+			let config;
+
+			init();
+
+			function init() {
+
+				initScene();
+				initMisc();
+
+				// Init gui
+				const gui = new GUI();
+
+				config = {
+					spotlightRadius: 4,
+					spotlightSamples: 8,
+					dirlightRadius: 4,
+					dirlightSamples: 8,
+					animate: true
+				};
+
+				const spotlightFolder = gui.addFolder( 'Spotlight' );
+				spotlightFolder.add( config, 'spotlightRadius' ).name( 'radius' ).min( 0 ).max( 25 ).onChange( function ( value ) {
+
+					spotLight.shadow.radius = value;
+
+				} );
+
+				spotlightFolder.add( config, 'spotlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
+
+					spotLight.shadow.blurSamples = value;
+
+				} );
+				spotlightFolder.open();
+
+				const dirlightFolder = gui.addFolder( 'Directional Light' );
+				dirlightFolder.add( config, 'dirlightRadius' ).name( 'radius' ).min( 0 ).max( 25 ).onChange( function ( value ) {
+
+					dirLight.shadow.radius = value;
+
+				} );
+
+				dirlightFolder.add( config, 'dirlightSamples', 1, 25, 1 ).name( 'samples' ).onChange( function ( value ) {
+
+					dirLight.shadow.blurSamples = value;
+
+				} );
+				dirlightFolder.open();
+
+				gui.add( config, 'animate' );
+
+				document.body.appendChild( renderer.domElement );
+				window.addEventListener( 'resize', onWindowResize );
+
+			}
+
+			function initScene() {
+
+				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
+				camera.position.set( 0, 10, 30 );
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0x222244 );
+				scene.fog = new THREE.Fog( 0x222244, 50, 100 );
+
+				// Lights
+
+				scene.add( new THREE.AmbientLight( 0x444444 ) );
+
+				spotLight = new THREE.SpotLight( 0xff8888, 400 );
+				spotLight.angle = Math.PI / 5;
+				spotLight.penumbra = 0.3;
+				spotLight.position.set( 8, 10, 5 );
+				spotLight.castShadow = true;
+				spotLight.shadow.camera.near = 8;
+				spotLight.shadow.camera.far = 200;
+				spotLight.shadow.mapSize.width = 256;
+				spotLight.shadow.mapSize.height = 256;
+				spotLight.shadow.bias = - 0.002;
+				spotLight.shadow.radius = 4;
+				scene.add( spotLight );
+
+
+				dirLight = new THREE.DirectionalLight( 0x8888ff, 3 );
+				dirLight.position.set( 3, 12, 17 );
+				dirLight.castShadow = true;
+				dirLight.shadow.camera.near = 0.1;
+				dirLight.shadow.camera.far = 500;
+				dirLight.shadow.camera.right = 17;
+				dirLight.shadow.camera.left = - 17;
+				dirLight.shadow.camera.top	= 17;
+				dirLight.shadow.camera.bottom = - 17;
+				dirLight.shadow.mapSize.width = 512;
+				dirLight.shadow.mapSize.height = 512;
+				dirLight.shadow.radius = 4;
+				dirLight.shadow.bias = - 0.0005;
+
+				dirGroup = new THREE.Group();
+				dirGroup.add( dirLight );
+				scene.add( dirGroup );
+
+				// Geometry
+
+				const geometry = new THREE.TorusKnotGeometry( 25, 8, 75, 20 );
+				const material = new THREE.MeshPhongMaterial( {
+					color: 0x999999,
+					shininess: 0,
+					specular: 0x222222
+				} );
+
+				torusKnot = new THREE.Mesh( geometry, material );
+				torusKnot.scale.multiplyScalar( 1 / 18 );
+				torusKnot.position.y = 3;
+				torusKnot.castShadow = true;
+				torusKnot.receiveShadow = true;
+				scene.add( torusKnot );
+
+				const cylinderGeometry = new THREE.CylinderGeometry( 0.75, 0.75, 7, 32 );
+
+				const pillar1 = new THREE.Mesh( cylinderGeometry, material );
+				pillar1.position.set( 8, 3.5, 8 );
+				pillar1.castShadow = true;
+				pillar1.receiveShadow = true;
+
+				const pillar2 = pillar1.clone();
+				pillar2.position.set( 8, 3.5, - 8 );
+				const pillar3 = pillar1.clone();
+				pillar3.position.set( - 8, 3.5, 8 );
+				const pillar4 = pillar1.clone();
+				pillar4.position.set( - 8, 3.5, - 8 );
+
+				scene.add( pillar1 );
+				scene.add( pillar2 );
+				scene.add( pillar3 );
+				scene.add( pillar4 );
+
+				const planeGeometry = new THREE.PlaneGeometry( 200, 200 );
+				const planeMaterial = new THREE.MeshPhongMaterial( {
+					color: 0x999999,
+					shininess: 0,
+					specular: 0x111111
+				} );
+
+				const ground = new THREE.Mesh( planeGeometry, planeMaterial );
+				ground.rotation.x = - Math.PI / 2;
+				ground.scale.multiplyScalar( 3 );
+				ground.castShadow = true;
+				ground.receiveShadow = true;
+				scene.add( ground );
+
+			}
+
+			function initMisc() {
+
+				renderer = new THREE.WebGPURenderer( { antialias: true } );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( animate );
+				renderer.shadowMap.enabled = true;
+				renderer.shadowMap.type = THREE.VSMShadowMap;
+
+				// Mouse control
+				const controls = new OrbitControls( camera, renderer.domElement );
+				controls.target.set( 0, 2, 0 );
+				controls.update();
+
+				clock = new THREE.Clock();
+
+				stats = new Stats();
+				document.body.appendChild( stats.dom );
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function animate( time ) {
+
+				const delta = clock.getDelta();
+
+				if ( config.animate === true ) {
+
+					torusKnot.rotation.x += 0.25 * delta;
+					torusKnot.rotation.y += 0.5 * delta;
+					torusKnot.rotation.z += 1 * delta;
+
+					dirGroup.rotation.y += 0.7 * delta;
+					dirLight.position.z = 17 + Math.sin( time * 0.001 ) * 5;
+
+				}
+
+				renderer.render( scene, camera );
+
+				stats.update();
+
+			}
+
+		</script>
+	</body>
+</html>

+ 178 - 18
src/nodes/lighting/AnalyticLightNode.js

@@ -1,18 +1,20 @@
 import LightingNode from './LightingNode.js';
 import LightingNode from './LightingNode.js';
 import { NodeUpdateType } from '../core/constants.js';
 import { NodeUpdateType } from '../core/constants.js';
 import { uniform } from '../core/UniformNode.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 { reference } from '../accessors/ReferenceNode.js';
 import { texture } from '../accessors/TextureNode.js';
 import { texture } from '../accessors/TextureNode.js';
 import { positionWorld } from '../accessors/Position.js';
 import { positionWorld } from '../accessors/Position.js';
 import { normalWorld } from '../accessors/Normal.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 { Color } from '../../math/Color.js';
 import { DepthTexture } from '../../textures/DepthTexture.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 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 } ) => {
 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 {
 class AnalyticLightNode extends LightingNode {
 
 
@@ -146,6 +225,12 @@ class AnalyticLightNode extends LightingNode {
 		this.shadowNode = null;
 		this.shadowNode = null;
 		this.shadowColorNode = null;
 		this.shadowColorNode = null;
 
 
+		this.vsmShadowMapVertical = null;
+		this.vsmShadowMapHorizontal = null;
+
+		this.vsmMaterialVertical = null;
+		this.vsmMaterialHorizontal = null;
+
 		this.isAnalyticLightNode = true;
 		this.isAnalyticLightNode = true;
 
 
 	}
 	}
@@ -170,24 +255,52 @@ class AnalyticLightNode extends LightingNode {
 
 
 		if ( shadowColorNode === null ) {
 		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();
 			const depthTexture = new DepthTexture();
 			depthTexture.compareFunction = LessCompare;
 			depthTexture.compareFunction = LessCompare;
 
 
-			const shadow = this.light.shadow;
 			const shadowMap = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
 			const shadowMap = builder.createRenderTarget( shadow.mapSize.width, shadow.mapSize.height );
 			shadowMap.depthTexture = depthTexture;
 			shadowMap.depthTexture = depthTexture;
 
 
 			shadow.camera.updateProjectionMatrix();
 			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 );
 			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 ) {
 			if ( filterFn === null ) {
 
 
@@ -230,7 +343,7 @@ class AnalyticLightNode extends LightingNode {
 			}
 			}
 
 
 			const shadowColor = texture( shadowMap.texture, shadowCoord );
 			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;
 			this.shadowMap = shadowMap;
 
 
@@ -274,13 +387,14 @@ class AnalyticLightNode extends LightingNode {
 		const { shadowMap, light } = this;
 		const { shadowMap, light } = this;
 		const { renderer, scene, camera } = frame;
 		const { renderer, scene, camera } = frame;
 
 
+		const shadowType = renderer.shadowMap.type;
 
 
 		const depthVersion = shadowMap.depthTexture.version;
 		const depthVersion = shadowMap.depthTexture.version;
 		this._depthVersionCached = depthVersion;
 		this._depthVersionCached = depthVersion;
 
 
 		const currentOverrideMaterial = scene.overrideMaterial;
 		const currentOverrideMaterial = scene.overrideMaterial;
 
 
-		scene.overrideMaterial = overrideMaterial;
+		scene.overrideMaterial = _overrideMaterial;
 
 
 		shadowMap.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
 		shadowMap.setSize( light.shadow.mapSize.width, light.shadow.mapSize.height );
 
 
@@ -292,7 +406,7 @@ class AnalyticLightNode extends LightingNode {
 
 
 		renderer.setRenderObjectFunction( ( object, ...params ) => {
 		renderer.setRenderObjectFunction( ( object, ...params ) => {
 
 
-			if ( object.castShadow === true ) {
+			if ( object.castShadow === true || ( object.receiveShadow && shadowType === VSMShadowMap ) ) {
 
 
 				renderer.renderObject( object, ...params );
 				renderer.renderObject( object, ...params );
 
 
@@ -303,18 +417,64 @@ class AnalyticLightNode extends LightingNode {
 		renderer.setRenderTarget( shadowMap );
 		renderer.setRenderTarget( shadowMap );
 		renderer.render( scene, light.shadow.camera );
 		renderer.render( scene, light.shadow.camera );
 
 
-		renderer.setRenderTarget( currentRenderTarget );
 		renderer.setRenderObjectFunction( currentRenderObjectFunction );
 		renderer.setRenderObjectFunction( currentRenderObjectFunction );
 
 
+		// vsm blur pass
+
+		if ( light.isPointLight !== true && shadowType === VSMShadowMap ) {
+
+			this.vsmPass( frame, light );
+
+		}
+
+		renderer.setRenderTarget( currentRenderTarget );
+
 		scene.overrideMaterial = currentOverrideMaterial;
 		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() {
 	disposeShadow() {
 
 
 		this.shadowMap.dispose();
 		this.shadowMap.dispose();
 		this.shadowMap = null;
 		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.shadowNode = null;
 		this.shadowColorNode = null;
 		this.shadowColorNode = null;
 
 

粤ICP备19079148号