2

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?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
gaitat
  • 12,449
  • 4
  • 52
  • 76
  • just as a reference for people landing here. see http://stackoverflow.com/a/33199591/1980846 – gaitat Apr 14 '16 at 03:01

3 Answers3

5

Try this:

object.traverse( function ( child ) {

    if ( child.geometry !== undefined ) {

        child.geometry.dispose();
        child.material.dispose();

    }

} );
mrdoob
  • 19,334
  • 4
  • 63
  • 62
  • Well if I add these lines after line 81 in webgl_loader_obj_mtl.html nothing happens. I actually would have expected to see no geometry since once it is loaded, I dispose of it. – gaitat Feb 01 '13 at 18:24
  • I also verified in Firebug that the code inside the if statement does get executed. What am I missing ? – gaitat Feb 01 '13 at 18:38
1

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).

Community
  • 1
  • 1
Marco Sulla
  • 15,299
  • 14
  • 65
  • 100
0

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){}
}
2pha
  • 9,798
  • 2
  • 29
  • 43