import { assert } from '../auxiliaries' ; import { Buffer } from '../buffer' ; import { Context } from '../context' ; import { Geometry } from '../geometry' ; import { Material } from '../scene' ; import { Texture2D } from '../texture2d' ; todo export class ResourceManager { /**
     * Context, used to get context information and WebGL API access.
     */ protected _context : Context ; /**
     * Internal storage of 2D textures.
     */ protected _texture2Ds = new Map < string , Texture2D > ( ) ; /**
     * Internal storage of material.
     */ protected _materials = new Map < string , Material > ( ) ; /**
     * Internal storage of geometries.
     */ protected _geometries = new Map < string , Geometry > ( ) ; /**
     * Internal storage of buffers.
     */ protected _buffers = new Map < string , Buffer > ( ) ; param context constructor ( context ) { this . _context = context ; } /**
     * Creates a Texture2D object and asynchronously loads its image via URL or data URI (@see  Texture2D . fetch ).
     *  param url param crossOrigin param identifier returns fetchTexture2D ( identifier , url , crossOrigin = false ) { if ( this . _texture2Ds . has ( identifier ) ) { return undefined ; } const texture = new Texture2D ( this . _context , identifier ) ; assert ( texture . identifier === identifier , `expected object identifier to be unchanged` ) ; this . _texture2Ds . set ( identifier , texture ) ; return texture . fetch ( url , crossOrigin ) ; } param resource param identifiers returns add ( resource , identifiers ) { const addedIdentifiers = new Array < string > ( ) ; if ( resource instanceof Texture2D ) { const texture = resource as Texture2D ; for ( const identifier of identifiers ) { if ( ! this . _texture2Ds . has ( identifier ) ) { this . _texture2Ds . set ( identifier , texture ) ; addedIdentifiers . push ( identifier ) ; } } } if ( resource instanceof Material ) { const material = resource as Material ; for ( const identifier of identifiers ) { if ( ! this . _materials . has ( identifier ) ) { this . _materials . set ( identifier , material ) ; addedIdentifiers . push ( identifier ) ; } } } if ( resource instanceof Geometry ) { const geometry = resource as Geometry ; for ( const identifier of identifiers ) { if ( ! this . _geometries . has ( identifier ) ) { this . _geometries . set ( identifier , geometry ) ; addedIdentifiers . push ( identifier ) ; } } } if ( resource instanceof Buffer ) { const buffer = resource as Buffer ; for ( const identifier of identifiers ) { if ( ! this . _buffers . has ( identifier ) ) { this . _buffers . set ( identifier , buffer ) ; addedIdentifiers . push ( identifier ) ; } } } return addedIdentifiers ; } param identifier get ( identifier ) { if ( this . _texture2Ds . has ( identifier ) ) { return this . _texture2Ds . get ( identifier ) ; } if ( this . _materials . has ( identifier ) ) { return this . _materials . get ( identifier ) ; } if ( this . _geometries . has ( identifier ) ) { return this . _geometries . get ( identifier ) ; } if ( this . _buffers . has ( identifier ) ) { return this . _buffers . get ( identifier ) ; } return undefined ; } uninitialize ( ) { for ( const geometry of Array . from ( this . _geometries . values ( ) ) ) { if ( geometry . initialized ) { geometry . uninitialize ( ) ; } } this . _geometries . clear ( ) ; for ( const tex2D of Array . from ( this . _texture2Ds . values ( ) ) ) { if ( tex2D . initialized ) { tex2D . uninitialize ( ) ; } } this . _texture2Ds . clear ( ) ; for ( const buffer of Array . from ( this . _buffers . values ( ) ) ) { if ( buffer . initialized ) { buffer . uninitialize ( ) ; } } this . _buffers . clear ( ) ; this . _materials . clear ( ) ; } }