Skip to content

Commit 8c1544a

Browse files
authored
Examples: Clean up webgl_instancing_raycast.html (#23000)
* Clean up * Updated screenshot
1 parent fb8a3f2 commit 8c1544a

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed
-15 KB
Loading

examples/webgl_instancing_raycast.html

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import { GUI } from './jsm/libs/lil-gui.module.min.js';
1616
import { OrbitControls } from "./jsm/controls/OrbitControls.js";
1717

18-
let camera, scene, renderer, stats;
18+
let camera, scene, renderer, controls, stats;
1919

2020
let mesh;
2121
const amount = parseInt( window.location.search.substr( 1 ) ) || 10;
@@ -25,6 +25,7 @@
2525
const mouse = new THREE.Vector2( 1, 1 );
2626

2727
const color = new THREE.Color();
28+
const white = new THREE.Color().setHex( 0xffffff );
2829

2930
init();
3031
animate();
@@ -37,16 +38,12 @@
3738

3839
scene = new THREE.Scene();
3940

40-
const light1 = new THREE.HemisphereLight( 0xffffff, 0x000088 );
41-
light1.position.set( - 1, 1.5, 1 );
42-
scene.add( light1 );
43-
44-
const light2 = new THREE.HemisphereLight( 0xffffff, 0x880000, 0.5 );
45-
light2.position.set( - 1, - 1.5, - 1 );
46-
scene.add( light2 );
41+
const light = new THREE.HemisphereLight( 0xffffff, 0x888888 );
42+
light.position.set( 0, 1, 0 );
43+
scene.add( light );
4744

4845
const geometry = new THREE.IcosahedronGeometry( 0.5, 3 );
49-
const material = new THREE.MeshPhongMaterial();
46+
const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );
5047

5148
mesh = new THREE.InstancedMesh( geometry, material, count );
5249

@@ -86,7 +83,10 @@
8683
renderer.setSize( window.innerWidth, window.innerHeight );
8784
document.body.appendChild( renderer.domElement );
8885

89-
new OrbitControls( camera, renderer.domElement );
86+
controls = new OrbitControls( camera, renderer.domElement );
87+
controls.enableDamping = true;
88+
controls.enableZoom = false;
89+
controls.enablePan = false;
9090

9191
stats = new Stats();
9292
document.body.appendChild( stats.dom );
@@ -118,11 +118,7 @@
118118

119119
requestAnimationFrame( animate );
120120

121-
render();
122-
123-
}
124-
125-
function render() {
121+
controls.update();
126122

127123
raycaster.setFromCamera( mouse, camera );
128124

@@ -132,17 +128,30 @@
132128

133129
const instanceId = intersection[ 0 ].instanceId;
134130

135-
mesh.setColorAt( instanceId, color.setHex( Math.random() * 0xffffff ) );
136-
mesh.instanceColor.needsUpdate = true;
131+
mesh.getColorAt( instanceId, color );
132+
133+
if ( color.equals( white ) ) {
134+
135+
mesh.setColorAt( instanceId, color.setHex( Math.random() * 0xffffff ) );
136+
137+
mesh.instanceColor.needsUpdate = true;
138+
139+
}
137140

138141
}
139142

140-
renderer.render( scene, camera );
143+
render();
141144

142145
stats.update();
143146

144147
}
145148

149+
function render() {
150+
151+
renderer.render( scene, camera );
152+
153+
}
154+
146155
</script>
147156
</body>
148157
</html>

0 commit comments

Comments
 (0)