【问题标题】:Three.JS instance can't be re-created三.JS实例无法重新创建
【发布时间】:2022-01-16 23:20:08
【问题描述】:

我正在尝试使用以下方法销毁(或销毁或处置) Three.JS 的“实例”:

完整示例:https://jsfiddle.net/v7oLzs4m/58/

function kill() {
  const rendererDomWas = renderer.domElement;
  renderer.dispose();
  controls.dispose();
  renderer = scene = camera = controls = null;
  document.body.removeChild( rendererDomWas );
  shouldRender = false;
}

function animate() {
  if(!shouldRender) {return} // no more requesting animation frames if 'shouldRender' is false
  frameID = window.requestAnimationFrame( animate );
  renderer.render( scene, camera );
}

(即在 shouldRenderfalse 时,从 touching 渲染器处理、设置对 null 的引用并停止绘制循环)

一开始它似乎可以工作(渲染器内容停止显示)但是当我重新创建实例时,它再也没有回来。

就好像有什么东西还在……持有 GLContext 以防止它再次被调用。

为什么我不能重新创建 Three.JS 的新实例?

【问题讨论】:

    标签: javascript three.js


    【解决方案1】:

    我不确定为什么会这样(因为我已经在我的 Fiddle 中这样做了......)

    但事实证明(对我而言)让 Three.JS 获得 GC'd/disposed 的秘诀是:

    window.cancelAnimationFrame(frameID);
    

    我想这会阻止渲染函数存储在硬件级绘制循环中,该循环保存对 GLContext(和链)的引用

    除非“循环被破坏”,否则 GC 永远不会发生

    (如有错误请指正)

    【讨论】:

      猜你喜欢
      • 2015-12-04
      • 2013-12-14
      • 2014-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 2020-01-16
      相关资源
      最近更新 更多