@loaders.gl/gltf 4.2.0-alpha.3 → 4.2.0-alpha.5

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 (148) hide show
  1. package/dist/dist.dev.js +1049 -517
  2. package/dist/dist.min.js +9 -0
  3. package/dist/glb-loader.d.ts +2 -2
  4. package/dist/glb-loader.d.ts.map +1 -1
  5. package/dist/glb-loader.js +22 -21
  6. package/dist/glb-writer.d.ts +2 -2
  7. package/dist/glb-writer.d.ts.map +1 -1
  8. package/dist/glb-writer.js +27 -24
  9. package/dist/gltf-loader.d.ts +3 -3
  10. package/dist/gltf-loader.d.ts.map +1 -1
  11. package/dist/gltf-loader.js +31 -36
  12. package/dist/gltf-writer.js +24 -26
  13. package/dist/index.cjs +95 -284
  14. package/dist/index.cjs.map +7 -0
  15. package/dist/index.d.ts +17 -17
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +3 -1
  18. package/dist/lib/api/gltf-extensions.d.ts +2 -2
  19. package/dist/lib/api/gltf-extensions.d.ts.map +1 -1
  20. package/dist/lib/api/gltf-extensions.js +45 -22
  21. package/dist/lib/api/gltf-scenegraph.d.ts +2 -2
  22. package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -1
  23. package/dist/lib/api/gltf-scenegraph.js +561 -438
  24. package/dist/lib/api/normalize-gltf-v1.js +250 -181
  25. package/dist/lib/api/post-process-gltf.d.ts +3 -3
  26. package/dist/lib/api/post-process-gltf.d.ts.map +1 -1
  27. package/dist/lib/api/post-process-gltf.js +375 -339
  28. package/dist/lib/encoders/encode-glb.js +62 -48
  29. package/dist/lib/encoders/encode-gltf.js +24 -10
  30. package/dist/lib/extensions/EXT_mesh_features.d.ts +2 -2
  31. package/dist/lib/extensions/EXT_mesh_features.d.ts.map +1 -1
  32. package/dist/lib/extensions/EXT_mesh_features.js +55 -33
  33. package/dist/lib/extensions/EXT_meshopt_compression.d.ts +2 -2
  34. package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -1
  35. package/dist/lib/extensions/EXT_meshopt_compression.js +27 -31
  36. package/dist/lib/extensions/EXT_structural_metadata.d.ts +2 -2
  37. package/dist/lib/extensions/EXT_structural_metadata.d.ts.map +1 -1
  38. package/dist/lib/extensions/EXT_structural_metadata.js +434 -230
  39. package/dist/lib/extensions/EXT_texture_webp.d.ts +2 -2
  40. package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -1
  41. package/dist/lib/extensions/EXT_texture_webp.js +24 -17
  42. package/dist/lib/extensions/KHR_binary_gltf.d.ts +1 -1
  43. package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -1
  44. package/dist/lib/extensions/KHR_binary_gltf.js +29 -15
  45. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +2 -2
  46. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -1
  47. package/dist/lib/extensions/KHR_draco_mesh_compression.js +110 -87
  48. package/dist/lib/extensions/KHR_texture_basisu.d.ts +2 -2
  49. package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -1
  50. package/dist/lib/extensions/KHR_texture_basisu.js +19 -12
  51. package/dist/lib/extensions/KHR_texture_transform.d.ts +2 -2
  52. package/dist/lib/extensions/KHR_texture_transform.d.ts.map +1 -1
  53. package/dist/lib/extensions/KHR_texture_transform.js +194 -154
  54. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts +2 -2
  55. package/dist/lib/extensions/deprecated/EXT_feature_metadata.d.ts.map +1 -1
  56. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js +263 -143
  57. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +1 -1
  58. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -1
  59. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +44 -32
  60. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +1 -1
  61. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -1
  62. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +30 -24
  63. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +1 -1
  64. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -1
  65. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +65 -52
  66. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts +2 -2
  67. package/dist/lib/extensions/utils/3d-tiles-utils.d.ts.map +1 -1
  68. package/dist/lib/extensions/utils/3d-tiles-utils.js +298 -181
  69. package/dist/lib/gltf-utils/get-typed-array.d.ts +1 -1
  70. package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -1
  71. package/dist/lib/gltf-utils/get-typed-array.js +54 -42
  72. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +1 -1
  73. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -1
  74. package/dist/lib/gltf-utils/gltf-attribute-utils.js +58 -52
  75. package/dist/lib/gltf-utils/gltf-constants.js +27 -27
  76. package/dist/lib/gltf-utils/gltf-utils.d.ts +1 -1
  77. package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -1
  78. package/dist/lib/gltf-utils/gltf-utils.js +67 -60
  79. package/dist/lib/gltf-utils/resolve-url.js +12 -10
  80. package/dist/lib/parsers/parse-glb.d.ts +1 -1
  81. package/dist/lib/parsers/parse-glb.d.ts.map +1 -1
  82. package/dist/lib/parsers/parse-glb.js +132 -89
  83. package/dist/lib/parsers/parse-gltf.d.ts +3 -3
  84. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -1
  85. package/dist/lib/parsers/parse-gltf.js +155 -126
  86. package/dist/lib/types/glb-types.js +0 -1
  87. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts +1 -1
  88. package/dist/lib/types/gltf-ext-feature-metadata-schema.d.ts.map +1 -1
  89. package/dist/lib/types/gltf-ext-feature-metadata-schema.js +0 -1
  90. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts +1 -1
  91. package/dist/lib/types/gltf-ext-mesh-features-schema.d.ts.map +1 -1
  92. package/dist/lib/types/gltf-ext-mesh-features-schema.js +0 -1
  93. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts +1 -1
  94. package/dist/lib/types/gltf-ext-structural-metadata-schema.d.ts.map +1 -1
  95. package/dist/lib/types/gltf-ext-structural-metadata-schema.js +0 -1
  96. package/dist/lib/types/gltf-json-schema.js +2 -1
  97. package/dist/lib/types/gltf-postprocessed-schema.js +2 -1
  98. package/dist/lib/types/gltf-types.d.ts +3 -3
  99. package/dist/lib/types/gltf-types.d.ts.map +1 -1
  100. package/dist/lib/types/gltf-types.js +1 -1
  101. package/dist/lib/utils/assert.js +6 -4
  102. package/dist/lib/utils/version.js +4 -2
  103. package/dist/meshopt/meshopt-decoder.js +86 -67
  104. package/dist/webp/webp.js +28 -19
  105. package/package.json +12 -8
  106. package/dist/glb-loader.js.map +0 -1
  107. package/dist/glb-writer.js.map +0 -1
  108. package/dist/gltf-loader.js.map +0 -1
  109. package/dist/gltf-writer.js.map +0 -1
  110. package/dist/index.js.map +0 -1
  111. package/dist/lib/api/gltf-extensions.js.map +0 -1
  112. package/dist/lib/api/gltf-scenegraph.js.map +0 -1
  113. package/dist/lib/api/normalize-gltf-v1.js.map +0 -1
  114. package/dist/lib/api/post-process-gltf.js.map +0 -1
  115. package/dist/lib/encoders/encode-glb.js.map +0 -1
  116. package/dist/lib/encoders/encode-gltf.js.map +0 -1
  117. package/dist/lib/extensions/EXT_mesh_features.js.map +0 -1
  118. package/dist/lib/extensions/EXT_meshopt_compression.js.map +0 -1
  119. package/dist/lib/extensions/EXT_structural_metadata.js.map +0 -1
  120. package/dist/lib/extensions/EXT_texture_webp.js.map +0 -1
  121. package/dist/lib/extensions/KHR_binary_gltf.js.map +0 -1
  122. package/dist/lib/extensions/KHR_draco_mesh_compression.js.map +0 -1
  123. package/dist/lib/extensions/KHR_texture_basisu.js.map +0 -1
  124. package/dist/lib/extensions/KHR_texture_transform.js.map +0 -1
  125. package/dist/lib/extensions/deprecated/EXT_feature_metadata.js.map +0 -1
  126. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js.map +0 -1
  127. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js.map +0 -1
  128. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js.map +0 -1
  129. package/dist/lib/extensions/utils/3d-tiles-utils.js.map +0 -1
  130. package/dist/lib/gltf-utils/get-typed-array.js.map +0 -1
  131. package/dist/lib/gltf-utils/gltf-attribute-utils.js.map +0 -1
  132. package/dist/lib/gltf-utils/gltf-constants.js.map +0 -1
  133. package/dist/lib/gltf-utils/gltf-utils.js.map +0 -1
  134. package/dist/lib/gltf-utils/resolve-url.js.map +0 -1
  135. package/dist/lib/parsers/parse-glb.js.map +0 -1
  136. package/dist/lib/parsers/parse-gltf.js.map +0 -1
  137. package/dist/lib/types/glb-types.js.map +0 -1
  138. package/dist/lib/types/gltf-ext-feature-metadata-schema.js.map +0 -1
  139. package/dist/lib/types/gltf-ext-mesh-features-schema.js.map +0 -1
  140. package/dist/lib/types/gltf-ext-structural-metadata-schema.js.map +0 -1
  141. package/dist/lib/types/gltf-json-schema.js.map +0 -1
  142. package/dist/lib/types/gltf-postprocessed-schema.js.map +0 -1
  143. package/dist/lib/types/gltf-types.js.map +0 -1
  144. package/dist/lib/utils/assert.js.map +0 -1
  145. package/dist/lib/utils/version.js.map +0 -1
  146. package/dist/meshopt/meshopt-decoder.js.map +0 -1
  147. package/dist/meshopt/meshopt-encoder.ts.disabled +0 -409
  148. package/dist/webp/webp.js.map +0 -1
@@ -1,447 +1,570 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { getBinaryImageMetadata } from '@loaders.gl/images';
2
5
  import { padToNBytes, copyToArray } from '@loaders.gl/loader-utils';
3
6
  import { assert } from "../utils/assert.js";
4
7
  import { getAccessorTypeFromSize, getComponentTypeFromArray } from "../gltf-utils/gltf-utils.js";
5
8
  import { getTypedArrayForAccessor as _getTypedArrayForAccessor } from "../gltf-utils/get-typed-array.js";
6
9
  function makeDefaultGLTFJson() {
7
- return {
8
- asset: {
9
- version: '2.0',
10
- generator: 'loaders.gl'
11
- },
12
- buffers: [],
13
- extensions: {},
14
- extensionsRequired: [],
15
- extensionsUsed: []
16
- };
10
+ return {
11
+ asset: {
12
+ version: '2.0',
13
+ generator: 'loaders.gl'
14
+ },
15
+ buffers: [],
16
+ extensions: {},
17
+ extensionsRequired: [],
18
+ extensionsUsed: []
19
+ };
17
20
  }
21
+ /**
22
+ * Class for structured access to GLTF data
23
+ */
18
24
  export class GLTFScenegraph {
19
- constructor(gltf) {
20
- this.gltf = void 0;
21
- this.sourceBuffers = void 0;
22
- this.byteLength = void 0;
23
- this.gltf = {
24
- json: (gltf === null || gltf === void 0 ? void 0 : gltf.json) || makeDefaultGLTFJson(),
25
- buffers: (gltf === null || gltf === void 0 ? void 0 : gltf.buffers) || [],
26
- images: (gltf === null || gltf === void 0 ? void 0 : gltf.images) || []
27
- };
28
- this.sourceBuffers = [];
29
- this.byteLength = 0;
30
- if (this.gltf.buffers && this.gltf.buffers[0]) {
31
- this.byteLength = this.gltf.buffers[0].byteLength;
32
- this.sourceBuffers = [this.gltf.buffers[0]];
33
- }
34
- }
35
- get json() {
36
- return this.gltf.json;
37
- }
38
- getApplicationData(key) {
39
- const data = this.json[key];
40
- return data;
41
- }
42
- getExtraData(key) {
43
- const extras = this.json.extras || {};
44
- return extras[key];
45
- }
46
- hasExtension(extensionName) {
47
- const isUsedExtension = this.getUsedExtensions().find(name => name === extensionName);
48
- const isRequiredExtension = this.getRequiredExtensions().find(name => name === extensionName);
49
- return typeof isUsedExtension === 'string' || typeof isRequiredExtension === 'string';
50
- }
51
- getExtension(extensionName) {
52
- const isExtension = this.getUsedExtensions().find(name => name === extensionName);
53
- const extensions = this.json.extensions || {};
54
- return isExtension ? extensions[extensionName] : null;
55
- }
56
- getRequiredExtension(extensionName) {
57
- const isRequired = this.getRequiredExtensions().find(name => name === extensionName);
58
- return isRequired ? this.getExtension(extensionName) : null;
59
- }
60
- getRequiredExtensions() {
61
- return this.json.extensionsRequired || [];
62
- }
63
- getUsedExtensions() {
64
- return this.json.extensionsUsed || [];
65
- }
66
- getRemovedExtensions() {
67
- return this.json.extensionsRemoved || [];
68
- }
69
- getObjectExtension(object, extensionName) {
70
- const extensions = object.extensions || {};
71
- return extensions[extensionName];
72
- }
73
- getScene(index) {
74
- return this.getObject('scenes', index);
75
- }
76
- getNode(index) {
77
- return this.getObject('nodes', index);
78
- }
79
- getSkin(index) {
80
- return this.getObject('skins', index);
81
- }
82
- getMesh(index) {
83
- return this.getObject('meshes', index);
84
- }
85
- getMaterial(index) {
86
- return this.getObject('materials', index);
87
- }
88
- getAccessor(index) {
89
- return this.getObject('accessors', index);
90
- }
91
- getTexture(index) {
92
- return this.getObject('textures', index);
93
- }
94
- getSampler(index) {
95
- return this.getObject('samplers', index);
96
- }
97
- getImage(index) {
98
- return this.getObject('images', index);
99
- }
100
- getBufferView(index) {
101
- return this.getObject('bufferViews', index);
102
- }
103
- getBuffer(index) {
104
- return this.getObject('buffers', index);
105
- }
106
- getObject(array, index) {
107
- if (typeof index === 'object') {
108
- return index;
109
- }
110
- const object = this.json[array] && this.json[array][index];
111
- if (!object) {
112
- throw new Error(`glTF file error: Could not find ${array}[${index}]`);
113
- }
114
- return object;
115
- }
116
- getTypedArrayForBufferView(bufferView) {
117
- bufferView = this.getBufferView(bufferView);
118
- const bufferIndex = bufferView.buffer;
119
- const binChunk = this.gltf.buffers[bufferIndex];
120
- assert(binChunk);
121
- const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
122
- return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
123
- }
124
- getTypedArrayForAccessor(accessor) {
125
- const gltfAccessor = this.getAccessor(accessor);
126
- return _getTypedArrayForAccessor(this.gltf.json, this.gltf.buffers, gltfAccessor);
127
- }
128
- getTypedArrayForImageData(image) {
129
- image = this.getAccessor(image);
130
- const bufferView = this.getBufferView(image.bufferView);
131
- const buffer = this.getBuffer(bufferView.buffer);
132
- const arrayBuffer = buffer.data;
133
- const byteOffset = bufferView.byteOffset || 0;
134
- return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
135
- }
136
- addApplicationData(key, data) {
137
- this.json[key] = data;
138
- return this;
139
- }
140
- addExtraData(key, data) {
141
- this.json.extras = this.json.extras || {};
142
- this.json.extras[key] = data;
143
- return this;
144
- }
145
- addObjectExtension(object, extensionName, data) {
146
- object.extensions = object.extensions || {};
147
- object.extensions[extensionName] = data;
148
- this.registerUsedExtension(extensionName);
149
- return this;
150
- }
151
- setObjectExtension(object, extensionName, data) {
152
- const extensions = object.extensions || {};
153
- extensions[extensionName] = data;
154
- }
155
- removeObjectExtension(object, extensionName) {
156
- const extensions = (object === null || object === void 0 ? void 0 : object.extensions) || {};
157
- if (extensions[extensionName]) {
158
- this.json.extensionsRemoved = this.json.extensionsRemoved || [];
159
- const extensionsRemoved = this.json.extensionsRemoved;
160
- if (!extensionsRemoved.includes(extensionName)) {
161
- extensionsRemoved.push(extensionName);
162
- }
163
- }
164
- delete extensions[extensionName];
165
- }
166
- addExtension(extensionName) {
167
- let extensionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
168
- assert(extensionData);
169
- this.json.extensions = this.json.extensions || {};
170
- this.json.extensions[extensionName] = extensionData;
171
- this.registerUsedExtension(extensionName);
172
- return extensionData;
173
- }
174
- addRequiredExtension(extensionName) {
175
- let extensionData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
176
- assert(extensionData);
177
- this.addExtension(extensionName, extensionData);
178
- this.registerRequiredExtension(extensionName);
179
- return extensionData;
180
- }
181
- registerUsedExtension(extensionName) {
182
- this.json.extensionsUsed = this.json.extensionsUsed || [];
183
- if (!this.json.extensionsUsed.find(ext => ext === extensionName)) {
184
- this.json.extensionsUsed.push(extensionName);
185
- }
186
- }
187
- registerRequiredExtension(extensionName) {
188
- this.registerUsedExtension(extensionName);
189
- this.json.extensionsRequired = this.json.extensionsRequired || [];
190
- if (!this.json.extensionsRequired.find(ext => ext === extensionName)) {
191
- this.json.extensionsRequired.push(extensionName);
192
- }
193
- }
194
- removeExtension(extensionName) {
195
- var _this$json$extensions;
196
- if ((_this$json$extensions = this.json.extensions) !== null && _this$json$extensions !== void 0 && _this$json$extensions[extensionName]) {
197
- this.json.extensionsRemoved = this.json.extensionsRemoved || [];
198
- const extensionsRemoved = this.json.extensionsRemoved;
199
- if (!extensionsRemoved.includes(extensionName)) {
200
- extensionsRemoved.push(extensionName);
201
- }
202
- }
203
- if (this.json.extensions) {
204
- delete this.json.extensions[extensionName];
205
- }
206
- if (this.json.extensionsRequired) {
207
- this._removeStringFromArray(this.json.extensionsRequired, extensionName);
208
- }
209
- if (this.json.extensionsUsed) {
210
- this._removeStringFromArray(this.json.extensionsUsed, extensionName);
211
- }
212
- }
213
- setDefaultScene(sceneIndex) {
214
- this.json.scene = sceneIndex;
215
- }
216
- addScene(scene) {
217
- const {
218
- nodeIndices
219
- } = scene;
220
- this.json.scenes = this.json.scenes || [];
221
- this.json.scenes.push({
222
- nodes: nodeIndices
223
- });
224
- return this.json.scenes.length - 1;
225
- }
226
- addNode(node) {
227
- const {
228
- meshIndex,
229
- matrix
230
- } = node;
231
- this.json.nodes = this.json.nodes || [];
232
- const nodeData = {
233
- mesh: meshIndex
234
- };
235
- if (matrix) {
236
- nodeData.matrix = matrix;
237
- }
238
- this.json.nodes.push(nodeData);
239
- return this.json.nodes.length - 1;
240
- }
241
- addMesh(mesh) {
242
- const {
243
- attributes,
244
- indices,
245
- material,
246
- mode = 4
247
- } = mesh;
248
- const accessors = this._addAttributes(attributes);
249
- const glTFMesh = {
250
- primitives: [{
251
- attributes: accessors,
252
- mode
253
- }]
254
- };
255
- if (indices) {
256
- const indicesAccessor = this._addIndices(indices);
257
- glTFMesh.primitives[0].indices = indicesAccessor;
258
- }
259
- if (Number.isFinite(material)) {
260
- glTFMesh.primitives[0].material = material;
261
- }
262
- this.json.meshes = this.json.meshes || [];
263
- this.json.meshes.push(glTFMesh);
264
- return this.json.meshes.length - 1;
265
- }
266
- addPointCloud(attributes) {
267
- const accessorIndices = this._addAttributes(attributes);
268
- const glTFMesh = {
269
- primitives: [{
270
- attributes: accessorIndices,
271
- mode: 0
272
- }]
273
- };
274
- this.json.meshes = this.json.meshes || [];
275
- this.json.meshes.push(glTFMesh);
276
- return this.json.meshes.length - 1;
277
- }
278
- addImage(imageData, mimeTypeOpt) {
279
- const metadata = getBinaryImageMetadata(imageData);
280
- const mimeType = mimeTypeOpt || (metadata === null || metadata === void 0 ? void 0 : metadata.mimeType);
281
- const bufferViewIndex = this.addBufferView(imageData);
282
- const glTFImage = {
283
- bufferView: bufferViewIndex,
284
- mimeType
285
- };
286
- this.json.images = this.json.images || [];
287
- this.json.images.push(glTFImage);
288
- return this.json.images.length - 1;
289
- }
290
- addBufferView(buffer) {
291
- let bufferIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
292
- let byteOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.byteLength;
293
- const byteLength = buffer.byteLength;
294
- assert(Number.isFinite(byteLength));
295
- this.sourceBuffers = this.sourceBuffers || [];
296
- this.sourceBuffers.push(buffer);
297
- const glTFBufferView = {
298
- buffer: bufferIndex,
299
- byteOffset,
300
- byteLength
301
- };
302
- this.byteLength += padToNBytes(byteLength, 4);
303
- this.json.bufferViews = this.json.bufferViews || [];
304
- this.json.bufferViews.push(glTFBufferView);
305
- return this.json.bufferViews.length - 1;
306
- }
307
- addAccessor(bufferViewIndex, accessor) {
308
- const glTFAccessor = {
309
- bufferView: bufferViewIndex,
310
- type: getAccessorTypeFromSize(accessor.size),
311
- componentType: accessor.componentType,
312
- count: accessor.count,
313
- max: accessor.max,
314
- min: accessor.min
315
- };
316
- this.json.accessors = this.json.accessors || [];
317
- this.json.accessors.push(glTFAccessor);
318
- return this.json.accessors.length - 1;
319
- }
320
- addBinaryBuffer(sourceBuffer) {
321
- let accessor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
322
- size: 3
323
- };
324
- const bufferViewIndex = this.addBufferView(sourceBuffer);
325
- let minMax = {
326
- min: accessor.min,
327
- max: accessor.max
328
- };
329
- if (!minMax.min || !minMax.max) {
330
- minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
331
- }
332
- const accessorDefaults = {
333
- size: accessor.size,
334
- componentType: getComponentTypeFromArray(sourceBuffer),
335
- count: Math.round(sourceBuffer.length / accessor.size),
336
- min: minMax.min,
337
- max: minMax.max
338
- };
339
- return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
340
- }
341
- addTexture(texture) {
342
- const {
343
- imageIndex
344
- } = texture;
345
- const glTFTexture = {
346
- source: imageIndex
347
- };
348
- this.json.textures = this.json.textures || [];
349
- this.json.textures.push(glTFTexture);
350
- return this.json.textures.length - 1;
351
- }
352
- addMaterial(pbrMaterialInfo) {
353
- this.json.materials = this.json.materials || [];
354
- this.json.materials.push(pbrMaterialInfo);
355
- return this.json.materials.length - 1;
356
- }
357
- createBinaryChunk() {
358
- var _this$json, _this$json$buffers;
359
- this.gltf.buffers = [];
360
- const totalByteLength = this.byteLength;
361
- const arrayBuffer = new ArrayBuffer(totalByteLength);
362
- const targetArray = new Uint8Array(arrayBuffer);
363
- let dstByteOffset = 0;
364
- for (const sourceBuffer of this.sourceBuffers || []) {
365
- dstByteOffset = copyToArray(sourceBuffer, targetArray, dstByteOffset);
366
- }
367
- 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]) {
368
- this.json.buffers[0].byteLength = totalByteLength;
369
- } else {
370
- this.json.buffers = [{
371
- byteLength: totalByteLength
372
- }];
373
- }
374
- this.gltf.binary = arrayBuffer;
375
- this.sourceBuffers = [arrayBuffer];
376
- }
377
- _removeStringFromArray(array, string) {
378
- let found = true;
379
- while (found) {
380
- const index = array.indexOf(string);
381
- if (index > -1) {
382
- array.splice(index, 1);
383
- } else {
384
- found = false;
385
- }
386
- }
387
- }
388
- _addAttributes() {
389
- let attributes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
390
- const result = {};
391
- for (const attributeKey in attributes) {
392
- const attributeData = attributes[attributeKey];
393
- const attrName = this._getGltfAttributeName(attributeKey);
394
- const accessor = this.addBinaryBuffer(attributeData.value, attributeData);
395
- result[attrName] = accessor;
396
- }
397
- return result;
398
- }
399
- _addIndices(indices) {
400
- return this.addBinaryBuffer(indices, {
401
- size: 1
402
- });
403
- }
404
- _getGltfAttributeName(attributeName) {
405
- switch (attributeName.toLowerCase()) {
406
- case 'position':
407
- case 'positions':
408
- case 'vertices':
409
- return 'POSITION';
410
- case 'normal':
411
- case 'normals':
412
- return 'NORMAL';
413
- case 'color':
414
- case 'colors':
415
- return 'COLOR_0';
416
- case 'texcoord':
417
- case 'texcoords':
418
- return 'TEXCOORD_0';
419
- default:
420
- return attributeName;
421
- }
422
- }
423
- _getAccessorMinMax(buffer, size) {
424
- const result = {
425
- min: null,
426
- max: null
427
- };
428
- if (buffer.length < size) {
429
- return result;
430
- }
431
- result.min = [];
432
- result.max = [];
433
- const initValues = buffer.subarray(0, size);
434
- for (const value of initValues) {
435
- result.min.push(value);
436
- result.max.push(value);
437
- }
438
- for (let index = size; index < buffer.length; index += size) {
439
- for (let componentIndex = 0; componentIndex < size; componentIndex++) {
440
- result.min[0 + componentIndex] = Math.min(result.min[0 + componentIndex], buffer[index + componentIndex]);
441
- result.max[0 + componentIndex] = Math.max(result.max[0 + componentIndex], buffer[index + componentIndex]);
442
- }
443
- }
444
- return result;
445
- }
25
+ // TODO - why is this not GLTFWithBuffers - what happens to images?
26
+ constructor(gltf) {
27
+ // Declare locally so
28
+ this.gltf = {
29
+ json: gltf?.json || makeDefaultGLTFJson(),
30
+ buffers: gltf?.buffers || [],
31
+ images: gltf?.images || []
32
+ };
33
+ this.sourceBuffers = [];
34
+ this.byteLength = 0;
35
+ // Initialize buffers
36
+ if (this.gltf.buffers && this.gltf.buffers[0]) {
37
+ this.byteLength = this.gltf.buffers[0].byteLength;
38
+ this.sourceBuffers = [this.gltf.buffers[0]];
39
+ }
40
+ }
41
+ // Accessors
42
+ get json() {
43
+ return this.gltf.json;
44
+ }
45
+ getApplicationData(key) {
46
+ // TODO - Data is already unpacked by GLBParser
47
+ const data = this.json[key];
48
+ return data;
49
+ }
50
+ getExtraData(key) {
51
+ // TODO - Data is already unpacked by GLBParser
52
+ const extras = (this.json.extras || {});
53
+ return extras[key];
54
+ }
55
+ hasExtension(extensionName) {
56
+ const isUsedExtension = this.getUsedExtensions().find((name) => name === extensionName);
57
+ const isRequiredExtension = this.getRequiredExtensions().find((name) => name === extensionName);
58
+ return typeof isUsedExtension === 'string' || typeof isRequiredExtension === 'string';
59
+ }
60
+ getExtension(extensionName) {
61
+ const isExtension = this.getUsedExtensions().find((name) => name === extensionName);
62
+ const extensions = this.json.extensions || {};
63
+ return isExtension ? extensions[extensionName] : null;
64
+ }
65
+ getRequiredExtension(extensionName) {
66
+ const isRequired = this.getRequiredExtensions().find((name) => name === extensionName);
67
+ return isRequired ? this.getExtension(extensionName) : null;
68
+ }
69
+ getRequiredExtensions() {
70
+ return this.json.extensionsRequired || [];
71
+ }
72
+ getUsedExtensions() {
73
+ return this.json.extensionsUsed || [];
74
+ }
75
+ getRemovedExtensions() {
76
+ return (this.json.extensionsRemoved || []);
77
+ }
78
+ getObjectExtension(object, extensionName) {
79
+ const extensions = object.extensions || {};
80
+ return extensions[extensionName];
81
+ }
82
+ getScene(index) {
83
+ return this.getObject('scenes', index);
84
+ }
85
+ getNode(index) {
86
+ return this.getObject('nodes', index);
87
+ }
88
+ getSkin(index) {
89
+ return this.getObject('skins', index);
90
+ }
91
+ getMesh(index) {
92
+ return this.getObject('meshes', index);
93
+ }
94
+ getMaterial(index) {
95
+ return this.getObject('materials', index);
96
+ }
97
+ getAccessor(index) {
98
+ return this.getObject('accessors', index);
99
+ }
100
+ // getCamera(index: number): object | null {
101
+ // return null; // TODO: fix thi: object as null;
102
+ // }
103
+ getTexture(index) {
104
+ return this.getObject('textures', index);
105
+ }
106
+ getSampler(index) {
107
+ return this.getObject('samplers', index);
108
+ }
109
+ getImage(index) {
110
+ return this.getObject('images', index);
111
+ }
112
+ getBufferView(index) {
113
+ return this.getObject('bufferViews', index);
114
+ }
115
+ getBuffer(index) {
116
+ return this.getObject('buffers', index);
117
+ }
118
+ getObject(array, index) {
119
+ // check if already resolved
120
+ if (typeof index === 'object') {
121
+ return index;
122
+ }
123
+ const object = this.json[array] && this.json[array][index];
124
+ if (!object) {
125
+ throw new Error(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
126
+ }
127
+ return object;
128
+ }
129
+ /**
130
+ * Accepts buffer view index or buffer view object
131
+ * @returns a `Uint8Array`
132
+ */
133
+ getTypedArrayForBufferView(bufferView) {
134
+ bufferView = this.getBufferView(bufferView);
135
+ // @ts-ignore
136
+ const bufferIndex = bufferView.buffer;
137
+ // Get hold of the arrayBuffer
138
+ // const buffer = this.getBuffer(bufferIndex);
139
+ const binChunk = this.gltf.buffers[bufferIndex];
140
+ assert(binChunk);
141
+ // @ts-ignore
142
+ const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset;
143
+ // @ts-ignore
144
+ return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength);
145
+ }
146
+ /** Accepts accessor index or accessor object
147
+ * @returns a typed array with type that matches the types
148
+ */
149
+ getTypedArrayForAccessor(accessor) {
150
+ // @ts-ignore
151
+ const gltfAccessor = this.getAccessor(accessor);
152
+ return _getTypedArrayForAccessor(this.gltf.json, this.gltf.buffers, gltfAccessor);
153
+ }
154
+ /** accepts accessor index or accessor object
155
+ * returns a `Uint8Array`
156
+ */
157
+ getTypedArrayForImageData(image) {
158
+ // @ts-ignore
159
+ image = this.getAccessor(image);
160
+ // @ts-ignore
161
+ const bufferView = this.getBufferView(image.bufferView);
162
+ const buffer = this.getBuffer(bufferView.buffer);
163
+ // @ts-ignore
164
+ const arrayBuffer = buffer.data;
165
+ const byteOffset = bufferView.byteOffset || 0;
166
+ return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
167
+ }
168
+ // MODIFERS
169
+ /**
170
+ * Add an extra application-defined key to the top-level data structure
171
+ */
172
+ addApplicationData(key, data) {
173
+ this.json[key] = data;
174
+ return this;
175
+ }
176
+ /**
177
+ * `extras` - Standard GLTF field for storing application specific data
178
+ */
179
+ addExtraData(key, data) {
180
+ this.json.extras = this.json.extras || {};
181
+ this.json.extras[key] = data;
182
+ return this;
183
+ }
184
+ addObjectExtension(object, extensionName, data) {
185
+ // @ts-ignore
186
+ object.extensions = object.extensions || {};
187
+ // TODO - clobber or merge?
188
+ // @ts-ignore
189
+ object.extensions[extensionName] = data;
190
+ this.registerUsedExtension(extensionName);
191
+ return this;
192
+ }
193
+ setObjectExtension(object, extensionName, data) {
194
+ const extensions = object.extensions || {};
195
+ extensions[extensionName] = data;
196
+ // TODO - add to usedExtensions...
197
+ }
198
+ removeObjectExtension(object, extensionName) {
199
+ const extensions = object?.extensions || {};
200
+ if (extensions[extensionName]) {
201
+ this.json.extensionsRemoved = this.json.extensionsRemoved || [];
202
+ const extensionsRemoved = this.json.extensionsRemoved;
203
+ if (!extensionsRemoved.includes(extensionName)) {
204
+ extensionsRemoved.push(extensionName);
205
+ }
206
+ }
207
+ delete extensions[extensionName];
208
+ }
209
+ /**
210
+ * Add to standard GLTF top level extension object, mark as used
211
+ */
212
+ addExtension(extensionName, extensionData = {}) {
213
+ assert(extensionData);
214
+ this.json.extensions = this.json.extensions || {};
215
+ this.json.extensions[extensionName] = extensionData;
216
+ this.registerUsedExtension(extensionName);
217
+ return extensionData;
218
+ }
219
+ /**
220
+ * Standard GLTF top level extension object, mark as used and required
221
+ */
222
+ addRequiredExtension(extensionName, extensionData = {}) {
223
+ assert(extensionData);
224
+ this.addExtension(extensionName, extensionData);
225
+ this.registerRequiredExtension(extensionName);
226
+ return extensionData;
227
+ }
228
+ /**
229
+ * Add extensionName to list of used extensions
230
+ */
231
+ registerUsedExtension(extensionName) {
232
+ this.json.extensionsUsed = this.json.extensionsUsed || [];
233
+ if (!this.json.extensionsUsed.find((ext) => ext === extensionName)) {
234
+ this.json.extensionsUsed.push(extensionName);
235
+ }
236
+ }
237
+ /**
238
+ * Add extensionName to list of required extensions
239
+ */
240
+ registerRequiredExtension(extensionName) {
241
+ this.registerUsedExtension(extensionName);
242
+ this.json.extensionsRequired = this.json.extensionsRequired || [];
243
+ if (!this.json.extensionsRequired.find((ext) => ext === extensionName)) {
244
+ this.json.extensionsRequired.push(extensionName);
245
+ }
246
+ }
247
+ /**
248
+ * Removes an extension from the top-level list
249
+ */
250
+ removeExtension(extensionName) {
251
+ if (this.json.extensions?.[extensionName]) {
252
+ this.json.extensionsRemoved = this.json.extensionsRemoved || [];
253
+ const extensionsRemoved = this.json.extensionsRemoved;
254
+ if (!extensionsRemoved.includes(extensionName)) {
255
+ extensionsRemoved.push(extensionName);
256
+ }
257
+ }
258
+ if (this.json.extensions) {
259
+ delete this.json.extensions[extensionName];
260
+ }
261
+ if (this.json.extensionsRequired) {
262
+ this._removeStringFromArray(this.json.extensionsRequired, extensionName);
263
+ }
264
+ if (this.json.extensionsUsed) {
265
+ this._removeStringFromArray(this.json.extensionsUsed, extensionName);
266
+ }
267
+ }
268
+ /**
269
+ * Set default scene which is to be displayed at load time
270
+ */
271
+ setDefaultScene(sceneIndex) {
272
+ this.json.scene = sceneIndex;
273
+ }
274
+ /**
275
+ * @todo: add more properties for scene initialization:
276
+ * name`, `extensions`, `extras`
277
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#reference-scene
278
+ */
279
+ addScene(scene) {
280
+ const { nodeIndices } = scene;
281
+ this.json.scenes = this.json.scenes || [];
282
+ this.json.scenes.push({ nodes: nodeIndices });
283
+ return this.json.scenes.length - 1;
284
+ }
285
+ /**
286
+ * @todo: add more properties for node initialization:
287
+ * `name`, `extensions`, `extras`, `camera`, `children`, `skin`, `rotation`, `scale`, `translation`, `weights`
288
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#node
289
+ */
290
+ addNode(node) {
291
+ const { meshIndex, matrix } = node;
292
+ this.json.nodes = this.json.nodes || [];
293
+ const nodeData = { mesh: meshIndex };
294
+ if (matrix) {
295
+ // @ts-ignore
296
+ nodeData.matrix = matrix;
297
+ }
298
+ this.json.nodes.push(nodeData);
299
+ return this.json.nodes.length - 1;
300
+ }
301
+ /** Adds a mesh to the json part */
302
+ addMesh(mesh) {
303
+ const { attributes, indices, material, mode = 4 } = mesh;
304
+ const accessors = this._addAttributes(attributes);
305
+ const glTFMesh = {
306
+ primitives: [
307
+ {
308
+ attributes: accessors,
309
+ mode
310
+ }
311
+ ]
312
+ };
313
+ if (indices) {
314
+ const indicesAccessor = this._addIndices(indices);
315
+ // @ts-ignore
316
+ glTFMesh.primitives[0].indices = indicesAccessor;
317
+ }
318
+ if (Number.isFinite(material)) {
319
+ // @ts-ignore
320
+ glTFMesh.primitives[0].material = material;
321
+ }
322
+ this.json.meshes = this.json.meshes || [];
323
+ this.json.meshes.push(glTFMesh);
324
+ return this.json.meshes.length - 1;
325
+ }
326
+ addPointCloud(attributes) {
327
+ // @ts-ignore
328
+ const accessorIndices = this._addAttributes(attributes);
329
+ const glTFMesh = {
330
+ primitives: [
331
+ {
332
+ attributes: accessorIndices,
333
+ mode: 0 // GL.POINTS
334
+ }
335
+ ]
336
+ };
337
+ this.json.meshes = this.json.meshes || [];
338
+ this.json.meshes.push(glTFMesh);
339
+ return this.json.meshes.length - 1;
340
+ }
341
+ /**
342
+ * Adds a binary image. Builds glTF "JSON metadata" and saves buffer reference
343
+ * Buffer will be copied into BIN chunk during "pack"
344
+ * Currently encodes as glTF image
345
+ * @param imageData
346
+ * @param mimeType
347
+ */
348
+ addImage(imageData, mimeTypeOpt) {
349
+ // If image is referencing a bufferView instead of URI, mimeType must be defined:
350
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#images
351
+ // "a reference to a bufferView; in that case mimeType must be defined."
352
+ const metadata = getBinaryImageMetadata(imageData);
353
+ const mimeType = mimeTypeOpt || metadata?.mimeType;
354
+ const bufferViewIndex = this.addBufferView(imageData);
355
+ const glTFImage = {
356
+ bufferView: bufferViewIndex,
357
+ mimeType
358
+ };
359
+ this.json.images = this.json.images || [];
360
+ this.json.images.push(glTFImage);
361
+ return this.json.images.length - 1;
362
+ }
363
+ /**
364
+ * Add one untyped source buffer, create a matching glTF `bufferView`, and return its index
365
+ * @param buffer
366
+ */
367
+ addBufferView(buffer, bufferIndex = 0, byteOffset = this.byteLength) {
368
+ const byteLength = buffer.byteLength;
369
+ assert(Number.isFinite(byteLength));
370
+ // Add this buffer to the list of buffers to be written to the body.
371
+ this.sourceBuffers = this.sourceBuffers || [];
372
+ this.sourceBuffers.push(buffer);
373
+ const glTFBufferView = {
374
+ buffer: bufferIndex,
375
+ // Write offset from the start of the binary body
376
+ byteOffset,
377
+ byteLength
378
+ };
379
+ // We've now added the contents to the body, so update the total length
380
+ // Every sub-chunk needs to be 4-byte align ed
381
+ this.byteLength += padToNBytes(byteLength, 4);
382
+ // Add a bufferView indicating start and length of this binary sub-chunk
383
+ this.json.bufferViews = this.json.bufferViews || [];
384
+ this.json.bufferViews.push(glTFBufferView);
385
+ return this.json.bufferViews.length - 1;
386
+ }
387
+ /**
388
+ * Adds an accessor to a bufferView
389
+ * @param bufferViewIndex
390
+ * @param accessor
391
+ */
392
+ addAccessor(bufferViewIndex, accessor) {
393
+ const glTFAccessor = {
394
+ bufferView: bufferViewIndex,
395
+ // @ts-ignore
396
+ type: getAccessorTypeFromSize(accessor.size),
397
+ // @ts-ignore
398
+ componentType: accessor.componentType,
399
+ // @ts-ignore
400
+ count: accessor.count,
401
+ // @ts-ignore
402
+ max: accessor.max,
403
+ // @ts-ignore
404
+ min: accessor.min
405
+ };
406
+ this.json.accessors = this.json.accessors || [];
407
+ this.json.accessors.push(glTFAccessor);
408
+ return this.json.accessors.length - 1;
409
+ }
410
+ /**
411
+ * Add a binary buffer. Builds glTF "JSON metadata" and saves buffer reference
412
+ * Buffer will be copied into BIN chunk during "pack"
413
+ * Currently encodes buffers as glTF accessors, but this could be optimized
414
+ * @param sourceBuffer
415
+ * @param accessor
416
+ */
417
+ addBinaryBuffer(sourceBuffer, accessor = { size: 3 }) {
418
+ const bufferViewIndex = this.addBufferView(sourceBuffer);
419
+ // @ts-ignore
420
+ let minMax = { min: accessor.min, max: accessor.max };
421
+ if (!minMax.min || !minMax.max) {
422
+ // @ts-ignore
423
+ minMax = this._getAccessorMinMax(sourceBuffer, accessor.size);
424
+ }
425
+ const accessorDefaults = {
426
+ // @ts-ignore
427
+ size: accessor.size,
428
+ componentType: getComponentTypeFromArray(sourceBuffer),
429
+ // @ts-ignore
430
+ count: Math.round(sourceBuffer.length / accessor.size),
431
+ min: minMax.min,
432
+ max: minMax.max
433
+ };
434
+ return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor));
435
+ }
436
+ /**
437
+ * Adds a texture to the json part
438
+ * @todo: add more properties for texture initialization
439
+ * `sampler`, `name`, `extensions`, `extras`
440
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
441
+ */
442
+ addTexture(texture) {
443
+ const { imageIndex } = texture;
444
+ const glTFTexture = {
445
+ source: imageIndex
446
+ };
447
+ this.json.textures = this.json.textures || [];
448
+ this.json.textures.push(glTFTexture);
449
+ return this.json.textures.length - 1;
450
+ }
451
+ /** Adds a material to the json part */
452
+ addMaterial(pbrMaterialInfo) {
453
+ this.json.materials = this.json.materials || [];
454
+ this.json.materials.push(pbrMaterialInfo);
455
+ return this.json.materials.length - 1;
456
+ }
457
+ /** Pack the binary chunk */
458
+ createBinaryChunk() {
459
+ // Encoder expects this array undefined or empty
460
+ this.gltf.buffers = [];
461
+ // Allocate total array
462
+ const totalByteLength = this.byteLength;
463
+ const arrayBuffer = new ArrayBuffer(totalByteLength);
464
+ const targetArray = new Uint8Array(arrayBuffer);
465
+ // Copy each array into
466
+ let dstByteOffset = 0;
467
+ for (const sourceBuffer of this.sourceBuffers || []) {
468
+ dstByteOffset = copyToArray(sourceBuffer, targetArray, dstByteOffset);
469
+ }
470
+ // Update the glTF BIN CHUNK byte length
471
+ if (this.json?.buffers?.[0]) {
472
+ this.json.buffers[0].byteLength = totalByteLength;
473
+ }
474
+ else {
475
+ this.json.buffers = [{ byteLength: totalByteLength }];
476
+ }
477
+ // Save generated arrayBuffer
478
+ this.gltf.binary = arrayBuffer;
479
+ // Put arrayBuffer to sourceBuffers for possible additional writing data in the chunk
480
+ this.sourceBuffers = [arrayBuffer];
481
+ }
482
+ // PRIVATE
483
+ _removeStringFromArray(array, string) {
484
+ let found = true;
485
+ while (found) {
486
+ const index = array.indexOf(string);
487
+ if (index > -1) {
488
+ array.splice(index, 1);
489
+ }
490
+ else {
491
+ found = false;
492
+ }
493
+ }
494
+ }
495
+ /**
496
+ * Add attributes to buffers and create `attributes` object which is part of `mesh`
497
+ */
498
+ _addAttributes(attributes = {}) {
499
+ const result = {};
500
+ for (const attributeKey in attributes) {
501
+ const attributeData = attributes[attributeKey];
502
+ const attrName = this._getGltfAttributeName(attributeKey);
503
+ const accessor = this.addBinaryBuffer(attributeData.value, attributeData);
504
+ result[attrName] = accessor;
505
+ }
506
+ return result;
507
+ }
508
+ /**
509
+ * Add indices to buffers
510
+ */
511
+ _addIndices(indices) {
512
+ return this.addBinaryBuffer(indices, { size: 1 });
513
+ }
514
+ /**
515
+ * Deduce gltf specific attribue name from input attribute name
516
+ */
517
+ _getGltfAttributeName(attributeName) {
518
+ switch (attributeName.toLowerCase()) {
519
+ case 'position':
520
+ case 'positions':
521
+ case 'vertices':
522
+ return 'POSITION';
523
+ case 'normal':
524
+ case 'normals':
525
+ return 'NORMAL';
526
+ case 'color':
527
+ case 'colors':
528
+ return 'COLOR_0';
529
+ case 'texcoord':
530
+ case 'texcoords':
531
+ return 'TEXCOORD_0';
532
+ default:
533
+ return attributeName;
534
+ }
535
+ }
536
+ /**
537
+ * Calculate `min` and `max` arrays of accessor according to spec:
538
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#reference-accessor
539
+ */
540
+ _getAccessorMinMax(buffer, size) {
541
+ const result = { min: null, max: null };
542
+ if (buffer.length < size) {
543
+ return result;
544
+ }
545
+ // @ts-ignore
546
+ result.min = [];
547
+ // @ts-ignore
548
+ result.max = [];
549
+ const initValues = buffer.subarray(0, size);
550
+ for (const value of initValues) {
551
+ // @ts-ignore
552
+ result.min.push(value);
553
+ // @ts-ignore
554
+ result.max.push(value);
555
+ }
556
+ for (let index = size; index < buffer.length; index += size) {
557
+ for (let componentIndex = 0; componentIndex < size; componentIndex++) {
558
+ // @ts-ignore
559
+ result.min[0 + componentIndex] = Math.min(
560
+ // @ts-ignore
561
+ result.min[0 + componentIndex], buffer[index + componentIndex]);
562
+ // @ts-ignore
563
+ result.max[0 + componentIndex] = Math.max(
564
+ // @ts-ignore
565
+ result.max[0 + componentIndex], buffer[index + componentIndex]);
566
+ }
567
+ }
568
+ return result;
569
+ }
446
570
  }
447
- //# sourceMappingURL=gltf-scenegraph.js.map