@loaders.gl/gltf 3.1.0-alpha.5 → 3.1.0-beta.1

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 (135) hide show
  1. package/dist/bundle.d.ts +1 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +4483 -0
  4. package/dist/es5/lib/api/normalize-gltf-v1.js +8 -3
  5. package/dist/es5/lib/api/normalize-gltf-v1.js.map +1 -1
  6. package/dist/es5/lib/api/post-process-gltf.js +12 -1
  7. package/dist/es5/lib/api/post-process-gltf.js.map +1 -1
  8. package/dist/es5/lib/encoders/encode-glb.js.map +1 -1
  9. package/dist/es5/lib/encoders/encode-gltf.js.map +1 -1
  10. package/dist/es5/lib/extensions/KHR_texture_basisu.js.map +1 -1
  11. package/dist/es5/lib/utils/assert.js.map +1 -1
  12. package/dist/es5/lib/utils/version.js +1 -1
  13. package/dist/es5/lib/utils/version.js.map +1 -1
  14. package/dist/esm/lib/api/normalize-gltf-v1.js +6 -3
  15. package/dist/esm/lib/api/normalize-gltf-v1.js.map +1 -1
  16. package/dist/esm/lib/api/post-process-gltf.js +12 -1
  17. package/dist/esm/lib/api/post-process-gltf.js.map +1 -1
  18. package/dist/esm/lib/encoders/encode-glb.js.map +1 -1
  19. package/dist/esm/lib/encoders/encode-gltf.js.map +1 -1
  20. package/dist/esm/lib/extensions/KHR_texture_basisu.js.map +1 -1
  21. package/dist/esm/lib/utils/assert.js.map +1 -1
  22. package/dist/esm/lib/utils/version.js +1 -1
  23. package/dist/esm/lib/utils/version.js.map +1 -1
  24. package/dist/glb-loader.d.ts +1 -0
  25. package/dist/glb-loader.d.ts.map +1 -0
  26. package/dist/glb-loader.js +39 -0
  27. package/dist/glb-writer.d.ts +1 -0
  28. package/dist/glb-writer.d.ts.map +1 -0
  29. package/dist/glb-writer.js +37 -0
  30. package/dist/gltf-loader.d.ts +2 -1
  31. package/dist/gltf-loader.d.ts.map +1 -0
  32. package/dist/gltf-loader.js +50 -0
  33. package/dist/gltf-writer.d.ts +1 -0
  34. package/dist/gltf-writer.d.ts.map +1 -0
  35. package/dist/gltf-writer.js +32 -0
  36. package/dist/index.d.ts +1 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +21 -0
  39. package/dist/lib/api/gltf-extensions.d.ts +1 -0
  40. package/dist/lib/api/gltf-extensions.d.ts.map +1 -0
  41. package/dist/lib/api/gltf-extensions.js +75 -0
  42. package/dist/lib/api/gltf-scenegraph.d.ts +1 -0
  43. package/dist/lib/api/gltf-scenegraph.d.ts.map +1 -0
  44. package/dist/lib/api/gltf-scenegraph.js +553 -0
  45. package/dist/lib/api/normalize-gltf-v1.d.ts +2 -1
  46. package/dist/lib/api/normalize-gltf-v1.d.ts.map +1 -0
  47. package/dist/lib/api/normalize-gltf-v1.js +295 -0
  48. package/dist/lib/api/post-process-gltf.d.ts +2 -1
  49. package/dist/lib/api/post-process-gltf.d.ts.map +1 -0
  50. package/dist/lib/api/post-process-gltf.js +346 -0
  51. package/dist/lib/encoders/encode-glb.d.ts +1 -0
  52. package/dist/lib/encoders/encode-glb.d.ts.map +1 -0
  53. package/dist/lib/encoders/encode-glb.js +61 -0
  54. package/dist/lib/encoders/encode-gltf.d.ts +2 -1
  55. package/dist/lib/encoders/encode-gltf.d.ts.map +1 -0
  56. package/dist/lib/encoders/encode-gltf.js +27 -0
  57. package/dist/lib/extensions/EXT_meshopt_compression.d.ts +1 -0
  58. package/dist/lib/extensions/EXT_meshopt_compression.d.ts.map +1 -0
  59. package/dist/lib/extensions/EXT_meshopt_compression.js +54 -0
  60. package/dist/lib/extensions/EXT_texture_webp.d.ts +1 -0
  61. package/dist/lib/extensions/EXT_texture_webp.d.ts.map +1 -0
  62. package/dist/lib/extensions/EXT_texture_webp.js +38 -0
  63. package/dist/lib/extensions/KHR_binary_gltf.d.ts +1 -0
  64. package/dist/lib/extensions/KHR_binary_gltf.d.ts.map +1 -0
  65. package/dist/lib/extensions/KHR_binary_gltf.js +42 -0
  66. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts +1 -0
  67. package/dist/lib/extensions/KHR_draco_mesh_compression.d.ts.map +1 -0
  68. package/dist/lib/extensions/KHR_draco_mesh_compression.js +141 -0
  69. package/dist/lib/extensions/KHR_texture_basisu.d.ts +1 -0
  70. package/dist/lib/extensions/KHR_texture_basisu.d.ts.map +1 -0
  71. package/dist/lib/extensions/KHR_texture_basisu.js +31 -0
  72. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts +1 -0
  73. package/dist/lib/extensions/deprecated/KHR_lights_punctual.d.ts.map +1 -0
  74. package/dist/lib/extensions/deprecated/KHR_lights_punctual.js +62 -0
  75. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts +1 -0
  76. package/dist/lib/extensions/deprecated/KHR_materials_unlit.d.ts.map +1 -0
  77. package/dist/lib/extensions/deprecated/KHR_materials_unlit.js +47 -0
  78. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts +1 -0
  79. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.d.ts.map +1 -0
  80. package/dist/lib/extensions/deprecated/KHR_techniques_webgl.js +81 -0
  81. package/dist/lib/gltf-utils/get-typed-array.d.ts +1 -0
  82. package/dist/lib/gltf-utils/get-typed-array.d.ts.map +1 -0
  83. package/dist/lib/gltf-utils/get-typed-array.js +41 -0
  84. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts +1 -0
  85. package/dist/lib/gltf-utils/gltf-attribute-utils.d.ts.map +1 -0
  86. package/dist/lib/gltf-utils/gltf-attribute-utils.js +73 -0
  87. package/dist/lib/gltf-utils/gltf-constants.d.ts +1 -0
  88. package/dist/lib/gltf-utils/gltf-constants.d.ts.map +1 -0
  89. package/dist/lib/gltf-utils/gltf-constants.js +43 -0
  90. package/dist/lib/gltf-utils/gltf-utils.d.ts +1 -0
  91. package/dist/lib/gltf-utils/gltf-utils.d.ts.map +1 -0
  92. package/dist/lib/gltf-utils/gltf-utils.js +63 -0
  93. package/dist/lib/gltf-utils/resolve-url.d.ts +1 -0
  94. package/dist/lib/gltf-utils/resolve-url.d.ts.map +1 -0
  95. package/dist/lib/gltf-utils/resolve-url.js +18 -0
  96. package/dist/lib/parsers/parse-glb.d.ts +1 -0
  97. package/dist/lib/parsers/parse-glb.d.ts.map +1 -0
  98. package/dist/lib/parsers/parse-glb.js +141 -0
  99. package/dist/lib/parsers/parse-gltf.d.ts +2 -1
  100. package/dist/lib/parsers/parse-gltf.d.ts.map +1 -0
  101. package/dist/lib/parsers/parse-gltf.js +189 -0
  102. package/dist/lib/types/glb-types.d.ts +1 -0
  103. package/dist/lib/types/glb-types.d.ts.map +1 -0
  104. package/dist/lib/types/glb-types.js +2 -0
  105. package/dist/lib/types/gltf-json-schema.d.ts +1 -0
  106. package/dist/lib/types/gltf-json-schema.d.ts.map +1 -0
  107. package/dist/lib/types/gltf-json-schema.js +4 -0
  108. package/dist/lib/types/gltf-postprocessed-schema.d.ts +1 -0
  109. package/dist/lib/types/gltf-postprocessed-schema.d.ts.map +1 -0
  110. package/dist/lib/types/gltf-postprocessed-schema.js +4 -0
  111. package/dist/lib/types/gltf-types.d.ts +1 -0
  112. package/dist/lib/types/gltf-types.d.ts.map +1 -0
  113. package/dist/lib/types/gltf-types.js +3 -0
  114. package/dist/lib/utils/assert.d.ts +2 -1
  115. package/dist/lib/utils/assert.d.ts.map +1 -0
  116. package/dist/lib/utils/assert.js +12 -0
  117. package/dist/lib/utils/version.d.ts +2 -1
  118. package/dist/lib/utils/version.d.ts.map +1 -0
  119. package/dist/lib/utils/version.js +7 -0
  120. package/dist/meshopt/meshopt-decoder.d.ts +1 -0
  121. package/dist/meshopt/meshopt-decoder.d.ts.map +1 -0
  122. package/dist/meshopt/meshopt-decoder.js +118 -0
  123. package/dist/webp/webp.d.ts +1 -0
  124. package/dist/webp/webp.d.ts.map +1 -0
  125. package/dist/webp/webp.js +38 -0
  126. package/package.json +8 -9
  127. package/src/lib/api/{normalize-gltf-v1.js → normalize-gltf-v1.ts} +18 -16
  128. package/src/lib/api/{post-process-gltf.js → post-process-gltf.ts} +8 -1
  129. package/src/lib/encoders/{encode-glb.js → encode-glb.ts} +0 -0
  130. package/src/lib/encoders/{encode-gltf.js → encode-gltf.ts} +0 -0
  131. package/src/lib/extensions/KHR_texture_basisu.ts +2 -2
  132. package/src/lib/utils/{assert.js → assert.ts} +1 -1
  133. package/src/lib/utils/{version.js → version.ts} +0 -0
  134. package/dist/dist.min.js +0 -2
  135. package/dist/dist.min.js.map +0 -1
@@ -0,0 +1,295 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ exports.normalizeGLTFV1 = void 0;
23
+ /* eslint-disable camelcase */
24
+ const KHR_binary_glTF = __importStar(require("../extensions/KHR_binary_gltf"));
25
+ // Binary format changes (mainly implemented by GLBLoader)
26
+ // https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF
27
+ // JSON format changes:
28
+ // https://github.com/khronosgroup/gltf/issues/605
29
+ // - [x] Top-level JSON objects are arrays now
30
+ // - [ ] Removed indirection from animation: sampler now refers directly to accessors, #712
31
+ // - [ ] material.parameter.value and technique.parameter.value must be an array, #690
32
+ // - [ ] Node can have only one mesh #821
33
+ // - [ ] Added reqs on JSON encoding
34
+ // - [ ] Added reqs on binary data alignment #802 (comment)
35
+ // Additions:
36
+ // - [ ] Added accessor.normalized, #691, #706
37
+ // - [ ] Added glExtensionsUsed property and 5125 (UNSIGNED_INT) accessor.componentType value, #619
38
+ // - [ ] Added extensionsRequired property, #720, #721
39
+ // - [ ] Added "STEP" as valid animation.sampler.interpolation value, #712
40
+ // Removals:
41
+ // - [x] Removed buffer.type, #786, #629
42
+ // - [ ] Removed revision number from profile.version, #709
43
+ // - [ ] Removed technique.functions.scissor and removed 3089 (SCISSOR_TEST) as a valid value for technique.states.enable, #681
44
+ // - [ ] Techniques, programs, and shaders were moved out to KHR_technique_webgl extension.
45
+ // Other edits:
46
+ // - [x] asset is now required, #642
47
+ // - [ ] buffer.byteLength and bufferView.byteLength are now required, #560.
48
+ // - [ ] accessor.min and accessor.max are now required, #593, and clarified that the JSON value and binary data must be the same, #628.
49
+ // - [ ] Clarified animation.sampler and animation.channel restrictions, #712
50
+ // - [ ] skin.inverseBindMatrices is now optional, #461.
51
+ // - [ ] Attribute parameters can't have a value defined in the technique or parameter, #563 (comment).
52
+ // - [ ] Only TEXCOORD and COLOR attribute semantics can be written in the form [semantic]_[set_index], #563 (comment).
53
+ // - [ ] TEXCOORD and COLOR attribute semantics must be written in the form [semantic]_[set_index], e.g., just TEXCOORD should be TEXCOORD_0, and just COLOR should be COLOR_0, #649
54
+ // - [ ] camera.perspective.aspectRatio and camera.perspective.yfov must now be > 0, not >= 0, #563 (comment).
55
+ // - [ ] Application-specific parameter semantics must start with an underscore, e.g., _TEMPERATURE and _SIMULATION_TIME, #563 (comment).
56
+ // - [ ] Properties in technique.parameters must be defined in technique.uniforms or technique.attributes,
57
+ // #563 (comment).
58
+ // - [ ] technique.parameter.count can only be defined when the semantic is JOINTMATRIX or an application-specific semantic is used. It can never be defined for attribute parameters; only uniforms, d2f6945
59
+ // - [ ] technique.parameter.semantic is required when the parameter is an attribute, 28e113d
60
+ // - [ ] Mesh-only models are allowed, e.g., without materials, #642
61
+ // - [ ] Skeleton hierarchies (nodes containing jointName) must be separated from non-skeleton hierarchies., #647
62
+ // - [ ] technique.states.functions.blendColor and technique.states.functions.depthRange parameters now must match WebGL function min/max, #707
63
+ const GLTF_ARRAYS = {
64
+ accessors: 'accessor',
65
+ animations: 'animation',
66
+ buffers: 'buffer',
67
+ bufferViews: 'bufferView',
68
+ images: 'image',
69
+ materials: 'material',
70
+ meshes: 'mesh',
71
+ nodes: 'node',
72
+ samplers: 'sampler',
73
+ scenes: 'scene',
74
+ skins: 'skin',
75
+ textures: 'texture'
76
+ };
77
+ const GLTF_KEYS = {
78
+ accessor: 'accessors',
79
+ animations: 'animation',
80
+ buffer: 'buffers',
81
+ bufferView: 'bufferViews',
82
+ image: 'images',
83
+ material: 'materials',
84
+ mesh: 'meshes',
85
+ node: 'nodes',
86
+ sampler: 'samplers',
87
+ scene: 'scenes',
88
+ skin: 'skins',
89
+ texture: 'textures'
90
+ };
91
+ /**
92
+ * Converts (normalizes) glTF v1 to v2
93
+ */
94
+ class GLTFV1Normalizer {
95
+ constructor() {
96
+ this.idToIndexMap = {
97
+ animations: {},
98
+ accessors: {},
99
+ buffers: {},
100
+ bufferViews: {},
101
+ images: {},
102
+ materials: {},
103
+ meshes: {},
104
+ nodes: {},
105
+ samplers: {},
106
+ scenes: {},
107
+ skins: {},
108
+ textures: {}
109
+ };
110
+ }
111
+ // constructor() {}
112
+ /**
113
+ * Convert (normalize) glTF < 2.0 to glTF 2.0
114
+ * @param gltf - object with json and binChunks
115
+ * @param options
116
+ * @param options normalize Whether to actually normalize
117
+ */
118
+ normalize(gltf, options) {
119
+ this.json = gltf.json;
120
+ const json = gltf.json;
121
+ // Check version
122
+ switch (json.asset && json.asset.version) {
123
+ // We are converting to v2 format. Return if there is nothing to do
124
+ case '2.0':
125
+ return;
126
+ // This class is written to convert 1.0
127
+ case undefined:
128
+ case '1.0':
129
+ break;
130
+ default:
131
+ // eslint-disable-next-line no-undef, no-console
132
+ console.warn(`glTF: Unknown version ${json.asset.version}`);
133
+ return;
134
+ }
135
+ if (!options.normalize) {
136
+ // We are still missing a few conversion tricks, remove once addressed
137
+ throw new Error('glTF v1 is not supported.');
138
+ }
139
+ // eslint-disable-next-line no-undef, no-console
140
+ console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
141
+ this._addAsset(json);
142
+ // In glTF2 top-level fields are Arrays not Object maps
143
+ this._convertTopLevelObjectsToArrays(json);
144
+ // Extract bufferView indices for images
145
+ // (this extension needs to be invoked early in the normalization process)
146
+ // TODO can this be handled by standard extension processing instead of called explicitly?
147
+ KHR_binary_glTF.preprocess(gltf);
148
+ // Convert object references from ids to indices
149
+ this._convertObjectIdsToArrayIndices(json);
150
+ this._updateObjects(json);
151
+ this._updateMaterial(json);
152
+ }
153
+ // asset is now required, #642 https://github.com/KhronosGroup/glTF/issues/639
154
+ _addAsset(json) {
155
+ json.asset = json.asset || {};
156
+ // We are normalizing to glTF v2, so change version to "2.0"
157
+ json.asset.version = '2.0';
158
+ json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
159
+ }
160
+ _convertTopLevelObjectsToArrays(json) {
161
+ // TODO check that all arrays are covered
162
+ for (const arrayName in GLTF_ARRAYS) {
163
+ this._convertTopLevelObjectToArray(json, arrayName);
164
+ }
165
+ }
166
+ /** Convert one top level object to array */
167
+ _convertTopLevelObjectToArray(json, mapName) {
168
+ const objectMap = json[mapName];
169
+ if (!objectMap || Array.isArray(objectMap)) {
170
+ return;
171
+ }
172
+ // Rewrite the top-level field as an array
173
+ json[mapName] = [];
174
+ // Copy the map key into object.id
175
+ for (const id in objectMap) {
176
+ const object = objectMap[id];
177
+ object.id = object.id || id; // Mutates the loaded object
178
+ const index = json[mapName].length;
179
+ json[mapName].push(object);
180
+ this.idToIndexMap[mapName][id] = index;
181
+ }
182
+ }
183
+ /** Go through all objects in all top-level arrays and replace ids with indices */
184
+ _convertObjectIdsToArrayIndices(json) {
185
+ for (const arrayName in GLTF_ARRAYS) {
186
+ this._convertIdsToIndices(json, arrayName);
187
+ }
188
+ if ('scene' in json) {
189
+ json.scene = this._convertIdToIndex(json.scene, 'scene');
190
+ }
191
+ // Convert any index references that are not using array names
192
+ // texture.source (image)
193
+ for (const texture of json.textures) {
194
+ this._convertTextureIds(texture);
195
+ }
196
+ for (const mesh of json.meshes) {
197
+ this._convertMeshIds(mesh);
198
+ }
199
+ for (const node of json.nodes) {
200
+ this._convertNodeIds(node);
201
+ }
202
+ for (const node of json.scenes) {
203
+ this._convertSceneIds(node);
204
+ }
205
+ }
206
+ _convertTextureIds(texture) {
207
+ if (texture.source) {
208
+ texture.source = this._convertIdToIndex(texture.source, 'image');
209
+ }
210
+ }
211
+ _convertMeshIds(mesh) {
212
+ for (const primitive of mesh.primitives) {
213
+ const { attributes, indices, material } = primitive;
214
+ for (const attributeName in attributes) {
215
+ attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
216
+ }
217
+ if (indices) {
218
+ primitive.indices = this._convertIdToIndex(indices, 'accessor');
219
+ }
220
+ if (material) {
221
+ primitive.material = this._convertIdToIndex(material, 'material');
222
+ }
223
+ }
224
+ }
225
+ _convertNodeIds(node) {
226
+ if (node.children) {
227
+ node.children = node.children.map((child) => this._convertIdToIndex(child, 'node'));
228
+ }
229
+ if (node.meshes) {
230
+ node.meshes = node.meshes.map((mesh) => this._convertIdToIndex(mesh, 'mesh'));
231
+ }
232
+ }
233
+ _convertSceneIds(scene) {
234
+ if (scene.nodes) {
235
+ scene.nodes = scene.nodes.map((node) => this._convertIdToIndex(node, 'node'));
236
+ }
237
+ }
238
+ /** Go through all objects in a top-level array and replace ids with indices */
239
+ _convertIdsToIndices(json, topLevelArrayName) {
240
+ if (!json[topLevelArrayName]) {
241
+ console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`); // eslint-disable-line no-console, no-undef
242
+ json[topLevelArrayName] = [];
243
+ }
244
+ for (const object of json[topLevelArrayName]) {
245
+ for (const key in object) {
246
+ const id = object[key];
247
+ const index = this._convertIdToIndex(id, key);
248
+ object[key] = index;
249
+ }
250
+ }
251
+ }
252
+ _convertIdToIndex(id, key) {
253
+ const arrayName = GLTF_KEYS[key];
254
+ if (arrayName in this.idToIndexMap) {
255
+ const index = this.idToIndexMap[arrayName][id];
256
+ if (!Number.isFinite(index)) {
257
+ throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);
258
+ }
259
+ return index;
260
+ }
261
+ return id;
262
+ }
263
+ /**
264
+ *
265
+ * @param {*} json
266
+ */
267
+ _updateObjects(json) {
268
+ for (const buffer of this.json.buffers) {
269
+ // - [x] Removed buffer.type, #786, #629
270
+ delete buffer.type;
271
+ }
272
+ }
273
+ /**
274
+ * Update material (set pbrMetallicRoughness)
275
+ * @param {*} json
276
+ */
277
+ _updateMaterial(json) {
278
+ for (const material of json.materials) {
279
+ material.pbrMetallicRoughness = {
280
+ baseColorFactor: [1, 1, 1, 1],
281
+ metallicFactor: 1,
282
+ roughnessFactor: 1
283
+ };
284
+ const textureId = material.values && material.values.tex;
285
+ const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);
286
+ if (textureIndex !== -1) {
287
+ material.pbrMetallicRoughness.baseColorTexture = { index: textureIndex };
288
+ }
289
+ }
290
+ }
291
+ }
292
+ function normalizeGLTFV1(gltf, options = {}) {
293
+ return new GLTFV1Normalizer().normalize(gltf, options);
294
+ }
295
+ exports.normalizeGLTFV1 = normalizeGLTFV1;
@@ -1 +1,2 @@
1
- export function postProcessGLTF(gltf: any, options: any): any;
1
+ export declare function postProcessGLTF(gltf: any, options: any): Record<string, any>;
2
+ //# sourceMappingURL=post-process-gltf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"post-process-gltf.d.ts","sourceRoot":"","sources":["../../../src/lib/api/post-process-gltf.ts"],"names":[],"mappings":"AAwYA,wBAAgB,eAAe,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA,uBAE5C"}
@@ -0,0 +1,346 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.postProcessGLTF = void 0;
4
+ const assert_1 = require("../utils/assert");
5
+ const gltf_utils_1 = require("../gltf-utils/gltf-utils");
6
+ // This is a post processor for loaded glTF files
7
+ // The goal is to make the loaded data easier to use in WebGL applications
8
+ //
9
+ // Functions:
10
+ // * Resolve indexed arrays structure of glTF into a linked tree.
11
+ // * Translate stringified enum keys and values into WebGL constants.
12
+ // * Load images (optional)
13
+ // ENUM LOOKUP
14
+ const COMPONENTS = {
15
+ SCALAR: 1,
16
+ VEC2: 2,
17
+ VEC3: 3,
18
+ VEC4: 4,
19
+ MAT2: 4,
20
+ MAT3: 9,
21
+ MAT4: 16
22
+ };
23
+ const BYTES = {
24
+ 5120: 1,
25
+ 5121: 1,
26
+ 5122: 2,
27
+ 5123: 2,
28
+ 5125: 4,
29
+ 5126: 4 // FLOAT
30
+ };
31
+ const GL_SAMPLER = {
32
+ // Sampler parameters
33
+ TEXTURE_MAG_FILTER: 0x2800,
34
+ TEXTURE_MIN_FILTER: 0x2801,
35
+ TEXTURE_WRAP_S: 0x2802,
36
+ TEXTURE_WRAP_T: 0x2803,
37
+ // Sampler default values
38
+ REPEAT: 0x2901,
39
+ LINEAR: 0x2601,
40
+ NEAREST_MIPMAP_LINEAR: 0x2702
41
+ };
42
+ const SAMPLER_PARAMETER_GLTF_TO_GL = {
43
+ magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER,
44
+ minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER,
45
+ wrapS: GL_SAMPLER.TEXTURE_WRAP_S,
46
+ wrapT: GL_SAMPLER.TEXTURE_WRAP_T
47
+ };
48
+ // When undefined, a sampler with repeat wrapping and auto filtering should be used.
49
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#texture
50
+ const DEFAULT_SAMPLER = {
51
+ [GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR,
52
+ [GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR,
53
+ [GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT,
54
+ [GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT
55
+ };
56
+ function getBytesFromComponentType(componentType) {
57
+ return BYTES[componentType];
58
+ }
59
+ function getSizeFromAccessorType(type) {
60
+ return COMPONENTS[type];
61
+ }
62
+ class GLTFPostProcessor {
63
+ constructor() {
64
+ this.baseUri = '';
65
+ this.json = {};
66
+ this.buffers = [];
67
+ this.images = [];
68
+ }
69
+ postProcess(gltf, options = {}) {
70
+ const { json, buffers = [], images = [], baseUri = '' } = gltf;
71
+ (0, assert_1.assert)(json);
72
+ this.baseUri = baseUri;
73
+ this.json = json;
74
+ this.buffers = buffers;
75
+ this.images = images;
76
+ this._resolveTree(this.json, options);
77
+ return this.json;
78
+ }
79
+ // Convert indexed glTF structure into tree structure
80
+ // cross-link index resolution, enum lookup, convenience calculations
81
+ // eslint-disable-next-line complexity
82
+ _resolveTree(json, options = {}) {
83
+ if (json.bufferViews) {
84
+ json.bufferViews = json.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i));
85
+ }
86
+ if (json.images) {
87
+ json.images = json.images.map((image, i) => this._resolveImage(image, i));
88
+ }
89
+ if (json.samplers) {
90
+ json.samplers = json.samplers.map((sampler, i) => this._resolveSampler(sampler, i));
91
+ }
92
+ if (json.textures) {
93
+ json.textures = json.textures.map((texture, i) => this._resolveTexture(texture, i));
94
+ }
95
+ if (json.accessors) {
96
+ json.accessors = json.accessors.map((accessor, i) => this._resolveAccessor(accessor, i));
97
+ }
98
+ if (json.materials) {
99
+ json.materials = json.materials.map((material, i) => this._resolveMaterial(material, i));
100
+ }
101
+ if (json.meshes) {
102
+ json.meshes = json.meshes.map((mesh, i) => this._resolveMesh(mesh, i));
103
+ }
104
+ if (json.nodes) {
105
+ json.nodes = json.nodes.map((node, i) => this._resolveNode(node, i));
106
+ }
107
+ if (json.skins) {
108
+ json.skins = json.skins.map((skin, i) => this._resolveSkin(skin, i));
109
+ }
110
+ if (json.scenes) {
111
+ json.scenes = json.scenes.map((scene, i) => this._resolveScene(scene, i));
112
+ }
113
+ if (json.scene !== undefined) {
114
+ json.scene = json.scenes[this.json.scene];
115
+ }
116
+ }
117
+ getScene(index) {
118
+ return this._get('scenes', index);
119
+ }
120
+ getNode(index) {
121
+ return this._get('nodes', index);
122
+ }
123
+ getSkin(index) {
124
+ return this._get('skins', index);
125
+ }
126
+ getMesh(index) {
127
+ return this._get('meshes', index);
128
+ }
129
+ getMaterial(index) {
130
+ return this._get('materials', index);
131
+ }
132
+ getAccessor(index) {
133
+ return this._get('accessors', index);
134
+ }
135
+ getCamera(index) {
136
+ return null; // TODO: fix this
137
+ }
138
+ getTexture(index) {
139
+ return this._get('textures', index);
140
+ }
141
+ getSampler(index) {
142
+ return this._get('samplers', index);
143
+ }
144
+ getImage(index) {
145
+ return this._get('images', index);
146
+ }
147
+ getBufferView(index) {
148
+ return this._get('bufferViews', index);
149
+ }
150
+ getBuffer(index) {
151
+ return this._get('buffers', index);
152
+ }
153
+ _get(array, index) {
154
+ // check if already resolved
155
+ if (typeof index === 'object') {
156
+ return index;
157
+ }
158
+ const object = this.json[array] && this.json[array][index];
159
+ if (!object) {
160
+ console.warn(`glTF file error: Could not find ${array}[${index}]`); // eslint-disable-line
161
+ }
162
+ return object;
163
+ }
164
+ // PARSING HELPERS
165
+ _resolveScene(scene, index) {
166
+ // scene = {...scene};
167
+ scene.id = scene.id || `scene-${index}`;
168
+ scene.nodes = (scene.nodes || []).map((node) => this.getNode(node));
169
+ return scene;
170
+ }
171
+ _resolveNode(node, index) {
172
+ // node = {...node};
173
+ node.id = node.id || `node-${index}`;
174
+ if (node.children) {
175
+ node.children = node.children.map((child) => this.getNode(child));
176
+ }
177
+ if (node.mesh !== undefined) {
178
+ node.mesh = this.getMesh(node.mesh);
179
+ }
180
+ else if (node.meshes !== undefined && node.meshes.length) {
181
+ node.mesh = node.meshes.reduce((accum, meshIndex) => {
182
+ const mesh = this.getMesh(meshIndex);
183
+ accum.id = mesh.id;
184
+ accum.primitives = accum.primitives.concat(mesh.primitives);
185
+ return accum;
186
+ }, { primitives: [] });
187
+ }
188
+ if (node.camera !== undefined) {
189
+ node.camera = this.getCamera(node.camera);
190
+ }
191
+ if (node.skin !== undefined) {
192
+ node.skin = this.getSkin(node.skin);
193
+ }
194
+ return node;
195
+ }
196
+ _resolveSkin(skin, index) {
197
+ // skin = {...skin};
198
+ skin.id = skin.id || `skin-${index}`;
199
+ skin.inverseBindMatrices = this.getAccessor(skin.inverseBindMatrices);
200
+ return skin;
201
+ }
202
+ _resolveMesh(mesh, index) {
203
+ // mesh = {...mesh};
204
+ mesh.id = mesh.id || `mesh-${index}`;
205
+ if (mesh.primitives) {
206
+ mesh.primitives = mesh.primitives.map((primitive) => {
207
+ primitive = { ...primitive };
208
+ const attributes = primitive.attributes;
209
+ primitive.attributes = {};
210
+ for (const attribute in attributes) {
211
+ primitive.attributes[attribute] = this.getAccessor(attributes[attribute]);
212
+ }
213
+ if (primitive.indices !== undefined) {
214
+ primitive.indices = this.getAccessor(primitive.indices);
215
+ }
216
+ if (primitive.material !== undefined) {
217
+ primitive.material = this.getMaterial(primitive.material);
218
+ }
219
+ return primitive;
220
+ });
221
+ }
222
+ return mesh;
223
+ }
224
+ _resolveMaterial(material, index) {
225
+ // material = {...material};
226
+ material.id = material.id || `material-${index}`;
227
+ if (material.normalTexture) {
228
+ material.normalTexture = { ...material.normalTexture };
229
+ material.normalTexture.texture = this.getTexture(material.normalTexture.index);
230
+ }
231
+ if (material.occlusionTexture) {
232
+ material.occlustionTexture = { ...material.occlustionTexture };
233
+ material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index);
234
+ }
235
+ if (material.emissiveTexture) {
236
+ material.emmisiveTexture = { ...material.emmisiveTexture };
237
+ material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index);
238
+ }
239
+ if (!material.emissiveFactor) {
240
+ material.emissiveFactor = material.emmisiveTexture ? [1, 1, 1] : [0, 0, 0];
241
+ }
242
+ if (material.pbrMetallicRoughness) {
243
+ material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness };
244
+ const mr = material.pbrMetallicRoughness;
245
+ if (mr.baseColorTexture) {
246
+ mr.baseColorTexture = { ...mr.baseColorTexture };
247
+ mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index);
248
+ }
249
+ if (mr.metallicRoughnessTexture) {
250
+ mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture };
251
+ mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index);
252
+ }
253
+ }
254
+ return material;
255
+ }
256
+ _resolveAccessor(accessor, index) {
257
+ // accessor = {...accessor};
258
+ accessor.id = accessor.id || `accessor-${index}`;
259
+ if (accessor.bufferView !== undefined) {
260
+ // Draco encoded meshes don't have bufferView
261
+ accessor.bufferView = this.getBufferView(accessor.bufferView);
262
+ }
263
+ // Look up enums
264
+ accessor.bytesPerComponent = getBytesFromComponentType(accessor.componentType);
265
+ accessor.components = getSizeFromAccessorType(accessor.type);
266
+ accessor.bytesPerElement = accessor.bytesPerComponent * accessor.components;
267
+ // Create TypedArray for the accessor
268
+ // Note: The canonical way to instantiate is to ignore this array and create
269
+ // WebGLBuffer's using the bufferViews.
270
+ if (accessor.bufferView) {
271
+ const buffer = accessor.bufferView.buffer;
272
+ const { ArrayType, byteLength } = (0, gltf_utils_1.getAccessorArrayTypeAndLength)(accessor, accessor.bufferView);
273
+ const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset;
274
+ const cutBufffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength);
275
+ accessor.value = new ArrayType(cutBufffer);
276
+ }
277
+ return accessor;
278
+ }
279
+ _resolveTexture(texture, index) {
280
+ // texture = {...texture};
281
+ texture.id = texture.id || `texture-${index}`;
282
+ texture.sampler = 'sampler' in texture ? this.getSampler(texture.sampler) : DEFAULT_SAMPLER;
283
+ texture.source = this.getImage(texture.source);
284
+ return texture;
285
+ }
286
+ _resolveSampler(sampler, index) {
287
+ // sampler = {...sampler};
288
+ sampler.id = sampler.id || `sampler-${index}`;
289
+ // Map textual parameters to GL parameter values
290
+ sampler.parameters = {};
291
+ for (const key in sampler) {
292
+ const glEnum = this._enumSamplerParameter(key);
293
+ if (glEnum !== undefined) {
294
+ sampler.parameters[glEnum] = sampler[key];
295
+ }
296
+ }
297
+ return sampler;
298
+ }
299
+ _enumSamplerParameter(key) {
300
+ return SAMPLER_PARAMETER_GLTF_TO_GL[key];
301
+ }
302
+ _resolveImage(image, index) {
303
+ // image = {...image};
304
+ image.id = image.id || `image-${index}`;
305
+ if (image.bufferView !== undefined) {
306
+ image.bufferView = this.getBufferView(image.bufferView);
307
+ }
308
+ // Check if image has been preloaded by the GLTFLoader
309
+ // If so, link it into the JSON and drop the URI
310
+ const preloadedImage = this.images[index];
311
+ if (preloadedImage) {
312
+ image.image = preloadedImage;
313
+ }
314
+ return image;
315
+ }
316
+ _resolveBufferView(bufferView, index) {
317
+ // bufferView = {...bufferView};
318
+ bufferView.id = bufferView.id || `bufferView-${index}`;
319
+ const bufferIndex = bufferView.buffer;
320
+ bufferView.buffer = this.buffers[bufferIndex];
321
+ // @ts-expect-error
322
+ const arrayBuffer = this.buffers[bufferIndex].arrayBuffer;
323
+ // @ts-expect-error
324
+ let byteOffset = this.buffers[bufferIndex].byteOffset || 0;
325
+ if ('byteOffset' in bufferView) {
326
+ byteOffset += bufferView.byteOffset;
327
+ }
328
+ bufferView.data = new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength);
329
+ return bufferView;
330
+ }
331
+ _resolveCamera(camera, index) {
332
+ camera.id = camera.id || `camera-${index}`;
333
+ // TODO - create 4x4 matrices
334
+ if (camera.perspective) {
335
+ // camera.matrix = createPerspectiveMatrix(camera.perspective);
336
+ }
337
+ if (camera.orthographic) {
338
+ // camera.matrix = createOrthographicMatrix(camera.orthographic);
339
+ }
340
+ return camera;
341
+ }
342
+ }
343
+ function postProcessGLTF(gltf, options) {
344
+ return new GLTFPostProcessor().postProcess(gltf, options);
345
+ }
346
+ exports.postProcessGLTF = postProcessGLTF;
@@ -1 +1,2 @@
1
1
  export default function encodeGLBSync(glb: any, dataView: any, byteOffset?: number, options?: {}): number;
2
+ //# sourceMappingURL=encode-glb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode-glb.d.ts","sourceRoot":"","sources":["../../../src/lib/encoders/encode-glb.ts"],"names":[],"mappings":"AAeA,MAAM,CAAC,OAAO,UAAU,aAAa,CAAC,GAAG,KAAA,EAAE,QAAQ,KAAA,EAAE,UAAU,SAAI,EAAE,OAAO,KAAK,UA2DhF"}