loadCRN.js 3.6 KB

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