瀏覽代碼

Examples: Improved webgl_lights_spotlight.

Mr.doob 2 年之前
父節點
當前提交
5ae10928d0
共有 2 個文件被更改,包括 77 次插入85 次删除
  1. 二進制
      examples/screenshots/webgl_lights_spotlight.jpg
  2. 77 85
      examples/webgl_lights_spotlight.html

二進制
examples/screenshots/webgl_lights_spotlight.jpg


+ 77 - 85
examples/webgl_lights_spotlight.html

@@ -9,7 +9,7 @@
 	<body>
 
 		<div id="info">
-			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - spotlight by <a href="http://master-domain.com" target="_blank" rel="noopener">Master James</a><br />
+			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - spotlight<br />
 		</div>
 
 		<!-- Import maps polyfill -->
@@ -31,42 +31,47 @@
 
 			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
 
+			import { PLYLoader } from 'three/addons/loaders/PLYLoader.js';
 			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
 
-			let renderer, scene, camera, material;
+			let renderer, scene, camera;
 
-			let spotLight, lightHelper, shadowCameraHelper;
+			let spotLight, lightHelper;
 
-			let textures = { none: null };
-
-			let gui;
+			init();
 
 			function init() {
 
 				renderer = new THREE.WebGLRenderer();
 				renderer.setPixelRatio( window.devicePixelRatio );
 				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( render );
 				renderer.shadowMap.enabled = true;
 				renderer.shadowMap.type = THREE.PCFSoftShadowMap;
 				renderer.outputEncoding = THREE.sRGBEncoding;
+				renderer.toneMapping = THREE.ACESFilmicToneMapping;
 				document.body.appendChild( renderer.domElement );
 
 
 				scene = new THREE.Scene();
 
 				camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 1000 );
-				camera.position.set( 160, 40, 10 );
+				camera.position.set( 76, 50, 10 );
+				camera.rotation.set( - 1.29, 1.15, 1.26 );
 
 				const controls = new OrbitControls( camera, renderer.domElement );
-				controls.addEventListener( 'change', render );
 				controls.minDistance = 20;
 				controls.maxDistance = 500;
+				controls.target.set( 0, 18, 0 );
+				controls.update();
 
-				const ambient = new THREE.AmbientLight( 0xffffff, 0.1 );
+				const ambient = new THREE.HemisphereLight( 0xffffff, 0x444444, 0.05 );
 				scene.add( ambient );
 
 				const loader = new THREE.TextureLoader().setPath( 'textures/' );
-				const filenames = [ 'uv_grid_opengl.jpg', 'sprite2.png', 'colors.png' ];
+				const filenames = [ 'disturb.jpg', 'uv_grid_opengl.jpg', 'colors.png' ];
+
+				const textures = { none: null };
 
 				for ( let i = 0; i < filenames.length; i ++ ) {
 
@@ -80,16 +85,16 @@
 
 				}
 
-				spotLight = new THREE.SpotLight( 0xffffff, 1 );
-				spotLight.position.set( 15, 40, 35 );
-				spotLight.angle = Math.PI / 4;
-				spotLight.penumbra = 0.1;
+				spotLight = new THREE.SpotLight( 0xffffff, 1.75 );
+				spotLight.position.set( 25, 50, 25 );
+				spotLight.angle = Math.PI / 6;
+				spotLight.penumbra = 1;
 				spotLight.decay = 2;
-				spotLight.distance = 200;
+				spotLight.distance = 100;
 
 				spotLight.castShadow = true;
-				spotLight.shadow.mapSize.width = 512;
-				spotLight.shadow.mapSize.height = 512;
+				spotLight.shadow.mapSize.width = 1024;
+				spotLight.shadow.mapSize.height = 1024;
 				spotLight.shadow.camera.near = 10;
 				spotLight.shadow.camera.far = 200;
 				spotLight.shadow.focus = 1;
@@ -98,17 +103,14 @@
 				lightHelper = new THREE.SpotLightHelper( spotLight );
 				scene.add( lightHelper );
 
-				shadowCameraHelper = new THREE.CameraHelper( spotLight.shadow.camera );
-				shadowCameraHelper.visible = false;
-				scene.add( shadowCameraHelper );
-
 				//
 
-				material = new THREE.MeshPhongMaterial( { color: 0x808080, dithering: true } );
+				
 
-				let geometry = new THREE.PlaneGeometry( 2000, 2000 );
+				const geometry = new THREE.PlaneGeometry( 1000, 1000 );
+				const material = new THREE.MeshLambertMaterial( { color: 0x808080 } );
 
-				let mesh = new THREE.Mesh( geometry, material );
+				const mesh = new THREE.Mesh( geometry, material );
 				mesh.position.set( 0, - 1, 0 );
 				mesh.rotation.x = - Math.PI * 0.5;
 				mesh.receiveShadow = true;
@@ -116,70 +118,55 @@
 
 				//
 
-				let material2 = new THREE.MeshPhongMaterial( { color: 0x4080ff, dithering: true } );
+				new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
 
-				geometry = new THREE.CylinderGeometry( 5, 5, 2, 32, 1, false );
+					geometry.scale( 0.024, 0.024, 0.024 );
+					geometry.computeVertexNormals();
 
-				mesh = new THREE.Mesh( geometry, material2 );
-				mesh.position.set( 0, 5, 0 );
-				mesh.castShadow = true;
-				scene.add( mesh );
-
-				render();
+					const material = new THREE.MeshLambertMaterial();
 
-				window.addEventListener( 'resize', onWindowResize );
+					const mesh = new THREE.Mesh( geometry, material );
+					mesh.rotation.y = - Math.PI / 2;
+					mesh.position.y = 18;
+					mesh.castShadow = true;
+					mesh.receiveShadow = true;
+					scene.add( mesh );
 
-			}
-
-			function onWindowResize() {
-
-				camera.aspect = window.innerWidth / window.innerHeight;
-				camera.updateProjectionMatrix();
-
-				renderer.setSize( window.innerWidth, window.innerHeight );
-
-				render();
-
-			}
-
-			function render() {
-
-				lightHelper.update();
-
-				shadowCameraHelper.update();
-
-				renderer.render( scene, camera );
+				} );
 
-			}
+				window.addEventListener( 'resize', onWindowResize );
 
-			function buildGui() {
+				// GUI
 
-				gui = new GUI();
+				const gui = new GUI();
 
 				const params = {
-					'light color': spotLight.color.getHex(),
+					map: 'none',
+					color: spotLight.color.getHex(),
 					intensity: spotLight.intensity,
 					distance: spotLight.distance,
 					angle: spotLight.angle,
 					penumbra: spotLight.penumbra,
 					decay: spotLight.decay,
 					focus: spotLight.shadow.focus,
-					map: 'none',
-					'shadow camera': false,
-					'enable shadows': true
+					shadows: true
 				};
 
-				gui.addColor( params, 'light color' ).onChange( function ( val ) {
+				gui.add( params, 'map', textures ).onChange( function ( val ) {
+
+					spotLight.map = val;
+
+				} );
+
+				gui.addColor( params, 'color' ).onChange( function ( val ) {
 
 					spotLight.color.setHex( val );
-					render();
 
 				} );
 
-				gui.add( params, 'intensity', 0, 2 ).onChange( function ( val ) {
+				gui.add( params, 'intensity', 0, 3 ).onChange( function ( val ) {
 
 					spotLight.intensity = val;
-					render();
 
 				} );
 
@@ -187,58 +174,47 @@
 				gui.add( params, 'distance', 50, 200 ).onChange( function ( val ) {
 
 					spotLight.distance = val;
-					render();
 
 				} );
 
 				gui.add( params, 'angle', 0, Math.PI / 3 ).onChange( function ( val ) {
 
 					spotLight.angle = val;
-					render();
 
 				} );
 
 				gui.add( params, 'penumbra', 0, 1 ).onChange( function ( val ) {
 
 					spotLight.penumbra = val;
-					render();
 
 				} );
 
 				gui.add( params, 'decay', 1, 2 ).onChange( function ( val ) {
 
 					spotLight.decay = val;
-					render();
 
 				} );
 
 				gui.add( params, 'focus', 0, 1 ).onChange( function ( val ) {
 
 					spotLight.shadow.focus = val;
-					render();
 
 				} );
 
-				gui.add( params, 'map', textures ).onChange( function ( val ) {
 
-					spotLight.map = val;
-					render();
-
-				} );
+				gui.add( params, 'shadows' ).onChange( function ( val ) {
 
-				gui.add( params, 'shadow camera' ).onChange( function ( val ) {
+					renderer.shadowMap.enabled = val;
 
-					shadowCameraHelper.visible = val;
-					render();
+					scene.traverse( function ( child ) {
 
-				} );
+						if ( child.material ) {
 
+							child.material.needsUpdate = true;
 
-				gui.add( params, 'enable shadows' ).onChange( function ( val ) {
+						}
 
-					renderer.shadowMap.enabled = val
-					material.needsUpdate = true;
-					render();
+					} );
 
 				} );
 
@@ -246,11 +222,27 @@
 
 			}
 
-			init();
+			function onWindowResize() {
 
-			buildGui();
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			function render() {
 
-			render();
+				const time = performance.now() / 3000;
+
+				spotLight.position.x = Math.cos( time ) * 25;
+				spotLight.position.z = Math.sin( time ) * 25;
+
+				lightHelper.update();
+
+				renderer.render( scene, camera );
+
+			}
 
 		</script>