首页 > 解决方案 > Raycast 没有击中原始几何网格

问题描述

我正在尝试使用射线将鼠标悬停在对象上,但只有从 3D 资产文件加载的网格被它击中。从原始几何体创建的任何网格都像CylinderGeometry或被BoxGeometry忽略。

我要命中的对象是CameraPole,它是一个扩展THREE.Group并包含pole网格的类。

export default class CameraPole extends Group {
    public pole: Mesh;
    constructor() {
        super();

        this.name = 'CameraPole';

        this.pole = new Mesh(
            new CylinderGeometry(.25, .25, 5, 32),
            new MeshPhongMaterial({color: 0xffffff})
        );

        this.position.x = -34;
        this.position.y = 5;
        this.position.z = 17.8;

        this.add(this.pole);
    }
}

这只是一个示例,但我的场景中还有其他原始对象,并且它们都没有被光线投射检测到。

我构建并将其添加到场景中:

const cameraPole = new CameraPole();
scene.add(cameraPole);

但是当我尝试用光线投射命中来检测它时,我只命中了从 3D 资产文件加载的对象。返回的数组从不包含从原始几何生成的任何对象。

document.addEventListener('mousemove', (e) => {
    const mouse = {
        x: (e.clientX / window.innerWidth) * 2 - 1,
        y: (e.clientY / window.innerHeight) * 2 - 1
    };
    const ray = new Raycaster();
    ray.setFromCamera(mouse, camera);
    console.log(ray.intersectObjects(scene.children, true)); // Only returning complex meshes, not geometric objects created dynamically.
});

我也尝试过专门寻找相机杆,但我仍然一无所获:

ray.intersectObject(cameraPole, true);

编辑:如果相关,相机是OrthographicCamera.

编辑2:所以我发现如果我不改变它的位置CameraPole就可以了。当位置更新时,光线投射不再检测到它。关于如何解决这个问题的任何消息?

标签: typescriptthree.js

解决方案


推荐阅读