@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
@@ -1,17 +1,31 @@
1
1
  "use strict";
2
2
 
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _typeof = require("@babel/runtime/helpers/typeof");
6
+
3
7
  Object.defineProperty(exports, "__esModule", {
4
8
  value: true
5
9
  });
6
10
  exports.normalizeGLTFV1 = normalizeGLTFV1;
7
11
 
12
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
+
14
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
+
8
16
  var KHR_binary_glTF = _interopRequireWildcard(require("../extensions/KHR_binary_gltf"));
9
17
 
10
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
18
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
19
+
20
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
11
21
 
12
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22
+ 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; } } }; }
13
23
 
14
- const GLTF_ARRAYS = {
24
+ 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); }
25
+
26
+ 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; }
27
+
28
+ var GLTF_ARRAYS = {
15
29
  accessors: 'accessor',
16
30
  animations: 'animation',
17
31
  buffers: 'buffer',
@@ -25,7 +39,7 @@ const GLTF_ARRAYS = {
25
39
  skins: 'skin',
26
40
  textures: 'texture'
27
41
  };
28
- const GLTF_KEYS = {
42
+ var GLTF_KEYS = {
29
43
  accessor: 'accessors',
30
44
  animations: 'animation',
31
45
  buffer: 'buffers',
@@ -40,8 +54,9 @@ const GLTF_KEYS = {
40
54
  texture: 'textures'
41
55
  };
42
56
 
43
- class GLTFV1Normalizer {
44
- constructor(gltf) {
57
+ var GLTFV1Normalizer = function () {
58
+ function GLTFV1Normalizer(gltf) {
59
+ (0, _classCallCheck2.default)(this, GLTFV1Normalizer);
45
60
  this.idToIndexMap = {
46
61
  animations: {},
47
62
  accessors: {},
@@ -58,202 +73,317 @@ class GLTFV1Normalizer {
58
73
  };
59
74
  }
60
75
 
61
- normalize(gltf, options) {
62
- this.json = gltf.json;
63
- const json = gltf.json;
76
+ (0, _createClass2.default)(GLTFV1Normalizer, [{
77
+ key: "normalize",
78
+ value: function normalize(gltf, options) {
79
+ this.json = gltf.json;
80
+ var json = gltf.json;
64
81
 
65
- switch (json.asset && json.asset.version) {
66
- case '2.0':
67
- return;
82
+ switch (json.asset && json.asset.version) {
83
+ case '2.0':
84
+ return;
68
85
 
69
- case undefined:
70
- case '1.0':
71
- break;
86
+ case undefined:
87
+ case '1.0':
88
+ break;
72
89
 
73
- default:
74
- console.warn("glTF: Unknown version ".concat(json.asset.version));
75
- return;
76
- }
90
+ default:
91
+ console.warn("glTF: Unknown version ".concat(json.asset.version));
92
+ return;
93
+ }
77
94
 
78
- if (!options.normalize) {
79
- throw new Error('glTF v1 is not supported.');
80
- }
95
+ if (!options.normalize) {
96
+ throw new Error('glTF v1 is not supported.');
97
+ }
81
98
 
82
- console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
99
+ console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
83
100
 
84
- this._addAsset(json);
101
+ this._addAsset(json);
85
102
 
86
- this._convertTopLevelObjectsToArrays(json);
103
+ this._convertTopLevelObjectsToArrays(json);
87
104
 
88
- KHR_binary_glTF.decode(gltf);
105
+ KHR_binary_glTF.decode(gltf);
89
106
 
90
- this._convertObjectIdsToArrayIndices(json);
107
+ this._convertObjectIdsToArrayIndices(json);
91
108
 
92
- this._updateObjects(json);
109
+ this._updateObjects(json);
93
110
 
94
- this._updateMaterial(json);
95
- }
111
+ this._updateMaterial(json);
112
+ }
113
+ }, {
114
+ key: "_addAsset",
115
+ value: function _addAsset(json) {
116
+ json.asset = json.asset || {};
117
+ json.asset.version = '2.0';
118
+ json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
119
+ }
120
+ }, {
121
+ key: "_convertTopLevelObjectsToArrays",
122
+ value: function _convertTopLevelObjectsToArrays(json) {
123
+ for (var arrayName in GLTF_ARRAYS) {
124
+ this._convertTopLevelObjectToArray(json, arrayName);
125
+ }
126
+ }
127
+ }, {
128
+ key: "_convertTopLevelObjectToArray",
129
+ value: function _convertTopLevelObjectToArray(json, mapName) {
130
+ var objectMap = json[mapName];
96
131
 
97
- _addAsset(json) {
98
- json.asset = json.asset || {};
99
- json.asset.version = '2.0';
100
- json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
101
- }
132
+ if (!objectMap || Array.isArray(objectMap)) {
133
+ return;
134
+ }
102
135
 
103
- _convertTopLevelObjectsToArrays(json) {
104
- for (const arrayName in GLTF_ARRAYS) {
105
- this._convertTopLevelObjectToArray(json, arrayName);
136
+ json[mapName] = [];
137
+
138
+ for (var id in objectMap) {
139
+ var object = objectMap[id];
140
+ object.id = object.id || id;
141
+ var index = json[mapName].length;
142
+ json[mapName].push(object);
143
+ this.idToIndexMap[mapName][id] = index;
144
+ }
106
145
  }
107
- }
146
+ }, {
147
+ key: "_convertObjectIdsToArrayIndices",
148
+ value: function _convertObjectIdsToArrayIndices(json) {
149
+ for (var arrayName in GLTF_ARRAYS) {
150
+ this._convertIdsToIndices(json, arrayName);
151
+ }
108
152
 
109
- _convertTopLevelObjectToArray(json, mapName) {
110
- const objectMap = json[mapName];
153
+ if ('scene' in json) {
154
+ json.scene = this._convertIdToIndex(json.scene, 'scene');
155
+ }
111
156
 
112
- if (!objectMap || Array.isArray(objectMap)) {
113
- return;
114
- }
157
+ var _iterator = _createForOfIteratorHelper(json.textures),
158
+ _step;
115
159
 
116
- json[mapName] = [];
160
+ try {
161
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
162
+ var texture = _step.value;
117
163
 
118
- for (const id in objectMap) {
119
- const object = objectMap[id];
120
- object.id = object.id || id;
121
- const index = json[mapName].length;
122
- json[mapName].push(object);
123
- this.idToIndexMap[mapName][id] = index;
124
- }
125
- }
164
+ this._convertTextureIds(texture);
165
+ }
166
+ } catch (err) {
167
+ _iterator.e(err);
168
+ } finally {
169
+ _iterator.f();
170
+ }
126
171
 
127
- _convertObjectIdsToArrayIndices(json) {
128
- for (const arrayName in GLTF_ARRAYS) {
129
- this._convertIdsToIndices(json, arrayName);
130
- }
172
+ var _iterator2 = _createForOfIteratorHelper(json.meshes),
173
+ _step2;
131
174
 
132
- if ('scene' in json) {
133
- json.scene = this._convertIdToIndex(json.scene, 'scene');
134
- }
175
+ try {
176
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
177
+ var mesh = _step2.value;
135
178
 
136
- for (const texture of json.textures) {
137
- this._convertTextureIds(texture);
138
- }
179
+ this._convertMeshIds(mesh);
180
+ }
181
+ } catch (err) {
182
+ _iterator2.e(err);
183
+ } finally {
184
+ _iterator2.f();
185
+ }
139
186
 
140
- for (const mesh of json.meshes) {
141
- this._convertMeshIds(mesh);
142
- }
187
+ var _iterator3 = _createForOfIteratorHelper(json.nodes),
188
+ _step3;
143
189
 
144
- for (const node of json.nodes) {
145
- this._convertNodeIds(node);
146
- }
190
+ try {
191
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
192
+ var node = _step3.value;
147
193
 
148
- for (const node of json.scenes) {
149
- this._convertSceneIds(node);
150
- }
151
- }
194
+ this._convertNodeIds(node);
195
+ }
196
+ } catch (err) {
197
+ _iterator3.e(err);
198
+ } finally {
199
+ _iterator3.f();
200
+ }
152
201
 
153
- _convertTextureIds(texture) {
154
- if (texture.source) {
155
- texture.source = this._convertIdToIndex(texture.source, 'image');
156
- }
157
- }
202
+ var _iterator4 = _createForOfIteratorHelper(json.scenes),
203
+ _step4;
158
204
 
159
- _convertMeshIds(mesh) {
160
- for (const primitive of mesh.primitives) {
161
- const {
162
- attributes,
163
- indices,
164
- material
165
- } = primitive;
205
+ try {
206
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
207
+ var _node = _step4.value;
166
208
 
167
- for (const attributeName in attributes) {
168
- attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
209
+ this._convertSceneIds(_node);
210
+ }
211
+ } catch (err) {
212
+ _iterator4.e(err);
213
+ } finally {
214
+ _iterator4.f();
169
215
  }
170
-
171
- if (indices) {
172
- primitive.indices = this._convertIdToIndex(indices, 'accessor');
216
+ }
217
+ }, {
218
+ key: "_convertTextureIds",
219
+ value: function _convertTextureIds(texture) {
220
+ if (texture.source) {
221
+ texture.source = this._convertIdToIndex(texture.source, 'image');
173
222
  }
174
-
175
- if (material) {
176
- primitive.material = this._convertIdToIndex(material, 'material');
223
+ }
224
+ }, {
225
+ key: "_convertMeshIds",
226
+ value: function _convertMeshIds(mesh) {
227
+ var _iterator5 = _createForOfIteratorHelper(mesh.primitives),
228
+ _step5;
229
+
230
+ try {
231
+ for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
232
+ var primitive = _step5.value;
233
+ var attributes = primitive.attributes,
234
+ indices = primitive.indices,
235
+ material = primitive.material;
236
+
237
+ for (var attributeName in attributes) {
238
+ attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
239
+ }
240
+
241
+ if (indices) {
242
+ primitive.indices = this._convertIdToIndex(indices, 'accessor');
243
+ }
244
+
245
+ if (material) {
246
+ primitive.material = this._convertIdToIndex(material, 'material');
247
+ }
248
+ }
249
+ } catch (err) {
250
+ _iterator5.e(err);
251
+ } finally {
252
+ _iterator5.f();
177
253
  }
178
254
  }
179
- }
255
+ }, {
256
+ key: "_convertNodeIds",
257
+ value: function _convertNodeIds(node) {
258
+ var _this = this;
259
+
260
+ if (node.children) {
261
+ node.children = node.children.map(function (child) {
262
+ return _this._convertIdToIndex(child, 'node');
263
+ });
264
+ }
180
265
 
181
- _convertNodeIds(node) {
182
- if (node.children) {
183
- node.children = node.children.map(child => this._convertIdToIndex(child, 'node'));
266
+ if (node.meshes) {
267
+ node.meshes = node.meshes.map(function (mesh) {
268
+ return _this._convertIdToIndex(mesh, 'mesh');
269
+ });
270
+ }
184
271
  }
185
-
186
- if (node.meshes) {
187
- node.meshes = node.meshes.map(mesh => this._convertIdToIndex(mesh, 'mesh'));
272
+ }, {
273
+ key: "_convertSceneIds",
274
+ value: function _convertSceneIds(scene) {
275
+ var _this2 = this;
276
+
277
+ if (scene.nodes) {
278
+ scene.nodes = scene.nodes.map(function (node) {
279
+ return _this2._convertIdToIndex(node, 'node');
280
+ });
281
+ }
188
282
  }
189
- }
283
+ }, {
284
+ key: "_convertIdsToIndices",
285
+ value: function _convertIdsToIndices(json, topLevelArrayName) {
286
+ if (!json[topLevelArrayName]) {
287
+ console.warn("gltf v1: json doesn't contain attribute ".concat(topLevelArrayName));
288
+ json[topLevelArrayName] = [];
289
+ }
190
290
 
191
- _convertSceneIds(scene) {
192
- if (scene.nodes) {
193
- scene.nodes = scene.nodes.map(node => this._convertIdToIndex(node, 'node'));
194
- }
195
- }
291
+ var _iterator6 = _createForOfIteratorHelper(json[topLevelArrayName]),
292
+ _step6;
196
293
 
197
- _convertIdsToIndices(json, topLevelArrayName) {
198
- if (!json[topLevelArrayName]) {
199
- console.warn("gltf v1: json doesn't contain attribute ".concat(topLevelArrayName));
200
- json[topLevelArrayName] = [];
201
- }
294
+ try {
295
+ for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
296
+ var object = _step6.value;
202
297
 
203
- for (const object of json[topLevelArrayName]) {
204
- for (const key in object) {
205
- const id = object[key];
298
+ for (var key in object) {
299
+ var id = object[key];
206
300
 
207
- const index = this._convertIdToIndex(id, key);
301
+ var index = this._convertIdToIndex(id, key);
208
302
 
209
- object[key] = index;
303
+ object[key] = index;
304
+ }
305
+ }
306
+ } catch (err) {
307
+ _iterator6.e(err);
308
+ } finally {
309
+ _iterator6.f();
210
310
  }
211
311
  }
212
- }
312
+ }, {
313
+ key: "_convertIdToIndex",
314
+ value: function _convertIdToIndex(id, key) {
315
+ var arrayName = GLTF_KEYS[key];
213
316
 
214
- _convertIdToIndex(id, key) {
215
- const arrayName = GLTF_KEYS[key];
317
+ if (arrayName in this.idToIndexMap) {
318
+ var index = this.idToIndexMap[arrayName][id];
216
319
 
217
- if (arrayName in this.idToIndexMap) {
218
- const index = this.idToIndexMap[arrayName][id];
320
+ if (!Number.isFinite(index)) {
321
+ throw new Error("gltf v1: failed to resolve ".concat(key, " with id ").concat(id));
322
+ }
219
323
 
220
- if (!Number.isFinite(index)) {
221
- throw new Error("gltf v1: failed to resolve ".concat(key, " with id ").concat(id));
324
+ return index;
222
325
  }
223
326
 
224
- return index;
327
+ return id;
225
328
  }
226
-
227
- return id;
228
- }
229
-
230
- _updateObjects(json) {
231
- for (const buffer of this.json.buffers) {
232
- delete buffer.type;
329
+ }, {
330
+ key: "_updateObjects",
331
+ value: function _updateObjects(json) {
332
+ var _iterator7 = _createForOfIteratorHelper(this.json.buffers),
333
+ _step7;
334
+
335
+ try {
336
+ for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
337
+ var buffer = _step7.value;
338
+ delete buffer.type;
339
+ }
340
+ } catch (err) {
341
+ _iterator7.e(err);
342
+ } finally {
343
+ _iterator7.f();
344
+ }
233
345
  }
234
- }
235
-
236
- _updateMaterial(json) {
237
- for (const material of json.materials) {
238
- material.pbrMetallicRoughness = {
239
- baseColorFactor: [1, 1, 1, 1],
240
- metallicFactor: 1,
241
- roughnessFactor: 1
242
- };
243
- const textureId = material.values && material.values.tex;
244
- const textureIndex = json.textures.findIndex(texture => texture.id === textureId);
245
-
246
- if (textureIndex !== -1) {
247
- material.pbrMetallicRoughness.baseColorTexture = {
248
- index: textureIndex
346
+ }, {
347
+ key: "_updateMaterial",
348
+ value: function _updateMaterial(json) {
349
+ var _iterator8 = _createForOfIteratorHelper(json.materials),
350
+ _step8;
351
+
352
+ try {
353
+ var _loop = function _loop() {
354
+ var material = _step8.value;
355
+ material.pbrMetallicRoughness = {
356
+ baseColorFactor: [1, 1, 1, 1],
357
+ metallicFactor: 1,
358
+ roughnessFactor: 1
359
+ };
360
+ var textureId = material.values && material.values.tex;
361
+ var textureIndex = json.textures.findIndex(function (texture) {
362
+ return texture.id === textureId;
363
+ });
364
+
365
+ if (textureIndex !== -1) {
366
+ material.pbrMetallicRoughness.baseColorTexture = {
367
+ index: textureIndex
368
+ };
369
+ }
249
370
  };
371
+
372
+ for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
373
+ _loop();
374
+ }
375
+ } catch (err) {
376
+ _iterator8.e(err);
377
+ } finally {
378
+ _iterator8.f();
250
379
  }
251
380
  }
252
- }
253
-
254
- }
381
+ }]);
382
+ return GLTFV1Normalizer;
383
+ }();
255
384
 
256
- function normalizeGLTFV1(gltf, options = {}) {
385
+ function normalizeGLTFV1(gltf) {
386
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
257
387
  return new GLTFV1Normalizer().normalize(gltf, options);
258
388
  }
259
389
  //# sourceMappingURL=normalize-gltf-v1.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../src/lib/api/normalize-gltf-v1.js"],"names":["GLTF_ARRAYS","accessors","animations","buffers","bufferViews","images","materials","meshes","nodes","samplers","scenes","skins","textures","GLTF_KEYS","accessor","buffer","bufferView","image","material","mesh","node","sampler","scene","skin","texture","GLTFV1Normalizer","constructor","gltf","idToIndexMap","normalize","options","json","asset","version","undefined","console","warn","Error","_addAsset","_convertTopLevelObjectsToArrays","KHR_binary_glTF","decode","_convertObjectIdsToArrayIndices","_updateObjects","_updateMaterial","generator","arrayName","_convertTopLevelObjectToArray","mapName","objectMap","Array","isArray","id","object","index","length","push","_convertIdsToIndices","_convertIdToIndex","_convertTextureIds","_convertMeshIds","_convertNodeIds","_convertSceneIds","source","primitive","primitives","attributes","indices","attributeName","children","map","child","topLevelArrayName","key","Number","isFinite","type","pbrMetallicRoughness","baseColorFactor","metallicFactor","roughnessFactor","textureId","values","tex","textureIndex","findIndex","baseColorTexture","normalizeGLTFV1"],"mappings":";;;;;;;AACA;;;;;;AA+CA,MAAMA,WAAW,GAAG;AAClBC,EAAAA,SAAS,EAAE,UADO;AAElBC,EAAAA,UAAU,EAAE,WAFM;AAGlBC,EAAAA,OAAO,EAAE,QAHS;AAIlBC,EAAAA,WAAW,EAAE,YAJK;AAKlBC,EAAAA,MAAM,EAAE,OALU;AAMlBC,EAAAA,SAAS,EAAE,UANO;AAOlBC,EAAAA,MAAM,EAAE,MAPU;AAQlBC,EAAAA,KAAK,EAAE,MARW;AASlBC,EAAAA,QAAQ,EAAE,SATQ;AAUlBC,EAAAA,MAAM,EAAE,OAVU;AAWlBC,EAAAA,KAAK,EAAE,MAXW;AAYlBC,EAAAA,QAAQ,EAAE;AAZQ,CAApB;AAeA,MAAMC,SAAS,GAAG;AAChBC,EAAAA,QAAQ,EAAE,WADM;AAEhBZ,EAAAA,UAAU,EAAE,WAFI;AAGhBa,EAAAA,MAAM,EAAE,SAHQ;AAIhBC,EAAAA,UAAU,EAAE,aAJI;AAKhBC,EAAAA,KAAK,EAAE,QALS;AAMhBC,EAAAA,QAAQ,EAAE,WANM;AAOhBC,EAAAA,IAAI,EAAE,QAPU;AAQhBC,EAAAA,IAAI,EAAE,OARU;AAShBC,EAAAA,OAAO,EAAE,UATO;AAUhBC,EAAA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,MAAT,IAAmBhE,QAAQ,CAACgE,MAAT,CAAgBC,GAArD;AACA,YAAMC,YAAY,GAAGrD,IAAI,CAACnB,QAAL,CAAcyE,SAAd,CAAyB7D,OAAD,IAAaA,OAAO,CAAC4B,EAAR,KAAe6B,SAApD,CAArB;;AACA,UAAIG,YAAY,KAAK,CAAC,CAAtB,EAAyB;AACvBlE,QAAAA,QAAQ,CAAC2D,oBAAT,CAA8BS,gBAA9B,GAAiD;AAAChC,UAAAA,KAAK,EAAE8B;AAAR,SAAjD;AACD;AACF;AACF;;AA9NoB;;AAiOhB,SAASG,eAAT,CAAyB5D,IAAzB,EAA+BG,OAAO,GAAG,EAAzC,EAA6C;AAClD,SAAO,IAAIL,gBAAJ,GAAuBI,SAAvB,CAAiCF,IAAjC,EAAuCG,OAAvC,CAAP;AACD","sourcesContent":["/* eslint-disable camelcase */\nimport * as KHR_binary_glTF from '../extensions/KHR_binary_gltf';\n\n// Binary format changes (mainly implemented by GLBLoader)\n// https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF\n\n// JSON format changes:\n// https://github.com/khronosgroup/gltf/issues/605\n\n// - [x] Top-level JSON objects are arrays now\n// - [ ] Removed indirection from animation: sampler now refers directly to accessors, #712\n// - [ ] material.parameter.value and technique.parameter.value must be an array, #690\n// - [ ] Node can have only one mesh #821\n// - [ ] Added reqs on JSON encoding\n// - [ ] Added reqs on binary data alignment #802 (comment)\n\n// Additions:\n// - [ ] Added accessor.normalized, #691, #706\n// - [ ] Added glExtensionsUsed property and 5125 (UNSIGNED_INT) accessor.componentType value, #619\n// - [ ] Added extensionsRequired property, #720, #721\n// - [ ] Added \"STEP\" as valid animation.sampler.interpolation value, #712\n\n// Removals:\n// - [x] Removed buffer.type, #786, #629\n// - [ ] Removed revision number from profile.version, #709\n// - [ ] Removed technique.functions.scissor and removed 3089 (SCISSOR_TEST) as a valid value for technique.states.enable, #681\n// - [ ] Techniques, programs, and shaders were moved out to KHR_technique_webgl extension.\n\n// Other edits:\n// - [x] asset is now required, #642\n// - [ ] buffer.byteLength and bufferView.byteLength are now required, #560.\n// - [ ] accessor.min and accessor.max are now required, #593, and clarified that the JSON value and binary data must be the same, #628.\n// - [ ] Clarified animation.sampler and animation.channel restrictions, #712\n// - [ ] skin.inverseBindMatrices is now optional, #461.\n// - [ ] Attribute parameters can't have a value defined in the technique or parameter, #563 (comment).\n// - [ ] Only TEXCOORD and COLOR attribute semantics can be written in the form [semantic]_[set_index], #563 (comment).\n// - [ ] TEXCOORD and COLOR attribute semantics must be written in the form [semantic]_[set_index], e.g., just TEXCOORD should be TEXCOORD_0, and just COLOR should be COLOR_0, #649\n// - [ ] camera.perspective.aspectRatio and camera.perspective.yfov must now be > 0, not >= 0, #563 (comment).\n// - [ ] Application-specific parameter semantics must start with an underscore, e.g., _TEMPERATURE and _SIMULATION_TIME, #563 (comment).\n// - [ ] Properties in technique.parameters must be defined in technique.uniforms or technique.attributes,\n\n// #563 (comment).\n// - [ ] technique.parameter.count can only be defined when the semantic is JOINTMATRIX or an application-specific semantic is used. It can never be defined for attribute parameters; only uniforms, d2f6945\n// - [ ] technique.parameter.semantic is required when the parameter is an attribute, 28e113d\n// - [ ] Mesh-only models are allowed, e.g., without materials, #642\n// - [ ] Skeleton hierarchies (nodes containing jointName) must be separated from non-skeleton hierarchies., #647\n// - [ ] technique.states.functions.blendColor and technique.states.functions.depthRange parameters now must match WebGL function min/max, #707\n\nconst GLTF_ARRAYS = {\n accessors: 'accessor',\n animations: 'animation',\n buffers: 'buffer',\n bufferViews: 'bufferView',\n images: 'image',\n materials: 'material',\n meshes: 'mesh',\n nodes: 'node',\n samplers: 'sampler',\n scenes: 'scene',\n skins: 'skin',\n textures: 'texture'\n};\n\nconst GLTF_KEYS = {\n accessor: 'accessors',\n animations: 'animation',\n buffer: 'buffers',\n bufferView: 'bufferViews',\n image: 'images',\n material: 'materials',\n mesh: 'meshes',\n node: 'nodes',\n sampler: 'samplers',\n scene: 'scenes',\n skin: 'skins',\n texture: 'textures'\n};\n\n/**\n * Converts (normalizes) glTF v1 to v2\n */\nclass GLTFV1Normalizer {\n constructor(gltf) {\n this.idToIndexMap = {\n animations: {},\n accessors: {},\n buffers: {},\n bufferViews: {},\n images: {},\n materials: {},\n meshes: {},\n nodes: {},\n samplers: {},\n scenes: {},\n skins: {},\n textures: {}\n };\n }\n\n /**\n * Convert (normalize) glTF < 2.0 to glTF 2.0\n * @param gltf - object with json and binChunks\n * @param options\n * @param options normalize Whether to actually normalize\n */\n normalize(gltf, options) {\n this.json = gltf.json;\n const json = gltf.json;\n\n // Check version\n switch (json.asset && json.asset.version) {\n // We are converting to v2 format. Return if there is nothing to do\n case '2.0':\n return;\n\n // This class is written to convert 1.0\n case undefined:\n case '1.0':\n break;\n\n default:\n // eslint-disable-next-line no-undef, no-console\n console.warn(`glTF: Unknown version ${json.asset.version}`);\n return;\n }\n\n if (!options.normalize) {\n // We are still missing a few conversion tricks, remove once addressed\n throw new Error('glTF v1 is not supported.');\n }\n\n // eslint-disable-next-line no-undef, no-console\n console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');\n\n this._addAsset(json);\n\n // In glTF2 top-level fields are Arrays not Object maps\n this._convertTopLevelObjectsToArrays(json);\n\n // Extract bufferView indices for images\n // (this extension needs to be invoked early in the normalization process)\n KHR_binary_glTF.decode(gltf);\n\n // Convert object references from ids to indices\n this._convertObjectIdsToArrayIndices(json);\n\n this._updateObjects(json);\n\n this._updateMaterial(json);\n }\n\n // asset is now required, #642 https://github.com/KhronosGroup/glTF/issues/639\n _addAsset(json) {\n json.asset = json.asset || {};\n // We are normalizing to glTF v2, so change version to \"2.0\"\n json.asset.version = '2.0';\n json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';\n }\n\n _convertTopLevelObjectsToArrays(json) {\n // TODO check that all arrays are covered\n for (const arrayName in GLTF_ARRAYS) {\n this._convertTopLevelObjectToArray(json, arrayName);\n }\n }\n\n /** Convert one top level object to array */\n _convertTopLevelObjectToArray(json, mapName) {\n const objectMap = json[mapName];\n if (!objectMap || Array.isArray(objectMap)) {\n return;\n }\n\n // Rewrite the top-level field as an array\n json[mapName] = [];\n // Copy the map key into object.id\n for (const id in objectMap) {\n const object = objectMap[id];\n object.id = object.id || id; // Mutates the loaded object\n const index = json[mapName].length;\n json[mapName].push(object);\n this.idToIndexMap[mapName][id] = index;\n }\n }\n\n /** Go through all objects in all top-level arrays and replace ids with indices */\n _convertObjectIdsToArrayIndices(json) {\n for (const arrayName in GLTF_ARRAYS) {\n this._convertIdsToIndices(json, arrayName);\n }\n if ('scene' in json) {\n json.scene = this._convertIdToIndex(json.scene, 'scene');\n }\n\n // Convert any index references that are not using array names\n\n // texture.source (image)\n for (const texture of json.textures) {\n this._convertTextureIds(texture);\n }\n for (const mesh of json.meshes) {\n this._convertMeshIds(mesh);\n }\n for (const node of json.nodes) {\n this._convertNodeIds(node);\n }\n for (const node of json.scenes) {\n this._convertSceneIds(node);\n }\n }\n\n _convertTextureIds(texture) {\n if (texture.source) {\n texture.source = this._convertIdToIndex(texture.source, 'image');\n }\n }\n\n _convertMeshIds(mesh) {\n for (const primitive of mesh.primitives) {\n const {attributes, indices, material} = primitive;\n for (const attributeName in attributes) {\n attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');\n }\n if (indices) {\n primitive.indices = this._convertIdToIndex(indices, 'accessor');\n }\n if (material) {\n primitive.material = this._convertIdToIndex(material, 'material');\n }\n }\n }\n\n _convertNodeIds(node) {\n if (node.children) {\n node.children = node.children.map((child) => this._convertIdToIndex(child, 'node'));\n }\n if (node.meshes) {\n node.meshes = node.meshes.map((mesh) => this._convertIdToIndex(mesh, 'mesh'));\n }\n }\n\n _convertSceneIds(scene) {\n if (scene.nodes) {\n scene.nodes = scene.nodes.map((node) => this._convertIdToIndex(node, 'node'));\n }\n }\n\n /** Go through all objects in a top-level array and replace ids with indices */\n _convertIdsToIndices(json, topLevelArrayName) {\n if (!json[topLevelArrayName]) {\n console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`); // eslint-disable-line no-console, no-undef\n json[topLevelArrayName] = [];\n }\n for (const object of json[topLevelArrayName]) {\n for (const key in object) {\n const id = object[key];\n const index = this._convertIdToIndex(id, key);\n object[key] = index;\n }\n }\n }\n\n _convertIdToIndex(id, key) {\n const arrayName = GLTF_KEYS[key];\n if (arrayName in this.idToIndexMap) {\n const index = this.idToIndexMap[arrayName][id];\n if (!Number.isFinite(index)) {\n throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);\n }\n return index;\n }\n return id;\n }\n\n /**\n *\n * @param {*} json\n */\n _updateObjects(json) {\n for (const buffer of this.json.buffers) {\n // - [x] Removed buffer.type, #786, #629\n delete buffer.type;\n }\n }\n\n /**\n * Update material (set pbrMetallicRoughness)\n * @param {*} json\n */\n _updateMaterial(json) {\n for (const material of json.materials) {\n material.pbrMetallicRoughness = {\n baseColorFactor: [1, 1, 1, 1],\n metallicFactor: 1,\n roughnessFactor: 1\n };\n\n const textureId = material.values && material.values.tex;\n const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);\n if (textureIndex !== -1) {\n material.pbrMetallicRoughness.baseColorTexture = {index: textureIndex};\n }\n }\n }\n}\n\nexport function normalizeGLTFV1(gltf, options = {}) {\n return new GLTFV1Normalizer().normalize(gltf, options);\n}\n"],"file":"normalize-gltf-v1.js"}
1
+ {"version":3,"sources":["../../../../src/lib/api/normalize-gltf-v1.js"],"names":["GLTF_ARRAYS","accessors","animations","buffers","bufferViews","images","materials","meshes","nodes","samplers","scenes","skins","textures","GLTF_KEYS","accessor","buffer","bufferView","image","material","mesh","node","sampler","scene","skin","texture","GLTFV1Normalizer","gltf","idToIndexMap","options","json","asset","version","undefined","console","warn","normalize","Error","_addAsset","_convertTopLevelObjectsToArrays","KHR_binary_glTF","decode","_convertObjectIdsToArrayIndices","_updateObjects","_updateMaterial","generator","arrayName","_convertTopLevelObjectToArray","mapName","objectMap","Array","isArray","id","object","index","length","push","_convertIdsToIndices","_convertIdToIndex","_convertTextureIds","_convertMeshIds","_convertNodeIds","_convertSceneIds","source","primitives","primitive","attributes","indices","attributeName","children","map","child","topLevelArrayName","key","Number","isFinite","type","pbrMetallicRoughness","baseColorFactor","metallicFactor","roughnessFactor","textureId","values","tex","textureIndex","findIndex","baseColorTexture","normalizeGLTFV1"],"mappings":";;;;;;;;;;;;;;;AACA;;;;;;;;;;;;AA+CA,IAAMA,WAAW,GAAG;AAClBC,EAAAA,SAAS,EAAE,UADO;AAElBC,EAAAA,UAAU,EAAE,WAFM;AAGlBC,EAAAA,OAAO,EAAE,QAHS;AAIlBC,EAAAA,WAAW,EAAE,YAJK;AAKlBC,EAAAA,MAAM,EAAE,OALU;AAMlBC,EAAAA,SAAS,EAAE,UANO;AAOlBC,EAAAA,MAAM,EAAE,MAPU;AAQlBC,EAAAA,KAAK,EAAE,MARW;AASlBC,EAAAA,QAAQ,EAAE,SATQ;AAUlBC,EAAAA,MAAM,EAAE,OAVU;AAWlBC,EAAAA,KAAK,EAAE,MAXW;AAYlBC,EAAAA,QAAQ,EAAE;AAZQ,CAApB;AAeA,IAAMC,SAAS,GAAG;AAChBC,EAAAA,QAAQ,EAAE,WADM;AAEhBZ,EAAAA,UAAU,EAAE,WAFI;AAGhBa,EAAAA,MAAM,EAAE,SAHQ;AAIhBC,EAAAA,UAAU,EAAE,aAJI;AAKhBC,EAAAA,KAAK,EAAE,QALS;AAMhBC,EAAAA,QAAQ,EAAE,WANM;AAOhBC,EAAAA,IAAI,EAAE,QAPU;AAQhBC,EAAAA,IAAI,EAAE,OARU;AAShBC,EAAAA,OAAO,EAAE,UATO;AAUhBC,EAAAA,KAAK,EAAE,QAVS;AAWhBC,EAAAA,IAAI,EAAE,OAXU;AAYhBC,EAAAA,OAAO,EAAE;AAZO,CAAlB;;IAkBMC,gB;AACJ,4BAAYC,IAAZ,EAAkB;AAAA;AAChB,SAAKC,YAAL,GAAoB;AAClBzB,MAAAA,UAAU,EAAE,EADM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BA,UAAAA,QAAQ,CAAC0D,oBAAT,GAAgC;AAC9BC,YAAAA,eAAe,EAAE,CAAC,CAAD,EAAI,CAAJ,EAAO,CAAP,EAAU,CAAV,CADa;AAE9BC,YAAAA,cAAc,EAAE,CAFc;AAG9BC,YAAAA,eAAe,EAAE;AAHa,WAAhC;AAMA,cAAMC,SAAS,GAAG9D,QAAQ,CAAC+D,MAAT,IAAmB/D,QAAQ,CAAC+D,MAAT,CAAgBC,GAArD;AACA,cAAMC,YAAY,GAAGtD,IAAI,CAACjB,QAAL,CAAcwE,SAAd,CAAwB,UAAC5D,OAAD;AAAA,mBAAaA,OAAO,CAAC2B,EAAR,KAAe6B,SAA5B;AAAA,WAAxB,CAArB;;AACA,cAAIG,YAAY,KAAK,CAAC,CAAtB,EAAyB;AACvBjE,YAAAA,QAAQ,CAAC0D,oBAAT,CAA8BS,gBAA9B,GAAiD;AAAChC,cAAAA,KAAK,EAAE8B;AAAR,aAAjD;AACD;AAZiB;;AACpB,+DAAuC;AAAA;AAYtC;AAbmB;AAAA;AAAA;AAAA;AAAA;AAcrB;;;;;AAGI,SAASG,eAAT,CAAyB5D,IAAzB,EAA6C;AAAA,MAAdE,OAAc,uEAAJ,EAAI;AAClD,SAAO,IAAIH,gBAAJ,GAAuBU,SAAvB,CAAiCT,IAAjC,EAAuCE,OAAvC,CAAP;AACD","sourcesContent":["/* eslint-disable camelcase */\nimport * as KHR_binary_glTF from '../extensions/KHR_binary_gltf';\n\n// Binary format changes (mainly implemented by GLBLoader)\n// https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF\n\n// JSON format changes:\n// https://github.com/khronosgroup/gltf/issues/605\n\n// - [x] Top-level JSON objects are arrays now\n// - [ ] Removed indirection from animation: sampler now refers directly to accessors, #712\n// - [ ] material.parameter.value and technique.parameter.value must be an array, #690\n// - [ ] Node can have only one mesh #821\n// - [ ] Added reqs on JSON encoding\n// - [ ] Added reqs on binary data alignment #802 (comment)\n\n// Additions:\n// - [ ] Added accessor.normalized, #691, #706\n// - [ ] Added glExtensionsUsed property and 5125 (UNSIGNED_INT) accessor.componentType value, #619\n// - [ ] Added extensionsRequired property, #720, #721\n// - [ ] Added \"STEP\" as valid animation.sampler.interpolation value, #712\n\n// Removals:\n// - [x] Removed buffer.type, #786, #629\n// - [ ] Removed revision number from profile.version, #709\n// - [ ] Removed technique.functions.scissor and removed 3089 (SCISSOR_TEST) as a valid value for technique.states.enable, #681\n// - [ ] Techniques, programs, and shaders were moved out to KHR_technique_webgl extension.\n\n// Other edits:\n// - [x] asset is now required, #642\n// - [ ] buffer.byteLength and bufferView.byteLength are now required, #560.\n// - [ ] accessor.min and accessor.max are now required, #593, and clarified that the JSON value and binary data must be the same, #628.\n// - [ ] Clarified animation.sampler and animation.channel restrictions, #712\n// - [ ] skin.inverseBindMatrices is now optional, #461.\n// - [ ] Attribute parameters can't have a value defined in the technique or parameter, #563 (comment).\n// - [ ] Only TEXCOORD and COLOR attribute semantics can be written in the form [semantic]_[set_index], #563 (comment).\n// - [ ] TEXCOORD and COLOR attribute semantics must be written in the form [semantic]_[set_index], e.g., just TEXCOORD should be TEXCOORD_0, and just COLOR should be COLOR_0, #649\n// - [ ] camera.perspective.aspectRatio and camera.perspective.yfov must now be > 0, not >= 0, #563 (comment).\n// - [ ] Application-specific parameter semantics must start with an underscore, e.g., _TEMPERATURE and _SIMULATION_TIME, #563 (comment).\n// - [ ] Properties in technique.parameters must be defined in technique.uniforms or technique.attributes,\n\n// #563 (comment).\n// - [ ] technique.parameter.count can only be defined when the semantic is JOINTMATRIX or an application-specific semantic is used. It can never be defined for attribute parameters; only uniforms, d2f6945\n// - [ ] technique.parameter.semantic is required when the parameter is an attribute, 28e113d\n// - [ ] Mesh-only models are allowed, e.g., without materials, #642\n// - [ ] Skeleton hierarchies (nodes containing jointName) must be separated from non-skeleton hierarchies., #647\n// - [ ] technique.states.functions.blendColor and technique.states.functions.depthRange parameters now must match WebGL function min/max, #707\n\nconst GLTF_ARRAYS = {\n accessors: 'accessor',\n animations: 'animation',\n buffers: 'buffer',\n bufferViews: 'bufferView',\n images: 'image',\n materials: 'material',\n meshes: 'mesh',\n nodes: 'node',\n samplers: 'sampler',\n scenes: 'scene',\n skins: 'skin',\n textures: 'texture'\n};\n\nconst GLTF_KEYS = {\n accessor: 'accessors',\n animations: 'animation',\n buffer: 'buffers',\n bufferView: 'bufferViews',\n image: 'images',\n material: 'materials',\n mesh: 'meshes',\n node: 'nodes',\n sampler: 'samplers',\n scene: 'scenes',\n skin: 'skins',\n texture: 'textures'\n};\n\n/**\n * Converts (normalizes) glTF v1 to v2\n */\nclass GLTFV1Normalizer {\n constructor(gltf) {\n this.idToIndexMap = {\n animations: {},\n accessors: {},\n buffers: {},\n bufferViews: {},\n images: {},\n materials: {},\n meshes: {},\n nodes: {},\n samplers: {},\n scenes: {},\n skins: {},\n textures: {}\n };\n }\n\n /**\n * Convert (normalize) glTF < 2.0 to glTF 2.0\n * @param gltf - object with json and binChunks\n * @param options\n * @param options normalize Whether to actually normalize\n */\n normalize(gltf, options) {\n this.json = gltf.json;\n const json = gltf.json;\n\n // Check version\n switch (json.asset && json.asset.version) {\n // We are converting to v2 format. Return if there is nothing to do\n case '2.0':\n return;\n\n // This class is written to convert 1.0\n case undefined:\n case '1.0':\n break;\n\n default:\n // eslint-disable-next-line no-undef, no-console\n console.warn(`glTF: Unknown version ${json.asset.version}`);\n return;\n }\n\n if (!options.normalize) {\n // We are still missing a few conversion tricks, remove once addressed\n throw new Error('glTF v1 is not supported.');\n }\n\n // eslint-disable-next-line no-undef, no-console\n console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');\n\n this._addAsset(json);\n\n // In glTF2 top-level fields are Arrays not Object maps\n this._convertTopLevelObjectsToArrays(json);\n\n // Extract bufferView indices for images\n // (this extension needs to be invoked early in the normalization process)\n KHR_binary_glTF.decode(gltf);\n\n // Convert object references from ids to indices\n this._convertObjectIdsToArrayIndices(json);\n\n this._updateObjects(json);\n\n this._updateMaterial(json);\n }\n\n // asset is now required, #642 https://github.com/KhronosGroup/glTF/issues/639\n _addAsset(json) {\n json.asset = json.asset || {};\n // We are normalizing to glTF v2, so change version to \"2.0\"\n json.asset.version = '2.0';\n json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';\n }\n\n _convertTopLevelObjectsToArrays(json) {\n // TODO check that all arrays are covered\n for (const arrayName in GLTF_ARRAYS) {\n this._convertTopLevelObjectToArray(json, arrayName);\n }\n }\n\n /** Convert one top level object to array */\n _convertTopLevelObjectToArray(json, mapName) {\n const objectMap = json[mapName];\n if (!objectMap || Array.isArray(objectMap)) {\n return;\n }\n\n // Rewrite the top-level field as an array\n json[mapName] = [];\n // Copy the map key into object.id\n for (const id in objectMap) {\n const object = objectMap[id];\n object.id = object.id || id; // Mutates the loaded object\n const index = json[mapName].length;\n json[mapName].push(object);\n this.idToIndexMap[mapName][id] = index;\n }\n }\n\n /** Go through all objects in all top-level arrays and replace ids with indices */\n _convertObjectIdsToArrayIndices(json) {\n for (const arrayName in GLTF_ARRAYS) {\n this._convertIdsToIndices(json, arrayName);\n }\n if ('scene' in json) {\n json.scene = this._convertIdToIndex(json.scene, 'scene');\n }\n\n // Convert any index references that are not using array names\n\n // texture.source (image)\n for (const texture of json.textures) {\n this._convertTextureIds(texture);\n }\n for (const mesh of json.meshes) {\n this._convertMeshIds(mesh);\n }\n for (const node of json.nodes) {\n this._convertNodeIds(node);\n }\n for (const node of json.scenes) {\n this._convertSceneIds(node);\n }\n }\n\n _convertTextureIds(texture) {\n if (texture.source) {\n texture.source = this._convertIdToIndex(texture.source, 'image');\n }\n }\n\n _convertMeshIds(mesh) {\n for (const primitive of mesh.primitives) {\n const {attributes, indices, material} = primitive;\n for (const attributeName in attributes) {\n attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');\n }\n if (indices) {\n primitive.indices = this._convertIdToIndex(indices, 'accessor');\n }\n if (material) {\n primitive.material = this._convertIdToIndex(material, 'material');\n }\n }\n }\n\n _convertNodeIds(node) {\n if (node.children) {\n node.children = node.children.map((child) => this._convertIdToIndex(child, 'node'));\n }\n if (node.meshes) {\n node.meshes = node.meshes.map((mesh) => this._convertIdToIndex(mesh, 'mesh'));\n }\n }\n\n _convertSceneIds(scene) {\n if (scene.nodes) {\n scene.nodes = scene.nodes.map((node) => this._convertIdToIndex(node, 'node'));\n }\n }\n\n /** Go through all objects in a top-level array and replace ids with indices */\n _convertIdsToIndices(json, topLevelArrayName) {\n if (!json[topLevelArrayName]) {\n console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`); // eslint-disable-line no-console, no-undef\n json[topLevelArrayName] = [];\n }\n for (const object of json[topLevelArrayName]) {\n for (const key in object) {\n const id = object[key];\n const index = this._convertIdToIndex(id, key);\n object[key] = index;\n }\n }\n }\n\n _convertIdToIndex(id, key) {\n const arrayName = GLTF_KEYS[key];\n if (arrayName in this.idToIndexMap) {\n const index = this.idToIndexMap[arrayName][id];\n if (!Number.isFinite(index)) {\n throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);\n }\n return index;\n }\n return id;\n }\n\n /**\n *\n * @param {*} json\n */\n _updateObjects(json) {\n for (const buffer of this.json.buffers) {\n // - [x] Removed buffer.type, #786, #629\n delete buffer.type;\n }\n }\n\n /**\n * Update material (set pbrMetallicRoughness)\n * @param {*} json\n */\n _updateMaterial(json) {\n for (const material of json.materials) {\n material.pbrMetallicRoughness = {\n baseColorFactor: [1, 1, 1, 1],\n metallicFactor: 1,\n roughnessFactor: 1\n };\n\n const textureId = material.values && material.values.tex;\n const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);\n if (textureIndex !== -1) {\n material.pbrMetallicRoughness.baseColorTexture = {index: textureIndex};\n }\n }\n }\n}\n\nexport function normalizeGLTFV1(gltf, options = {}) {\n return new GLTFV1Normalizer().normalize(gltf, options);\n}\n"],"file":"normalize-gltf-v1.js"}