ThreeJS: Remove object from scene

前端 未结 8 2085
后悔当初
后悔当初 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:04

    clearScene: function() {
        var objsToRemove = _.rest(scene.children, 1);
        _.each(objsToRemove, function( object ) {
              scene.remove(object);
        });
    },
    

    this uses undescore.js to iterrate over all children (except the first) in a scene (it's part of code I use to clear a scene). just make sure you render the scene at least once after deleting, because otherwise the canvas does not change! There is no need for a "special" obj flag or anything like this.

    Also you don't delete the object by name, just by the object itself, so calling

    scene.remove(object); 
    

    instead of scene.remove(object.name); can be enough

    PS: _.each is a function of underscore.js

提交回复
热议问题