Best strategy for disposing invisible textures loaded by GLTFLoader

Hello everyone!

I have a GLTF file with a lot of textures distributed on a large scene: while the user explores the scene, the memory load goes up because new textures are loaded in memory, but old ones aren’t disposed of.

I’d like to call .dispose() on textures that are not visible on the scene, but I don’t have a direct reference to them, since they are managed by the GLTFloader. How can I dispose them, without having to use a custom loader?

Thanks!

Disposal of textures only works if you call .dispose() on a texture object. Have you tried to traverse through the glTF scene that is returned by the loader? Something like:

loader.load( 'model.gltf', function ( gltf ) {

    gltf.scene.traverse( function ( child ) {

        if ( child.material ) {

            // do something with child.material
						
       }

    } );

    scene.add( gltf.scene );

}

In this way you can create a map/set of all textures in the scene and dispose them if necessary.

1 Like

You set material opacity = 0

That does not dispose a texture…

2 Likes

Thanks! I already keep track of the children, I haven’t tough about using child.material.map.dispose(), that was easy, I just needed to add some logic for checking visibility of the texture :slight_smile:

Thank you very much!