当对象不属于场景时,Three.js raycaster 交叉点为空 [英] Three.js raycaster intersection empty when objects not part of scene

查看:41
本文介绍了当对象不属于场景时,Three.js raycaster 交叉点为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过创建网格并将它们作为更大几何体的一部分来缩短项目的渲染时间,并将该单个几何体作为我添加到场景中的对象.我认为我仍然可以通过拥有一组原始网格来管理对象的拾取,并将它们传递给 raycaster.我使用了以下代码:

I've tried improving rendering time on my project by creating meshes and putting them as part of a larger geometry, and having just that single geometry as the object I add to the scene. I thought that I would still be able to manage picking of objects by having an array of the original meshes, and pass those to the raycaster. I used the following code:

var vector = new THREE.Vector3( ( loc_x / window.innerWidth ) * 2 - 1, - ( loc_y / window.innerHeight ) * 2 + 1, 0.5 );
projector.unprojectVector(vector, camera);
var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
var objects = [];

var i = active_regions.length;
while (i--) {
  objects = objects.concat(active_regions[i].mesh_entities);
}
var intersects = raycaster.intersectObjects( objects );
if ( intersects.length > 0 ) {
  console.log("Intersection: " + intersects);
}

所以在上面的代码中,active_regions 包含原始的单个网格,我动态创建了一个数组来指定我想从中选择哪些对象.不幸的是,相交是空的.

So in the above code, active_regions contains the original individual meshes, and I create an array on the fly to specify which objects I want to select from. Unfortunately intersects comes up empty.

如果我稍微修改我的项目,以便将所有这些 mesh_entities 单独添加到场景中,那么上面的代码就可以工作并且我可以成功地选择对象.不幸的是,整个场景渲染缓慢.

If I modify my project slightly so that I have all those mesh_entities added to the scene individually, then the above code works and I can successfully select objects. Unfortunately, the whole scene then renders slowly.

有什么好方法(或一些好方法)可以让我成功检查与光线的交集,同时又不会减慢渲染速度?

What's a good way (or some good ways) for me to successfully check for intersection with the ray, without slowing down my rendering?

谢谢!

推荐答案

我用鬼场景解决了这个问题.本质上,我将所有对象作为单独的网格添加到幽灵场景中,然后当我使用 raycaster 时它就可以工作了.

I solved this by having a ghost scene. Essentially, I added all objects to the ghost scene as their individual meshes, and then when I use raycaster it works.

但是,我不得不使用以下方面的函数:

However, I had to use functions along these lines:

function flip_render_ghost(yes) {
  if (yes == true) {
    scene_ghost.add(camera);
    render_ghost = true;
  } else {
    scene.add(camera);
    render_ghost = false;
  }
  render();
}

function render() {

  if (render_ghost == true) {
    renderer.render( scene_ghost, camera );
  } else {
    renderer.render( scene, camera );
  }

}

每当我要检查碰撞时,我都会切换到渲染重影场景,检查命中,然后切换回正常渲染.

Whenever I am about to check for collisions, I flip to rendering ghost scene, check for hits, then flip back to normal rendering.

此后我发现对象不能属于多个场景(尽管可以共享几何图形).所以我所做的是为采摘场景创建简单的网格.这需要更多内存,但可以选择使用更简单的网格来进行选择以加快拾取速度.此外,将鬼场景本身的孩子送到光线投射器似乎对我有用.您可能需要像我一样,为幽灵场景中的每个对象添加一个属性,以引用您尝试选取的主要对象.

I have since discovered that objects cannot belong to multiple scenes (though geometries can be shared). So what I have done is created simple meshes for the picking scene. This requires more memory, but gives the option of having a simpler mesh to use for selection for faster picking. Also, it seemed to work for me to send the children of the ghost scene itself to the raycaster. You may need to, like me, add a property to each object in the ghost scene that references the main object you are trying to pick.

这篇关于当对象不属于场景时,Three.js raycaster 交叉点为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆