4
在我的three.js場景中,我有一些立方體。我想爲用戶提供使用鼠標選擇一個框的可能性。Three.js/Intersection
這個顯著代碼(我用three.js所版本69):
function init() {
[...]
camera = new THREE.PerspectiveCamera(VIEW_ANGLE,ASPECT,NEAR,FAR);
[...]
// in a 'for' iteration, I a many boxes in a 'objects' array :
abox = new THREE.Mesh(geometry, material);
objects.push(abox);
[...]
vector = new THREE.Vector3(0,0,0);
raycaster = new THREE.Raycaster();
animate();
document.addEventListener('mousedown', onDocumentMouseDown, false );
}
function onDocumentMouseDown(event) {
event.preventDefault();
if (camera instanceof THREE.OrthographicCamera) {
vector.set((event.clientX/window.innerWidth) * 2 - 1, - (event.clientY/window.innerHeight) * 2 + 1, - 1);
vector.unproject(camera);
dir.set(0, 0, - 1).transformDirection(camera.matrixWorld);
raycaster.set(vector, dir);
} else if (camera instanceof THREE.PerspectiveCamera) {
vector.set((event.clientX/window.innerWidth) * 2 - 1, - (event.clientY/window.innerHeight) * 2 + 1, 0.5);
vector.unproject(camera);
raycaster.set(camera.position, vector.sub(camera.position).normalize());
}
var intersects = raycaster.intersectObjects(objects , false);
if (intersects.length > 0) {
var intersect = intersects[ 0 ];
// change color of the face
intersect.object.material.color.setHex(Math.random() * 0xFF0000);
renderer.render(scene, camera);
}
}
當我點擊一個立方體,選擇的點(相交),不完全(接近,但不完全),其中我點擊了。有一個轉變,一個翻譯(我嘗試了一個正交或相機相同的問題)。
任何幫助,歡迎。
感謝
看看http://stackoverflow.com/questions/13542175/three-js-ray-intersect-fails-by-adding-div/13544277#13544277可以幫助你。 – WestLangley
完美!非常感謝 ! –