Kaynağa Gözat

Examples: Improve `webgpu_lights_ies_spotlight` (#30243)

* Examples: Added sphere to webgpu_lights_ies_spotlight.

* update example

---------

Co-authored-by: sunag <sunagbrasil@gmail.com>
mrdoob 1 yıl önce
ebeveyn
işleme
7d72e711aa

BIN
examples/screenshots/webgpu_lights_ies_spotlight.jpg


+ 64 - 7
examples/webgpu_lights_ies_spotlight.html

@@ -31,6 +31,8 @@
 
 			import { IESLoader } from 'three/addons/loaders/IESLoader.js';
 
+			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+
 			let renderer, scene, camera;
 			let lights;
 
@@ -53,42 +55,58 @@
 				//
 
 				const spotLight = new THREE.IESSpotLight( 0xff0000, 500 );
-				spotLight.position.set( 6.5, 1.5, 6.5 );
+				spotLight.position.set( 6.5, 3, 6.5 );
 				spotLight.angle = Math.PI / 8;
 				spotLight.penumbra = 0.7;
 				spotLight.distance = 20;
+				spotLight.castShadow = true;
 				spotLight.iesMap = iesTexture1;
+				spotLight.userData.helper = new THREE.SpotLightHelper( spotLight );
 				scene.add( spotLight );
+				scene.add( spotLight.target );
+				scene.add( spotLight.userData.helper );
 
 				//
 
 				const spotLight2 = new THREE.IESSpotLight( 0x00ff00, 500 );
-				spotLight2.position.set( 6.5, 1.5, - 6.5 );
+				spotLight2.position.set( - 6.5, 3, 6.5 );
 				spotLight2.angle = Math.PI / 8;
 				spotLight2.penumbra = 0.7;
 				spotLight2.distance = 20;
+				spotLight2.castShadow = true;
 				spotLight2.iesMap = iesTexture2;
+				spotLight2.userData.helper = new THREE.SpotLightHelper( spotLight2 );
 				scene.add( spotLight2 );
+				scene.add( spotLight2.target );
+				scene.add( spotLight2.userData.helper );
 
 				//
 
 				const spotLight3 = new THREE.IESSpotLight( 0x0000ff, 500 );
-				spotLight3.position.set( - 6.5, 1.5, - 6.5 );
+				spotLight3.position.set( - 6.5, 3, - 6.5 );
 				spotLight3.angle = Math.PI / 8;
 				spotLight3.penumbra = 0.7;
 				spotLight3.distance = 20;
+				spotLight3.castShadow = true;
 				spotLight3.iesMap = iesTexture3;
+				spotLight3.userData.helper = new THREE.SpotLightHelper( spotLight3 );
 				scene.add( spotLight3 );
+				scene.add( spotLight3.target );
+				scene.add( spotLight3.userData.helper );
 
 				//
 
 				const spotLight4 = new THREE.IESSpotLight( 0xffffff, 500 );
-				spotLight4.position.set( - 6.5, 1.5, 6.5 );
+				spotLight4.position.set( 6.5, 3, - 6.5 );
 				spotLight4.angle = Math.PI / 8;
 				spotLight4.penumbra = 0.7;
 				spotLight4.distance = 20;
+				spotLight4.castShadow = true;
 				spotLight4.iesMap = iesTexture4;
+				spotLight4.userData.helper = new THREE.SpotLightHelper( spotLight4 );
 				scene.add( spotLight4 );
+				scene.add( spotLight4.target );
+				scene.add( spotLight4.userData.helper );
 
 				//
 
@@ -96,20 +114,30 @@
 
 				//
 
-				const material = new THREE.MeshPhongMaterial( { color: 0x808080/*, dithering: true*/ } );
+				const material = new THREE.MeshPhongMaterial( { color: 0x999999/*, dithering: true*/ } );
 
 				const geometry = new THREE.PlaneGeometry( 200, 200 );
 
 				const mesh = new THREE.Mesh( geometry, material );
 				mesh.rotation.x = - Math.PI * 0.5;
+				mesh.receiveShadow = true;
 				scene.add( mesh );
 
+				const geometry2 = new THREE.BoxGeometry( 2, 2, 2 );
+				//const geometry2 = new THREE.IcosahedronGeometry( 1, 5 );
+
+				const mesh2 = new THREE.Mesh( geometry2, material );
+				mesh2.position.y = 1;
+				mesh2.castShadow = true;
+				scene.add( mesh2 );
+
 				//
 
 				renderer = new THREE.WebGPURenderer( { antialias: true } );
 				renderer.setPixelRatio( window.devicePixelRatio );
 				renderer.setSize( window.innerWidth, window.innerHeight );
 				renderer.setAnimationLoop( render );
+				renderer.shadowMap.enabled = true;
 				document.body.appendChild( renderer.domElement );
 
 				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, .1, 100 );
@@ -122,6 +150,28 @@
 
 				//
 
+				function setHelperVisible( value ) {
+
+					for ( let i = 0; i < lights.length; i ++ ) {
+
+						lights[ i ].userData.helper.visible = value;
+
+					}
+
+				}
+
+				setHelperVisible( false );
+
+				//
+
+				const gui = new GUI();
+
+				gui.add( { helper: false }, 'helper' ).onChange( ( v ) => setHelperVisible( v ) );
+
+				gui.open();
+
+				//
+
 				window.addEventListener( 'resize', onWindowResize );
 
 			}
@@ -137,11 +187,18 @@
 
 			function render( time ) {
 
-				time = ( time / 1000 ) * 2.0;
+				time = time / 1000;
 
 				for ( let i = 0; i < lights.length; i ++ ) {
 
-					lights[ i ].position.y = Math.sin( time + i ) + 0.97;
+					const t = ( Math.sin( ( time + i ) * ( Math.PI / 2 ) ) + 1 ) / 2;
+
+					const x = THREE.MathUtils.lerp( lights[ i ].position.x, 0, t );
+					const z = THREE.MathUtils.lerp( lights[ i ].position.z, 0, t );
+
+					lights[ i ].target.position.x = x;
+					lights[ i ].target.position.z = z;
+					if ( lights[ i ].userData.helper ) lights[ i ].userData.helper.update();
 
 				}
 

粤ICP备19079148号