我怎么能从记忆中擦除场景和网格? three.js所

时间:2015-09-14 22:48:42

标签: javascript three.js

我正在添加" n"场景中的圈数。

var radius = 1; 
var segments = 32; 
var circleGeometry = new THREE.CircleGeometry( radius, segments); 
function generateCircles(){
  //scene.remove(circle);
  var count=0;
  while (1000> count) {
   circle = new THREE.Mesh (circleGeometry, material);
   scene.add (circle);
   count ++;
  }
}

以这种方式这样做是否有效?。

在我的代码中我调用了这个函数。每当你调用它时,一切都会变慢,我猜它是因为场景中有更多的物体。我该怎么办?

每次调用该函数时,我都需要从内存阶段和生成的圆圈中完全删除。

"慢",我的意思是我希望我的应用程序运行得更快。每次运行该功能时都会添加越来越多的圈子。所以我想早点删除。添加新的。如果场景中有很多圆圈,则会减慢执行速度。

http://jsfiddle.net/v8oxsxtc/

1 个答案:

答案 0 :(得分:0)

您可以通过在之前添加的每个圈子上调用scene.remove方法从场景中删除旧圈子。以下是使用代码的简单示例:

var lastCircles = null;
function generateCircles(){
  var count=0;
  if(lastCircles) { // remove old circles if they exist
    lastCircles.forEach(function(c) {
      scene.remove(c);
    });
  }
  lastCircles = []; // clear cache
  while (1000 > count) {
    circle = new THREE.Mesh (circleGeometry, material);
    lastCircles.push(circle); // add each circle to cache
    scene.add (circle);
      circle.position.set(count,count,count)
    count ++;
  }
}
相关问题