【问题标题】:Raycaster fails to detect CubesRaycaster 无法检测到立方体
【发布时间】:2020-12-16 23:28:58
【问题描述】:

如何将 Square 和 Rectangle 组合成一个 Raycaster 可以成功检测到的对象?

我创建了一个自定义的“树”对象,方法是制作一个“树干”——它只是一个长矩形,然后将一个方形对象粘贴在该树干的顶部。 然后我将那棵树“种植”在一个球体的顶部,并试图让我的raycaster 检测到它。 它不工作。

这是我的代码:

// My custom “Tree” Object:
var Tree = function(treeColor) {
    this.mesh = new THREE.Object3D();
    this.mesh.name = "tree";

    // I start with the TRUNK - which is just an elongated Cube - meaning a Rectangle:
    var trunkGeometry = new THREE.CubeGeometry(0.2, 0.5, 0.2);
    var trunkMaterial = new THREE.MeshBasicMaterial({ color: "blue", wireframe: false });
    var treeTrunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
    treeTrunk.position.set(0, 0, 0);
    treeTrunk.rotation.x = -Math.PI * 0.5;
    this.mesh.add(treeTrunk);

    // Then I create the FOLIAGE - which is just a regular Cube:
    var foliageGeometry = new THREE.CubeGeometry(0.5, 0.5, 0.5);
    var foliageMaterial = new THREE.MeshBasicMaterial({ color: treeColor, wireframe: false });
    var treeFoliage = new THREE.Mesh(foliageGeometry, foliageMaterial);
    treeFoliage.position.set(0, 0.5, 0);

    // And then I attach/add the FOLIAGE to the TRUNK:
    treeTrunk.add(treeFoliage);
}



// Next I make a basic Sphere:
theSun = new THREE.Mesh(new THREE.SphereGeometry(3, 32, 24), new THREE.MeshBasicMaterial( {color: "white", wireframe: false} ));
scene.add(theSun);


// And then I make a Tree and add it to my Sphere (`theSun`):
var oneTree = new Tree("red");
let rx = Math.random() * Math.PI * 2;
let ry = Math.random() * Math.PI;
oneTree.mesh.position.setFromSphericalCoords(3.2, ry, rx);
oneTree.mesh.lookAt(theSun.position);
theSun.add(oneTree.mesh);

// Next I add both theSun and the Tree to my “objectsForRayCasterArray” - a global var I use in my raycaster test:
objectsForRayCasterArray.push(oneTree);
objectsForRayCasterArray.push(theSun);


// In my render() function, I do the usual raycasting business:
        rayCaster = new THREE.Raycaster();

function render() {
    requestAnimationFrame(render);

    controls.update();
    renderer.render(scene, camera);
        
    // Raycasting stuff:
    rayCaster.setFromCamera(mousePosition, camera);
     
    var intersectingObjectsArray = rayCaster.intersectObjects(objectsForRayCasterArray);
        
    if (intersectingObjectsArray.length > 0) {
        if (intersectedObject != intersectingObjectsArray[0].object) {
            console.log(“Intersected!”);
        }      
    }

注意:当我使用相同的代码而不是Tree 时,我只是在我的Sphere 对象上放置一个常规的Cube,一切正常。 raycaster 检测到Cube 并触发Alert

【问题讨论】:

    标签: three.js mesh raycasting vertices


    【解决方案1】:

    我最好的猜测是,由于您将 treeFoliage 嵌套在 treeTrunk 中,并且将 mesh 嵌套在 mesh 中,因此您将不得不使用递归交集测试。

    根据文档,intersectObjects() accepts a second argument 执行递归命中测试。这意味着它将遍历所有后代,而不仅仅是对顶级对象进行浅层检查:

    rayCaster.intersectObjects(objectsForRayCasterArray, true);

    【讨论】:

    • 嘿,你是对的 - 有点。 Raycaster 现在确实检测到了树 - 但不是作为一个整体。意思是,当我将鼠标悬停在树干上时,它 - 并且只有它 - 亮起(我做了它,所以当任何东西悬停在上面时颜色会变为黄色)但 FOLIAGE 部分保持正常。当我将鼠标悬停在 FOLIAGE 上时,它会变成黄色 - 但 Trunk 不会。因此,出于某种原因,它们仍然被视为单独的对象。有关纠正此问题的任何进一步提示?
    • 乐于助人。如果您认为答案有帮助,请不要忘记投票。而且您正在寻找的行为是不同的。简单地做mesh.add(anotherMesh) 不会合并几何,它会嵌套它们。如果您想将多个立方体组合成一个对象,请考虑使用Geometry.merge
    • 好的,Geometry.mesh - 一定会调查的!同时,我将您的答案标记为正确答案 - 谢谢一百万!
    猜你喜欢
    • 2021-09-22
    • 1970-01-01
    • 2019-05-02
    • 2014-08-18
    • 1970-01-01
    • 2021-09-14
    • 2013-05-27
    • 2013-07-14
    • 1970-01-01
    相关资源
    最近更新 更多