You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/engine/Source/Renderer/TextureAtlas.js
+54-23Lines changed: 54 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -59,12 +59,16 @@ function TextureAtlas(options) {
59
59
this._initialSize=initialSize;
60
60
61
61
this._texturePacker=undefined;
62
+
/** @type {BoundingRectangle[]} */
62
63
this._rectangles=[];
64
+
/** @type {Map<number, number>} */
63
65
this._subRegions=newMap();
64
66
this._guid=createGuid();
65
67
66
68
this._imagesToAddQueue=[];
69
+
/** @type {Map<string, number>} */
67
70
this._indexById=newMap();
71
+
/** @type {Map<string, Promise<number>>} */
68
72
this._indexPromiseById=newMap();
69
73
this._nextIndex=0;
70
74
}
@@ -644,7 +648,7 @@ async function resolveImage(image, id) {
644
648
* @param {string} id An identifier to detect whether the image already exists in the atlas.
645
649
* @param {HTMLImageElement|HTMLCanvasElement|string|Resource|Promise|TextureAtlas.CreateImageCallback} image An image or canvas to add to the texture atlas,
646
650
* or a URL to an Image, or a Promise for an image, or a function that creates an image.
647
-
* @returns {Promise<number>} A Promise that resolves to the image region index. -1 is returned if resouces are in the process of being destroyed.
651
+
* @returns {Promise<number>} A Promise that resolves to the image region index, or -1 if resources are in the process of being destroyed.
@@ -653,12 +657,17 @@ TextureAtlas.prototype.addImage = function (id, image) {
653
657
//>>includeEnd('debug');
654
658
655
659
letpromise=this._indexPromiseById.get(id);
660
+
letindex=this._indexById.get(id);
656
661
if(defined(promise)){
657
-
// This image has already been added
662
+
// This image is already being added
658
663
returnpromise;
659
664
}
665
+
if(defined(index)){
666
+
// This image has already been added and resolved
667
+
returnPromise.resolve(index);
668
+
}
660
669
661
-
constindex=this._nextIndex++;
670
+
index=this._nextIndex++;
662
671
this._indexById.set(id,index);
663
672
664
673
constresolveAndAddImage=async()=>{
@@ -668,57 +677,79 @@ TextureAtlas.prototype.addImage = function (id, image) {
668
677
//>>includeEnd('debug');
669
678
670
679
if(this.isDestroyed()||!defined(image)){
680
+
this._indexPromiseById.delete(id);
671
681
return-1;
672
682
}
673
683
674
-
returnthis._addImage(index,image);
684
+
constimageIndex=awaitthis._addImage(index,image);
685
+
this._indexPromiseById.delete(id);
686
+
returnimageIndex;
675
687
};
676
688
677
689
promise=resolveAndAddImage();
678
690
this._indexPromiseById.set(id,promise);
679
691
returnpromise;
680
692
};
681
693
694
+
/**
695
+
* Get an existing sub-region of an existing atlas image as additional image indices.
696
+
* @private
697
+
* @param {string} id The identifier of the existing image.
698
+
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
699
+
* @param {number} imageIndex The index of the image.
700
+
* @returns {Promise<number> | number | undefined} The existing subRegion index, or undefined if not yet added.
* Add a sub-region of an existing atlas image as additional image indices.
684
726
* @private
685
727
* @param {string} id The identifier of the existing image.
686
728
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
687
-
* @returns {Promise<number>} A Promise that resolves to the image region index. -1 is returned if resouces are in the process of being destroyed.
729
+
* @returns {number | Promise<number>} The resolved image region index, or a Promise that resolves to it. -1 is returned if resources are in the process of being destroyed.
* @param {string} id An identifier to detect whether the image already exists in the atlas.
252
252
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
* @param {string} id An identifier to detect whether the image already exists in the atlas.
274
+
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
275
+
* @param {Promise<number>} indexPromise A promise that resolves to the image region index.
// There was an error loading the referenced image
266
288
this._loadState=BillboardLoadState.ERROR;
267
289
this._loadError=error;
268
290
return;
269
291
}
270
292
293
+
if(this._id!==id){
294
+
// Another load was initiated and resolved resolved before this one. This operation is cancelled.
295
+
return;
296
+
}
297
+
298
+
this._loadState=BillboardLoadState.LOADED;
299
+
300
+
this.setImageSubRegion(index,subRegion);
301
+
};
302
+
303
+
/**
304
+
* @see {TextureAtlas#addImageSubRegion}
305
+
* @private
306
+
* @param {number} index The resolved index in the {@link TextureAtlas}
307
+
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
0 commit comments