实现Three.JS中鼠标选择几何图元

时间:2022-06-07 04:27:02

 var raycaster,lastSelectObj;raycaster = new THREE.Raycaster();  function render() {raycaster.setFromCamera( mouse, camera );        var intersects = raycaster.intersectObjects( scene.children );        if ( intersects.length > 0 ) {            if ( lastSelectObj != intersects[ 0 ].object ) {                if ( lastSelectObj ) lastSelectObj.material.emissive.setHex( lastSelectObj.currentHex );                lastSelectObj = intersects[ 0 ].object;                lastSelectObj.currentHex = lastSelectObj.material.emissive.getHex();                lastSelectObj.material.emissive.setHex( 0xff0000 );            }        }        else        {            if ( lastSelectObj )                lastSelectObj.material.emissive.setHex( lastSelectObj.currentHex );            lastSelectObj = null;        }        renderer.render( scene, camera );    }
var mouse = new THREE.Vector2(); function onDocumentMouseMove( event ) {        event.preventDefault();        mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;        mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;    }


射线法获取鼠标选择的元素,然后修元素的材质。