@loaders.gl/gltf 4.2.0-alpha.4 → 4.2.0-alpha.6

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