美文网首页
threejs 删除模型,释放内存

threejs 删除模型,释放内存

作者: 时间煮鱼 | 来源:发表于2023-05-28 22:07 被阅读0次
// 优化删除模型
    removeModel(parentGroup, group){
      parentGroup.remove(group);
      if (!this.$isEmpty(group)) {
        group.traverse(child => {
          if (child instanceof THREE.Mesh || child instanceof THREE.Line) {
            this.disposeMaterial(child.material);
            child.geometry.dispose && child.geometry.dispose();
          } else  {
            this.disposeMaterial(child.material);
          }
        });
        group = null;
      }
    },
    // 自己写的材质贴图释放
    disposeMaterial(material) { 
      if (material instanceof THREE.Material) {
        for (const value of Object.values(material)) {
          if (value instanceof THREE.Texture) {
            value.dispose();
          }
        }
        if (material.uniforms) {
          for (const value of Object.values(material.uniforms)) {
            if (value) {
              const uniformValue = value.value;
              if (uniformValue instanceof THREE.Texture) {
                uniformValue.dispose();
              }
              if(Array.isArray(uniformValue)){
                uniformValue.length = 0;
              }
            }
          }
        }
        material.dispose();
      }
    },

相关文章

网友评论

      本文标题:threejs 删除模型,释放内存

      本文链接:https://www.haomeiwen.com/subject/zfodedtx.html