Skip to content

Commit 972d4b5

Browse files
committed
Suggested CR changes
1 parent 5d00fca commit 972d4b5

File tree

3 files changed

+38
-37
lines changed

3 files changed

+38
-37
lines changed

packages/engine/Source/DataSources/BillboardVisualizer.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ function EntityData(entity) {
4141
this.entity = entity;
4242
this.billboard = undefined;
4343
this.textureValue = undefined;
44-
this.subRegion = undefined;
4544
}
4645

4746
/**
@@ -129,7 +128,6 @@ BillboardVisualizer.prototype.update = function (time) {
129128
billboard.id = entity;
130129
item.billboard = billboard;
131130
item.textureValue = undefined;
132-
item.subRegion = undefined;
133131
}
134132

135133
billboard.show = show;
@@ -235,14 +233,7 @@ BillboardVisualizer.prototype.update = function (time) {
235233
time,
236234
boundingRectangleScratch,
237235
);
238-
const subRegionUpdated = !BoundingRectangle.equals(
239-
subRegion,
240-
item.subRegion,
241-
);
242-
if (subRegionUpdated) {
243-
item.subRegion = BoundingRectangle.clone(subRegion, item.subRegion);
244-
}
245-
if (defined(subRegion) && (imageUpdated || subRegionUpdated)) {
236+
if (defined(subRegion)) {
246237
billboard.setImageSubRegion(billboard.image, subRegion);
247238
}
248239
}

packages/engine/Source/Renderer/TextureAtlas.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,13 @@ TextureAtlas.prototype.addImage = function (id, image) {
680680
};
681681

682682
/**
683-
* Add a sub-region of an existing atlas image as additional image indices.
683+
* Get a sub-region of an existing atlas image as additional image indices.
684684
* @private
685685
* @param {string} id The identifier of the existing image.
686686
* @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.
687+
* @returns {number | undefined} The existing subRegion index, or undefined if not yet added.
688688
*/
689-
TextureAtlas.prototype.addImageSubRegion = function (id, subRegion) {
689+
TextureAtlas.prototype.getImageSubRegion = function (id, subRegion) {
690690
//>>includeStart('debug', pragmas.debug);
691691
Check.typeOf.string("id", id);
692692
Check.defined("subRegion", subRegion);
@@ -697,28 +697,36 @@ TextureAtlas.prototype.addImageSubRegion = function (id, subRegion) {
697697
throw new RuntimeError(`image with id "${id}" not found in the atlas.`);
698698
}
699699

700-
const indexPromise = this._indexPromiseById.get(id);
701700
for (const [index, parentIndex] of this._subRegions.entries()) {
702701
if (imageIndex === parentIndex) {
703702
const boundingRegion = this._rectangles[index];
704703
if (boundingRegion.equals(subRegion)) {
705704
// The subregion is already being tracked
706-
return indexPromise.then((resolvedImageIndex) => {
707-
if (resolvedImageIndex === -1) {
708-
// The atlas has been destroyed
709-
return -1;
710-
}
711-
712-
return index;
713-
});
705+
return index;
714706
}
715707
}
716708
}
709+
};
717710

718-
const index = this._nextIndex++;
711+
/**
712+
* Add a sub-region of an existing atlas image as additional image indices.
713+
* @private
714+
* @param {string} id The identifier of the existing image.
715+
* @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
716+
* @returns {Promise<number>} A Promise that resolves to the image region index. -1 is returned if resouces are in the process of being destroyed.
717+
*/
718+
TextureAtlas.prototype.addImageSubRegion = function (id, subRegion) {
719+
let index = this.getImageSubRegion(id, subRegion);
720+
if (index) {
721+
return index;
722+
}
723+
const imageIndex = this._indexById.get(id);
724+
725+
index = this._nextIndex++;
719726
this._subRegions.set(index, imageIndex);
720727
this._rectangles[index] = subRegion.clone();
721728

729+
const indexPromise = this._indexPromiseById.get(id);
722730
return indexPromise.then((imageIndex) => {
723731
if (imageIndex === -1) {
724732
// The atlas has been destroyed

packages/engine/Source/Scene/BillboardTexture.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,27 @@ BillboardTexture.prototype.loadImage = async function (id, image) {
253253
*/
254254
BillboardTexture.prototype.addImageSubRegion = async function (id, subRegion) {
255255
this._id = id;
256-
this._loadState = BillboardLoadState.LOADING;
257256
this._loadError = undefined;
258257
this._hasSubregion = true;
259258

260-
let index;
261259
const atlas = this._billboardCollection.textureAtlas;
262-
try {
263-
index = await atlas.addImageSubRegion(id, subRegion);
264-
} catch (error) {
265-
// There was an error loading the referenced image
266-
this._loadState = BillboardLoadState.ERROR;
267-
this._loadError = error;
268-
return;
269-
}
260+
let index = atlas.getImageSubRegion(id, subRegion);
261+
if (!index) {
262+
try {
263+
this._loadState = BillboardLoadState.LOADING;
264+
index = await atlas.addImageSubRegion(id, subRegion);
265+
} catch (error) {
266+
// There was an error loading the referenced image
267+
this._loadState = BillboardLoadState.ERROR;
268+
this._loadError = error;
269+
return;
270+
}
270271

271-
if (this._id !== id) {
272-
// Another load was initiated and resolved resolved before this one. This operation is cancelled.
273-
return;
272+
if (this._id !== id) {
273+
// Another load was initiated and resolved resolved before this one. This operation is cancelled.
274+
return;
275+
}
276+
this._loadState = BillboardLoadState.LOADED;
274277
}
275278

276279
if (!defined(index) || index === -1) {
@@ -285,7 +288,6 @@ BillboardTexture.prototype.addImageSubRegion = async function (id, subRegion) {
285288
this._height = subRegion.height;
286289

287290
this._index = index;
288-
this._loadState = BillboardLoadState.LOADED;
289291

290292
this.dirty = true;
291293
};

0 commit comments

Comments
 (0)