【发布时间】:2020-05-14 19:20:24
【问题描述】:
我正在使用 three.js 来创建一个“落下的甜甜圈”的动画。这些甜甜圈在每次场景渲染时动态生成,并被推送到一个数组中。
每个甜甜圈都是用随机的起始 x 位置和速度创建的,但它们都应该从屏幕顶部开始,然后垂直向下移动,直到它们到达我希望删除/移除它们的屏幕的某个点.虽然我可以轻松地从场景中移除对象,但我正在努力从数组中移除引用,该数组在每次渲染时都会变得更大,并且希望以某种方式进行优化。有没有其他方法或解决方案?我对threejs很陌生,对javascript也比较陌生。在此先感谢:)))
这是我的主循环函数:
let donuts = [];
let mainLoop = function() {
//create donut with a starting position and add to donuts array
donuts.push(createDonut());
donuts.forEach((donut)=>{
scene.add(donut);
donut.position.y += gravity * Math.random();
if(donut.position.y <= -1) {
donut.geometry.dispose();
donut.material.dispose();
scene.remove(donut);
}
});
renderer.render(scene, camera);
requestId = requestAnimationFrame(mainLoop);
};
【问题讨论】:
标签: three.js