Orthographic camera and selecting objects with raycast

冷暖自知 提交于 2019-11-27 01:52:35

Here is the pattern to use when raycasting with either an orthographic camera or perspective camera:

var raycaster = new THREE.Raycaster(); // create once
var mouse = new THREE.Vector2(); // create once

...

mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;

raycaster.setFromCamera( mouse, camera );

var intersects = raycaster.intersectObjects( objects, recursiveFlag );

three.js r.84

One more note that might save you some trouble. If you have a camera like this:

var camera = new THREE.OrthographicCamera(0, window.innerWidth, -window.innerHeight, 0, -100, 100);

Then during raycasting, be sure to move the ray origin.z to camera.far for it to hit anything in the entire visible range:

this.ray.origin.set(0, 0, 0);
this.camera.localToWorld(this.ray.origin);
this.raycaster.setFromCamera(this.mouseCoord, this.camera);
this.ray.origin.z = this.camera.far;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!