@@ -35,20 +35,19 @@ type AssetData = (Uint8Array | string | Texture | TextureAtlas | object) & Parti
3535type AssetCallback < T extends AssetData > = ( path : string , data : T ) => void ;
3636type ErrorCallback = ( path : string , message : string ) => void ;
3737
38+ export type TextureLoader = ( image : HTMLImageElement | ImageBitmap , pma ?: boolean ) => Texture ;
39+
3840export class AssetManagerBase implements Disposable {
39- private pathPrefix : string = "" ;
40- private textureLoader : ( image : HTMLImageElement | ImageBitmap ) => Texture ;
41- private downloader : Downloader ;
42- private cache : AssetCache ;
4341 private errors : StringMap < string > = { } ;
4442 private toLoad = 0 ;
4543 private loaded = 0 ;
44+ private texturePmaInfo : Record < string , boolean > = { } ;
4645
47- constructor ( textureLoader : ( image : HTMLImageElement | ImageBitmap ) => Texture , pathPrefix : string = "" , downloader = new Downloader ( ) , cache = new AssetCache ( ) ) {
48- this . textureLoader = textureLoader ;
49- this . pathPrefix = pathPrefix ;
50- this . downloader = downloader ;
51- this . cache = cache ;
46+ constructor (
47+ private textureLoader : TextureLoader ,
48+ private pathPrefix : string = "" ,
49+ private downloader = new Downloader ( ) ,
50+ private cache = new AssetCache ( ) ) {
5251 }
5352
5453 private start ( path : string ) : string {
@@ -175,6 +174,7 @@ export class AssetManagerBase implements Disposable {
175174
176175 if ( this . reuseAssets ( path , success , error ) ) return ;
177176
177+ const pma = this . texturePmaInfo [ path ] ;
178178 this . cache . assetsLoaded [ path ] = new Promise < Texture > ( ( resolve , reject ) => {
179179 const isBrowser = ! ! ( typeof window !== 'undefined' && typeof navigator !== 'undefined' && window . document ) ;
180180 const isWebWorker = ! isBrowser ; // && typeof importScripts !== 'undefined';
@@ -188,7 +188,7 @@ export class AssetManagerBase implements Disposable {
188188 return blob ? createImageBitmap ( blob , { premultiplyAlpha : "none" , colorSpaceConversion : "none" } ) : null ;
189189 } ) . then ( ( bitmap ) => {
190190 if ( bitmap ) {
191- const texture = this . createTexture ( path , bitmap ) ;
191+ const texture = this . createTexture ( path , pma , bitmap ) ;
192192 this . success ( success , path , texture ) ;
193193 resolve ( texture ) ;
194194 } ;
@@ -197,7 +197,7 @@ export class AssetManagerBase implements Disposable {
197197 const image = new Image ( ) ;
198198 image . crossOrigin = "anonymous" ;
199199 image . onload = ( ) => {
200- const texture = this . createTexture ( path , image ) ;
200+ const texture = this . createTexture ( path , pma , image ) ;
201201 this . success ( success , path , texture ) ;
202202 resolve ( texture ) ;
203203 } ;
@@ -216,7 +216,7 @@ export class AssetManagerBase implements Disposable {
216216 path : string ,
217217 success : AssetCallback < TextureAtlas > = ( ) => { } ,
218218 error : ErrorCallback = ( ) => { } ,
219- fileAlias ?: { [ keyword : string ] : string }
219+ fileAlias ?: Record < string , string >
220220 ) {
221221 const index = path . lastIndexOf ( "/" ) ;
222222 const parent = index >= 0 ? path . substring ( 0 , index + 1 ) : "" ;
@@ -227,10 +227,11 @@ export class AssetManagerBase implements Disposable {
227227 this . cache . assetsLoaded [ path ] = new Promise < TextureAtlas > ( ( resolve , reject ) => {
228228 this . downloader . downloadText ( path , ( atlasText : string ) : void => {
229229 try {
230- const atlas = this . createTextureAtlas ( path , atlasText ) ;
230+ const atlas = this . createTextureAtlas ( atlasText , parent , path , fileAlias ) ;
231231 let toLoad = atlas . pages . length , abort = false ;
232232 for ( const page of atlas . pages ) {
233- this . loadTexture ( ! fileAlias ? parent + page . name : fileAlias [ page . name ] ,
233+ this . loadTexture (
234+ this . texturePath ( parent , page . name , fileAlias ) ,
234235 ( imagePath : string , texture : Texture ) => {
235236 if ( ! abort ) {
236237 page . setTexture ( texture ) ;
@@ -268,14 +269,16 @@ export class AssetManagerBase implements Disposable {
268269 success : AssetCallback < TextureAtlas > = ( ) => { } ,
269270 error : ErrorCallback = ( ) => { } ,
270271 ) {
272+ const index = path . lastIndexOf ( "/" ) ;
273+ const parent = index >= 0 ? path . substring ( 0 , index + 1 ) : "" ;
271274 path = this . start ( path ) ;
272275
273276 if ( this . reuseAssets ( path , success , error ) ) return ;
274277
275278 this . cache . assetsLoaded [ path ] = new Promise < TextureAtlas > ( ( resolve , reject ) => {
276279 this . downloader . downloadText ( path , ( atlasText : string ) : void => {
277280 try {
278- const atlas = this . createTextureAtlas ( path , atlasText ) ;
281+ const atlas = this . createTextureAtlas ( atlasText , parent , path ) ;
279282 this . success ( success , path , atlas ) ;
280283 resolve ( atlas ) ;
281284 } catch ( e ) {
@@ -291,7 +294,6 @@ export class AssetManagerBase implements Disposable {
291294 } ) ;
292295 }
293296
294- // Promisified versions of load function
295297 async loadBinaryAsync ( path : string ) {
296298 return new Promise ( ( resolve , reject ) => {
297299 this . loadBinary ( path ,
@@ -413,7 +415,7 @@ export class AssetManagerBase implements Disposable {
413415 }
414416 }
415417
416- private createTextureAtlas ( path : string , atlasText : string ) : TextureAtlas {
418+ private createTextureAtlas ( atlasText : string , parentPath : string , path : string , fileAlias ?: Record < string , string > ) : TextureAtlas {
417419 const atlas = new TextureAtlas ( atlasText ) ;
418420 atlas . dispose = ( ) => {
419421 if ( this . cache . assetsRefCount [ path ] <= 0 ) return ;
@@ -422,17 +424,26 @@ export class AssetManagerBase implements Disposable {
422424 page . texture ?. dispose ( ) ;
423425 }
424426 }
427+ for ( const page of atlas . pages ) {
428+ const texturePath = this . texturePath ( parentPath , page . name , fileAlias ) ;
429+ this . texturePmaInfo [ this . pathPrefix + texturePath ] = page . pma ;
430+ }
425431 return atlas ;
426432 }
427433
428- private createTexture ( path : string , image : HTMLImageElement | ImageBitmap ) : Texture {
429- const texture = this . textureLoader ( image ) ;
434+ private createTexture ( path : string , pma : boolean , image : HTMLImageElement | ImageBitmap ) : Texture {
435+ const texture = this . textureLoader ( image , pma ) ;
430436 const textureDispose = texture . dispose . bind ( texture ) ;
431437 texture . dispose = ( ) => {
432438 if ( this . disposeAssetInternal ( path ) ) textureDispose ( ) ;
433439 }
434440 return texture ;
435441 }
442+
443+ private texturePath ( parentPath : string , pageName : string , fileAlias ?: Record < string , string > ) {
444+ if ( ! fileAlias ) return parentPath + pageName ;
445+ return fileAlias [ pageName ] ;
446+ }
436447}
437448
438449export class AssetCache {
0 commit comments