@loaders.gl/gltf 3.0.13 → 3.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/dist/dist.es5.min.js +1 -1
  2. package/dist/dist.es5.min.js.map +1 -1
  3. package/dist/dist.min.js +1 -1
  4. package/dist/dist.min.js.map +1 -1
  5. package/dist/es5/bundle.js +2 -2
  6. package/dist/es5/bundle.js.map +1 -1
  7. package/dist/es5/glb-loader.js +33 -10
  8. package/dist/es5/glb-loader.js.map +1 -1
  9. package/dist/es5/glb-writer.js +8 -9
  10. package/dist/es5/glb-writer.js.map +1 -1
  11. package/dist/es5/gltf-loader.js +52 -14
  12. package/dist/es5/gltf-loader.js.map +1 -1
  13. package/dist/es5/gltf-writer.js +10 -10
  14. package/dist/es5/gltf-writer.js.map +1 -1
  15. package/dist/es5/index.js +7 -7
  16. package/dist/es5/lib/api/gltf-scenegraph.js +541 -447
  17. package/dist/es5/lib/api/gltf-scenegraph.js.map +1 -1
  18. package/dist/es5/lib/api/normalize-gltf-v1.js +280 -150
  19. package/dist/es5/lib/api/normalize-gltf-v1.js.map +1 -1
  20. package/dist/es5/lib/api/post-process-gltf.js +354 -279
  21. package/dist/es5/lib/api/post-process-gltf.js.map +1 -1
  22. package/dist/es5/lib/encoders/encode-glb.js +22 -19
  23. package/dist/es5/lib/encoders/encode-glb.js.map +1 -1
  24. package/dist/es5/lib/encoders/encode-gltf.js +5 -3
  25. package/dist/es5/lib/encoders/encode-gltf.js.map +1 -1
  26. package/dist/es5/lib/extensions/EXT_meshopt_compression.js +99 -34
  27. package/dist/es5/lib/extensions/EXT_meshopt_compression.js.map +1 -1
  28. package/dist/es5/lib/extensions/KHR_binary_gltf.js +22 -8
  29. package/dist/es5/lib/extensions/KHR_binary_gltf.js.map +1 -1
  30. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js +244 -70
  31. package/dist/es5/lib/extensions/KHR_draco_mesh_compression.js.map +1 -1
  32. package/dist/es5/lib/extensions/KHR_lights_punctual.js +102 -38
  33. package/dist/es5/lib/extensions/KHR_lights_punctual.js.map +1 -1
  34. package/dist/es5/lib/extensions/KHR_materials_unlit.js +67 -22
  35. package/dist/es5/lib/extensions/KHR_materials_unlit.js.map +1 -1
  36. package/dist/es5/lib/extensions/KHR_techniques_webgl.js +88 -32
  37. package/dist/es5/lib/extensions/KHR_techniques_webgl.js.map +1 -1
  38. package/dist/es5/lib/extensions/gltf-extensions.js +67 -17
  39. package/dist/es5/lib/extensions/gltf-extensions.js.map +1 -1
  40. package/dist/es5/lib/gltf-utils/get-typed-array.js +6 -6
  41. package/dist/es5/lib/gltf-utils/get-typed-array.js.map +1 -1
  42. package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js +21 -19
  43. package/dist/es5/lib/gltf-utils/gltf-attribute-utils.js.map +1 -1
  44. package/dist/es5/lib/gltf-utils/gltf-constants.js +13 -13
  45. package/dist/es5/lib/gltf-utils/gltf-constants.js.map +1 -1
  46. package/dist/es5/lib/gltf-utils/gltf-utils.js +16 -16
  47. package/dist/es5/lib/gltf-utils/gltf-utils.js.map +1 -1
  48. package/dist/es5/lib/gltf-utils/resolve-url.js +2 -2
  49. package/dist/es5/lib/gltf-utils/resolve-url.js.map +1 -1
  50. package/dist/es5/lib/parsers/parse-glb.js +38 -34
  51. package/dist/es5/lib/parsers/parse-glb.js.map +1 -1
  52. package/dist/es5/lib/parsers/parse-gltf.js +215 -80
  53. package/dist/es5/lib/parsers/parse-gltf.js.map +1 -1
  54. package/dist/es5/lib/utils/version.js +1 -1
  55. package/dist/es5/lib/utils/version.js.map +1 -1
  56. package/dist/es5/meshopt/meshopt-decoder.js +197 -46
  57. package/dist/es5/meshopt/meshopt-decoder.js.map +1 -1
  58. package/dist/esm/lib/utils/version.js +1 -1
  59. package/package.json +6 -6
@@ -7,6 +7,12 @@ Object.defineProperty(exports, "__esModule", {
7
7
  });
8
8
  exports.default = void 0;
9
9
 
10
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
11
+
12
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
+
14
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
+
10
16
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
17
 
12
18
  var _images = require("@loaders.gl/images");
@@ -17,7 +23,17 @@ var _assert = require("../utils/assert");
17
23
 
18
24
  var _gltfUtils = require("../gltf-utils/gltf-utils");
19
25
 
20
- const DEFAULT_GLTF_JSON = {
26
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
27
+
28
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
29
+
30
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
31
+
32
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
33
+
34
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
35
+
36
+ var DEFAULT_GLTF_JSON = {
21
37
  asset: {
22
38
  version: '2.0',
23
39
  generator: 'loaders.gl'
@@ -25,14 +41,14 @@ const DEFAULT_GLTF_JSON = {
25
41
  buffers: []
26
42
  };
27
43
 
28
- class GLTFScenegraph {
29
- constructor(gltf) {
44
+ var GLTFScenegraph = function () {
45
+ function GLTFScenegraph(gltf) {
46
+ (0, _classCallCheck2.default)(this, GLTFScenegraph);
30
47
  (0, _defineProperty2.default)(this, "gltf", void 0);
31
48
  (0, _defineProperty2.default)(this, "sourceBuffers", void 0);
32
49
  (0, _defineProperty2.default)(this, "byteLength", void 0);
33
50
  this.gltf = gltf || {
34
- json: { ...DEFAULT_GLTF_JSON
35
- },
51
+ json: _objectSpread({}, DEFAULT_GLTF_JSON),
36
52
  buffers: []
37
53
  };
38
54
  this.sourceBuffers = [];
@@ -44,486 +60,564 @@ class GLTFScenegraph {
44
60
  }
45
61
  }
46
62
 
47
- get json() {
48
- return this.gltf.json;
49
- }
50
-
51
- getApplicationData(key) {
52
- const data = this.json[key];
53
- return data;
54
- }
55
-
56
- getExtraData(key) {
57
- const extras = this.json.extras || {};
58
- return extras[key];
59
- }
60
-
61
- getExtension(extensionName) {
62
- const isExtension = this.getUsedExtensions().find(name => name === extensionName);
63
- const extensions = this.json.extensions || {};
64
- return isExtension ? extensions[extensionName] || true : null;
65
- }
66
-
67
- getRequiredExtension(extensionName) {
68
- const isRequired = this.getRequiredExtensions().find(name => name === extensionName);
69
- return isRequired ? this.getExtension(extensionName) : null;
70
- }
71
-
72
- getRequiredExtensions() {
73
- return this.json.extensionsRequired || [];
74
- }
75
-
76
- getUsedExtensions() {
77
- return this.json.extensionsUsed || [];
78
- }
79
-
80
- getObjectExtension(object, extensionName) {
81
- const extensions = object.extensions || {};
82
- return extensions[extensionName];
83
- }
84
-
85
- getScene(index) {
86
- return this.getObject('scenes', index);
87
- }
88
-
89
- getNode(index) {
90
- return this.getObject('nodes', index);
91
- }
92
-
93
- getSkin(index) {
94
- return this.getObject('skins', index);
95
- }
96
-
97
- getMesh(index) {
98
- return this.getObject('meshes', index);
99
- }
100
-
101
- getMaterial(index) {
102
- return this.getObject('materials', index);
103
- }
104
-
105
- getAccessor(index) {
106
- return this.getObject('accessors', index);
107
- }
108
-
109
- getTexture(index) {
110
- return this.getObject('textures', index);
111
- }
112
-
113
- getSampler(index) {
114
- return this.getObject('samplers', index);
115
- }
116
-
117
- getImage(index) {
118
- return this.getObject('images', index);
119
- }
120
-
121
- getBufferView(index) {
122
- return this.getObject('bufferViews', index);
123
- }
124
-
125
- getBuffer(index) {
126
- return this.getObject('buffers', index);
127
- }
128
-
129
- getObject(array, index) {
130
- if (typeof index === 'object') {
131
- return index;
63
+ (0, _createClass2.default)(GLTFScenegraph, [{
64
+ key: "json",
65
+ get: function get() {
66
+ return this.gltf.json;
132
67
  }
133
-
134
- const object = this.json[array] && this.json[array][index];
135
-
136
- if (!object) {
137
- throw new Error("glTF file error: Could not find ".concat(array, "[").concat(index, "]"));
68
+ }, {
69
+ key: "getApplicationData",
70
+ value: function getApplicationData(key) {
71
+ var data = this.json[key];
72
+ return data;
138
73
  }
139
-
140
- return object;
141
- }
142
-
143
- getTypedArrayForBufferView(bufferView) {
144
- bufferView = this.getBufferView(bufferView);
145
- const bufferIndex = bufferView.buffer;
146
- const binChunk = this.gltf.buffers[bufferIndex];
147
- (0, _assert.assert)(binChunk);
148
- const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
149
- return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
150
- }
151
-
152
- getTypedArrayForAccessor(accessor) {
153
- accessor = this.getAccessor(accessor);
154
- const bufferView = this.getBufferView(accessor.bufferView);
155
- const buffer = this.getBuffer(bufferView.buffer);
156
- const arrayBuffer = buffer.data;
157
- const {
158
- ArrayType,
159
- length
160
- } = (0, _gltfUtils.getAccessorArrayTypeAndLength)(accessor, bufferView);
161
- const byteOffset = bufferView.byteOffset + accessor.byteOffset;
162
- return new ArrayType(arrayBuffer, byteOffset, length);
163
- }
164
-
165
- getTypedArrayForImageData(image) {
166
- image = this.getAccessor(image);
167
- const bufferView = this.getBufferView(image.bufferView);
168
- const buffer = this.getBuffer(bufferView.buffer);
169
- const arrayBuffer = buffer.data;
170
- const byteOffset = bufferView.byteOffset || 0;
171
- return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
172
- }
173
-
174
- addApplicationData(key, data) {
175
- this.json[key] = data;
176
- return this;
177
- }
178
-
179
- addExtraData(key, data) {
180
- this.json.extras = this.json.extras || {};
181
- this.json.extras[key] = data;
182
- return this;
183
- }
184
-
185
- addObjectExtension(object, extensionName, data) {
186
- object.extensions = object.extensions || {};
187
- object.extensions[extensionName] = data;
188
- this.registerUsedExtension(extensionName);
189
- return this;
190
- }
191
-
192
- setObjectExtension(object, extensionName, data) {
193
- const extensions = object.extensions || {};
194
- extensions[extensionName] = data;
195
- }
196
-
197
- removeObjectExtension(object, extensionName) {
198
- const extensions = object.extensions || {};
199
- const extension = extensions[extensionName];
200
- delete extensions[extensionName];
201
- return extension;
202
- }
203
-
204
- addExtension(extensionName, extensionData = {}) {
205
- (0, _assert.assert)(extensionData);
206
- this.json.extensions = this.json.extensions || {};
207
- this.json.extensions[extensionName] = extensionData;
208
- this.registerUsedExtension(extensionName);
209
- return extensionData;
210
- }
211
-
212
- addRequiredExtension(extensionName, extensionData = {}) {
213
- (0, _assert.assert)(extensionData);
214
- this.addExtension(extensionName, extensionData);
215
- this.registerRequiredExtension(extensionName);
216
- return extensionData;
217
- }
218
-
219
- registerUsedExtension(extensionName) {
220
- this.json.extensionsUsed = this.json.extensionsUsed || [];
221
-
222
- if (!this.json.extensionsUsed.find(ext => ext === extensionName)) {
223
- this.json.extensionsUsed.push(extensionName);
74
+ }, {
75
+ key: "getExtraData",
76
+ value: function getExtraData(key) {
77
+ var extras = this.json.extras || {};
78
+ return extras[key];
224
79
  }
225
- }
226
-
227
- registerRequiredExtension(extensionName) {
228
- this.registerUsedExtension(extensionName);
229
- this.json.extensionsRequired = this.json.extensionsRequired || [];
230
-
231
- if (!this.json.extensionsRequired.find(ext => ext === extensionName)) {
232
- this.json.extensionsRequired.push(extensionName);
80
+ }, {
81
+ key: "getExtension",
82
+ value: function getExtension(extensionName) {
83
+ var isExtension = this.getUsedExtensions().find(function (name) {
84
+ return name === extensionName;
85
+ });
86
+ var extensions = this.json.extensions || {};
87
+ return isExtension ? extensions[extensionName] || true : null;
233
88
  }
234
- }
235
-
236
- removeExtension(extensionName) {
237
- if (this.json.extensionsRequired) {
238
- this._removeStringFromArray(this.json.extensionsRequired, extensionName);
89
+ }, {
90
+ key: "getRequiredExtension",
91
+ value: function getRequiredExtension(extensionName) {
92
+ var isRequired = this.getRequiredExtensions().find(function (name) {
93
+ return name === extensionName;
94
+ });
95
+ return isRequired ? this.getExtension(extensionName) : null;
239
96
  }
240
-
241
- if (this.json.extensionsUsed) {
242
- this._removeStringFromArray(this.json.extensionsUsed, extensionName);
97
+ }, {
98
+ key: "getRequiredExtensions",
99
+ value: function getRequiredExtensions() {
100
+ return this.json.extensionsRequired || [];
243
101
  }
244
-
245
- if (this.json.extensions) {
246
- delete this.json.extensions[extensionName];
102
+ }, {
103
+ key: "getUsedExtensions",
104
+ value: function getUsedExtensions() {
105
+ return this.json.extensionsUsed || [];
247
106
  }
248
- }
249
-
250
- setDefaultScene(sceneIndex) {
251
- this.json.scene = sceneIndex;
252
- }
253
-
254
- addScene(scene) {
255
- const {
256
- nodeIndices
257
- } = scene;
258
- this.json.scenes = this.json.scenes || [];
259
- this.json.scenes.push({
260
- nodes: nodeIndices
261
- });
262
- return this.json.scenes.length - 1;
263
- }
264
-
265
- addNode(node) {
266
- const {
267
- meshIndex,
268
- matrix
269
- } = node;
270
- this.json.nodes = this.json.nodes || [];
271
- const nodeData = {
272
- mesh: meshIndex
273
- };
274
-
275
- if (matrix) {
276
- nodeData.matrix = matrix;
107
+ }, {
108
+ key: "getObjectExtension",
109
+ value: function getObjectExtension(object, extensionName) {
110
+ var extensions = object.extensions || {};
111
+ return extensions[extensionName];
277
112
  }
278
-
279
- this.json.nodes.push(nodeData);
280
- return this.json.nodes.length - 1;
281
- }
282
-
283
- addMesh(mesh) {
284
- const {
285
- attributes,
286
- indices,
287
- material,
288
- mode = 4
289
- } = mesh;
290
-
291
- const accessors = this._addAttributes(attributes);
292
-
293
- const glTFMesh = {
294
- primitives: [{
295
- attributes: accessors,
296
- mode
297
- }]
298
- };
299
-
300
- if (indices) {
301
- const indicesAccessor = this._addIndices(indices);
302
-
303
- glTFMesh.primitives[0].indices = indicesAccessor;
113
+ }, {
114
+ key: "getScene",
115
+ value: function getScene(index) {
116
+ return this.getObject('scenes', index);
304
117
  }
305
-
306
- if (Number.isFinite(material)) {
307
- glTFMesh.primitives[0].material = material;
118
+ }, {
119
+ key: "getNode",
120
+ value: function getNode(index) {
121
+ return this.getObject('nodes', index);
308
122
  }
309
-
310
- this.json.meshes = this.json.meshes || [];
311
- this.json.meshes.push(glTFMesh);
312
- return this.json.meshes.length - 1;
313
- }
314
-
315
- addPointCloud(attributes) {
316
- const accessorIndices = this._addAttributes(attributes);
317
-
318
- const glTFMesh = {
319
- primitives: [{
320
- attributes: accessorIndices,
321
- mode: 0
322
- }]
323
- };
324
- this.json.meshes = this.json.meshes || [];
325
- this.json.meshes.push(glTFMesh);
326
- return this.json.meshes.length - 1;
327
- }
328
-
329
- addImage(imageData, mimeTypeOpt) {
330
- const metadata = (0, _images.getBinaryImageMetadata)(imageData);
331
- const mimeType = mimeTypeOpt || (metadata === null || metadata === void 0 ? void 0 : metadata.mimeType);
332
- const bufferViewIndex = this.addBufferView(imageData);
333
- const glTFImage = {
334
- bufferView: bufferViewIndex,
335
- mimeType
336
- };
337
- this.json.images = this.json.images || [];
338
- this.json.images.push(glTFImage);
339
- return this.json.images.length - 1;
340
- }
341
-
342
- addBufferView(buffer) {
343
- const byteLength = buffer.byteLength;
344
- (0, _assert.assert)(Number.isFinite(byteLength));
345
- this.sourceBuffers = this.sourceBuffers || [];
346
- this.sourceBuffers.push(buffer);
347
- const glTFBufferView = {
348
- buffer: 0,
349
- byteOffset: this.byteLength,
350
- byteLength
351
- };
352
- this.byteLength += (0, _loaderUtils.padToNBytes)(byteLength, 4);
353
- this.json.bufferViews = this.json.bufferViews || [];
354
- this.json.bufferViews.push(glTFBufferView);
355
- return this.json.bufferViews.length - 1;
356
- }
357
-
358
- addAccessor(bufferViewIndex, accessor) {
359
- const glTFAccessor = {
360
- bufferView: bufferViewIndex,
361
- type: (0, _gltfUtils.getAccessorTypeFromSize)(accessor.size),
362
- componentType: accessor.componentType,
363
- count: accessor.count,
364
- max: accessor.max,
365
- min: accessor.min
366
- };
367
- this.json.accessors = this.json.accessors || [];
368
- this.json.accessors.push(glTFAccessor);
369
- return this.json.accessors.length - 1;
370
- }
371
-
372
- addBinaryBuffer(sourceBuffer, accessor = {
373
- size: 3
374
- }) {
375
- const bufferViewIndex = this.addBufferView(sourceBuffer);
376
- let minMax = {
377
- min: accessor.min,
378
- max: accessor.max
379
- };
380
-
381
- if (!minMax.min || !minMax.max) {
382
- minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
123
+ }, {
124
+ key: "getSkin",
125
+ value: function getSkin(index) {
126
+ return this.getObject('skins', index);
383
127
  }
384
-
385
- const accessorDefaults = {
386
- size: accessor.size,
387
- componentType: (0, _gltfUtils.getComponentTypeFromArray)(sourceBuffer),
388
- count: Math.round(sourceBuffer.length / accessor.size),
389
- min: minMax.min,
390
- max: minMax.max
391
- };
392
- return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
393
- }
394
-
395
- addTexture(texture) {
396
- const {
397
- imageIndex
398
- } = texture;
399
- const glTFTexture = {
400
- source: imageIndex
401
- };
402
- this.json.textures = this.json.textures || [];
403
- this.json.textures.push(glTFTexture);
404
- return this.json.textures.length - 1;
405
- }
406
-
407
- addMaterial(pbrMaterialInfo) {
408
- this.json.materials = this.json.materials || [];
409
- this.json.materials.push(pbrMaterialInfo);
410
- return this.json.materials.length - 1;
411
- }
412
-
413
- createBinaryChunk() {
414
- var _this$json, _this$json$buffers;
415
-
416
- this.gltf.buffers = [];
417
- const totalByteLength = this.byteLength;
418
- const arrayBuffer = new ArrayBuffer(totalByteLength);
419
- const targetArray = new Uint8Array(arrayBuffer);
420
- let dstByteOffset = 0;
421
-
422
- for (const sourceBuffer of this.sourceBuffers || []) {
423
- dstByteOffset = (0, _loaderUtils.copyToArray)(sourceBuffer, targetArray, dstByteOffset);
128
+ }, {
129
+ key: "getMesh",
130
+ value: function getMesh(index) {
131
+ return this.getObject('meshes', index);
424
132
  }
425
-
426
- if ((_this$json = this.json) !== null && _this$json !== void 0 && (_this$json$buffers = _this$json.buffers) !== null && _this$json$buffers !== void 0 && _this$json$buffers[0]) {
427
- this.json.buffers[0].byteLength = totalByteLength;
428
- } else {
429
- this.json.buffers = [{
430
- byteLength: totalByteLength
431
- }];
133
+ }, {
134
+ key: "getMaterial",
135
+ value: function getMaterial(index) {
136
+ return this.getObject('materials', index);
432
137
  }
138
+ }, {
139
+ key: "getAccessor",
140
+ value: function getAccessor(index) {
141
+ return this.getObject('accessors', index);
142
+ }
143
+ }, {
144
+ key: "getTexture",
145
+ value: function getTexture(index) {
146
+ return this.getObject('textures', index);
147
+ }
148
+ }, {
149
+ key: "getSampler",
150
+ value: function getSampler(index) {
151
+ return this.getObject('samplers', index);
152
+ }
153
+ }, {
154
+ key: "getImage",
155
+ value: function getImage(index) {
156
+ return this.getObject('images', index);
157
+ }
158
+ }, {
159
+ key: "getBufferView",
160
+ value: function getBufferView(index) {
161
+ return this.getObject('bufferViews', index);
162
+ }
163
+ }, {
164
+ key: "getBuffer",
165
+ value: function getBuffer(index) {
166
+ return this.getObject('buffers', index);
167
+ }
168
+ }, {
169
+ key: "getObject",
170
+ value: function getObject(array, index) {
171
+ if ((0, _typeof2.default)(index) === 'object') {
172
+ return index;
173
+ }
433
174
 
434
- this.gltf.binary = arrayBuffer;
435
- this.sourceBuffers = [arrayBuffer];
436
- }
437
-
438
- _removeStringFromArray(array, string) {
439
- let found = true;
175
+ var object = this.json[array] && this.json[array][index];
440
176
 
441
- while (found) {
442
- const index = array.indexOf(string);
177
+ if (!object) {
178
+ throw new Error("glTF file error: Could not find ".concat(array, "[").concat(index, "]"));
179
+ }
443
180
 
444
- if (index > -1) {
445
- array.splice(index, 1);
446
- } else {
447
- found = false;
181
+ return object;
182
+ }
183
+ }, {
184
+ key: "getTypedArrayForBufferView",
185
+ value: function getTypedArrayForBufferView(bufferView) {
186
+ bufferView = this.getBufferView(bufferView);
187
+ var bufferIndex = bufferView.buffer;
188
+ var binChunk = this.gltf.buffers[bufferIndex];
189
+ (0, _assert.assert)(binChunk);
190
+ var byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
191
+ return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
192
+ }
193
+ }, {
194
+ key: "getTypedArrayForAccessor",
195
+ value: function getTypedArrayForAccessor(accessor) {
196
+ accessor = this.getAccessor(accessor);
197
+ var bufferView = this.getBufferView(accessor.bufferView);
198
+ var buffer = this.getBuffer(bufferView.buffer);
199
+ var arrayBuffer = buffer.data;
200
+
201
+ var _getAccessorArrayType = (0, _gltfUtils.getAccessorArrayTypeAndLength)(accessor, bufferView),
202
+ ArrayType = _getAccessorArrayType.ArrayType,
203
+ length = _getAccessorArrayType.length;
204
+
205
+ var byteOffset = bufferView.byteOffset + accessor.byteOffset;
206
+ return new ArrayType(arrayBuffer, byteOffset, length);
207
+ }
208
+ }, {
209
+ key: "getTypedArrayForImageData",
210
+ value: function getTypedArrayForImageData(image) {
211
+ image = this.getAccessor(image);
212
+ var bufferView = this.getBufferView(image.bufferView);
213
+ var buffer = this.getBuffer(bufferView.buffer);
214
+ var arrayBuffer = buffer.data;
215
+ var byteOffset = bufferView.byteOffset || 0;
216
+ return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
217
+ }
218
+ }, {
219
+ key: "addApplicationData",
220
+ value: function addApplicationData(key, data) {
221
+ this.json[key] = data;
222
+ return this;
223
+ }
224
+ }, {
225
+ key: "addExtraData",
226
+ value: function addExtraData(key, data) {
227
+ this.json.extras = this.json.extras || {};
228
+ this.json.extras[key] = data;
229
+ return this;
230
+ }
231
+ }, {
232
+ key: "addObjectExtension",
233
+ value: function addObjectExtension(object, extensionName, data) {
234
+ object.extensions = object.extensions || {};
235
+ object.extensions[extensionName] = data;
236
+ this.registerUsedExtension(extensionName);
237
+ return this;
238
+ }
239
+ }, {
240
+ key: "setObjectExtension",
241
+ value: function setObjectExtension(object, extensionName, data) {
242
+ var extensions = object.extensions || {};
243
+ extensions[extensionName] = data;
244
+ }
245
+ }, {
246
+ key: "removeObjectExtension",
247
+ value: function removeObjectExtension(object, extensionName) {
248
+ var extensions = object.extensions || {};
249
+ var extension = extensions[extensionName];
250
+ delete extensions[extensionName];
251
+ return extension;
252
+ }
253
+ }, {
254
+ key: "addExtension",
255
+ value: function addExtension(extensionName) {
256
+ var extensionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
257
+ (0, _assert.assert)(extensionData);
258
+ this.json.extensions = this.json.extensions || {};
259
+ this.json.extensions[extensionName] = extensionData;
260
+ this.registerUsedExtension(extensionName);
261
+ return extensionData;
262
+ }
263
+ }, {
264
+ key: "addRequiredExtension",
265
+ value: function addRequiredExtension(extensionName) {
266
+ var extensionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
267
+ (0, _assert.assert)(extensionData);
268
+ this.addExtension(extensionName, extensionData);
269
+ this.registerRequiredExtension(extensionName);
270
+ return extensionData;
271
+ }
272
+ }, {
273
+ key: "registerUsedExtension",
274
+ value: function registerUsedExtension(extensionName) {
275
+ this.json.extensionsUsed = this.json.extensionsUsed || [];
276
+
277
+ if (!this.json.extensionsUsed.find(function (ext) {
278
+ return ext === extensionName;
279
+ })) {
280
+ this.json.extensionsUsed.push(extensionName);
448
281
  }
449
282
  }
450
- }
451
-
452
- _addAttributes(attributes = {}) {
453
- const result = {};
283
+ }, {
284
+ key: "registerRequiredExtension",
285
+ value: function registerRequiredExtension(extensionName) {
286
+ this.registerUsedExtension(extensionName);
287
+ this.json.extensionsRequired = this.json.extensionsRequired || [];
288
+
289
+ if (!this.json.extensionsRequired.find(function (ext) {
290
+ return ext === extensionName;
291
+ })) {
292
+ this.json.extensionsRequired.push(extensionName);
293
+ }
294
+ }
295
+ }, {
296
+ key: "removeExtension",
297
+ value: function removeExtension(extensionName) {
298
+ if (this.json.extensionsRequired) {
299
+ this._removeStringFromArray(this.json.extensionsRequired, extensionName);
300
+ }
454
301
 
455
- for (const attributeKey in attributes) {
456
- const attributeData = attributes[attributeKey];
302
+ if (this.json.extensionsUsed) {
303
+ this._removeStringFromArray(this.json.extensionsUsed, extensionName);
304
+ }
457
305
 
458
- const attrName = this._getGltfAttributeName(attributeKey);
306
+ if (this.json.extensions) {
307
+ delete this.json.extensions[extensionName];
308
+ }
309
+ }
310
+ }, {
311
+ key: "setDefaultScene",
312
+ value: function setDefaultScene(sceneIndex) {
313
+ this.json.scene = sceneIndex;
314
+ }
315
+ }, {
316
+ key: "addScene",
317
+ value: function addScene(scene) {
318
+ var nodeIndices = scene.nodeIndices;
319
+ this.json.scenes = this.json.scenes || [];
320
+ this.json.scenes.push({
321
+ nodes: nodeIndices
322
+ });
323
+ return this.json.scenes.length - 1;
324
+ }
325
+ }, {
326
+ key: "addNode",
327
+ value: function addNode(node) {
328
+ var meshIndex = node.meshIndex,
329
+ matrix = node.matrix;
330
+ this.json.nodes = this.json.nodes || [];
331
+ var nodeData = {
332
+ mesh: meshIndex
333
+ };
334
+
335
+ if (matrix) {
336
+ nodeData.matrix = matrix;
337
+ }
459
338
 
460
- const accessor = this.addBinaryBuffer(attributeData.value, attributeData);
461
- result[attrName] = accessor;
339
+ this.json.nodes.push(nodeData);
340
+ return this.json.nodes.length - 1;
462
341
  }
342
+ }, {
343
+ key: "addMesh",
344
+ value: function addMesh(mesh) {
345
+ var attributes = mesh.attributes,
346
+ indices = mesh.indices,
347
+ material = mesh.material,
348
+ _mesh$mode = mesh.mode,
349
+ mode = _mesh$mode === void 0 ? 4 : _mesh$mode;
350
+
351
+ var accessors = this._addAttributes(attributes);
352
+
353
+ var glTFMesh = {
354
+ primitives: [{
355
+ attributes: accessors,
356
+ mode: mode
357
+ }]
358
+ };
359
+
360
+ if (indices) {
361
+ var indicesAccessor = this._addIndices(indices);
362
+
363
+ glTFMesh.primitives[0].indices = indicesAccessor;
364
+ }
463
365
 
464
- return result;
465
- }
366
+ if (Number.isFinite(material)) {
367
+ glTFMesh.primitives[0].material = material;
368
+ }
466
369
 
467
- _addIndices(indices) {
468
- return this.addBinaryBuffer(indices, {
469
- size: 1
470
- });
471
- }
370
+ this.json.meshes = this.json.meshes || [];
371
+ this.json.meshes.push(glTFMesh);
372
+ return this.json.meshes.length - 1;
373
+ }
374
+ }, {
375
+ key: "addPointCloud",
376
+ value: function addPointCloud(attributes) {
377
+ var accessorIndices = this._addAttributes(attributes);
378
+
379
+ var glTFMesh = {
380
+ primitives: [{
381
+ attributes: accessorIndices,
382
+ mode: 0
383
+ }]
384
+ };
385
+ this.json.meshes = this.json.meshes || [];
386
+ this.json.meshes.push(glTFMesh);
387
+ return this.json.meshes.length - 1;
388
+ }
389
+ }, {
390
+ key: "addImage",
391
+ value: function addImage(imageData, mimeTypeOpt) {
392
+ var metadata = (0, _images.getBinaryImageMetadata)(imageData);
393
+ var mimeType = mimeTypeOpt || (metadata === null || metadata === void 0 ? void 0 : metadata.mimeType);
394
+ var bufferViewIndex = this.addBufferView(imageData);
395
+ var glTFImage = {
396
+ bufferView: bufferViewIndex,
397
+ mimeType: mimeType
398
+ };
399
+ this.json.images = this.json.images || [];
400
+ this.json.images.push(glTFImage);
401
+ return this.json.images.length - 1;
402
+ }
403
+ }, {
404
+ key: "addBufferView",
405
+ value: function addBufferView(buffer) {
406
+ var byteLength = buffer.byteLength;
407
+ (0, _assert.assert)(Number.isFinite(byteLength));
408
+ this.sourceBuffers = this.sourceBuffers || [];
409
+ this.sourceBuffers.push(buffer);
410
+ var glTFBufferView = {
411
+ buffer: 0,
412
+ byteOffset: this.byteLength,
413
+ byteLength: byteLength
414
+ };
415
+ this.byteLength += (0, _loaderUtils.padToNBytes)(byteLength, 4);
416
+ this.json.bufferViews = this.json.bufferViews || [];
417
+ this.json.bufferViews.push(glTFBufferView);
418
+ return this.json.bufferViews.length - 1;
419
+ }
420
+ }, {
421
+ key: "addAccessor",
422
+ value: function addAccessor(bufferViewIndex, accessor) {
423
+ var glTFAccessor = {
424
+ bufferView: bufferViewIndex,
425
+ type: (0, _gltfUtils.getAccessorTypeFromSize)(accessor.size),
426
+ componentType: accessor.componentType,
427
+ count: accessor.count,
428
+ max: accessor.max,
429
+ min: accessor.min
430
+ };
431
+ this.json.accessors = this.json.accessors || [];
432
+ this.json.accessors.push(glTFAccessor);
433
+ return this.json.accessors.length - 1;
434
+ }
435
+ }, {
436
+ key: "addBinaryBuffer",
437
+ value: function addBinaryBuffer(sourceBuffer) {
438
+ var accessor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
439
+ size: 3
440
+ };
441
+ var bufferViewIndex = this.addBufferView(sourceBuffer);
442
+ var minMax = {
443
+ min: accessor.min,
444
+ max: accessor.max
445
+ };
446
+
447
+ if (!minMax.min || !minMax.max) {
448
+ minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
449
+ }
472
450
 
473
- _getGltfAttributeName(attributeName) {
474
- switch (attributeName.toLowerCase()) {
475
- case 'position':
476
- case 'positions':
477
- case 'vertices':
478
- return 'POSITION';
451
+ var accessorDefaults = {
452
+ size: accessor.size,
453
+ componentType: (0, _gltfUtils.getComponentTypeFromArray)(sourceBuffer),
454
+ count: Math.round(sourceBuffer.length / accessor.size),
455
+ min: minMax.min,
456
+ max: minMax.max
457
+ };
458
+ return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
459
+ }
460
+ }, {
461
+ key: "addTexture",
462
+ value: function addTexture(texture) {
463
+ var imageIndex = texture.imageIndex;
464
+ var glTFTexture = {
465
+ source: imageIndex
466
+ };
467
+ this.json.textures = this.json.textures || [];
468
+ this.json.textures.push(glTFTexture);
469
+ return this.json.textures.length - 1;
470
+ }
471
+ }, {
472
+ key: "addMaterial",
473
+ value: function addMaterial(pbrMaterialInfo) {
474
+ this.json.materials = this.json.materials || [];
475
+ this.json.materials.push(pbrMaterialInfo);
476
+ return this.json.materials.length - 1;
477
+ }
478
+ }, {
479
+ key: "createBinaryChunk",
480
+ value: function createBinaryChunk() {
481
+ var _this$json, _this$json$buffers;
482
+
483
+ this.gltf.buffers = [];
484
+ var totalByteLength = this.byteLength;
485
+ var arrayBuffer = new ArrayBuffer(totalByteLength);
486
+ var targetArray = new Uint8Array(arrayBuffer);
487
+ var dstByteOffset = 0;
488
+
489
+ var _iterator = _createForOfIteratorHelper(this.sourceBuffers || []),
490
+ _step;
491
+
492
+ try {
493
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
494
+ var sourceBuffer = _step.value;
495
+ dstByteOffset = (0, _loaderUtils.copyToArray)(sourceBuffer, targetArray, dstByteOffset);
496
+ }
497
+ } catch (err) {
498
+ _iterator.e(err);
499
+ } finally {
500
+ _iterator.f();
501
+ }
479
502
 
480
- case 'normal':
481
- case 'normals':
482
- return 'NORMAL';
503
+ if ((_this$json = this.json) !== null && _this$json !== void 0 && (_this$json$buffers = _this$json.buffers) !== null && _this$json$buffers !== void 0 && _this$json$buffers[0]) {
504
+ this.json.buffers[0].byteLength = totalByteLength;
505
+ } else {
506
+ this.json.buffers = [{
507
+ byteLength: totalByteLength
508
+ }];
509
+ }
483
510
 
484
- case 'color':
485
- case 'colors':
486
- return 'COLOR_0';
511
+ this.gltf.binary = arrayBuffer;
512
+ this.sourceBuffers = [arrayBuffer];
513
+ }
514
+ }, {
515
+ key: "_removeStringFromArray",
516
+ value: function _removeStringFromArray(array, string) {
517
+ var found = true;
518
+
519
+ while (found) {
520
+ var index = array.indexOf(string);
521
+
522
+ if (index > -1) {
523
+ array.splice(index, 1);
524
+ } else {
525
+ found = false;
526
+ }
527
+ }
528
+ }
529
+ }, {
530
+ key: "_addAttributes",
531
+ value: function _addAttributes() {
532
+ var attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
533
+ var result = {};
487
534
 
488
- case 'texcoord':
489
- case 'texcoords':
490
- return 'TEXCOORD_0';
535
+ for (var attributeKey in attributes) {
536
+ var attributeData = attributes[attributeKey];
491
537
 
492
- default:
493
- return attributeName;
494
- }
495
- }
538
+ var attrName = this._getGltfAttributeName(attributeKey);
496
539
 
497
- _getAccessorMinMax(buffer, size) {
498
- const result = {
499
- min: null,
500
- max: null
501
- };
540
+ var accessor = this.addBinaryBuffer(attributeData.value, attributeData);
541
+ result[attrName] = accessor;
542
+ }
502
543
 
503
- if (buffer.length < size) {
504
544
  return result;
505
545
  }
506
-
507
- result.min = [];
508
- result.max = [];
509
- const initValues = buffer.subarray(0, size);
510
-
511
- for (const value of initValues) {
512
- result.min.push(value);
513
- result.max.push(value);
546
+ }, {
547
+ key: "_addIndices",
548
+ value: function _addIndices(indices) {
549
+ return this.addBinaryBuffer(indices, {
550
+ size: 1
551
+ });
514
552
  }
515
-
516
- for (let index = size; index < buffer.length; index += size) {
517
- for (let componentIndex = 0; componentIndex < size; componentIndex++) {
518
- result.min[0 + componentIndex] = Math.min(result.min[0 + componentIndex], buffer[index + componentIndex]);
519
- result.max[0 + componentIndex] = Math.max(result.max[0 + componentIndex], buffer[index + componentIndex]);
553
+ }, {
554
+ key: "_getGltfAttributeName",
555
+ value: function _getGltfAttributeName(attributeName) {
556
+ switch (attributeName.toLowerCase()) {
557
+ case 'position':
558
+ case 'positions':
559
+ case 'vertices':
560
+ return 'POSITION';
561
+
562
+ case 'normal':
563
+ case 'normals':
564
+ return 'NORMAL';
565
+
566
+ case 'color':
567
+ case 'colors':
568
+ return 'COLOR_0';
569
+
570
+ case 'texcoord':
571
+ case 'texcoords':
572
+ return 'TEXCOORD_0';
573
+
574
+ default:
575
+ return attributeName;
520
576
  }
521
577
  }
578
+ }, {
579
+ key: "_getAccessorMinMax",
580
+ value: function _getAccessorMinMax(buffer, size) {
581
+ var result = {
582
+ min: null,
583
+ max: null
584
+ };
585
+
586
+ if (buffer.length < size) {
587
+ return result;
588
+ }
522
589
 
523
- return result;
524
- }
590
+ result.min = [];
591
+ result.max = [];
592
+ var initValues = buffer.subarray(0, size);
593
+
594
+ var _iterator2 = _createForOfIteratorHelper(initValues),
595
+ _step2;
596
+
597
+ try {
598
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
599
+ var value = _step2.value;
600
+ result.min.push(value);
601
+ result.max.push(value);
602
+ }
603
+ } catch (err) {
604
+ _iterator2.e(err);
605
+ } finally {
606
+ _iterator2.f();
607
+ }
608
+
609
+ for (var index = size; index < buffer.length; index += size) {
610
+ for (var componentIndex = 0; componentIndex < size; componentIndex++) {
611
+ result.min[0 + componentIndex] = Math.min(result.min[0 + componentIndex], buffer[index + componentIndex]);
612
+ result.max[0 + componentIndex] = Math.max(result.max[0 + componentIndex], buffer[index + componentIndex]);
613
+ }
614
+ }
525
615
 
526
- }
616
+ return result;
617
+ }
618
+ }]);
619
+ return GLTFScenegraph;
620
+ }();
527
621
 
528
622
  exports.default = GLTFScenegraph;
529
623
  //# sourceMappingURL=gltf-scenegraph.js.map