admin管理员组文章数量:1429307
in a Three.js app I want to get the object that the perspective camera is pointing at and to do that I read the raycaster docs. All the docs that I found talk about doind raycasting with camera and a Vector2 from the mouse coordinates, but I don't want to use the 2d coordinates of the mouse. The camera can rotate for any reason: click and drag the screen, touch control or even VR control, I just want to raycast from the center point of the perspective camera and set as "selected" the object it looks at.
Any suggestions?
in a Three.js app I want to get the object that the perspective camera is pointing at and to do that I read the raycaster docs. All the docs that I found talk about doind raycasting with camera and a Vector2 from the mouse coordinates, but I don't want to use the 2d coordinates of the mouse. The camera can rotate for any reason: click and drag the screen, touch control or even VR control, I just want to raycast from the center point of the perspective camera and set as "selected" the object it looks at.
Any suggestions?
Share Improve this question asked Sep 28, 2015 at 19:42 ascallonisiascallonisi 9611 gold badge12 silver badges20 bronze badges2 Answers
Reset to default 8THREE.Raycaster.set()
expects origin
and a direction
vectors:
origin — The origin vector where the ray casts from.
direction — The direction vector that gives direction to the ray. Should be normalized.
Set the cameras world position as origin
and its world direction as direction
:
var raycaster = new THREE.Raycaster();
raycaster.set( camera.getWorldPosition(), camera.getWorldDirection() );
var intersects = raycaster.intersectObjects( objectsArray );
if ( intersects.length > 0 ) {
//...
}
The code from Falk still works but Three.js (>r90) requires you to define a target [1]:
target is now required
All you have to do is:
let camera_world_pos = new THREE.Vector3();
let camera_world_dir = new THREE.Vector3();
let raycaster = new THREE.Raycaster();
camera.getWorldPosition(camera_world_pos);
camera.getWorldDirection(camera_world_dir);
raycaster.set(camera_world_pos, camera_world_dir);
let intersects = raycaster.intersectObjects( objectsArray );
if ( intersects.length > 0 ) {
//...
}
[1] https://github./mrdoob/three.js/issues/12231
本文标签: javascriptThreejs select object with camerano mouseStack Overflow
版权声明:本文标题:javascript - Three.js select object with camera, no mouse - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745526554a2661860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论