浏览代码

Examples: Clean up webgl_instancing_raycast.html (#23000)

* Clean up

* Updated screenshot
WestLangley 3 年之前
父节点
当前提交
8c1544aa28
共有 2 个文件被更改,包括 27 次插入18 次删除
  1. 二进制
      examples/screenshots/webgl_instancing_raycast.jpg
  2. 27 18
      examples/webgl_instancing_raycast.html

二进制
examples/screenshots/webgl_instancing_raycast.jpg


+ 27 - 18
examples/webgl_instancing_raycast.html

@@ -15,7 +15,7 @@
 			import { GUI } from './jsm/libs/lil-gui.module.min.js';
 			import { GUI } from './jsm/libs/lil-gui.module.min.js';
 			import { OrbitControls } from "./jsm/controls/OrbitControls.js";
 			import { OrbitControls } from "./jsm/controls/OrbitControls.js";
 
 
-			let camera, scene, renderer, stats;
+			let camera, scene, renderer, controls, stats;
 
 
 			let mesh;
 			let mesh;
 			const amount = parseInt( window.location.search.substr( 1 ) ) || 10;
 			const amount = parseInt( window.location.search.substr( 1 ) ) || 10;
@@ -25,6 +25,7 @@
 			const mouse = new THREE.Vector2( 1, 1 );
 			const mouse = new THREE.Vector2( 1, 1 );
 
 
 			const color = new THREE.Color();
 			const color = new THREE.Color();
+			const white = new THREE.Color().setHex( 0xffffff );
 
 
 			init();
 			init();
 			animate();
 			animate();
@@ -37,16 +38,12 @@
 
 
 				scene = new THREE.Scene();
 				scene = new THREE.Scene();
 
 
-				const light1 = new THREE.HemisphereLight( 0xffffff, 0x000088 );
-				light1.position.set( - 1, 1.5, 1 );
-				scene.add( light1 );
-
-				const light2 = new THREE.HemisphereLight( 0xffffff, 0x880000, 0.5 );
-				light2.position.set( - 1, - 1.5, - 1 );
-				scene.add( light2 );
+				const light = new THREE.HemisphereLight( 0xffffff, 0x888888 );
+				light.position.set( 0, 1, 0 );
+				scene.add( light );
 
 
 				const geometry = new THREE.IcosahedronGeometry( 0.5, 3 );
 				const geometry = new THREE.IcosahedronGeometry( 0.5, 3 );
-				const material = new THREE.MeshPhongMaterial();
+				const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
 
 
 				mesh = new THREE.InstancedMesh( geometry, material, count );
 				mesh = new THREE.InstancedMesh( geometry, material, count );
 
 
@@ -86,7 +83,10 @@
 				renderer.setSize( window.innerWidth, window.innerHeight );
 				renderer.setSize( window.innerWidth, window.innerHeight );
 				document.body.appendChild( renderer.domElement );
 				document.body.appendChild( renderer.domElement );
 
 
-				new OrbitControls( camera, renderer.domElement );
+				controls = new OrbitControls( camera, renderer.domElement );
+				controls.enableDamping = true;
+				controls.enableZoom = false;
+				controls.enablePan = false;
 
 
 				stats = new Stats();
 				stats = new Stats();
 				document.body.appendChild( stats.dom );
 				document.body.appendChild( stats.dom );
@@ -118,11 +118,7 @@
 
 
 				requestAnimationFrame( animate );
 				requestAnimationFrame( animate );
 
 
-				render();
-
-			}
-
-			function render() {
+				controls.update();
 
 
 				raycaster.setFromCamera( mouse, camera );
 				raycaster.setFromCamera( mouse, camera );
 
 
@@ -132,17 +128,30 @@
 
 
 					const instanceId = intersection[ 0 ].instanceId;
 					const instanceId = intersection[ 0 ].instanceId;
 
 
-					mesh.setColorAt( instanceId, color.setHex( Math.random() * 0xffffff ) );
-					mesh.instanceColor.needsUpdate = true;
+					mesh.getColorAt( instanceId, color );
+
+					if ( color.equals( white ) ) {
+
+						mesh.setColorAt( instanceId, color.setHex( Math.random() * 0xffffff ) );
+
+						mesh.instanceColor.needsUpdate = true;
+
+					}
 
 
 				}
 				}
 
 
-				renderer.render( scene, camera );
+				render();
 
 
 				stats.update();
 				stats.update();
 
 
 			}
 			}
 
 
+			function render() {
+
+				renderer.render( scene, camera );
+
+			}
+
 		</script>
 		</script>
 	</body>
 	</body>
 </html>
 </html>