loadCRN.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import when from "../ThirdParty/when.js";
  2. import CompressedTextureBuffer from "./CompressedTextureBuffer.js";
  3. import defined from "./defined.js";
  4. import DeveloperError from "./DeveloperError.js";
  5. import Resource from "./Resource.js";
  6. import TaskProcessor from "./TaskProcessor.js";
  7. var transcodeTaskProcessor = new TaskProcessor(
  8. "transcodeCRNToDXT",
  9. Number.POSITIVE_INFINITY
  10. );
  11. /**
  12. * Asynchronously loads and parses the given URL to a CRN file or parses the raw binary data of a CRN file.
  13. * Returns a promise that will resolve to an object containing the image buffer, width, height and format once loaded,
  14. * or reject if the URL failed to load or failed to parse the data. The data is loaded
  15. * using XMLHttpRequest, which means that in order to make requests to another origin,
  16. * the server must have Cross-Origin Resource Sharing (CORS) headers enabled.
  17. *
  18. * @function loadCRN
  19. *
  20. * @param {Resource|String|ArrayBuffer} resourceOrUrlOrBuffer The URL of the binary data or an ArrayBuffer.
  21. * @returns {Promise.<CompressedTextureBuffer>|undefined} A promise that will resolve to the requested data when loaded. Returns undefined if <code>request.throttle</code> is true and the request does not have high enough priority.
  22. *
  23. * @exception {RuntimeError} Unsupported compressed format.
  24. *
  25. * @example
  26. * // load a single URL asynchronously
  27. * Cesium.loadCRN('some/url').then(function(textureData) {
  28. * var width = textureData.width;
  29. * var height = textureData.height;
  30. * var format = textureData.internalFormat;
  31. * var arrayBufferView = textureData.bufferView;
  32. * // use the data to create a texture
  33. * }).otherwise(function(error) {
  34. * // an error occurred
  35. * });
  36. *
  37. * @see {@link https://github.com/BinomialLLC/crunch|crunch DXTc texture compression and transcoding library}
  38. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  39. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  40. */
  41. function loadCRN(resourceOrUrlOrBuffer) {
  42. //>>includeStart('debug', pragmas.debug);
  43. if (!defined(resourceOrUrlOrBuffer)) {
  44. throw new DeveloperError("resourceOrUrlOrBuffer is required.");
  45. }
  46. //>>includeEnd('debug');
  47. var loadPromise;
  48. if (
  49. resourceOrUrlOrBuffer instanceof ArrayBuffer ||
  50. ArrayBuffer.isView(resourceOrUrlOrBuffer)
  51. ) {
  52. loadPromise = when.resolve(resourceOrUrlOrBuffer);
  53. } else {
  54. var resource = Resource.createIfNeeded(resourceOrUrlOrBuffer);
  55. loadPromise = resource.fetchArrayBuffer();
  56. }
  57. if (!defined(loadPromise)) {
  58. return undefined;
  59. }
  60. return loadPromise
  61. .then(function (data) {
  62. if (!defined(data)) {
  63. return;
  64. }
  65. var transferrableObjects = [];
  66. if (data instanceof ArrayBuffer) {
  67. transferrableObjects.push(data);
  68. } else if (
  69. data.byteOffset === 0 &&
  70. data.byteLength === data.buffer.byteLength
  71. ) {
  72. transferrableObjects.push(data.buffer);
  73. } else {
  74. // data is a view of an array buffer. need to copy so it is transferrable to web worker
  75. data = data.slice(0, data.length);
  76. transferrableObjects.push(data.buffer);
  77. }
  78. return transcodeTaskProcessor.scheduleTask(data, transferrableObjects);
  79. })
  80. .then(function (compressedTextureBuffer) {
  81. return CompressedTextureBuffer.clone(compressedTextureBuffer);
  82. });
  83. }
  84. export default loadCRN;