简体   繁体   中英

ThreeJS how to change color of hovered face on a PlaneGeometry

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM