1.如何通過鼠標(biāo)獲取網(wǎng)格對(duì)象
首先需要把鼠標(biāo)的起始位置在左上角的屏幕坐標(biāo)轉(zhuǎn)換為笛卡爾坐標(biāo)。然后將坐標(biāo)轉(zhuǎn)為為以Camera為中心點(diǎn)的三維空間坐標(biāo)。接下來(lái)根據(jù)攝像頭位置和鼠標(biāo)位置的法向量創(chuàng)建射線對(duì)象。最終根據(jù)射線對(duì)象的intersectObjects函數(shù)確認(rèn)哪個(gè)網(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) { &