@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,203 +1,271 @@
1
+ /* eslint-disable camelcase */
1
2
  import * as KHR_binary_glTF from "../extensions/KHR_binary_gltf.js";
3
+ // Binary format changes (mainly implemented by GLBLoader)
4
+ // https://github.com/KhronosGroup/glTF/tree/master/extensions/1.0/Khronos/KHR_binary_glTF
5
+ // JSON format changes:
6
+ // https://github.com/khronosgroup/gltf/issues/605
7
+ // - [x] Top-level JSON objects are arrays now
8
+ // - [ ] Removed indirection from animation: sampler now refers directly to accessors, #712
9
+ // - [ ] material.parameter.value and technique.parameter.value must be an array, #690
10
+ // - [ ] Node can have only one mesh #821
11
+ // - [ ] Added reqs on JSON encoding
12
+ // - [ ] Added reqs on binary data alignment #802 (comment)
13
+ // Additions:
14
+ // - [ ] Added accessor.normalized, #691, #706
15
+ // - [ ] Added glExtensionsUsed property and 5125 (UNSIGNED_INT) accessor.componentType value, #619
16
+ // - [ ] Added extensionsRequired property, #720, #721
17
+ // - [ ] Added "STEP" as valid animation.sampler.interpolation value, #712
18
+ // Removals:
19
+ // - [x] Removed buffer.type, #786, #629
20
+ // - [ ] Removed revision number from profile.version, #709
21
+ // - [ ] Removed technique.functions.scissor and removed 3089 (SCISSOR_TEST) as a valid value for technique.states.enable, #681
22
+ // - [ ] Techniques, programs, and shaders were moved out to KHR_technique_webgl extension.
23
+ // Other edits:
24
+ // - [x] asset is now required, #642
25
+ // - [ ] buffer.byteLength and bufferView.byteLength are now required, #560.
26
+ // - [ ] accessor.min and accessor.max are now required, #593, and clarified that the JSON value and binary data must be the same, #628.
27
+ // - [ ] Clarified animation.sampler and animation.channel restrictions, #712
28
+ // - [ ] skin.inverseBindMatrices is now optional, #461.
29
+ // - [ ] Attribute parameters can't have a value defined in the technique or parameter, #563 (comment).
30
+ // - [ ] Only TEXCOORD and COLOR attribute semantics can be written in the form [semantic]_[set_index], #563 (comment).
31
+ // - [ ] 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
32
+ // - [ ] camera.perspective.aspectRatio and camera.perspective.yfov must now be > 0, not >= 0, #563 (comment).
33
+ // - [ ] Application-specific parameter semantics must start with an underscore, e.g., _TEMPERATURE and _SIMULATION_TIME, #563 (comment).
34
+ // - [ ] Properties in technique.parameters must be defined in technique.uniforms or technique.attributes,
35
+ // #563 (comment).
36
+ // - [ ] 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
37
+ // - [ ] technique.parameter.semantic is required when the parameter is an attribute, 28e113d
38
+ // - [ ] Mesh-only models are allowed, e.g., without materials, #642
39
+ // - [ ] Skeleton hierarchies (nodes containing jointName) must be separated from non-skeleton hierarchies., #647
40
+ // - [ ] technique.states.functions.blendColor and technique.states.functions.depthRange parameters now must match WebGL function min/max, #707
2
41
  const GLTF_ARRAYS = {
3
- accessors: 'accessor',
4
- animations: 'animation',
5
- buffers: 'buffer',
6
- bufferViews: 'bufferView',
7
- images: 'image',
8
- materials: 'material',
9
- meshes: 'mesh',
10
- nodes: 'node',
11
- samplers: 'sampler',
12
- scenes: 'scene',
13
- skins: 'skin',
14
- textures: 'texture'
42
+ accessors: 'accessor',
43
+ animations: 'animation',
44
+ buffers: 'buffer',
45
+ bufferViews: 'bufferView',
46
+ images: 'image',
47
+ materials: 'material',
48
+ meshes: 'mesh',
49
+ nodes: 'node',
50
+ samplers: 'sampler',
51
+ scenes: 'scene',
52
+ skins: 'skin',
53
+ textures: 'texture'
15
54
  };
16
55
  const GLTF_KEYS = {
17
- accessor: 'accessors',
18
- animations: 'animation',
19
- buffer: 'buffers',
20
- bufferView: 'bufferViews',
21
- image: 'images',
22
- material: 'materials',
23
- mesh: 'meshes',
24
- node: 'nodes',
25
- sampler: 'samplers',
26
- scene: 'scenes',
27
- skin: 'skins',
28
- texture: 'textures'
56
+ accessor: 'accessors',
57
+ animations: 'animation',
58
+ buffer: 'buffers',
59
+ bufferView: 'bufferViews',
60
+ image: 'images',
61
+ material: 'materials',
62
+ mesh: 'meshes',
63
+ node: 'nodes',
64
+ sampler: 'samplers',
65
+ scene: 'scenes',
66
+ skin: 'skins',
67
+ texture: 'textures'
29
68
  };
69
+ /**
70
+ * Converts (normalizes) glTF v1 to v2
71
+ */
30
72
  class GLTFV1Normalizer {
31
- constructor() {
32
- this.idToIndexMap = {
33
- animations: {},
34
- accessors: {},
35
- buffers: {},
36
- bufferViews: {},
37
- images: {},
38
- materials: {},
39
- meshes: {},
40
- nodes: {},
41
- samplers: {},
42
- scenes: {},
43
- skins: {},
44
- textures: {}
73
+ idToIndexMap = {
74
+ animations: {},
75
+ accessors: {},
76
+ buffers: {},
77
+ bufferViews: {},
78
+ images: {},
79
+ materials: {},
80
+ meshes: {},
81
+ nodes: {},
82
+ samplers: {},
83
+ scenes: {},
84
+ skins: {},
85
+ textures: {}
45
86
  };
46
- this.json = void 0;
47
- }
48
- normalize(gltf, options) {
49
- this.json = gltf.json;
50
- const json = gltf.json;
51
- switch (json.asset && json.asset.version) {
52
- case '2.0':
53
- return;
54
- case undefined:
55
- case '1.0':
56
- break;
57
- default:
58
- console.warn(`glTF: Unknown version ${json.asset.version}`);
59
- return;
87
+ json;
88
+ // constructor() {}
89
+ /**
90
+ * Convert (normalize) glTF < 2.0 to glTF 2.0
91
+ * @param gltf - object with json and binChunks
92
+ * @param options
93
+ * @param options normalize Whether to actually normalize
94
+ */
95
+ normalize(gltf, options) {
96
+ this.json = gltf.json;
97
+ const json = gltf.json;
98
+ // Check version
99
+ switch (json.asset && json.asset.version) {
100
+ // We are converting to v2 format. Return if there is nothing to do
101
+ case '2.0':
102
+ return;
103
+ // This class is written to convert 1.0
104
+ case undefined:
105
+ case '1.0':
106
+ break;
107
+ default:
108
+ // eslint-disable-next-line no-undef, no-console
109
+ console.warn(`glTF: Unknown version ${json.asset.version}`);
110
+ return;
111
+ }
112
+ if (!options.normalize) {
113
+ // We are still missing a few conversion tricks, remove once addressed
114
+ throw new Error('glTF v1 is not supported.');
115
+ }
116
+ // eslint-disable-next-line no-undef, no-console
117
+ console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
118
+ this._addAsset(json);
119
+ // In glTF2 top-level fields are Arrays not Object maps
120
+ this._convertTopLevelObjectsToArrays(json);
121
+ // Extract bufferView indices for images
122
+ // (this extension needs to be invoked early in the normalization process)
123
+ // TODO can this be handled by standard extension processing instead of called explicitly?
124
+ KHR_binary_glTF.preprocess(gltf);
125
+ // Convert object references from ids to indices
126
+ this._convertObjectIdsToArrayIndices(json);
127
+ this._updateObjects(json);
128
+ this._updateMaterial(json);
60
129
  }
61
- if (!options.normalize) {
62
- throw new Error('glTF v1 is not supported.');
130
+ // asset is now required, #642 https://github.com/KhronosGroup/glTF/issues/639
131
+ _addAsset(json) {
132
+ json.asset = json.asset || {};
133
+ // We are normalizing to glTF v2, so change version to "2.0"
134
+ json.asset.version = '2.0';
135
+ json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
63
136
  }
64
- console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.');
65
- this._addAsset(json);
66
- this._convertTopLevelObjectsToArrays(json);
67
- KHR_binary_glTF.preprocess(gltf);
68
- this._convertObjectIdsToArrayIndices(json);
69
- this._updateObjects(json);
70
- this._updateMaterial(json);
71
- }
72
- _addAsset(json) {
73
- json.asset = json.asset || {};
74
- json.asset.version = '2.0';
75
- json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl';
76
- }
77
- _convertTopLevelObjectsToArrays(json) {
78
- for (const arrayName in GLTF_ARRAYS) {
79
- this._convertTopLevelObjectToArray(json, arrayName);
137
+ _convertTopLevelObjectsToArrays(json) {
138
+ // TODO check that all arrays are covered
139
+ for (const arrayName in GLTF_ARRAYS) {
140
+ this._convertTopLevelObjectToArray(json, arrayName);
141
+ }
80
142
  }
81
- }
82
- _convertTopLevelObjectToArray(json, mapName) {
83
- const objectMap = json[mapName];
84
- if (!objectMap || Array.isArray(objectMap)) {
85
- return;
143
+ /** Convert one top level object to array */
144
+ _convertTopLevelObjectToArray(json, mapName) {
145
+ const objectMap = json[mapName];
146
+ if (!objectMap || Array.isArray(objectMap)) {
147
+ return;
148
+ }
149
+ // Rewrite the top-level field as an array
150
+ json[mapName] = [];
151
+ // Copy the map key into object.id
152
+ for (const id in objectMap) {
153
+ const object = objectMap[id];
154
+ object.id = object.id || id; // Mutates the loaded object
155
+ const index = json[mapName].length;
156
+ json[mapName].push(object);
157
+ this.idToIndexMap[mapName][id] = index;
158
+ }
86
159
  }
87
- json[mapName] = [];
88
- for (const id in objectMap) {
89
- const object = objectMap[id];
90
- object.id = object.id || id;
91
- const index = json[mapName].length;
92
- json[mapName].push(object);
93
- this.idToIndexMap[mapName][id] = index;
160
+ /** Go through all objects in all top-level arrays and replace ids with indices */
161
+ _convertObjectIdsToArrayIndices(json) {
162
+ for (const arrayName in GLTF_ARRAYS) {
163
+ this._convertIdsToIndices(json, arrayName);
164
+ }
165
+ if ('scene' in json) {
166
+ json.scene = this._convertIdToIndex(json.scene, 'scene');
167
+ }
168
+ // Convert any index references that are not using array names
169
+ // texture.source (image)
170
+ for (const texture of json.textures) {
171
+ this._convertTextureIds(texture);
172
+ }
173
+ for (const mesh of json.meshes) {
174
+ this._convertMeshIds(mesh);
175
+ }
176
+ for (const node of json.nodes) {
177
+ this._convertNodeIds(node);
178
+ }
179
+ for (const node of json.scenes) {
180
+ this._convertSceneIds(node);
181
+ }
94
182
  }
95
- }
96
- _convertObjectIdsToArrayIndices(json) {
97
- for (const arrayName in GLTF_ARRAYS) {
98
- this._convertIdsToIndices(json, arrayName);
183
+ _convertTextureIds(texture) {
184
+ if (texture.source) {
185
+ texture.source = this._convertIdToIndex(texture.source, 'image');
186
+ }
99
187
  }
100
- if ('scene' in json) {
101
- json.scene = this._convertIdToIndex(json.scene, 'scene');
188
+ _convertMeshIds(mesh) {
189
+ for (const primitive of mesh.primitives) {
190
+ const { attributes, indices, material } = primitive;
191
+ for (const attributeName in attributes) {
192
+ attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
193
+ }
194
+ if (indices) {
195
+ primitive.indices = this._convertIdToIndex(indices, 'accessor');
196
+ }
197
+ if (material) {
198
+ primitive.material = this._convertIdToIndex(material, 'material');
199
+ }
200
+ }
102
201
  }
103
- for (const texture of json.textures) {
104
- this._convertTextureIds(texture);
202
+ _convertNodeIds(node) {
203
+ if (node.children) {
204
+ node.children = node.children.map((child) => this._convertIdToIndex(child, 'node'));
205
+ }
206
+ if (node.meshes) {
207
+ node.meshes = node.meshes.map((mesh) => this._convertIdToIndex(mesh, 'mesh'));
208
+ }
105
209
  }
106
- for (const mesh of json.meshes) {
107
- this._convertMeshIds(mesh);
210
+ _convertSceneIds(scene) {
211
+ if (scene.nodes) {
212
+ scene.nodes = scene.nodes.map((node) => this._convertIdToIndex(node, 'node'));
213
+ }
108
214
  }
109
- for (const node of json.nodes) {
110
- this._convertNodeIds(node);
215
+ /** Go through all objects in a top-level array and replace ids with indices */
216
+ _convertIdsToIndices(json, topLevelArrayName) {
217
+ if (!json[topLevelArrayName]) {
218
+ console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`); // eslint-disable-line no-console, no-undef
219
+ json[topLevelArrayName] = [];
220
+ }
221
+ for (const object of json[topLevelArrayName]) {
222
+ for (const key in object) {
223
+ const id = object[key];
224
+ const index = this._convertIdToIndex(id, key);
225
+ object[key] = index;
226
+ }
227
+ }
111
228
  }
112
- for (const node of json.scenes) {
113
- this._convertSceneIds(node);
229
+ _convertIdToIndex(id, key) {
230
+ const arrayName = GLTF_KEYS[key];
231
+ if (arrayName in this.idToIndexMap) {
232
+ const index = this.idToIndexMap[arrayName][id];
233
+ if (!Number.isFinite(index)) {
234
+ throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);
235
+ }
236
+ return index;
237
+ }
238
+ return id;
114
239
  }
115
- }
116
- _convertTextureIds(texture) {
117
- if (texture.source) {
118
- texture.source = this._convertIdToIndex(texture.source, 'image');
240
+ /**
241
+ *
242
+ * @param {*} json
243
+ */
244
+ _updateObjects(json) {
245
+ for (const buffer of this.json.buffers) {
246
+ // - [x] Removed buffer.type, #786, #629
247
+ delete buffer.type;
248
+ }
119
249
  }
120
- }
121
- _convertMeshIds(mesh) {
122
- for (const primitive of mesh.primitives) {
123
- const {
124
- attributes,
125
- indices,
126
- material
127
- } = primitive;
128
- for (const attributeName in attributes) {
129
- attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor');
130
- }
131
- if (indices) {
132
- primitive.indices = this._convertIdToIndex(indices, 'accessor');
133
- }
134
- if (material) {
135
- primitive.material = this._convertIdToIndex(material, 'material');
136
- }
250
+ /**
251
+ * Update material (set pbrMetallicRoughness)
252
+ * @param {*} json
253
+ */
254
+ _updateMaterial(json) {
255
+ for (const material of json.materials) {
256
+ material.pbrMetallicRoughness = {
257
+ baseColorFactor: [1, 1, 1, 1],
258
+ metallicFactor: 1,
259
+ roughnessFactor: 1
260
+ };
261
+ const textureId = material.values?.tex || material.values?.texture2d_0 || material.values?.diffuseTex;
262
+ const textureIndex = json.textures.findIndex((texture) => texture.id === textureId);
263
+ if (textureIndex !== -1) {
264
+ material.pbrMetallicRoughness.baseColorTexture = { index: textureIndex };
265
+ }
266
+ }
137
267
  }
138
- }
139
- _convertNodeIds(node) {
140
- if (node.children) {
141
- node.children = node.children.map(child => this._convertIdToIndex(child, 'node'));
142
- }
143
- if (node.meshes) {
144
- node.meshes = node.meshes.map(mesh => this._convertIdToIndex(mesh, 'mesh'));
145
- }
146
- }
147
- _convertSceneIds(scene) {
148
- if (scene.nodes) {
149
- scene.nodes = scene.nodes.map(node => this._convertIdToIndex(node, 'node'));
150
- }
151
- }
152
- _convertIdsToIndices(json, topLevelArrayName) {
153
- if (!json[topLevelArrayName]) {
154
- console.warn(`gltf v1: json doesn't contain attribute ${topLevelArrayName}`);
155
- json[topLevelArrayName] = [];
156
- }
157
- for (const object of json[topLevelArrayName]) {
158
- for (const key in object) {
159
- const id = object[key];
160
- const index = this._convertIdToIndex(id, key);
161
- object[key] = index;
162
- }
163
- }
164
- }
165
- _convertIdToIndex(id, key) {
166
- const arrayName = GLTF_KEYS[key];
167
- if (arrayName in this.idToIndexMap) {
168
- const index = this.idToIndexMap[arrayName][id];
169
- if (!Number.isFinite(index)) {
170
- throw new Error(`gltf v1: failed to resolve ${key} with id ${id}`);
171
- }
172
- return index;
173
- }
174
- return id;
175
- }
176
- _updateObjects(json) {
177
- for (const buffer of this.json.buffers) {
178
- delete buffer.type;
179
- }
180
- }
181
- _updateMaterial(json) {
182
- for (const material of json.materials) {
183
- var _material$values, _material$values2, _material$values3;
184
- material.pbrMetallicRoughness = {
185
- baseColorFactor: [1, 1, 1, 1],
186
- metallicFactor: 1,
187
- roughnessFactor: 1
188
- };
189
- const textureId = ((_material$values = material.values) === null || _material$values === void 0 ? void 0 : _material$values.tex) || ((_material$values2 = material.values) === null || _material$values2 === void 0 ? void 0 : _material$values2.texture2d_0) || ((_material$values3 = material.values) === null || _material$values3 === void 0 ? void 0 : _material$values3.diffuseTex);
190
- const textureIndex = json.textures.findIndex(texture => texture.id === textureId);
191
- if (textureIndex !== -1) {
192
- material.pbrMetallicRoughness.baseColorTexture = {
193
- index: textureIndex
194
- };
195
- }
196
- }
197
- }
198
268
  }
199
- export function normalizeGLTFV1(gltf) {
200
- let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
201
- return new GLTFV1Normalizer().normalize(gltf, options);
269
+ export function normalizeGLTFV1(gltf, options = {}) {
270
+ return new GLTFV1Normalizer().normalize(gltf, options);
202
271
  }
203
- //# sourceMappingURL=normalize-gltf-v1.js.map
@@ -1,5 +1,5 @@
1
- import type { GLTFWithBuffers } from '../types/gltf-types';
2
- import type { ParseGLTFOptions } from '../parsers/parse-gltf';
3
- import type { GLTFPostprocessed } from '../types/gltf-postprocessed-schema';
1
+ import type { GLTFWithBuffers } from "../types/gltf-types.js";
2
+ import type { ParseGLTFOptions } from "../parsers/parse-gltf.js";
3
+ import type { GLTFPostprocessed } from "../types/gltf-postprocessed-schema.js";
4
4
  export declare function postProcessGLTF(gltf: GLTFWithBuffers, options?: ParseGLTFOptions): GLTFPostprocessed;
5
5
  //# sourceMappingURL=post-process-gltf.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"post-process-gltf.d.ts","sourceRoot":"","sources":["../../../src/lib/api/post-process-gltf.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAC,gBAAgB,EAAC,MAAM,uBAAuB,CAAC;AAiB5D,OAAO,KAAK,EACV,iBAAiB,EAclB,MAAM,oCAAoC,CAAC;AAmgB5C,wBAAgB,eAAe,CAC7B,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,gBAAgB,GACzB,iBAAiB,CAEnB"}
1
+ {"version":3,"file":"post-process-gltf.d.ts","sourceRoot":"","sources":["../../../src/lib/api/post-process-gltf.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAC,eAAe,EAAC,+BAA4B;AACzD,OAAO,KAAK,EAAC,gBAAgB,EAAC,iCAA8B;AAiB5D,OAAO,KAAK,EACV,iBAAiB,EAclB,8CAA2C;AAmgB5C,wBAAgB,eAAe,CAC7B,IAAI,EAAE,eAAe,EACrB,OAAO,CAAC,EAAE,gBAAgB,GACzB,iBAAiB,CAEnB"}