0
votes

When I click on a 3d model, I want to get the object id, name or object to be able to remove this object.

    function onDocumentMouseDown( event ) {
if (enableRaycasting){
event.preventDefault();
mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( 
    event.clientY / window.innerHeight ) * 2 + 1 );
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObjects( scene.children, true );
console.log(intersects);    
if ( intersects.length > 0 ) {


scene.remove(intersects[0]); //doesnt work   
    scene.remove(intersects[0].object); //doesnt work
    scene.remove(intersects[1]); //doesnt work
scene.remove(intersects[1].object); //doesnt work                
    console.log(intersects[0].object.name) //Bot_Skinned_0
    console.log(intersects[0].object.id)// output: 72



}

    render();
}
}

Problem: when I create the object, the ID is 21 and name of the model is "BotSkinned" (because I coded object.name = "BotSkinned"). Then I add the object to the scene:

    scene.add( object );
    console.log(scene.getObjectByName("BotSkinned").id); //output: 21

With Raycasting, I want to get that object and f.e. remove it, but none of these remove commands works and name and ID are different. Has somebody an idea how to get the correct object.id? that would be the easiest solution. Edit: Of course I could just say scene.remove(scene.getObjectByName("BotSkinned")), but the raycast should get me the exact object because I will have many of these objects in the future.

1

1 Answers

0
votes

This is the correct id: intersects[0].object.parent.parent.parent.id; So its easy to remove now: scene.remove(scene.getObjectById(intersects[0].object.parent.parent.parent.id)). Hopefully its similar for gltf, collada and so on..