Skip to content

Commit c09783a

Browse files
committed
additional fixes for billboard image coords
1 parent eb2e510 commit c09783a

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
lines changed

packages/engine/Source/Renderer/TextureAtlas.js

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

195195
/**
196-
* Get the texture coordinates for reading the associated image in shaders.
196+
* TODO
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
201201
* @example
202202
* const index = await atlas.addImage("myImage", image);
203-
* const rectangle = atlas.computeTextureCoordinates(index);
203+
* const rectangle = atlas.computeImageCoordinates(index);
204204
* BoundingRectangle.pack(rectangle, bufferView);
205205
*/
206-
TextureAtlas.prototype.computeTextureCoordinates = function (index, result) {
206+
TextureAtlas.prototype.computeImageCoordinates = function (index, result) {
207207
//>>includeStart('debug', pragmas.debug);
208208
Check.typeOf.number.greaterThanOrEquals("index", index, 0);
209209
//>>includeEnd('debug');
210210

211-
const texture = this._texture;
212211
const rectangle = this._rectangles[index];
213212

214213
if (!defined(result)) {
@@ -224,11 +223,6 @@ TextureAtlas.prototype.computeTextureCoordinates = function (index, result) {
224223
return result;
225224
}
226225

227-
const atlasWidth = texture.width;
228-
const atlasHeight = texture.height;
229-
230-
const width = rectangle.width;
231-
const height = rectangle.height;
232226
let x = rectangle.x;
233227
let y = rectangle.y;
234228

@@ -240,10 +234,36 @@ TextureAtlas.prototype.computeTextureCoordinates = function (index, result) {
240234
y += parentRectangle.y;
241235
}
242236

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

248268
return result;
249269
};

packages/engine/Source/Scene/Billboard.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,16 @@ Billboard._updateClamping = function (collection, owner) {
11951195
updateFunction(scratchCartographic);
11961196
};
11971197

1198+
/**
1199+
* Get the image coordinates for reading the loaded texture in shaders.
1200+
* @param {BoundingRectangle} [result] The modified result parameter or a new BoundingRectangle instance if one was not provided.
1201+
* @return {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
1202+
* @private
1203+
*/
1204+
Billboard.prototype.computeImageCoordinates = function (result) {
1205+
return this._imageTexture.computeImageCoordinates(result);
1206+
};
1207+
11981208
/**
11991209
* Get the texture coordinates for reading the loaded texture in shaders.
12001210
* @param {BoundingRectangle} [result] The modified result parameter or a new BoundingRectangle instance if one was not provided.

packages/engine/Source/Scene/BillboardCollection.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,15 +1014,11 @@ function writeCompressedAttrib0(
10141014
let imageWidth = 0;
10151015
let imageHeight = 0;
10161016
if (billboard.ready) {
1017-
const imageRectangle = billboard.computeTextureCoordinates(
1018-
scratchBoundingRectangle,
1019-
);
1020-
const { width: atlasWidth, height: atlasHeight } =
1021-
billboardCollection.textureAtlas.texture;
1022-
imageX = imageRectangle.x * atlasWidth;
1023-
imageY = imageRectangle.y * atlasHeight;
1024-
imageWidth = imageRectangle.width * atlasWidth;
1025-
imageHeight = imageRectangle.height * atlasHeight;
1017+
billboard.computeImageCoordinates(scratchBoundingRectangle);
1018+
imageX = scratchBoundingRectangle.x;
1019+
imageY = scratchBoundingRectangle.y;
1020+
imageWidth = scratchBoundingRectangle.width;
1021+
imageHeight = scratchBoundingRectangle.height;
10261022
}
10271023

10281024
let compressed0 =
@@ -1060,13 +1056,13 @@ function writeCompressedAttrib0(
10601056
compressed1 += upperTranslateY;
10611057
compressed2 += lowerTranslateY;
10621058

1063-
// Compress image coordinates (px), integers 0-2^16 from lower-left of atlas. Avoid
1059+
// Compress image coordinates (px), integers 0-2^12 from lower-left of atlas. Avoid
10641060
// `AttributeCompression.compressTextureCoordinates` for lossless pixel values.
1065-
const compressedImageLL = imageX * LEFT_SHIFT16 + imageY;
1066-
const compressedImageLR = (imageX + imageWidth) * LEFT_SHIFT16 + imageY;
1061+
const compressedImageLL = imageX * LEFT_SHIFT12 + imageY;
1062+
const compressedImageLR = (imageX + imageWidth) * LEFT_SHIFT12 + imageY;
10671063
const compressedImageUR =
1068-
(imageX + imageWidth) * LEFT_SHIFT16 + imageY + imageHeight;
1069-
const compressedImageUL = imageX * LEFT_SHIFT16 + imageY + imageHeight;
1064+
(imageX + imageWidth) * LEFT_SHIFT12 + imageY + imageHeight;
1065+
const compressedImageUL = imageX * LEFT_SHIFT12 + imageY + imageHeight;
10701066

10711067
if (billboardCollection._instanced) {
10721068
i = billboard._index;

packages/engine/Source/Scene/BillboardTexture.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,17 @@ BillboardTexture.prototype.setImageSubRegion = function (index, subRegion) {
334334
this.dirty = true;
335335
};
336336

337+
/**
338+
* Get the image coordinates for reading the loaded texture in shaders.
339+
* @private
340+
* @param {BoundingRectangle} [result] The modified result parameter or a new BoundingRectangle instance if one was not provided.
341+
* @return {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
342+
*/
343+
BillboardTexture.prototype.computeImageCoordinates = function (result) {
344+
const atlas = this._billboardCollection.textureAtlas;
345+
return atlas.computeImageCoordinates(this._index, result);
346+
};
347+
337348
/**
338349
* Get the texture coordinates for reading the loaded texture in shaders.
339350
* @private

packages/engine/Source/Shaders/BillboardCollectionVS.glsl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,17 @@ void main()
168168
translate.y -= UPPER_BOUND;
169169
translate.y *= SHIFT_RIGHT2;
170170

171-
temp = compressedAttribute1.x * SHIFT_RIGHT8;
172-
float temp2 = floor(compressedAttribute2.w * SHIFT_RIGHT2);
171+
// TODO(donmccurdy): This is billboard size (px) on screen, not image size (px) in atlas?
172+
float imageWidth = floor(compressedAttribute1.x * SHIFT_RIGHT8);
173+
float imageHeight = floor(compressedAttribute2.w * SHIFT_RIGHT2);
174+
vec2 imageSize = vec2(imageWidth, imageHeight);
173175

174-
vec2 imageSize = vec2(floor(temp), temp2);
175-
176-
float imageOffsetX = floor(compressedAttribute0.w * SHIFT_RIGHT16);
177-
float imageOffsetY = compressedAttribute0.w - (imageOffsetX * SHIFT_LEFT16);
178-
vec2 textureCoordinates = vec2(imageOffsetX, imageOffsetY) / u_atlasSize.xy;
176+
float imageOffsetX = floor(compressedAttribute0.w * SHIFT_RIGHT12);
177+
float imageOffsetY = compressedAttribute0.w - (imageOffsetX * SHIFT_LEFT12);
178+
vec2 textureCoordinates = vec2(imageOffsetX + 0.5, imageOffsetY + 0.5) / u_atlasSize.xy;
179179

180180
#ifdef INSTANCED
181-
vec2 textureCoordinatesRange = imageSize.xy / u_atlasSize.xy;
181+
vec2 textureCoordinatesRange = imageSize.xy / u_atlasSize.xy; // TODO(donmccurdy): Needs -1.0 offset?
182182
textureCoordinates += direction * textureCoordinatesRange;
183183
#endif
184184

0 commit comments

Comments
 (0)