Three.js raycaster 可以与一组相交吗? [英] Can a Three.js raycaster intersect a group?

查看:24
本文介绍了Three.js raycaster 可以与一组相交吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的 raycaster 是否正在查看我加载的 OBJ.由于从 Cinema4D 导出的方式,我相信 OBJ 是一个有 3 个孩子的 THREE.Group,而不是一个 THREE.Object.我可以更改我的 raycaster 代码行来查找该组而不是对象吗?

I want to know if my raycaster is looking at an OBJ that I've loaded. Due to the way exported from Cinema4D, I believe the OBJ is a THREE.Group with 3 children, instead of a THREE.Object. Can I just change my raycaster line of code to look for this group instead of an object?

raycaster.set(controls.getObject().position, controls.getDirection(), 0, 40)

var intersects = raycaster.intersectObjects(scene.children, true);

     if (intersects.length > 0) {
      //CURRENTLY INTERSECTING SOMETHING
      for (var i = 0; i < onOffCubes.length; i++) {
      //if the first thing the raycaster sees is a one of my cubes
        if (intersects[0].object == onOffCubes[i]) {
                ExperiencesData[i].userClose = true
            }
         }
       }

onOffCubes 是一个包含 6 个 OBJ/THREE.js 组的数组:

onOffCubes is an array of 6 OBJs/THREE.js Groups:

Console.log(onOffCubes[0]) 是这样的:

Console.log(onOffCubes[0]) is this:

推荐答案

如此接近.相交对象是一个网格,该网格的父对象是组.您需要匹配相交的父对象而不是相交的对象.即:

So close. The intersect object is a mesh, the parent of that mesh is the group. You need to match the parent object of the intersect rather than the object of the intersect. That is:

intersects[ 0 ].object.parent === onOffCubes[ i ]

而不是:

intersects[ 0 ].object === onOffCubes[ i ]

TL;博士

为了使用类似的结构进行测试,我生成了六组,每组三个网格,每组网格共享相同的材料.请注意,onOffCubes 不是 THREE.js 组,而是一组组.这就像原海报的onOffCubes:

To test with a similar structure, I generated six groups of three meshes each, with each group of meshes sharing the same material. Note that onOffCubes isn't a THREE.js group, it's an array of groups. This is like the original poster's onOffCubes:

var onOffCubes = []
for ( var i = 0; i < 6; i++ ) {
    var material = new THREE.MeshBasicMaterial({ color: 0xee55aa })
    var group = new THREE.Group()
    for ( var j = 0; j < 3; j++ ) {
        var mesh = new THREE.Mesh( geometry, material );
        mesh.position.x = Math.random() * 100 - 50;
        mesh.position.y = Math.random() * 100 - 50;
        mesh.position.z = Math.random() * 200 - 200;
        group.add( mesh );
    }
    onOffCubes.push( group )
    scene.add( group )
}

查看全场景

var intersects = raycaster.intersectObjects( scene.children, true );

或只检查 onOffCubes

or check just onOffCubes

var intersects = raycaster.intersectObjects( onOffCubes, true );

基本上与原始海报相同的代码,并进行了一次修复:

Essentially the same code as original poster with the one fix:

if (intersects.length > 0) {
    for (var i = 0; i < onOffCubes.length; i++) { 
        if (intersects[ 0 ].object.parent === onOffCubes[ i ]) {
            // What I tested with
            //intersects[ 0 ].object.material.color.set( 0xff0000 )
            // Your code
            ExperiencesData[i].userClose = true
        }
    }
}

这篇关于Three.js raycaster 可以与一组相交吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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