GltfDracoLoader.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defined from "../Core/defined.js";
  4. import DracoLoader from "./DracoLoader.js";
  5. import ResourceLoader from "./ResourceLoader.js";
  6. import ResourceLoaderState from "./ResourceLoaderState.js";
  7. /**
  8. * Load a draco buffer from a glTF.
  9. * <p>
  10. * Implements the {@link ResourceLoader} interface.
  11. * </p>
  12. *
  13. * @alias GltfDracoLoader
  14. * @constructor
  15. * @augments ResourceLoader
  16. *
  17. * @param {object} options Object with the following properties:
  18. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  19. * @param {object} options.gltf The glTF JSON.
  20. * @param {object} options.draco The Draco extension object.
  21. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  22. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  23. * @param {string} [options.cacheKey] The cache key of the resource.
  24. *
  25. * @private
  26. */
  27. function GltfDracoLoader(options) {
  28. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  29. const resourceCache = options.resourceCache;
  30. const gltf = options.gltf;
  31. const draco = options.draco;
  32. const gltfResource = options.gltfResource;
  33. const baseResource = options.baseResource;
  34. const cacheKey = options.cacheKey;
  35. //>>includeStart('debug', pragmas.debug);
  36. Check.typeOf.func("options.resourceCache", resourceCache);
  37. Check.typeOf.object("options.gltf", gltf);
  38. Check.typeOf.object("options.draco", draco);
  39. Check.typeOf.object("options.gltfResource", gltfResource);
  40. Check.typeOf.object("options.baseResource", baseResource);
  41. //>>includeEnd('debug');
  42. this._resourceCache = resourceCache;
  43. this._gltfResource = gltfResource;
  44. this._baseResource = baseResource;
  45. this._gltf = gltf;
  46. this._draco = draco;
  47. this._cacheKey = cacheKey;
  48. this._bufferViewLoader = undefined;
  49. this._bufferViewTypedArray = undefined;
  50. this._decodePromise = undefined;
  51. this._decodedData = undefined;
  52. this._state = ResourceLoaderState.UNLOADED;
  53. this._promise = undefined;
  54. this._dracoError = undefined;
  55. }
  56. if (defined(Object.create)) {
  57. GltfDracoLoader.prototype = Object.create(ResourceLoader.prototype);
  58. GltfDracoLoader.prototype.constructor = GltfDracoLoader;
  59. }
  60. Object.defineProperties(GltfDracoLoader.prototype, {
  61. /**
  62. * The cache key of the resource.
  63. *
  64. * @memberof GltfDracoLoader.prototype
  65. *
  66. * @type {string}
  67. * @readonly
  68. * @private
  69. */
  70. cacheKey: {
  71. get: function () {
  72. return this._cacheKey;
  73. },
  74. },
  75. /**
  76. * The decoded data.
  77. *
  78. * @memberof GltfDracoLoader.prototype
  79. *
  80. * @type {object}
  81. * @readonly
  82. * @private
  83. */
  84. decodedData: {
  85. get: function () {
  86. return this._decodedData;
  87. },
  88. },
  89. });
  90. async function loadResources(loader) {
  91. const resourceCache = loader._resourceCache;
  92. try {
  93. const bufferViewLoader = resourceCache.getBufferViewLoader({
  94. gltf: loader._gltf,
  95. bufferViewId: loader._draco.bufferView,
  96. gltfResource: loader._gltfResource,
  97. baseResource: loader._baseResource,
  98. });
  99. loader._bufferViewLoader = bufferViewLoader;
  100. await bufferViewLoader.load();
  101. if (loader.isDestroyed()) {
  102. return;
  103. }
  104. loader._bufferViewTypedArray = bufferViewLoader.typedArray;
  105. loader._state = ResourceLoaderState.PROCESSING;
  106. return loader;
  107. } catch (error) {
  108. if (loader.isDestroyed()) {
  109. return;
  110. }
  111. handleError(loader, error);
  112. }
  113. }
  114. /**
  115. * Loads the resource.
  116. * @returns {Promise<GltfDracoLoader>} A promise which resolves to the loader when the resource loading is completed.
  117. * @private
  118. */
  119. GltfDracoLoader.prototype.load = async function () {
  120. if (defined(this._promise)) {
  121. return this._promise;
  122. }
  123. this._state = ResourceLoaderState.LOADING;
  124. this._promise = loadResources(this);
  125. return this._promise;
  126. };
  127. function handleError(dracoLoader, error) {
  128. dracoLoader.unload();
  129. dracoLoader._state = ResourceLoaderState.FAILED;
  130. const errorMessage = "Failed to load Draco";
  131. throw dracoLoader.getError(errorMessage, error);
  132. }
  133. async function processDecode(loader, decodePromise) {
  134. try {
  135. const results = await decodePromise;
  136. if (loader.isDestroyed()) {
  137. return;
  138. }
  139. // Unload everything except the decoded data
  140. loader.unload();
  141. loader._decodedData = {
  142. indices: results.indexArray,
  143. vertexAttributes: results.attributeData,
  144. };
  145. loader._state = ResourceLoaderState.READY;
  146. return loader._baseResource;
  147. } catch (error) {
  148. if (loader.isDestroyed()) {
  149. return;
  150. }
  151. // Capture this error so it can be thrown on the next `process` call
  152. loader._dracoError = error;
  153. }
  154. }
  155. /**
  156. * Processes the resource until it becomes ready.
  157. *
  158. * @param {FrameState} frameState The frame state.
  159. * @private
  160. */
  161. GltfDracoLoader.prototype.process = function (frameState) {
  162. //>>includeStart('debug', pragmas.debug);
  163. Check.typeOf.object("frameState", frameState);
  164. //>>includeEnd('debug');
  165. if (this._state === ResourceLoaderState.READY) {
  166. return true;
  167. }
  168. if (this._state !== ResourceLoaderState.PROCESSING) {
  169. return false;
  170. }
  171. if (defined(this._dracoError)) {
  172. handleError(this, this._dracoError);
  173. }
  174. if (!defined(this._bufferViewTypedArray)) {
  175. // Not ready to decode the Draco buffer
  176. return false;
  177. }
  178. if (defined(this._decodePromise)) {
  179. // Currently decoding
  180. return false;
  181. }
  182. const draco = this._draco;
  183. const gltf = this._gltf;
  184. const bufferViews = gltf.bufferViews;
  185. const bufferViewId = draco.bufferView;
  186. const bufferView = bufferViews[bufferViewId];
  187. const compressedAttributes = draco.attributes;
  188. const decodeOptions = {
  189. // Need to make a copy of the typed array otherwise the underlying
  190. // ArrayBuffer may be accessed on both the worker and the main thread. This
  191. // leads to errors such as "ArrayBuffer at index 0 is already detached".
  192. // PERFORMANCE_IDEA: Look into SharedArrayBuffer to get around this.
  193. array: new Uint8Array(this._bufferViewTypedArray),
  194. bufferView: bufferView,
  195. compressedAttributes: compressedAttributes,
  196. dequantizeInShader: true,
  197. };
  198. const decodePromise = DracoLoader.decodeBufferView(decodeOptions);
  199. if (!defined(decodePromise)) {
  200. // Cannot schedule task this frame
  201. return false;
  202. }
  203. this._decodePromise = processDecode(this, decodePromise);
  204. };
  205. /**
  206. * Unloads the resource.
  207. * @private
  208. */
  209. GltfDracoLoader.prototype.unload = function () {
  210. if (defined(this._bufferViewLoader)) {
  211. this._resourceCache.unload(this._bufferViewLoader);
  212. }
  213. this._bufferViewLoader = undefined;
  214. this._bufferViewTypedArray = undefined;
  215. this._decodedData = undefined;
  216. this._gltf = undefined;
  217. };
  218. export default GltfDracoLoader;