three.js从场景中删除特定对象

时间:2016-09-04 22:32:09

标签: javascript three.js

我有一个Meshes数组,每个Mesh都在name属性中存储了它的ID。我想问你,是否可以从场景中删除具有特定ID的对象。这样的事情。

var geo = some geometry;
var mat = some material;
for (var i = 0; i < 10; i++) {
  var object = new THREE.Mesh(geo, mat);
  object.name = i;  // i would serve as ID in this case
}

在此之后,我想删除/删除其中一些对象...... 也许有些功能如

 remove(id);

...

 var remove = function (id) {
  ... some magic
  scene.remove(...) // and this would remove that object, with id passed as parameter 
 }

这样的事情可能吗?

谢谢!

1 个答案:

答案 0 :(得分:7)

是的:

function remove(id) {
  scene.remove(scene.getObjectByName(id));
}

请参阅:Object3D.remove()Object3D.getObjectByName()