GltfBufferViewLoader.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import Check from "../Core/Check.js";
  2. import defaultValue from "../Core/defaultValue.js";
  3. import defer from "../Core/defer.js";
  4. import defined from "../Core/defined.js";
  5. import hasExtension from "./hasExtension.js";
  6. import MeshoptDecoder from "../ThirdParty/meshoptimizer.js";
  7. import ResourceLoader from "./ResourceLoader.js";
  8. import ResourceLoaderState from "./ResourceLoaderState.js";
  9. /**
  10. * Loads a glTF buffer view.
  11. * <p>
  12. * Implements the {@link ResourceLoader} interface.
  13. * </p>
  14. *
  15. * @alias GltfBufferViewLoader
  16. * @constructor
  17. * @augments ResourceLoader
  18. *
  19. * @param {Object} options Object with the following properties:
  20. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  21. * @param {Object} options.gltf The glTF JSON.
  22. * @param {Number} options.bufferViewId The buffer view ID.
  23. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  24. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  25. * @param {String} [options.cacheKey] The cache key of the resource.
  26. *
  27. * @private
  28. */
  29. export default function GltfBufferViewLoader(options) {
  30. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  31. const resourceCache = options.resourceCache;
  32. const gltf = options.gltf;
  33. const bufferViewId = options.bufferViewId;
  34. const gltfResource = options.gltfResource;
  35. const baseResource = options.baseResource;
  36. const cacheKey = options.cacheKey;
  37. //>>includeStart('debug', pragmas.debug);
  38. Check.typeOf.func("options.resourceCache", resourceCache);
  39. Check.typeOf.object("options.gltf", gltf);
  40. Check.typeOf.number("options.bufferViewId", bufferViewId);
  41. Check.typeOf.object("options.gltfResource", gltfResource);
  42. Check.typeOf.object("options.baseResource", baseResource);
  43. //>>includeEnd('debug');
  44. const bufferView = gltf.bufferViews[bufferViewId];
  45. let bufferId = bufferView.buffer;
  46. let byteOffset = bufferView.byteOffset;
  47. let byteLength = bufferView.byteLength;
  48. let hasMeshopt = false;
  49. let meshoptByteStride;
  50. let meshoptCount;
  51. let meshoptMode;
  52. let meshoptFilter;
  53. if (hasExtension(bufferView, "EXT_meshopt_compression")) {
  54. const meshopt = bufferView.extensions.EXT_meshopt_compression;
  55. bufferId = meshopt.buffer;
  56. byteOffset = defaultValue(meshopt.byteOffset, 0);
  57. byteLength = meshopt.byteLength;
  58. hasMeshopt = true;
  59. meshoptByteStride = meshopt.byteStride;
  60. meshoptCount = meshopt.count;
  61. meshoptMode = meshopt.mode;
  62. meshoptFilter = defaultValue(meshopt.filter, "NONE");
  63. }
  64. const buffer = gltf.buffers[bufferId];
  65. this._hasMeshopt = hasMeshopt;
  66. this._meshoptByteStride = meshoptByteStride;
  67. this._meshoptCount = meshoptCount;
  68. this._meshoptMode = meshoptMode;
  69. this._meshoptFilter = meshoptFilter;
  70. this._resourceCache = resourceCache;
  71. this._gltfResource = gltfResource;
  72. this._baseResource = baseResource;
  73. this._buffer = buffer;
  74. this._bufferId = bufferId;
  75. this._byteOffset = byteOffset;
  76. this._byteLength = byteLength;
  77. this._cacheKey = cacheKey;
  78. this._bufferLoader = undefined;
  79. this._typedArray = undefined;
  80. this._state = ResourceLoaderState.UNLOADED;
  81. this._promise = defer();
  82. }
  83. if (defined(Object.create)) {
  84. GltfBufferViewLoader.prototype = Object.create(ResourceLoader.prototype);
  85. GltfBufferViewLoader.prototype.constructor = GltfBufferViewLoader;
  86. }
  87. Object.defineProperties(GltfBufferViewLoader.prototype, {
  88. /**
  89. * A promise that resolves to the resource when the resource is ready.
  90. *
  91. * @memberof GltfBufferViewLoader.prototype
  92. *
  93. * @type {Promise.<GltfBufferViewLoader>}
  94. * @readonly
  95. * @private
  96. */
  97. promise: {
  98. get: function () {
  99. return this._promise.promise;
  100. },
  101. },
  102. /**
  103. * The cache key of the resource.
  104. *
  105. * @memberof GltfBufferViewLoader.prototype
  106. *
  107. * @type {String}
  108. * @readonly
  109. * @private
  110. */
  111. cacheKey: {
  112. get: function () {
  113. return this._cacheKey;
  114. },
  115. },
  116. /**
  117. * The typed array containing buffer view data.
  118. *
  119. * @memberof GltfBufferViewLoader.prototype
  120. *
  121. * @type {Uint8Array}
  122. * @readonly
  123. * @private
  124. */
  125. typedArray: {
  126. get: function () {
  127. return this._typedArray;
  128. },
  129. },
  130. });
  131. /**
  132. * Loads the resource.
  133. * @private
  134. */
  135. GltfBufferViewLoader.prototype.load = function () {
  136. const bufferLoader = getBufferLoader(this);
  137. this._bufferLoader = bufferLoader;
  138. this._state = ResourceLoaderState.LOADING;
  139. const that = this;
  140. bufferLoader.promise
  141. .then(function () {
  142. if (that.isDestroyed()) {
  143. return;
  144. }
  145. const bufferTypedArray = bufferLoader.typedArray;
  146. const bufferViewTypedArray = new Uint8Array(
  147. bufferTypedArray.buffer,
  148. bufferTypedArray.byteOffset + that._byteOffset,
  149. that._byteLength
  150. );
  151. // Unload the buffer
  152. that.unload();
  153. that._typedArray = bufferViewTypedArray;
  154. if (that._hasMeshopt) {
  155. that._state = ResourceLoaderState.PROCESSING;
  156. } else {
  157. that._state = ResourceLoaderState.READY;
  158. that._promise.resolve(that);
  159. }
  160. })
  161. .catch(function (error) {
  162. if (that.isDestroyed()) {
  163. return;
  164. }
  165. that.unload();
  166. that._state = ResourceLoaderState.FAILED;
  167. const errorMessage = "Failed to load buffer view";
  168. that._promise.reject(that.getError(errorMessage, error));
  169. });
  170. };
  171. function getBufferLoader(bufferViewLoader) {
  172. const resourceCache = bufferViewLoader._resourceCache;
  173. const buffer = bufferViewLoader._buffer;
  174. if (defined(buffer.uri)) {
  175. const baseResource = bufferViewLoader._baseResource;
  176. const resource = baseResource.getDerivedResource({
  177. url: buffer.uri,
  178. });
  179. return resourceCache.loadExternalBuffer({
  180. resource: resource,
  181. });
  182. }
  183. return resourceCache.loadEmbeddedBuffer({
  184. parentResource: bufferViewLoader._gltfResource,
  185. bufferId: bufferViewLoader._bufferId,
  186. });
  187. }
  188. /**
  189. * Processes the resources. For a BufferView that does not have the EXT_meshopt_compression extension, there
  190. * is no processing that needs to happen, so this function returns immediately.
  191. *
  192. * @param {FrameState} frameState The frame state.
  193. */
  194. GltfBufferViewLoader.prototype.process = function (frameState) {
  195. //>>includeStart('debug', pragmas.debug);
  196. Check.typeOf.object("frameState", frameState);
  197. //>>includeEnd('debug');
  198. if (!this._hasMeshopt) {
  199. return;
  200. }
  201. if (!defined(this._typedArray)) {
  202. return;
  203. }
  204. if (this._state !== ResourceLoaderState.PROCESSING) {
  205. return;
  206. }
  207. const count = this._meshoptCount;
  208. const byteStride = this._meshoptByteStride;
  209. const result = new Uint8Array(count * byteStride);
  210. MeshoptDecoder.decodeGltfBuffer(
  211. result,
  212. count,
  213. byteStride,
  214. this._typedArray,
  215. this._meshoptMode,
  216. this._meshoptFilter
  217. );
  218. this._typedArray = result;
  219. this._state = ResourceLoaderState.READY;
  220. this._promise.resolve(this);
  221. };
  222. /**
  223. * Unloads the resource.
  224. * @private
  225. */
  226. GltfBufferViewLoader.prototype.unload = function () {
  227. if (defined(this._bufferLoader)) {
  228. this._resourceCache.unload(this._bufferLoader);
  229. }
  230. this._bufferLoader = undefined;
  231. this._typedArray = undefined;
  232. };