@loaders.gl/gltf 4.0.0-alpha.23 → 4.0.0-alpha.25

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