Three.js清除场景中的模型和材质、贴图等

遍历所有子项并调用它们的几何形状,材质和纹理,亲测有效

function clearThree(obj){
  while(obj.children.length > 0){ 
    clearThree(obj.children[0])
    obj.remove(obj.children[0]);
  }
  if(obj.geometry) obj.geometry.dispose()

  if(obj.material){ 
    //in case of map, bumpMap, normalMap, envMap ...
    Object.keys(obj.material).forEach(prop => {
      if(!obj.material[prop])
        return         
      if(typeof obj.material[prop].dispose === 'function')                                  
        obj.material[prop].dispose()                                                        
    })
    obj.material.dispose()
  }
}   

clearThree(scene)

转载于博主别动,屎里有毒的一篇文章

你可能感兴趣的:(threejs,javascript,材质,贴图)