import * as THREE from 'three' ; function disposeMaterial ( material ) { Object . keys ( material ) . forEach ( ( propertyName ) => { const value = ( material as any ) [ propertyName ] ; if ( value ?. isTexture ) { const texture = value as THREE . Texture ; texture . dispose ( ) ; } } ) ; material . dispose ( ) ; } function dispose ( object3D ) { const geometry = ( object3D as any ) . geometry ; if ( geometry ) { geometry . dispose ( ) ; } const material = ( object3D as any ) . material ; if ( material ) { if ( Array . isArray ( material ) ) { material . forEach ( ( material ) => disposeMaterial ( material ) ) ; } else if ( material ) { disposeMaterial ( material ) ; } } } export function deepDispose ( object3D ) { object3D . traverse ( dispose ) ; }