Reputation: 2631
I want to change to color of the hovered face of my PlaneGeometry but I don't find how to get the selected face. Here is my code:
//THREE.WebGLRenderer 69
// Generating plane
var geometryPlane = new THREE.PlaneGeometry( 100, 100, 20, 10 );
for (var vertIndex = 0; vertIndex < geometryPlane.vertices.length; vertIndex++) {
geometryPlane.vertices[vertIndex].z += Math.random();
}
geometryPlane.dynamic = true;
geometryPlane.computeFaceNormals();
geometryPlane.normalsNeedUpdate = true;
var materialPlane = new THREE.MeshLambertMaterial( {
color: 0xffff00,
side: THREE.DoubleSide,
shading: THREE.FlatShading,
overdraw: 0.5,
vertexColors: THREE.FaceColors
} );
plane = new THREE.Mesh( geometryPlane, materialPlane );
plane.geometry.colorsNeedUpdate = true;
// Mouse event
container[0].addEventListener( 'mousemove', onMouseMove, false );
function onMouseMove( event ) {
var mouseX = ( event.clientX / window.innerWidth ) * 2 - 1;
var mouseY = -( event.clientY / window.innerHeight ) * 2 + 1;
var vector = new THREE.Vector3( mouseX, mouseY, camera.near );
vector.unproject( camera );
raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
if ( intersects.length > 0 ) {
var INTERSECTED = intersects[ 0 ].object;
for ( var i = 0; i < INTERSECTED.geometry.faces.length; i ++ ) {
// Change the color of all faces
// I want only the hovered one
INTERSECTED.geometry.faces[ i ].color.setHex( Math.random() * 0xffffff );
}
}
}
Upvotes: 2
Views: 1163
Reputation: 389
intersects[0].point is the intersection point.
intersects[0].face is the intersected face.
Edit : and the intersection function is not called in your code : raycaster is created but not used.
Upvotes: 2