Search code examples
three.jscolladaraycasting

Three.js raycast THREE.Object3D


I want to get the name of an object i clicked at. The model is loaded into the Scene with ColladaLoader.js. My Problem is, i just get the THREE.Mesh object but i need the THREE.Object3D object, because the Mesh doesn't contain the name.

If i use the following code:

scene.traverse (function (object){
    console.log(object);
});

I get:

THREE.Object3D {uuid: "085928DC-5493-4C57-B142-51D2A95F27B6", name: "Schraube_M4x16_002", type: "Object3D", parent: THREE.Object3D, children: Array[1]…} 
THREE.Mesh {uuid: "1AD3D989-CEB7-4B89-BE88-6D58C1C24AD6", name: "", type: "Mesh", parent: THREE.Object3D, children: Array[0]…} 

The Object3D has a name the Mesh not. But Raycast only returns meshes How to fix that?


Solution

  • I finally modifyed the ColladaLoader and added the node's name to the mesh.

    function createSceneGraph( node, parent ) {
        ...
        // geometries
        ...
        } else {
            if ( geom.isLineStrip === true ) {
                mesh = new THREE.Line ( geom );
            } else {
                mesh = new THREE.Mesh ( geom, material );
            }
            mesh.name = node.name;
        }
        ...
    }