All files / engine/Source/Scene BillboardTexture.js

92.02% Statements 127/138
94.59% Branches 35/37
93.75% Functions 15/16
92.02% Lines 127/138

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398                              68861x     68861x   68861x 68861x 68861x   68861x 68861x 68861x   68861x             68861x     1x                   319005x                                                           607549x                         1376x                         1347x                         315081x                         345208x                 1x 67799x 63x     67736x 67736x 67736x   67736x 67736x 67736x   67736x                         1x           67122x   3x     67119x 67119x 67119x 67119x                   67119x 228x 228x     67119x 67119x 67119x     67119x 67119x 67119x     5x 5x   5x   2x     3x 3x 3x     67110x   331x 331x   331x   15x     316x 316x   316x     66779x 66779x   66779x 66779x 66779x   66779x   21x     66758x 66758x 66758x 66758x   66758x                   1x 48x 48x 48x   48x 48x   48x 1x 1x     47x                   1x           47x 47x 47x               47x   3x     44x   44x                 1x 45x 32x     13x               13x 13x   13x   13x                 1x 292283x 292283x                 1x 1194x 1194x 1194x 1194x 1194x 1194x 1194x   1194x 42x 42x     1152x 1152x 1152x 1152x     1152x   947x     205x   12x     205x 205x 205x 205x 205x 205x 205x     1152x 1152x        
import Check from "../Core/Check.js";
import defined from "../Core/defined.js";
import BillboardLoadState from "./BillboardLoadState.js";
 
/**
 * Tracks a reference to an image and it's loading state, as used in a BillboardCollection and stored in a texture atlas.
 * @constructor
 * @private
 * @see BillboardCollection
 * @see Billboard#image
 * @alias BillboardTexture
 * @param {BillboardCollection} billboardCollection The associated billboard collecion.
 */
function BillboardTexture(billboardCollection) {
  //>>includeStart('debug', pragmas.debug);
  Check.defined("billboardCollection", billboardCollection);
  //>>includeEnd('debug');
 
  this._billboardCollection = billboardCollection;
 
  this._id = undefined;
  this._loadState = BillboardLoadState.NONE;
  this._loadError = undefined;
 
  this._index = -1;
  this._width = undefined;
  this._height = undefined;
 
  this._hasSubregion = false;
 
  /**
   * Used by billboardCollection to track whcih billboards to update.
   * @type {boolean}
   * @private
   */
  this.dirty = false;
}
 
Object.defineProperties(BillboardTexture.prototype, {
  /**
   * If defined, this error was encountered during the loading process.
   * @memberof BillboardTexture.prototype
   * @type {Error|undefined}
   * @readonly
   * @private
   */
  loadError: {
    get: function () {
      return this._loadError;
    },
  },
 
  /**
   * The current status of the image load. When <code>BillboardLoadState.LOADED</code>, this billboard is ready to render, i.e., the image
   * has been downloaded and the WebGL resources are created.
   * @memberof BillboardTexture.prototype
   * @type {BillboardLoadState}
   * @readonly
   * @default BillboardLoadState.NONE
   * @private
   */
  loadState: {
    get: function () {
      return this._loadState;
    },
  },
 
  /**
   * When <code>true</code>, this texture is ready to render, i.e., the image
   * has been downloaded and the WebGL resources are created.
   * @memberof BillboardTexture.prototype
   * @type {boolean}
   * @readonly
   * @default false
   * @private
   */
  ready: {
    get: function () {
      return this._loadState === BillboardLoadState.LOADED;
    },
  },
 
  /**
   * Returns <code>true</code> if there is image data associated with this instance.
   * @memberof BillboardTexture.prototype
   * @type {boolean}
   * @readonly
   * @private
   */
  hasImage: {
    get: function () {
      return this._loadState !== BillboardLoadState.NONE;
    },
  },
 
  /**
   * A unique identifier for the image, or undefined if no image data has been associated with this instance.
   * @memberof BillboardTexture.prototype
   * @type {string|undefined}
   * @readonly
   * @private
   */
  id: {
    get: function () {
      return this._id;
    },
  },
 
  /**
   * The width of the associated image. Before the instance is <code>ready</code>, this will be <code>undefined</code>.
   * @memberof BillboardTexture.prototype
   * @type {number|undefined}
   * @readonly
   * @private
   */
  width: {
    get: function () {
      return this._width;
    },
  },
 
  /**
   * The height of the associated image. Before the instance is <code>ready</code>, this will be <code>undefined</code>.
   * @memberof BillboardTexture.prototype
   * @type {number|undefined}
   * @readonly
   * @private
   */
  height: {
    get: function () {
      return this._height;
    },
  },
});
 
/**
 * Releases reference to any associated image data.
 * @private
 */
BillboardTexture.prototype.unload = async function () {
  if (this._loadState === BillboardLoadState.NONE) {
    return;
  }
 
  this._id = undefined;
  this._loadError = undefined;
  this._loadState = BillboardLoadState.NONE;
 
  this._index = -1;
  this._width = undefined;
  this._height = undefined;
 
  this.dirty = true;
};
 
/**
 * Starts loading an image into the texture atlas.
 * @see {TextureAtlas#addImage}
 * @private
 * @param {string} id An identifier to detect whether the image already exists in the atlas.
 * @param {HTMLImageElement|HTMLCanvasElement|string|Resource|Promise|TextureAtlas.CreateImageCallback} image An image or canvas to add to the texture atlas,
 *        or a URL to an Image, or a Promise for an image, or a function that creates an image.
 * @param {number} width A number specifying the width of the texture. If undefined, the image width will be used.
 * @param {number} height A number specifying the height of the texture. If undefined, the image height will be used.
 */
BillboardTexture.prototype.loadImage = async function (
  id,
  image,
  width,
  height,
) {
  if (this._id === id) {
    // This image has already been loaded
    return;
  }
 
  const collection = this._billboardCollection;
  const cache = collection.billboardTextureCache;
  let billboardTexture = cache.get(id);
  Iif (
    (defined(billboardTexture) &&
      image.loadState === BillboardLoadState.LOADING) ||
    image.loadState === BillboardLoadState.LOADED
  ) {
    // Use the cached texture if it is in progress or successful.
    BillboardTexture.clone(billboardTexture, this);
    return;
  }
  // Otherwise, load if not yet assigned an image, and try the load again if anything failed during the last billboard creation
  if (!defined(billboardTexture)) {
    billboardTexture = new BillboardTexture(collection);
    cache.set(id, billboardTexture);
  }
 
  billboardTexture._id = this._id = id;
  billboardTexture._loadState = this._loadState = BillboardLoadState.LOADING;
  billboardTexture._loadError = this._loadError = undefined;
 
  let index;
  const atlas = this._billboardCollection.textureAtlas;
  try {
    index = await atlas.addImage(id, image, width, height);
  } catch (error) {
    // There was an error loading the image
    billboardTexture._loadState = BillboardLoadState.ERROR;
    billboardTexture._loadError = error;
 
    if (this._id !== id) {
      // Another load was initiated and resolved resolved before this one. This operation is cancelled.
      return;
    }
 
    this._loadState = BillboardLoadState.ERROR;
    this._loadError = error;
    return;
  }
 
  if (!defined(index) || index === -1) {
    // Resources destroyed or otherwise
    billboardTexture._loadState = BillboardLoadState.FAILED;
    billboardTexture._index = -1;
 
    if (this._id !== id) {
      // Another load was initiated and resolved resolved before this one. This operation is cancelled.
      return;
    }
 
    this._loadState = BillboardLoadState.FAILED;
    this._index = -1;
 
    return;
  }
 
  billboardTexture._index = index;
  billboardTexture._loadState = BillboardLoadState.LOADED;
 
  const rectangle = atlas.rectangles[index];
  billboardTexture._width = rectangle.width;
  billboardTexture._height = rectangle.height;
 
  if (this._id !== id) {
    // Another load was initiated and resolved resolved before this one. This operation is cancelled.
    return;
  }
 
  this._index = index;
  this._loadState = BillboardLoadState.LOADED;
  this._width = rectangle.width;
  this._height = rectangle.height;
 
  this.dirty = true;
};
 
/**
 * Track a reference to a sub-region of an existing image.
 * @see {TextureAtlas#addImageSubRegion}
 * @private
 * @param {string} id An identifier to detect whether the image already exists in the atlas.
 * @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
 */
BillboardTexture.prototype.addImageSubRegion = function (id, subRegion) {
  this._id = id;
  this._loadError = undefined;
  this._hasSubregion = true;
 
  const atlas = this._billboardCollection.textureAtlas;
  const indexOrPromise = atlas.addImageSubRegion(id, subRegion);
 
  if (typeof indexOrPromise === "number") {
    this.setImageSubRegion(indexOrPromise, subRegion);
    return;
  }
 
  this.loadImageSubRegion(id, subRegion, indexOrPromise);
};
 
/**
 * @see {TextureAtlas#addImageSubRegion}
 * @private
 * @param {string} id An identifier to detect whether the image already exists in the atlas.
 * @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
 * @param {Promise<number>} indexPromise A promise that resolves to the image region index.
 */
BillboardTexture.prototype.loadImageSubRegion = async function (
  id,
  subRegion,
  indexPromise,
) {
  let index;
  try {
    this._loadState = BillboardLoadState.LOADING;
    index = await indexPromise;
  } catch (error) {
    // There was an error loading the referenced image
    this._loadState = BillboardLoadState.ERROR;
    this._loadError = error;
    return;
  }
 
  if (this._id !== id) {
    // Another load was initiated and resolved resolved before this one. This operation is cancelled.
    return;
  }
 
  this._loadState = BillboardLoadState.LOADED;
 
  this.setImageSubRegion(index, subRegion);
};
 
/**
 * @see {TextureAtlas#addImageSubRegion}
 * @private
 * @param {number} index The resolved index in the {@link TextureAtlas}
 * @param {BoundingRectangle} subRegion An {@link BoundingRectangle} defining a region of an existing image, measured in pixels from the bottom-left of the image.
 */
BillboardTexture.prototype.setImageSubRegion = function (index, subRegion) {
  if (this._index === index) {
    return;
  }
 
  Iif (!defined(index) || index === -1) {
    this._loadState = BillboardLoadState.FAILED;
    this._index = -1;
    this._width = undefined;
    this._height = undefined;
    return;
  }
 
  this._width = subRegion.width;
  this._height = subRegion.height;
 
  this._index = index;
 
  this.dirty = true;
};
 
/**
 * Get the texture coordinates for reading the loaded texture in shaders.
 * @private
 * @param {BoundingRectangle} [result] The modified result parameter or a new BoundingRectangle instance if one was not provided.
 * @return {BoundingRectangle} The modified result parameter or a new BoundingRectangle instance if one was not provided.
 */
BillboardTexture.prototype.computeTextureCoordinates = function (result) {
  const atlas = this._billboardCollection.textureAtlas;
  return atlas.computeTextureCoordinates(this._index, result);
};
 
/**
 * Clones an existing billboard texture, inlcuding any in-flight tracking, into the target billboard texture.
 * @param {BillboardTexture} billboardTexture
 * @param {BillboardTexture} target
 * @returns {BillboardTexture} target
 */
BillboardTexture.clone = function (billboardTexture, target) {
  target._id = billboardTexture._id;
  target._loadState = billboardTexture._loadState;
  target._loadError = undefined;
  target._index = billboardTexture._index;
  target._width = billboardTexture._width;
  target._height = billboardTexture._height;
  target._hasSubregion = billboardTexture._hasSubregion;
 
  if (billboardTexture.ready) {
    target.dirty = true;
    return;
  }
 
  const completeLoad = async () => {
    const id = billboardTexture._id;
    const atlas = billboardTexture._billboardCollection.textureAtlas;
    await atlas._indexPromiseById.get(id);
 
    // Any errors should have already been handled
    if (target._id !== id) {
      // Another load was initiated and resolved resolved before this one. This operation is cancelled.
      return;
    }
 
    if (billboardTexture._hasSubregion) {
      // Subregions must wait an additional frame to be ready
      await Promise.resolve();
    }
 
    target._id = id;
    target._loadState = billboardTexture._loadState;
    target._loadError = billboardTexture._loadError;
    target._index = billboardTexture._index;
    target._width = billboardTexture._width;
    target._height = billboardTexture._height;
    target.dirty = true;
  };
 
  completeLoad();
  return target;
};
 
export default BillboardTexture;