Răsfoiți Sursa

Nodes: Add `LightProbeNode`. (#29068)

* Nodes: Add `LightProbeNode`.

* E2E: Update screenshots.
Michael Herzog 1 an în urmă
părinte
comite
5a7ef89952

+ 1 - 0
examples/files.json

@@ -329,6 +329,7 @@
 		"webgpu_instance_points",
 		"webgpu_instance_uniform",
 		"webgpu_instancing_morph",
+		"webgpu_lightprobe",
 		"webgpu_lights_custom",
 		"webgpu_lights_ies_spotlight",
 		"webgpu_lights_phong",

BIN
examples/screenshots/webgpu_lightprobe.jpg


+ 173 - 0
examples/webgpu_lightprobe.html

@@ -0,0 +1,173 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - light probe</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> webgpu - light probe
+		</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 { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+
+			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
+			import { LightProbeGenerator } from 'three/addons/lights/LightProbeGenerator.js';
+
+			let mesh, renderer, scene, camera;
+
+			let gui;
+
+			let lightProbe;
+			let directionalLight;
+
+			// linear color space
+			const API = {
+				lightProbeIntensity: 1.0,
+				directionalLightIntensity: 0.6,
+				envMapIntensity: 1
+			};
+
+			init();
+
+			function init() {
+
+				// renderer
+				renderer = new THREE.WebGPURenderer( { antialias: true } );
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( animate );
+				document.body.appendChild( renderer.domElement );
+
+				// tone mapping
+				renderer.toneMapping = THREE.NoToneMapping;
+
+
+				// scene
+				scene = new THREE.Scene();
+
+				// camera
+				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
+				camera.position.set( 0, 0, 30 );
+
+				// controls
+				const controls = new OrbitControls( camera, renderer.domElement );
+				controls.minDistance = 10;
+				controls.maxDistance = 50;
+				controls.enablePan = false;
+
+				// probe
+				lightProbe = new THREE.LightProbe();
+				scene.add( lightProbe );
+
+				// light
+				directionalLight = new THREE.DirectionalLight( 0xffffff, API.directionalLightIntensity );
+				directionalLight.position.set( 10, 10, 10 );
+				scene.add( directionalLight );
+
+				// envmap
+				const genCubeUrls = function ( prefix, postfix ) {
+
+					return [
+						prefix + 'px' + postfix, prefix + 'nx' + postfix,
+						prefix + 'py' + postfix, prefix + 'ny' + postfix,
+						prefix + 'pz' + postfix, prefix + 'nz' + postfix
+					];
+
+				};
+
+				const urls = genCubeUrls( 'textures/cube/pisa/', '.png' );
+
+				new THREE.CubeTextureLoader().load( urls, function ( cubeTexture ) {
+
+					scene.background = cubeTexture;
+
+					lightProbe.copy( LightProbeGenerator.fromCubeTexture( cubeTexture ) );
+
+					const geometry = new THREE.SphereGeometry( 5, 64, 32 );
+					//const geometry = new THREE.TorusKnotGeometry( 4, 1.5, 256, 32, 2, 3 );
+
+					const material = new THREE.MeshStandardMaterial( {
+						color: 0xffffff,
+						metalness: 0,
+						roughness: 0,
+						envMap: cubeTexture,
+						envMapIntensity: API.envMapIntensity,
+					} );
+
+					// mesh
+					mesh = new THREE.Mesh( geometry, material );
+					scene.add( mesh );
+
+				} );
+
+
+				// gui
+				gui = new GUI( { title: 'Intensity' } );
+
+				gui.add( API, 'lightProbeIntensity', 0, 1, 0.02 )
+					.name( 'light probe' )
+					.onChange( function () {
+
+						lightProbe.intensity = API.lightProbeIntensity;
+
+					} );
+
+				gui.add( API, 'directionalLightIntensity', 0, 1, 0.02 )
+					.name( 'directional light' )
+					.onChange( function () {
+
+						directionalLight.intensity = API.directionalLightIntensity;
+
+					} );
+
+				gui.add( API, 'envMapIntensity', 0, 1, 0.02 )
+					.name( 'envMap' )
+					.onChange( function () {
+
+						mesh.material.envMapIntensity = API.envMapIntensity;
+
+					} );
+
+				// listener
+				window.addEventListener( 'resize', onWindowResize );
+
+			}
+
+			function onWindowResize() {
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+
+			}
+
+			function animate() {
+
+				renderer.render( scene, camera );
+
+			}
+
+		</script>
+
+	</body>
+</html>

+ 1 - 0
src/nodes/Nodes.js

@@ -180,6 +180,7 @@ export { default as LightsNode, lights, lightsNode, addLightNode } from './light
 export { default as LightingNode /* @TODO: lighting (abstract), light */ } from './lighting/LightingNode.js';
 export { default as LightingContextNode, lightingContext } from './lighting/LightingContextNode.js';
 export { default as HemisphereLightNode } from './lighting/HemisphereLightNode.js';
+export { default as LightProbeNode } from './lighting/LightProbeNode.js';
 export { default as EnvironmentNode } from './lighting/EnvironmentNode.js';
 export { default as BasicEnvironmentNode } from './lighting/BasicEnvironmentNode.js';
 export { default as IrradianceNode } from './lighting/IrradianceNode.js';

+ 80 - 0
src/nodes/lighting/LightProbeNode.js

@@ -0,0 +1,80 @@
+import AnalyticLightNode from './AnalyticLightNode.js';
+import { addLightNode } from './LightsNode.js';
+import { normalWorld } from '../accessors/NormalNode.js';
+import { addNodeClass } from '../core/Node.js';
+import { LightProbe } from '../../lights/LightProbe.js';
+import { uniformArray } from '../accessors/UniformArrayNode.js';
+import { Fn } from '../shadernode/ShaderNode.js';
+import { mul } from '../math/OperatorNode.js';
+import { Vector3 } from '../../math/Vector3.js';
+
+class LightProbeNode extends AnalyticLightNode {
+
+	constructor( light = null ) {
+
+		super( light );
+
+		const array = [];
+
+		for ( let i = 0; i < 9; i ++ ) array.push( new Vector3() );
+
+		this.lightProbe = uniformArray( array );
+
+	}
+
+	update( frame ) {
+
+		const { light } = this;
+
+		super.update( frame );
+
+		//
+
+		for ( let i = 0; i < 9; i ++ ) {
+
+			this.lightProbe.array[ i ].copy( light.sh.coefficients[ i ] ).multiplyScalar( light.intensity );
+
+		}
+
+	}
+
+	setup( builder ) {
+
+		const irradiance = shGetIrradianceAt( normalWorld, this.lightProbe );
+
+		builder.context.irradiance.addAssign( irradiance );
+
+	}
+
+}
+
+const shGetIrradianceAt = Fn( ( [ normal, shCoefficients ] ) => {
+
+	// normal is assumed to have unit length
+
+	const x = normal.x, y = normal.y, z = normal.z;
+
+	// band 0
+	const result = shCoefficients.element( 0 ).mul( 0.886227 );
+
+	// band 1
+	result.addAssign( shCoefficients.element( 1 ).mul( 2.0 * 0.511664 ).mul( y ) );
+	result.addAssign( shCoefficients.element( 2 ).mul( 2.0 * 0.511664 ).mul( z ) );
+	result.addAssign( shCoefficients.element( 3 ).mul( 2.0 * 0.511664 ).mul( x ) );
+
+	// band 2
+	result.addAssign( shCoefficients.element( 4 ).mul( 2.0 * 0.429043 ).mul( x ).mul( y ) );
+	result.addAssign( shCoefficients.element( 5 ).mul( 2.0 * 0.429043 ).mul( y ).mul( z ) );
+	result.addAssign( shCoefficients.element( 6 ).mul( z.mul( z ).mul( 0.743125 ).sub( 0.247708 ) ) );
+	result.addAssign( shCoefficients.element( 7 ).mul( 2.0 * 0.429043 ).mul( x ).mul( z ) );
+	result.addAssign( shCoefficients.element( 8 ).mul( 0.429043 ).mul( mul( x, x ).sub( mul( y, y ) ) ) );
+
+	return result;
+
+} );
+
+export default LightProbeNode;
+
+addNodeClass( 'LightProbeNode', LightProbeNode );
+
+addLightNode( LightProbe, LightProbeNode );

粤ICP备19079148号