Javascript 如何从记忆中删除场景和网格?3.js
我在场景中添加了“n”个圆圈Javascript 如何从记忆中删除场景和网格?3.js,javascript,three.js,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 (circleG
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 ++;
}
}
这样做是有效的
在我的代码中,我调用这个函数。每次你调用它,它都会变慢,我想这是因为场景中有更多的对象。我能做什么
每次调用函数时,我都需要从内存阶段和生成的圆圈中完全擦除
我的意思是,我希望我的应用程序运行得更快。每次我运行该函数时,都会添加越来越多的圆。所以我想早点离开。添加新的。如果场景中有许多圆圈,则会减慢执行速度
您可以通过调用
场景来从场景中删除旧的圆。删除先前添加的每个圆上的方法。下面是一个使用您的代码的简单示例:
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 ++;
}
}
“一切都变慢了”是什么意思?@SimpleJ加上“变慢了”,我的意思是我希望我的应用程序运行得更快。每次我运行该函数时,都会添加越来越多的圆。所以我想早点离开。添加新的。如果场景中有许多圆圈,则会减慢执行速度。