我想從我的Three.js場景中放置的網格物體中獲取碰撞檢測。我很困惑Raycaster reallu是如何工作的,以及我是否正確。請解釋three.js Raycaster方向參數
這裏是一個小提琴descripe我有什麼問題,
//Add cuba at 40/40
geometry = new THREE.CubeGeometry(20, 20, 20);
material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh(geometry, material);
mesh.position.setY(40)
scene.add(mesh);
//Add Ray
var origin = new THREE.Vector3(50, 0, 0),
direction = new THREE.Vector3(-1,0,0),
ray = new THREE.Raycaster(origin, direction),
collisionResults = ray.intersectObjects([mesh]);
if(collisionResults.length!==0){
alert('Ray collides with mesh. Distance :' + collisionResults[0].distance)
}
//Add Arrow to show ray
scene.add(new THREE.ArrowHelper(direction, origin, 50, 0x000000));
不工作: http://jsfiddle.net/FredricBerling/LwfPL/1/
工作: http://jsfiddle.net/FredricBerling/LwfPL/3/
基本上小提琴勾畫出一個立方體和50分的形式,我在一個「方向」射線。問題似乎是,即使它不應該,它也會說明「命中」。
我擺出一個Arrowhelper來顯示我懷疑Raycaster射出它的射線。
從其他測試看來,Raycaster的方向似乎與Arrowhelper的不同。 Raycaster似乎將射線投射到場景的0,0,0。我很困惑
編輯! Rob給出了答案。我需要確保網格被渲染以便應用矩陣。小提琴用正確的代碼進行更新,該代碼適用於按預期測試Raycaster。
您不需要先渲染 - 您可以在設置網格位置後調用'mesh.updateMatrixWorld();'。 – WestLangley
謝謝。我注意到了。完全重新渲染這個世界在性能上並不是很好。 :) – Fredric