threejs光线投射点击检测无法在加载的3dObject上工作

threejs raycast click detection not working on loaded 3dObject

本文关键字:加载 3dObject 工作 光线投射 检测 threejs      更新时间:2023-09-26

我加载一个.obj文件:

var loader = new THREE.OBJMTLLoader();
loader.load( "../obj/machine.obj", '../obj/machine.mtl',  this.loadObject);

并尝试检测点击:

 click: function(event){
        this.mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
        this.mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
        var vector = new THREE.Vector3( this.mouse.x, this.mouse.y, 1 ).unproject( this.camera );
        this.raycaster.set( this.camera.position, vector.sub( this.camera.position ).normalize() );
        console.log(this.scene.children);
        var intersects = this.raycaster.intersectObjects( this.scene.children );
        if ( intersects.length > 0 ) {
            console.log("hitting something");
        }
    },

这在网格上效果良好,但在加载的3DObject上效果不佳,因为它在This.cene.childeren:中可见

[THREE.Mesh, THREE.Line, THREE.PointLight, --> THREE.Object3D <-- ]0: THREE.Mesh__webglActive: true__webglInit: true_listeners: Object_modelViewMatrix: THREE.Matrix4_normalMatrix: THREE.Matrix3castShadow: falsechildren: Array[0]eulerOrder: (...)frustumCulled: truegeometry: THREE.IcosahedronGeometryid: 4material: THREE.MeshBasicMaterialmatrix: THREE.Matrix4matrixAutoUpdate: truematrixWorld: THREE.Matrix4matrixWorldNeedsUpdate: falsename: ""parent: THREE.Sceneposition: THREE.Vector3quaternion: THREE.QuaternionreceiveShadow: falserenderDepth: nullrotation: THREE.EulerrotationAutoUpdate: truescale: THREE.Vector3type: "Mesh"up: THREE.Vector3useQuaternion: (...)userData: Objectuuid: "46D85379-A9CE-4221-A599-39D13EE4CB34"visible: true__proto__: Object1: THREE.Line2: THREE.PointLight3: THREE.Object3D

因此,有东西告诉我,它需要其他东西来加载3dObjects。但我不知道这可能是什么。我的想法是,光线投射会寻找相交的顶点,无论是立方体网格还是3dObject网格都无关紧要。有人有主意吗?

您需要像这样传递递归标志

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

three.js r.69