如何销毁 THREEJS 场景?

2022-01-16 00:00:00 garbage-collection javascript three.js

我创建了一个 Threejs 场景,添加了相机、灯光和各种对象.

I created a Threejs Scene, adding camera, lights and various objects.

问题很简单:如何破坏场景?从场景中移除所有组件?

The question is simple: how can I destroy scene? Removing from scene all components?

我需要销毁场景,因为我确实不想将任务委托给垃圾收集器.

I need to destroy scene because and I do not want to delegate the task to the garbage collector.

推荐答案

我用这个:

    cancelAnimationFrame(this.id);// Stop the animation
    this.renderer.domElement.addEventListener('dblclick', null, false); //remove listener to render
    this.scene = null;
    this.projector = null;
    this.camera = null;
    this.controls = null;
    empty(this.modelContainer);

方法empty是jQuery empty的替代品,可以使用:

The method empty is a substitute to jQuery empty, you can use it:

function empty(elem) {
    while (elem.lastChild) elem.removeChild(elem.lastChild);
}

相关文章