I am working on a project and need to be able to click on a instance mesh and highlight clicked cubes that make up that mesh.
Here is the project: Test - CodeSandbox
I created a this._createRayCaster
method that creates a raycaster, with a event listener, that passes the intersects into a call back arguement. It should change the color of the a instanced cube index when clicked, but it does not do that. three.js docs states that this syntax for creating a color should work.
_rayCastCallback(intersects) {
console.log("Intersects");
console.log(intersects);
// Do what you want with the raycasted items here
if (intersects && intersects.length >= 1) {
console.log("Current Instnace Id");
console.log(intersects[0].instanceId);
// Set its color to the path color
intersects[0].object.setColorAt(
intersects[0].instanceId,
new THREE.Color(0xffff00)
);
// Trying to display the color
intersects[0].object.updateMatrix();
}
}
// Callback is where the interescting objects are passed into
_createRayCaster(callback) {
this._pointer = new THREE.Vector2();
this._raycaster = new THREE.Raycaster();
window.addEventListener("pointerdown", (event) => {
// Normalize the pointer coordinates from -1 and 1
this._pointer.x = (event.clientX / this._container.clientWidth) * 2 - 1;
this._pointer.y = -(event.clientY / this._container.clientHeight) * 2 + 1;
// Calculate the intersecting object using cameara position
this._raycaster.setFromCamera(this._pointer, this._camera);
// pass objects into callback
callback(this._raycaster.intersectObjects(this._scene.children));
});
}