GltfVertexBufferLoader.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. import arrayFill from "../Core/arrayFill.js";
  2. import Check from "../Core/Check.js";
  3. import defaultValue from "../Core/defaultValue.js";
  4. import defer from "../Core/defer.js";
  5. import defined from "../Core/defined.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import Buffer from "../Renderer/Buffer.js";
  8. import BufferUsage from "../Renderer/BufferUsage.js";
  9. import AttributeType from "./AttributeType.js";
  10. import JobType from "./JobType.js";
  11. import ModelComponents from "./ModelComponents.js";
  12. import ResourceLoader from "./ResourceLoader.js";
  13. import ResourceLoaderState from "./ResourceLoaderState.js";
  14. import AttributeCompression from "../Core/AttributeCompression.js";
  15. import ComponentDatatype from "../Core/ComponentDatatype.js";
  16. /**
  17. * Loads a vertex buffer from a glTF buffer view.
  18. * <p>
  19. * Implements the {@link ResourceLoader} interface.
  20. * </p>
  21. *
  22. * @alias GltfVertexBufferLoader
  23. * @constructor
  24. * @augments ResourceLoader
  25. *
  26. * @param {Object} options Object with the following properties:
  27. * @param {ResourceCache} options.resourceCache The {@link ResourceCache} (to avoid circular dependencies).
  28. * @param {Object} options.gltf The glTF JSON.
  29. * @param {Resource} options.gltfResource The {@link Resource} containing the glTF.
  30. * @param {Resource} options.baseResource The {@link Resource} that paths in the glTF JSON are relative to.
  31. * @param {Number} [options.bufferViewId] The bufferView ID corresponding to the vertex buffer.
  32. * @param {Object} [options.draco] The Draco extension object.
  33. * @param {String} [options.attributeSemantic] The attribute semantic, e.g. POSITION or NORMAL.
  34. * @param {Number} [options.accessorId] The accessor id.
  35. * @param {String} [options.cacheKey] The cache key of the resource.
  36. * @param {Boolean} [options.asynchronous=true] Determines if WebGL resource creation will be spread out over several frames or block until all WebGL resources are created.
  37. * @param {Boolean} [options.dequantize=false] Determines whether or not the vertex buffer will be dequantized on the CPU.
  38. * @param {Boolean} [options.loadAsTypedArray=false] Load vertex buffer as a typed array instead of a GPU vertex buffer.
  39. *
  40. * @exception {DeveloperError} One of options.bufferViewId and options.draco must be defined.
  41. * @exception {DeveloperError} When options.draco is defined options.attributeSemantic must also be defined.
  42. * @exception {DeveloperError} When options.draco is defined options.accessorId must also be defined.
  43. *
  44. * @private
  45. */
  46. export default function GltfVertexBufferLoader(options) {
  47. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  48. const resourceCache = options.resourceCache;
  49. const gltf = options.gltf;
  50. const gltfResource = options.gltfResource;
  51. const baseResource = options.baseResource;
  52. const bufferViewId = options.bufferViewId;
  53. const draco = options.draco;
  54. const attributeSemantic = options.attributeSemantic;
  55. const accessorId = options.accessorId;
  56. const cacheKey = options.cacheKey;
  57. const asynchronous = defaultValue(options.asynchronous, true);
  58. const dequantize = defaultValue(options.dequantize, false);
  59. const loadAsTypedArray = defaultValue(options.loadAsTypedArray, false);
  60. //>>includeStart('debug', pragmas.debug);
  61. Check.typeOf.func("options.resourceCache", resourceCache);
  62. Check.typeOf.object("options.gltf", gltf);
  63. Check.typeOf.object("options.gltfResource", gltfResource);
  64. Check.typeOf.object("options.baseResource", baseResource);
  65. const hasBufferViewId = defined(bufferViewId);
  66. const hasDraco = defined(draco);
  67. const hasAttributeSemantic = defined(attributeSemantic);
  68. const hasAccessorId = defined(accessorId);
  69. if (hasBufferViewId === hasDraco) {
  70. throw new DeveloperError(
  71. "One of options.bufferViewId and options.draco must be defined."
  72. );
  73. }
  74. if (hasDraco && !hasAttributeSemantic) {
  75. throw new DeveloperError(
  76. "When options.draco is defined options.attributeSemantic must also be defined."
  77. );
  78. }
  79. if (hasDraco && !hasAccessorId) {
  80. throw new DeveloperError(
  81. "When options.draco is defined options.accessorId must also be defined."
  82. );
  83. }
  84. if (hasDraco) {
  85. Check.typeOf.object("options.draco", draco);
  86. Check.typeOf.string("options.attributeSemantic", attributeSemantic);
  87. Check.typeOf.number("options.accessorId", accessorId);
  88. }
  89. //>>includeEnd('debug');
  90. this._resourceCache = resourceCache;
  91. this._gltfResource = gltfResource;
  92. this._baseResource = baseResource;
  93. this._gltf = gltf;
  94. this._bufferViewId = bufferViewId;
  95. this._draco = draco;
  96. this._attributeSemantic = attributeSemantic;
  97. this._accessorId = accessorId;
  98. this._cacheKey = cacheKey;
  99. this._asynchronous = asynchronous;
  100. this._dequantize = dequantize;
  101. this._loadAsTypedArray = loadAsTypedArray;
  102. this._bufferViewLoader = undefined;
  103. this._dracoLoader = undefined;
  104. this._quantization = undefined;
  105. this._typedArray = undefined;
  106. this._buffer = undefined;
  107. this._state = ResourceLoaderState.UNLOADED;
  108. this._promise = defer();
  109. }
  110. if (defined(Object.create)) {
  111. GltfVertexBufferLoader.prototype = Object.create(ResourceLoader.prototype);
  112. GltfVertexBufferLoader.prototype.constructor = GltfVertexBufferLoader;
  113. }
  114. Object.defineProperties(GltfVertexBufferLoader.prototype, {
  115. /**
  116. * A promise that resolves to the resource when the resource is ready.
  117. *
  118. * @memberof GltfVertexBufferLoader.prototype
  119. *
  120. * @type {Promise.<GltfVertexBufferLoader>}
  121. * @readonly
  122. * @private
  123. */
  124. promise: {
  125. get: function () {
  126. return this._promise.promise;
  127. },
  128. },
  129. /**
  130. * The cache key of the resource.
  131. *
  132. * @memberof GltfVertexBufferLoader.prototype
  133. *
  134. * @type {String}
  135. * @readonly
  136. * @private
  137. */
  138. cacheKey: {
  139. get: function () {
  140. return this._cacheKey;
  141. },
  142. },
  143. /**
  144. * The vertex buffer. This is only defined when <code>loadAsTypedArray</code> is false.
  145. *
  146. * @memberof GltfVertexBufferLoader.prototype
  147. *
  148. * @type {Buffer}
  149. * @readonly
  150. * @private
  151. */
  152. buffer: {
  153. get: function () {
  154. return this._buffer;
  155. },
  156. },
  157. /**
  158. * The typed array containing vertex buffer data. This is only defined when <code>loadAsTypedArray</code> is true.
  159. *
  160. * @memberof GltfVertexBufferLoader.prototype
  161. *
  162. * @type {Uint8Array}
  163. * @readonly
  164. * @private
  165. */
  166. typedArray: {
  167. get: function () {
  168. return this._typedArray;
  169. },
  170. },
  171. /**
  172. * Information about the quantized vertex attribute after Draco decode.
  173. *
  174. * @memberof GltfVertexBufferLoader.prototype
  175. *
  176. * @type {ModelComponents.Quantization}
  177. * @readonly
  178. * @private
  179. */
  180. quantization: {
  181. get: function () {
  182. return this._quantization;
  183. },
  184. },
  185. });
  186. /**
  187. * Loads the resource.
  188. * @private
  189. */
  190. GltfVertexBufferLoader.prototype.load = function () {
  191. if (defined(this._draco)) {
  192. loadFromDraco(this);
  193. } else {
  194. loadFromBufferView(this);
  195. }
  196. };
  197. function getQuantizationInformation(
  198. dracoQuantization,
  199. componentDatatype,
  200. componentCount,
  201. type
  202. ) {
  203. const quantizationBits = dracoQuantization.quantizationBits;
  204. const normalizationRange = (1 << quantizationBits) - 1;
  205. const normalizationDivisor = 1.0 / normalizationRange;
  206. const quantization = new ModelComponents.Quantization();
  207. quantization.componentDatatype = componentDatatype;
  208. quantization.octEncoded = dracoQuantization.octEncoded;
  209. quantization.octEncodedZXY = true;
  210. quantization.type = type;
  211. if (quantization.octEncoded) {
  212. quantization.type = AttributeType.VEC2;
  213. quantization.normalizationRange = normalizationRange;
  214. } else {
  215. const MathType = AttributeType.getMathType(type);
  216. if (MathType === Number) {
  217. const dimensions = dracoQuantization.range;
  218. quantization.quantizedVolumeOffset = dracoQuantization.minValues[0];
  219. quantization.quantizedVolumeDimensions = dimensions;
  220. quantization.normalizationRange = normalizationRange;
  221. quantization.quantizedVolumeStepSize = dimensions * normalizationDivisor;
  222. } else {
  223. quantization.quantizedVolumeOffset = MathType.unpack(
  224. dracoQuantization.minValues
  225. );
  226. quantization.normalizationRange = MathType.unpack(
  227. arrayFill(new Array(componentCount), normalizationRange)
  228. );
  229. const packedDimensions = arrayFill(
  230. new Array(componentCount),
  231. dracoQuantization.range
  232. );
  233. quantization.quantizedVolumeDimensions = MathType.unpack(
  234. packedDimensions
  235. );
  236. // Computing the step size
  237. const packedSteps = packedDimensions.map(function (dimension) {
  238. return dimension * normalizationDivisor;
  239. });
  240. quantization.quantizedVolumeStepSize = MathType.unpack(packedSteps);
  241. }
  242. }
  243. return quantization;
  244. }
  245. function loadFromDraco(vertexBufferLoader) {
  246. const resourceCache = vertexBufferLoader._resourceCache;
  247. const dracoLoader = resourceCache.loadDraco({
  248. gltf: vertexBufferLoader._gltf,
  249. draco: vertexBufferLoader._draco,
  250. gltfResource: vertexBufferLoader._gltfResource,
  251. baseResource: vertexBufferLoader._baseResource,
  252. });
  253. vertexBufferLoader._dracoLoader = dracoLoader;
  254. vertexBufferLoader._state = ResourceLoaderState.LOADING;
  255. dracoLoader.promise
  256. .then(function () {
  257. if (vertexBufferLoader.isDestroyed()) {
  258. return;
  259. }
  260. // Get the typed array and quantization information
  261. const decodedVertexAttributes = dracoLoader.decodedData.vertexAttributes;
  262. const attributeSemantic = vertexBufferLoader._attributeSemantic;
  263. const dracoAttribute = decodedVertexAttributes[attributeSemantic];
  264. const accessorId = vertexBufferLoader._accessorId;
  265. const accessor = vertexBufferLoader._gltf.accessors[accessorId];
  266. const type = accessor.type;
  267. const typedArray = dracoAttribute.array;
  268. const dracoQuantization = dracoAttribute.data.quantization;
  269. if (defined(dracoQuantization)) {
  270. vertexBufferLoader._quantization = getQuantizationInformation(
  271. dracoQuantization,
  272. dracoAttribute.data.componentDatatype,
  273. dracoAttribute.data.componentsPerAttribute,
  274. type
  275. );
  276. }
  277. // Now wait for process() to run to finish loading
  278. vertexBufferLoader._typedArray = typedArray;
  279. vertexBufferLoader._state = ResourceLoaderState.PROCESSING;
  280. })
  281. .catch(function (error) {
  282. if (vertexBufferLoader.isDestroyed()) {
  283. return;
  284. }
  285. handleError(vertexBufferLoader, error);
  286. });
  287. }
  288. function loadFromBufferView(vertexBufferLoader) {
  289. const resourceCache = vertexBufferLoader._resourceCache;
  290. const bufferViewLoader = resourceCache.loadBufferView({
  291. gltf: vertexBufferLoader._gltf,
  292. bufferViewId: vertexBufferLoader._bufferViewId,
  293. gltfResource: vertexBufferLoader._gltfResource,
  294. baseResource: vertexBufferLoader._baseResource,
  295. });
  296. vertexBufferLoader._state = ResourceLoaderState.LOADING;
  297. vertexBufferLoader._bufferViewLoader = bufferViewLoader;
  298. bufferViewLoader.promise
  299. .then(function () {
  300. if (vertexBufferLoader.isDestroyed()) {
  301. return;
  302. }
  303. // Now wait for process() to run to finish loading
  304. vertexBufferLoader._typedArray = bufferViewLoader.typedArray;
  305. vertexBufferLoader._state = ResourceLoaderState.PROCESSING;
  306. })
  307. .catch(function (error) {
  308. if (vertexBufferLoader.isDestroyed()) {
  309. return;
  310. }
  311. handleError(vertexBufferLoader, error);
  312. });
  313. }
  314. function handleError(vertexBufferLoader, error) {
  315. vertexBufferLoader.unload();
  316. vertexBufferLoader._state = ResourceLoaderState.FAILED;
  317. const errorMessage = "Failed to load vertex buffer";
  318. error = vertexBufferLoader.getError(errorMessage, error);
  319. vertexBufferLoader._promise.reject(error);
  320. }
  321. function CreateVertexBufferJob() {
  322. this.typedArray = undefined;
  323. this.dequantize = undefined;
  324. this.componentType = undefined;
  325. this.type = undefined;
  326. this.count = undefined;
  327. this.context = undefined;
  328. this.buffer = undefined;
  329. }
  330. CreateVertexBufferJob.prototype.set = function (
  331. typedArray,
  332. dequantize,
  333. componentType,
  334. type,
  335. count,
  336. context
  337. ) {
  338. this.typedArray = typedArray;
  339. this.dequantize = dequantize;
  340. this.componentType = componentType;
  341. this.type = type;
  342. this.count = count;
  343. this.context = context;
  344. };
  345. CreateVertexBufferJob.prototype.execute = function () {
  346. this.buffer = createVertexBuffer(
  347. this.typedArray,
  348. this.dequantize,
  349. this.componentType,
  350. this.type,
  351. this.count,
  352. this.context
  353. );
  354. };
  355. function createVertexBuffer(
  356. typedArray,
  357. dequantize,
  358. componentType,
  359. type,
  360. count,
  361. context
  362. ) {
  363. if (dequantize && componentType !== ComponentDatatype.FLOAT) {
  364. typedArray = AttributeCompression.dequantize(
  365. typedArray,
  366. componentType,
  367. type,
  368. count
  369. );
  370. }
  371. const buffer = Buffer.createVertexBuffer({
  372. typedArray: typedArray,
  373. context: context,
  374. usage: BufferUsage.STATIC_DRAW,
  375. });
  376. buffer.vertexArrayDestroyable = false;
  377. return buffer;
  378. }
  379. const scratchVertexBufferJob = new CreateVertexBufferJob();
  380. /**
  381. * Processes the resource until it becomes ready.
  382. *
  383. * @param {FrameState} frameState The frame state.
  384. * @private
  385. */
  386. GltfVertexBufferLoader.prototype.process = function (frameState) {
  387. //>>includeStart('debug', pragmas.debug);
  388. Check.typeOf.object("frameState", frameState);
  389. //>>includeEnd('debug');
  390. if (this._state === ResourceLoaderState.READY) {
  391. return;
  392. }
  393. const typedArray = this._typedArray;
  394. const dequantize = this._dequantize;
  395. if (defined(this._dracoLoader)) {
  396. this._dracoLoader.process(frameState);
  397. }
  398. if (defined(this._bufferViewLoader)) {
  399. this._bufferViewLoader.process(frameState);
  400. }
  401. if (!defined(typedArray)) {
  402. // Buffer view hasn't been loaded yet
  403. return;
  404. }
  405. if (this._loadAsTypedArray) {
  406. // Unload everything except the typed array
  407. this.unload();
  408. this._typedArray = typedArray;
  409. this._state = ResourceLoaderState.READY;
  410. this._promise.resolve(this);
  411. return;
  412. }
  413. const accessor = this._gltf.accessors[this._accessorId];
  414. let buffer;
  415. if (this._asynchronous) {
  416. const vertexBufferJob = scratchVertexBufferJob;
  417. vertexBufferJob.set(
  418. typedArray,
  419. dequantize,
  420. accessor.componentType,
  421. accessor.type,
  422. accessor.count,
  423. frameState.context
  424. );
  425. const jobScheduler = frameState.jobScheduler;
  426. if (!jobScheduler.execute(vertexBufferJob, JobType.BUFFER)) {
  427. // Job scheduler is full. Try again next frame.
  428. return;
  429. }
  430. buffer = vertexBufferJob.buffer;
  431. } else {
  432. buffer = createVertexBuffer(
  433. typedArray,
  434. dequantize,
  435. accessor.componentType,
  436. accessor.type,
  437. accessor.count,
  438. frameState.context
  439. );
  440. }
  441. // Unload everything except the vertex buffer
  442. this.unload();
  443. this._buffer = buffer;
  444. this._state = ResourceLoaderState.READY;
  445. this._promise.resolve(this);
  446. };
  447. /**
  448. * Unloads the resource.
  449. * @private
  450. */
  451. GltfVertexBufferLoader.prototype.unload = function () {
  452. if (defined(this._buffer)) {
  453. this._buffer.destroy();
  454. }
  455. const resourceCache = this._resourceCache;
  456. if (defined(this._bufferViewLoader)) {
  457. resourceCache.unload(this._bufferViewLoader);
  458. }
  459. if (defined(this._dracoLoader)) {
  460. resourceCache.unload(this._dracoLoader);
  461. }
  462. this._bufferViewLoader = undefined;
  463. this._dracoLoader = undefined;
  464. this._typedArray = undefined;
  465. this._buffer = undefined;
  466. this._gltf = undefined;
  467. };