1.如何通過鼠標獲取網(wǎng)格對象
首先需要把鼠標的起始位置在左上角的屏幕坐標轉(zhuǎn)換為笛卡爾坐標。然后將坐標轉(zhuǎn)為為以Camera為中心點的三維空間坐標。接下來根據(jù)攝像頭位置和鼠標位置的法向量創(chuàng)建射線對象。最終根據(jù)射線對象的intersectObjects函數(shù)確認哪個網(wǎng)格被選中。
下面是比較經(jīng)典的使用方法:
function onDocumentMouseMove(event) { if (controls.showRay) { var vector = new THREE.Vector3(( event.clientX / window.innerWidth ) * 2 - 1, -( event.clientY / window.innerHeight ) * 2 + 1, 0.5); vector = vector.unproject(camera); var raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize()); var intersects = raycaster.intersectObjects([sphere, cylinder, cube]); if (intersects.length > 0) { &