Deallocating Object3D

前端 未结 3 1632
太阳男子
太阳男子 2021-02-11 04:18

I load a model from an obj file using in addition the mtl file. How do I properly dispose off or deallocate all the geometry/materials/textures from the returned Object3D in r55

相关标签:
3条回答
  • 2021-02-11 04:51

    Try this:

    object.traverse( function ( child ) {
    
        if ( child.geometry !== undefined ) {
    
            child.geometry.dispose();
            child.material.dispose();
    
        }
    
    } );
    
    0 讨论(0)
  • 2021-02-11 04:51

    I use this:

    function removeReferences(removeme){
      try{
        removeme.traverse(function(ob){
          try{
            renderer.deallocateObject(ob);
          }catch(e){} 
          try{
            ob.geometry.deallocate();
          }catch(e){}
          try{
            ob.material.deallocate();
          }catch(e){} 
          try{
            ob.deallocate()
          }catch(e){}
        });
      }catch(e){}
    }
    
    0 讨论(0)
  • 2021-02-11 04:58

    Thanks to mrdoob's example, I created a function that recursively dispose a three.js object. I added it to my personal three.js util library: https://github.com/MarcoSulla/my3

    function dispose3(obj) {
        /**
         *  @author Marco Sulla (marcosullaroma@gmail.com)
         *  @date Mar 12, 2016
         */
    
        "use strict";
    
        var children = obj.children;
        var child;
    
        if (children) {
            for (var i=0; i<children.length; i+=1) {
                child = children[i];
    
                dispose3(child);
            }
        }
    
        var geometry = obj.geometry;
        var material = obj.material;
    
        if (geometry) {
            geometry.dispose();
        }
    
        if (material) {
            var texture = material.map;
    
            if (texture) {
                texture.dispose();
            }
    
            material.dispose();
        }
    },
    

    My hope is this function will be added in three.js code, in Scene.remove method (maybe called only if you set an optional flag).

    0 讨论(0)
提交回复
热议问题