ThreeJS: Remove object from scene

前端 未结 8 1970
后悔当初
后悔当初 2021-01-30 06:45

I\'m using ThreeJS to develop a web application that displays a list of entities, each with corresponding \"View\" and \"Hide\" button; e.g. entityName View Hide

8条回答
  •  走了就别回头了
    2021-01-30 07:08

    I think seeing your usage for addEntity and removeEntity code would be helpful, but my first thought is are you actually setting the object.name? Try in your loader just before scene.add(object); something like this:

    object.name = "test_name";
    scene.add(object);
    

    What might be happening is the default "name" for an Object3D is "", so when you then call your removeEntity function it fails due to the scene objects name being ""

    Also, I notice you pass in object.name to your loader? Is this where your storing the URL to the resource? If so, I would recommend using the Object3D's built in .userData method to store that information and keep the name field for scene identification purposes.

    Edit: Response to newly added Code

    First thing to note is it's not a great idea to have "/" in your object name, it seems to work fine but you never know if some algorithm will decide to escape that string and break your project.

    Second item is now that I've seen your code, its actually straight forward whats going on. Your delete function is trying to delete by name, you need an Object3D to delete. Try this:

    function removeEntity(object) {
        var selectedObject = scene.getObjectByName(object.name);
        scene.remove( selectedObject );
        animate();
    }
    

    Here you see I lookup your Object3D in the Three.js Scene by passing in your object tag's name attribute. Hope that helps

提交回复
热议问题