智慧水务管理系统 - 精河县供水工程综合管理平台

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