Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified examples/screenshots/webgl_instancing_raycast.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 27 additions & 18 deletions examples/webgl_instancing_raycast.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import { GUI } from './jsm/libs/lil-gui.module.min.js';
import { OrbitControls } from "./jsm/controls/OrbitControls.js";

let camera, scene, renderer, stats;
let camera, scene, renderer, controls, stats;

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

const color = new THREE.Color();
const white = new THREE.Color().setHex( 0xffffff );

init();
animate();
Expand All @@ -37,16 +38,12 @@

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 material = new THREE.MeshPhongMaterial();
const material = new THREE.MeshPhongMaterial( { color: 0xffffff } );

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

Expand Down Expand Up @@ -86,7 +83,10 @@
renderer.setSize( window.innerWidth, window.innerHeight );
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();
document.body.appendChild( stats.dom );
Expand Down Expand Up @@ -118,11 +118,7 @@

requestAnimationFrame( animate );

render();

}

function render() {
controls.update();

raycaster.setFromCamera( mouse, camera );

Expand All @@ -132,17 +128,30 @@

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();

}

function render() {

renderer.render( scene, camera );

}

</script>
</body>
</html>