Skip to content

Commit 9ea515f

Browse files
committed
refactor: avoid adding new helper function
1 parent ddb10cc commit 9ea515f

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

packages/engine/Source/Renderer/TextureAtlas.js

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -193,17 +193,22 @@ Object.defineProperties(TextureAtlas.prototype, {
193193
});
194194

195195
/**
196-
* Get integer pixel coordinates for a given image or image region.
196+
* Get the texture coordinates for reading the associated image in shaders.
197197
* @param {number} index The index of the image region.
198198
* @param {BoundingRectangle} [result] The object into which to store the result.
199199
* @return {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
200200
* @private
201+
* @example
202+
* const index = await atlas.addImage("myImage", image);
203+
* const rectangle = atlas.computeTextureCoordinates(index);
204+
* BoundingRectangle.pack(rectangle, bufferView);
201205
*/
202-
TextureAtlas.prototype.computePixelCoordinates = function (index, result) {
206+
TextureAtlas.prototype.computeTextureCoordinates = function (index, result) {
203207
//>>includeStart('debug', pragmas.debug);
204208
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
205209
//>>includeEnd('debug');
206210

211+
const texture = this._texture;
207212
const rectangle = this._rectangles[index];
208213

209214
if (!defined(result)) {
@@ -219,6 +224,9 @@ TextureAtlas.prototype.computePixelCoordinates = function (index, result) {
219224
return result;
220225
}
221226

227+
const atlasWidth = texture.width;
228+
const atlasHeight = texture.height;
229+
222230
const width = rectangle.width;
223231
const height = rectangle.height;
224232
let x = rectangle.x;
@@ -232,36 +240,10 @@ TextureAtlas.prototype.computePixelCoordinates = function (index, result) {
232240
y += parentRectangle.y;
233241
}
234242

235-
result.x = x;
236-
result.y = y;
237-
result.width = width;
238-
result.height = height;
239-
240-
return result;
241-
};
242-
243-
/**
244-
* Get the texture coordinates for reading the associated image in shaders.
245-
* @param {number} index The index of the image region.
246-
* @param {BoundingRectangle} [result] The object into which to store the result.
247-
* @return {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
248-
* @private
249-
* @example
250-
* const index = await atlas.addImage("myImage", image);
251-
* const rectangle = atlas.computeTextureCoordinates(index);
252-
* BoundingRectangle.pack(rectangle, bufferView);
253-
*/
254-
TextureAtlas.prototype.computeTextureCoordinates = function (index, result) {
255-
result = this.computePixelCoordinates(index, result);
256-
257-
const texture = this._texture;
258-
const atlasWidth = texture.width;
259-
const atlasHeight = texture.height;
260-
261-
result.x /= atlasWidth;
262-
result.y /= atlasHeight;
263-
result.width /= atlasWidth;
264-
result.height /= atlasHeight;
243+
result.x = x / atlasWidth;
244+
result.y = y / atlasHeight;
245+
result.width = width / atlasWidth;
246+
result.height = height / atlasHeight;
265247

266248
return result;
267249
};

packages/engine/Specs/Renderer/TextureAtlasSpec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,13 @@ function drawAtlas(atlas, indices) {
13981398

13991399
// draw each rectangle, filling with its index.
14001400
for (const index of indices) {
1401-
atlas.computePixelCoordinates(index, rect);
1401+
// compute rectangle coordinates in pixels.
1402+
atlas.computeTextureCoordinates(index, rect);
1403+
rect.x *= width;
1404+
rect.y *= height;
1405+
rect.width *= width;
1406+
rect.height *= height;
1407+
14021408
for (let dx = 0; dx < rect.width; dx++) {
14031409
const x = rect.x + dx;
14041410
for (let dy = 0; dy < rect.height; dy++) {

0 commit comments

Comments
 (0)