loadKTX2.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Check from "./Check.js";
  2. import Resource from "./Resource.js";
  3. import KTX2Transcoder from "./KTX2Transcoder.js";
  4. /**
  5. * Stores the supported formats that KTX2 can transcode to. Called during context creation.
  6. *
  7. * @param {boolean} s3tc Whether or not S3TC is supported
  8. * @param {boolean} pvrtc Whether or not PVRTC is supported
  9. * @param {boolean} astc Whether or not ASTC is supported
  10. * @param {boolean} etc Whether or not ETC is supported
  11. * @param {boolean} etc1 Whether or not ETC1 is supported
  12. * @param {boolean} bc7 Whether or not BC7 is supported
  13. * @private
  14. */
  15. let supportedTranscoderFormats;
  16. loadKTX2.setKTX2SupportedFormats = function (
  17. s3tc,
  18. pvrtc,
  19. astc,
  20. etc,
  21. etc1,
  22. bc7
  23. ) {
  24. supportedTranscoderFormats = {
  25. s3tc: s3tc,
  26. pvrtc: pvrtc,
  27. astc: astc,
  28. etc: etc,
  29. etc1: etc1,
  30. bc7: bc7,
  31. };
  32. };
  33. /**
  34. * Asynchronously loads and parses the given URL to a KTX2 file or parses the raw binary data of a KTX2 file.
  35. * Returns a promise that will resolve to an object containing the image buffer, width, height, and format once loaded,
  36. * or reject if the URL failed to load or failed to parse the data. The data is loaded
  37. * using XMLHttpRequest, which means that in order to make requests to another origin,
  38. * the server must have Cross-Origin Resource sharing (CORS) headers enabled.
  39. * <p>
  40. * The following are part of the KTX2 format specification but are not supported:
  41. * <ul>
  42. * <li>Metadata</li>
  43. * <li>3D textures</li>
  44. * <li>Texture Arrays</li>
  45. * <li>Video</li>
  46. * </ul>
  47. * </p>
  48. *
  49. * @function loadKTX2
  50. *
  51. * @param {Resource|string|ArrayBuffer} resourceOrUrlOrBuffer The URL of the binary data or an ArrayBuffer.
  52. * @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.
  53. *
  54. * @exception {RuntimeError} Invalid KTX2 file.
  55. * @exception {RuntimeError} KTX2 texture arrays are not supported.
  56. * @exception {RuntimeError} KTX2 3D textures are unsupported.
  57. * @exception {RuntimeError} No transcoding format target available for ETC1S compressed ktx2s.
  58. * @exception {RuntimeError} No transcoding format target available for UASTC compressed ktx2s.
  59. * @exception {RuntimeError} startTranscoding() failed.
  60. * @exception {RuntimeError} transcodeImage() failed.
  61. *
  62. * @example
  63. * // load a single URL asynchronously
  64. * Cesium.loadKTX2('some/url').then(function (ktx2Data) {
  65. * const width = ktx2Data.width;
  66. * const height = ktx2Data.height;
  67. * const format = ktx2Data.internalFormat;
  68. * const arrayBufferView = ktx2Data.bufferView;
  69. * // use the data to create a texture
  70. * }).catch(function (error) {
  71. * // an error occurred.
  72. * });
  73. *
  74. * @see {@link https://github.com/KhronosGroup/KTX-Specification|KTX file format}
  75. * @see {@link http://www.w3.org/TR/cors/|Cross-Origin Resource Sharing}
  76. * @see {@link http://wiki.commonjs.org/wiki/Promises/A|CommonJS Promises/A}
  77. * @private
  78. */
  79. function loadKTX2(resourceOrUrlOrBuffer) {
  80. //>>includeStart('debug', pragmas.debug);
  81. Check.defined("resourceOrUrlOrBuffer", resourceOrUrlOrBuffer);
  82. //>>includeEnd('debug');
  83. let loadPromise;
  84. if (
  85. resourceOrUrlOrBuffer instanceof ArrayBuffer ||
  86. ArrayBuffer.isView(resourceOrUrlOrBuffer)
  87. ) {
  88. loadPromise = Promise.resolve(resourceOrUrlOrBuffer);
  89. } else {
  90. const resource = Resource.createIfNeeded(resourceOrUrlOrBuffer);
  91. loadPromise = resource.fetchArrayBuffer();
  92. }
  93. // load module then return
  94. return loadPromise.then(function (data) {
  95. return KTX2Transcoder.transcode(data, supportedTranscoderFormats);
  96. });
  97. }
  98. export default loadKTX2;