@babylonjs/loaders 5.0.0-rc.0 → 5.0.0-rc.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. package/OBJ/index.d.ts +4 -4
  2. package/OBJ/index.js +4 -4
  3. package/OBJ/index.js.map +1 -1
  4. package/OBJ/mtlFileLoader.d.ts +41 -40
  5. package/OBJ/mtlFileLoader.js +232 -231
  6. package/OBJ/mtlFileLoader.js.map +1 -1
  7. package/OBJ/objFileLoader.d.ts +126 -133
  8. package/OBJ/objFileLoader.js +296 -300
  9. package/OBJ/objFileLoader.js.map +1 -1
  10. package/OBJ/objLoadingOptions.d.ts +43 -43
  11. package/OBJ/objLoadingOptions.js +1 -1
  12. package/OBJ/objLoadingOptions.js.map +1 -1
  13. package/OBJ/solidParser.d.ts +154 -153
  14. package/OBJ/solidParser.js +693 -699
  15. package/OBJ/solidParser.js.map +1 -1
  16. package/STL/index.d.ts +1 -1
  17. package/STL/index.js +1 -1
  18. package/STL/index.js.map +1 -1
  19. package/STL/stlFileLoader.d.ts +64 -71
  20. package/STL/stlFileLoader.js +233 -239
  21. package/STL/stlFileLoader.js.map +1 -1
  22. package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
  23. package/glTF/1.0/glTFBinaryExtension.js +60 -61
  24. package/glTF/1.0/glTFBinaryExtension.js.map +1 -1
  25. package/glTF/1.0/glTFLoader.d.ts +136 -106
  26. package/glTF/1.0/glTFLoader.js +1814 -1696
  27. package/glTF/1.0/glTFLoader.js.map +1 -1
  28. package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
  29. package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
  30. package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -1
  31. package/glTF/1.0/glTFLoaderUtils.d.ts +63 -62
  32. package/glTF/1.0/glTFLoaderUtils.js +250 -232
  33. package/glTF/1.0/glTFLoaderUtils.js.map +1 -1
  34. package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
  35. package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -127
  36. package/glTF/1.0/glTFMaterialsCommonExtension.js.map +1 -1
  37. package/glTF/1.0/index.d.ts +5 -5
  38. package/glTF/1.0/index.js +5 -5
  39. package/glTF/1.0/index.js.map +1 -1
  40. package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -36
  41. package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -110
  42. package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -1
  43. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -28
  44. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -80
  45. package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -1
  46. package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -27
  47. package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -42
  48. package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -1
  49. package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -21
  50. package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -32
  51. package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -1
  52. package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -32
  53. package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -55
  54. package/glTF/2.0/Extensions/ExtrasAsMetadata.js.map +1 -1
  55. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -31
  56. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -101
  57. package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -1
  58. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -28
  59. package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -81
  60. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -1
  61. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -31
  62. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -86
  63. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -1
  64. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -30
  65. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -45
  66. package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js.map +1 -1
  67. package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -34
  68. package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -54
  69. package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -1
  70. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -30
  71. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -72
  72. package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -1
  73. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -31
  74. package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -76
  75. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -1
  76. package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -30
  77. package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -66
  78. package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -1
  79. package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -31
  80. package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -76
  81. package/glTF/2.0/Extensions/KHR_materials_translucency.js.map +1 -1
  82. package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -30
  83. package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -277
  84. package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
  85. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -30
  86. package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -65
  87. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +1 -1
  88. package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -76
  89. package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -243
  90. package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -1
  91. package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -31
  92. package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -78
  93. package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -1
  94. package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -19
  95. package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -21
  96. package/glTF/2.0/Extensions/KHR_mesh_quantization.js.map +1 -1
  97. package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -21
  98. package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -32
  99. package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -1
  100. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -25
  101. package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -53
  102. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +1 -1
  103. package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -29
  104. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -46
  105. package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -1
  106. package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -38
  107. package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -216
  108. package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -1
  109. package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -75
  110. package/glTF/2.0/Extensions/MSFT_lod.js +369 -333
  111. package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -1
  112. package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
  113. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
  114. package/glTF/2.0/Extensions/MSFT_minecraftMesh.js.map +1 -1
  115. package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
  116. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
  117. package/glTF/2.0/Extensions/MSFT_sRGBFactors.js.map +1 -1
  118. package/glTF/2.0/Extensions/index.d.ts +26 -26
  119. package/glTF/2.0/Extensions/index.js +26 -26
  120. package/glTF/2.0/Extensions/index.js.map +1 -1
  121. package/glTF/2.0/glTFLoader.d.ts +406 -357
  122. package/glTF/2.0/glTFLoader.js +2366 -2266
  123. package/glTF/2.0/glTFLoader.js.map +1 -1
  124. package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
  125. package/glTF/2.0/glTFLoaderExtension.js +1 -1
  126. package/glTF/2.0/glTFLoaderExtension.js.map +1 -1
  127. package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -209
  128. package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
  129. package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -1
  130. package/glTF/2.0/index.d.ts +4 -4
  131. package/glTF/2.0/index.js +5 -4
  132. package/glTF/2.0/index.js.map +1 -1
  133. package/glTF/glTFFileLoader.d.ts +433 -373
  134. package/glTF/glTFFileLoader.js +927 -870
  135. package/glTF/glTFFileLoader.js.map +1 -1
  136. package/glTF/glTFValidation.d.ts +29 -29
  137. package/glTF/glTFValidation.js +123 -121
  138. package/glTF/glTFValidation.js.map +1 -1
  139. package/glTF/index.d.ts +5 -5
  140. package/glTF/index.js +6 -5
  141. package/glTF/index.js.map +1 -1
  142. package/index.d.ts +3 -3
  143. package/index.js +4 -3
  144. package/index.js.map +1 -1
  145. package/legacy/legacy-glTF.d.ts +2 -2
  146. package/legacy/legacy-glTF.js +18 -18
  147. package/legacy/legacy-glTF.js.map +1 -1
  148. package/legacy/legacy-glTF1.d.ts +2 -2
  149. package/legacy/legacy-glTF1.js +15 -14
  150. package/legacy/legacy-glTF1.js.map +1 -1
  151. package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
  152. package/legacy/legacy-glTF1FileLoader.js +3 -2
  153. package/legacy/legacy-glTF1FileLoader.js.map +1 -1
  154. package/legacy/legacy-glTF2.d.ts +2 -2
  155. package/legacy/legacy-glTF2.js +33 -32
  156. package/legacy/legacy-glTF2.js.map +1 -1
  157. package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
  158. package/legacy/legacy-glTF2FileLoader.js +3 -2
  159. package/legacy/legacy-glTF2FileLoader.js.map +1 -1
  160. package/legacy/legacy-glTFFileLoader.d.ts +3 -3
  161. package/legacy/legacy-glTFFileLoader.js +4 -3
  162. package/legacy/legacy-glTFFileLoader.js.map +1 -1
  163. package/legacy/legacy-objFileLoader.d.ts +1 -1
  164. package/legacy/legacy-objFileLoader.js +13 -12
  165. package/legacy/legacy-objFileLoader.js.map +1 -1
  166. package/legacy/legacy-stlFileLoader.d.ts +1 -1
  167. package/legacy/legacy-stlFileLoader.js +13 -12
  168. package/legacy/legacy-stlFileLoader.js.map +1 -1
  169. package/legacy/legacy.d.ts +6 -6
  170. package/legacy/legacy.js +8 -6
  171. package/legacy/legacy.js.map +1 -1
  172. package/package.json +23 -198
  173. package/readme.md +2 -2
@@ -1,244 +1,255 @@
1
- import { GLTFLoader, ArrayItem } from "../glTFLoader.js";
2
- import { Mesh } from '@babylonjs/core/Meshes/mesh.js';
3
- var NAME = "KHR_materials_variants";
4
- /**
5
- * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_variants/README.md)
6
- */
7
- var KHR_materials_variants = /** @class */ (function () {
8
- /** @hidden */
9
- function KHR_materials_variants(loader) {
10
- /**
11
- * The name of this extension.
12
- */
13
- this.name = NAME;
14
- this._loader = loader;
15
- this.enabled = this._loader.isExtensionUsed(NAME);
16
- }
17
- /** @hidden */
18
- KHR_materials_variants.prototype.dispose = function () {
19
- this._loader = null;
20
- };
21
- /**
22
- * Gets the list of available variant names for this asset.
23
- * @param rootMesh The glTF root mesh
24
- * @returns the list of all the variant names for this model
25
- */
26
- KHR_materials_variants.GetAvailableVariants = function (rootMesh) {
27
- var extensionMetadata = this._GetExtensionMetadata(rootMesh);
28
- if (!extensionMetadata) {
29
- return [];
30
- }
31
- return Object.keys(extensionMetadata.variants);
32
- };
33
- /**
34
- * Gets the list of available variant names for this asset.
35
- * @param rootMesh The glTF root mesh
36
- * @returns the list of all the variant names for this model
37
- */
38
- KHR_materials_variants.prototype.getAvailableVariants = function (rootMesh) {
39
- return KHR_materials_variants.GetAvailableVariants(rootMesh);
40
- };
41
- /**
42
- * Select a variant given a variant name or a list of variant names.
43
- * @param rootMesh The glTF root mesh
44
- * @param variantName The variant name(s) to select.
45
- */
46
- KHR_materials_variants.SelectVariant = function (rootMesh, variantName) {
47
- var extensionMetadata = this._GetExtensionMetadata(rootMesh);
48
- if (!extensionMetadata) {
49
- throw new Error("Cannot select variant on a glTF mesh that does not have the ".concat(NAME, " extension"));
50
- }
51
- var select = function (variantName) {
52
- var entries = extensionMetadata.variants[variantName];
53
- if (entries) {
54
- for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
55
- var entry = entries_1[_i];
56
- entry.mesh.material = entry.material;
57
- }
58
- }
59
- };
60
- if (variantName instanceof Array) {
61
- for (var _i = 0, variantName_1 = variantName; _i < variantName_1.length; _i++) {
62
- var name_1 = variantName_1[_i];
63
- select(name_1);
64
- }
65
- }
66
- else {
67
- select(variantName);
68
- }
69
- extensionMetadata.lastSelected = variantName;
70
- };
71
- /**
72
- * Select a variant given a variant name or a list of variant names.
73
- * @param rootMesh The glTF root mesh
74
- * @param variantName The variant name(s) to select.
75
- */
76
- KHR_materials_variants.prototype.selectVariant = function (rootMesh, variantName) {
77
- return KHR_materials_variants.SelectVariant(rootMesh, variantName);
78
- };
79
- /**
80
- * Reset back to the original before selecting a variant.
81
- * @param rootMesh The glTF root mesh
82
- */
83
- KHR_materials_variants.Reset = function (rootMesh) {
84
- var extensionMetadata = this._GetExtensionMetadata(rootMesh);
85
- if (!extensionMetadata) {
86
- throw new Error("Cannot reset on a glTF mesh that does not have the ".concat(NAME, " extension"));
87
- }
88
- for (var _i = 0, _a = extensionMetadata.original; _i < _a.length; _i++) {
89
- var entry = _a[_i];
90
- entry.mesh.material = entry.material;
91
- }
92
- extensionMetadata.lastSelected = null;
93
- };
94
- /**
95
- * Reset back to the original before selecting a variant.
96
- * @param rootMesh The glTF root mesh
97
- */
98
- KHR_materials_variants.prototype.reset = function (rootMesh) {
99
- return KHR_materials_variants.Reset(rootMesh);
100
- };
101
- /**
102
- * Gets the last selected variant name(s) or null if original.
103
- * @param rootMesh The glTF root mesh
104
- * @returns The selected variant name(s).
105
- */
106
- KHR_materials_variants.GetLastSelectedVariant = function (rootMesh) {
107
- var extensionMetadata = this._GetExtensionMetadata(rootMesh);
108
- if (!extensionMetadata) {
109
- throw new Error("Cannot get the last selected variant on a glTF mesh that does not have the ".concat(NAME, " extension"));
110
- }
111
- return extensionMetadata.lastSelected;
112
- };
113
- /**
114
- * Gets the last selected variant name(s) or null if original.
115
- * @param rootMesh The glTF root mesh
116
- * @returns The selected variant name(s).
117
- */
118
- KHR_materials_variants.prototype.getLastSelectedVariant = function (rootMesh) {
119
- return KHR_materials_variants.GetLastSelectedVariant(rootMesh);
120
- };
121
- KHR_materials_variants._GetExtensionMetadata = function (rootMesh) {
122
- var _a, _b;
123
- return ((_b = (_a = rootMesh === null || rootMesh === void 0 ? void 0 : rootMesh.metadata) === null || _a === void 0 ? void 0 : _a.gltf) === null || _b === void 0 ? void 0 : _b[NAME]) || null;
124
- };
125
- /** @hidden */
126
- KHR_materials_variants.prototype.onLoading = function () {
127
- var extensions = this._loader.gltf.extensions;
128
- if (extensions && extensions[this.name]) {
129
- var extension = extensions[this.name];
130
- this._variants = extension.variants;
131
- }
132
- };
133
- /** @hidden */
134
- KHR_materials_variants.prototype._loadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
135
- var _this = this;
136
- return GLTFLoader.LoadExtensionAsync(context, primitive, this.name, function (extensionContext, extension) {
137
- var promises = new Array();
138
- promises.push(_this._loader._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, function (babylonMesh) {
139
- assign(babylonMesh);
140
- if (babylonMesh instanceof Mesh) {
141
- var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
142
- var root_1 = _this._loader.rootBabylonMesh;
143
- var metadata = (root_1 ? root_1.metadata = root_1.metadata || {} : {});
144
- var gltf = (metadata.gltf = metadata.gltf || {});
145
- var extensionMetadata_1 = (gltf[NAME] = gltf[NAME] || { lastSelected: null, original: [], variants: {} });
146
- // Store the original material.
147
- extensionMetadata_1.original.push({ mesh: babylonMesh, material: babylonMesh.material });
148
- var _loop_1 = function (mappingIndex) {
149
- var mapping = extension.mappings[mappingIndex];
150
- var material = ArrayItem.Get("".concat(extensionContext, "/mappings/").concat(mappingIndex, "/material"), _this._loader.gltf.materials, mapping.material);
151
- promises.push(_this._loader._loadMaterialAsync("#/materials/".concat(mapping.material), material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
152
- var _loop_2 = function (mappingVariantIndex) {
153
- var variantIndex = mapping.variants[mappingVariantIndex];
154
- var variant = ArrayItem.Get("/extensions/".concat(NAME, "/variants/").concat(variantIndex), _this._variants, variantIndex);
155
- extensionMetadata_1.variants[variant.name] = extensionMetadata_1.variants[variant.name] || [];
156
- extensionMetadata_1.variants[variant.name].push({
157
- mesh: babylonMesh,
158
- material: babylonMaterial
159
- });
160
- // Replace the target when original mesh is cloned
161
- babylonMesh.onClonedObservable.add(function (newOne) {
162
- var newMesh = newOne;
163
- var metadata = null;
164
- var newRoot = newMesh;
165
- // Find root to get medata
166
- do {
167
- newRoot = newRoot.parent;
168
- if (!newRoot) {
169
- return;
170
- }
171
- metadata = KHR_materials_variants._GetExtensionMetadata(newRoot);
172
- } while (metadata === null);
173
- // Need to clone the metadata on the root (first time only)
174
- if (root_1 && metadata === KHR_materials_variants._GetExtensionMetadata(root_1)) {
175
- // Copy main metadata
176
- newRoot.metadata = {};
177
- for (var key in root_1.metadata) {
178
- newRoot.metadata[key] = root_1.metadata[key];
179
- }
180
- // Copy the gltf metadata
181
- newRoot.metadata.gltf = [];
182
- for (var key in root_1.metadata.gltf) {
183
- newRoot.metadata.gltf[key] = root_1.metadata.gltf[key];
184
- }
185
- // Duplicate the extension specific metadata
186
- newRoot.metadata.gltf[NAME] = { lastSelected: null, original: [], variants: {} };
187
- for (var _i = 0, _a = metadata.original; _i < _a.length; _i++) {
188
- var original = _a[_i];
189
- newRoot.metadata.gltf[NAME].original.push({
190
- mesh: original.mesh,
191
- material: original.material
192
- });
193
- }
194
- for (var key in metadata.variants) {
195
- if (metadata.variants.hasOwnProperty(key)) {
196
- newRoot.metadata.gltf[NAME].variants[key] = [];
197
- for (var _b = 0, _c = metadata.variants[key]; _b < _c.length; _b++) {
198
- var variantEntry = _c[_b];
199
- newRoot.metadata.gltf[NAME].variants[key].push({
200
- mesh: variantEntry.mesh,
201
- material: variantEntry.material
202
- });
203
- }
204
- }
205
- }
206
- metadata = newRoot.metadata.gltf[NAME];
207
- }
208
- // Relocate
209
- for (var _d = 0, _e = metadata.original; _d < _e.length; _d++) {
210
- var target = _e[_d];
211
- if (target.mesh === babylonMesh) {
212
- target.mesh = newMesh;
213
- }
214
- }
215
- for (var _f = 0, _g = metadata.variants[variant.name]; _f < _g.length; _f++) {
216
- var target = _g[_f];
217
- if (target.mesh === babylonMesh) {
218
- target.mesh = newMesh;
219
- }
220
- }
221
- });
222
- };
223
- for (var mappingVariantIndex = 0; mappingVariantIndex < mapping.variants.length; ++mappingVariantIndex) {
224
- _loop_2(mappingVariantIndex);
225
- }
226
- }));
227
- };
228
- // For each mapping, look at the variants and make a new entry for them.
229
- for (var mappingIndex = 0; mappingIndex < extension.mappings.length; ++mappingIndex) {
230
- _loop_1(mappingIndex);
231
- }
232
- }
233
- }));
234
- return Promise.all(promises).then(function (_a) {
235
- var babylonMesh = _a[0];
236
- return babylonMesh;
237
- });
238
- });
239
- };
240
- return KHR_materials_variants;
241
- }());
242
- export { KHR_materials_variants };
243
- GLTFLoader.RegisterExtension(NAME, function (loader) { return new KHR_materials_variants(loader); });
1
+ import { GLTFLoader, ArrayItem } from "../glTFLoader";
2
+ import { Mesh } from "@babylonjs/core/Meshes/mesh.js";
3
+ var NAME = "KHR_materials_variants";
4
+ /**
5
+ * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_variants/README.md)
6
+ */
7
+ var KHR_materials_variants = /** @class */ (function () {
8
+ /**
9
+ * @param loader
10
+ * @hidden
11
+ */
12
+ function KHR_materials_variants(loader) {
13
+ /**
14
+ * The name of this extension.
15
+ */
16
+ this.name = NAME;
17
+ this._loader = loader;
18
+ this.enabled = this._loader.isExtensionUsed(NAME);
19
+ }
20
+ /** @hidden */
21
+ KHR_materials_variants.prototype.dispose = function () {
22
+ this._loader = null;
23
+ };
24
+ /**
25
+ * Gets the list of available variant names for this asset.
26
+ * @param rootMesh The glTF root mesh
27
+ * @returns the list of all the variant names for this model
28
+ */
29
+ KHR_materials_variants.GetAvailableVariants = function (rootMesh) {
30
+ var extensionMetadata = this._GetExtensionMetadata(rootMesh);
31
+ if (!extensionMetadata) {
32
+ return [];
33
+ }
34
+ return Object.keys(extensionMetadata.variants);
35
+ };
36
+ /**
37
+ * Gets the list of available variant names for this asset.
38
+ * @param rootMesh The glTF root mesh
39
+ * @returns the list of all the variant names for this model
40
+ */
41
+ KHR_materials_variants.prototype.getAvailableVariants = function (rootMesh) {
42
+ return KHR_materials_variants.GetAvailableVariants(rootMesh);
43
+ };
44
+ /**
45
+ * Select a variant given a variant name or a list of variant names.
46
+ * @param rootMesh The glTF root mesh
47
+ * @param variantName The variant name(s) to select.
48
+ */
49
+ KHR_materials_variants.SelectVariant = function (rootMesh, variantName) {
50
+ var extensionMetadata = this._GetExtensionMetadata(rootMesh);
51
+ if (!extensionMetadata) {
52
+ throw new Error("Cannot select variant on a glTF mesh that does not have the ".concat(NAME, " extension"));
53
+ }
54
+ var select = function (variantName) {
55
+ var entries = extensionMetadata.variants[variantName];
56
+ if (entries) {
57
+ for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
58
+ var entry = entries_1[_i];
59
+ entry.mesh.material = entry.material;
60
+ }
61
+ }
62
+ };
63
+ if (variantName instanceof Array) {
64
+ for (var _i = 0, variantName_1 = variantName; _i < variantName_1.length; _i++) {
65
+ var name_1 = variantName_1[_i];
66
+ select(name_1);
67
+ }
68
+ }
69
+ else {
70
+ select(variantName);
71
+ }
72
+ extensionMetadata.lastSelected = variantName;
73
+ };
74
+ /**
75
+ * Select a variant given a variant name or a list of variant names.
76
+ * @param rootMesh The glTF root mesh
77
+ * @param variantName The variant name(s) to select.
78
+ */
79
+ KHR_materials_variants.prototype.selectVariant = function (rootMesh, variantName) {
80
+ return KHR_materials_variants.SelectVariant(rootMesh, variantName);
81
+ };
82
+ /**
83
+ * Reset back to the original before selecting a variant.
84
+ * @param rootMesh The glTF root mesh
85
+ */
86
+ KHR_materials_variants.Reset = function (rootMesh) {
87
+ var extensionMetadata = this._GetExtensionMetadata(rootMesh);
88
+ if (!extensionMetadata) {
89
+ throw new Error("Cannot reset on a glTF mesh that does not have the ".concat(NAME, " extension"));
90
+ }
91
+ for (var _i = 0, _a = extensionMetadata.original; _i < _a.length; _i++) {
92
+ var entry = _a[_i];
93
+ entry.mesh.material = entry.material;
94
+ }
95
+ extensionMetadata.lastSelected = null;
96
+ };
97
+ /**
98
+ * Reset back to the original before selecting a variant.
99
+ * @param rootMesh The glTF root mesh
100
+ */
101
+ KHR_materials_variants.prototype.reset = function (rootMesh) {
102
+ return KHR_materials_variants.Reset(rootMesh);
103
+ };
104
+ /**
105
+ * Gets the last selected variant name(s) or null if original.
106
+ * @param rootMesh The glTF root mesh
107
+ * @returns The selected variant name(s).
108
+ */
109
+ KHR_materials_variants.GetLastSelectedVariant = function (rootMesh) {
110
+ var extensionMetadata = this._GetExtensionMetadata(rootMesh);
111
+ if (!extensionMetadata) {
112
+ throw new Error("Cannot get the last selected variant on a glTF mesh that does not have the ".concat(NAME, " extension"));
113
+ }
114
+ return extensionMetadata.lastSelected;
115
+ };
116
+ /**
117
+ * Gets the last selected variant name(s) or null if original.
118
+ * @param rootMesh The glTF root mesh
119
+ * @returns The selected variant name(s).
120
+ */
121
+ KHR_materials_variants.prototype.getLastSelectedVariant = function (rootMesh) {
122
+ return KHR_materials_variants.GetLastSelectedVariant(rootMesh);
123
+ };
124
+ KHR_materials_variants._GetExtensionMetadata = function (rootMesh) {
125
+ var _a, _b;
126
+ return ((_b = (_a = rootMesh === null || rootMesh === void 0 ? void 0 : rootMesh.metadata) === null || _a === void 0 ? void 0 : _a.gltf) === null || _b === void 0 ? void 0 : _b[NAME]) || null;
127
+ };
128
+ /** @hidden */
129
+ KHR_materials_variants.prototype.onLoading = function () {
130
+ var extensions = this._loader.gltf.extensions;
131
+ if (extensions && extensions[this.name]) {
132
+ var extension = extensions[this.name];
133
+ this._variants = extension.variants;
134
+ }
135
+ };
136
+ /**
137
+ * @param context
138
+ * @param name
139
+ * @param node
140
+ * @param mesh
141
+ * @param primitive
142
+ * @param assign
143
+ * @hidden
144
+ */
145
+ KHR_materials_variants.prototype._loadMeshPrimitiveAsync = function (context, name, node, mesh, primitive, assign) {
146
+ var _this = this;
147
+ return GLTFLoader.LoadExtensionAsync(context, primitive, this.name, function (extensionContext, extension) {
148
+ var promises = new Array();
149
+ promises.push(_this._loader._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, function (babylonMesh) {
150
+ assign(babylonMesh);
151
+ if (babylonMesh instanceof Mesh) {
152
+ var babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);
153
+ var root_1 = _this._loader.rootBabylonMesh;
154
+ var metadata = root_1 ? (root_1.metadata = root_1.metadata || {}) : {};
155
+ var gltf = (metadata.gltf = metadata.gltf || {});
156
+ var extensionMetadata_1 = (gltf[NAME] = gltf[NAME] || { lastSelected: null, original: [], variants: {} });
157
+ // Store the original material.
158
+ extensionMetadata_1.original.push({ mesh: babylonMesh, material: babylonMesh.material });
159
+ var _loop_1 = function (mappingIndex) {
160
+ var mapping = extension.mappings[mappingIndex];
161
+ var material = ArrayItem.Get("".concat(extensionContext, "/mappings/").concat(mappingIndex, "/material"), _this._loader.gltf.materials, mapping.material);
162
+ promises.push(_this._loader._loadMaterialAsync("#/materials/".concat(mapping.material), material, babylonMesh, babylonDrawMode, function (babylonMaterial) {
163
+ var _loop_2 = function (mappingVariantIndex) {
164
+ var variantIndex = mapping.variants[mappingVariantIndex];
165
+ var variant = ArrayItem.Get("/extensions/".concat(NAME, "/variants/").concat(variantIndex), _this._variants, variantIndex);
166
+ extensionMetadata_1.variants[variant.name] = extensionMetadata_1.variants[variant.name] || [];
167
+ extensionMetadata_1.variants[variant.name].push({
168
+ mesh: babylonMesh,
169
+ material: babylonMaterial,
170
+ });
171
+ // Replace the target when original mesh is cloned
172
+ babylonMesh.onClonedObservable.add(function (newOne) {
173
+ var newMesh = newOne;
174
+ var metadata = null;
175
+ var newRoot = newMesh;
176
+ // Find root to get medata
177
+ do {
178
+ newRoot = newRoot.parent;
179
+ if (!newRoot) {
180
+ return;
181
+ }
182
+ metadata = KHR_materials_variants._GetExtensionMetadata(newRoot);
183
+ } while (metadata === null);
184
+ // Need to clone the metadata on the root (first time only)
185
+ if (root_1 && metadata === KHR_materials_variants._GetExtensionMetadata(root_1)) {
186
+ // Copy main metadata
187
+ newRoot.metadata = {};
188
+ for (var key in root_1.metadata) {
189
+ newRoot.metadata[key] = root_1.metadata[key];
190
+ }
191
+ // Copy the gltf metadata
192
+ newRoot.metadata.gltf = [];
193
+ for (var key in root_1.metadata.gltf) {
194
+ newRoot.metadata.gltf[key] = root_1.metadata.gltf[key];
195
+ }
196
+ // Duplicate the extension specific metadata
197
+ newRoot.metadata.gltf[NAME] = { lastSelected: null, original: [], variants: {} };
198
+ for (var _i = 0, _a = metadata.original; _i < _a.length; _i++) {
199
+ var original = _a[_i];
200
+ newRoot.metadata.gltf[NAME].original.push({
201
+ mesh: original.mesh,
202
+ material: original.material,
203
+ });
204
+ }
205
+ for (var key in metadata.variants) {
206
+ if (Object.prototype.hasOwnProperty.call(metadata.variants, key)) {
207
+ newRoot.metadata.gltf[NAME].variants[key] = [];
208
+ for (var _b = 0, _c = metadata.variants[key]; _b < _c.length; _b++) {
209
+ var variantEntry = _c[_b];
210
+ newRoot.metadata.gltf[NAME].variants[key].push({
211
+ mesh: variantEntry.mesh,
212
+ material: variantEntry.material,
213
+ });
214
+ }
215
+ }
216
+ }
217
+ metadata = newRoot.metadata.gltf[NAME];
218
+ }
219
+ // Relocate
220
+ for (var _d = 0, _e = metadata.original; _d < _e.length; _d++) {
221
+ var target = _e[_d];
222
+ if (target.mesh === babylonMesh) {
223
+ target.mesh = newMesh;
224
+ }
225
+ }
226
+ for (var _f = 0, _g = metadata.variants[variant.name]; _f < _g.length; _f++) {
227
+ var target = _g[_f];
228
+ if (target.mesh === babylonMesh) {
229
+ target.mesh = newMesh;
230
+ }
231
+ }
232
+ });
233
+ };
234
+ for (var mappingVariantIndex = 0; mappingVariantIndex < mapping.variants.length; ++mappingVariantIndex) {
235
+ _loop_2(mappingVariantIndex);
236
+ }
237
+ }));
238
+ };
239
+ // For each mapping, look at the variants and make a new entry for them.
240
+ for (var mappingIndex = 0; mappingIndex < extension.mappings.length; ++mappingIndex) {
241
+ _loop_1(mappingIndex);
242
+ }
243
+ }
244
+ }));
245
+ return Promise.all(promises).then(function (_a) {
246
+ var babylonMesh = _a[0];
247
+ return babylonMesh;
248
+ });
249
+ });
250
+ };
251
+ return KHR_materials_variants;
252
+ }());
253
+ export { KHR_materials_variants };
254
+ GLTFLoader.RegisterExtension(NAME, function (loader) { return new KHR_materials_variants(loader); });
244
255
  //# sourceMappingURL=KHR_materials_variants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"KHR_materials_variants.js","sourceRoot":"","sources":["../../../../../sourceES6/loaders/src/glTF/2.0/Extensions/KHR_materials_variants.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAGtD,OAAO,EAAE,IAAI,E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U,CAAC,iBAAiB,CAAC,IAAI,EAAE,UAAC,MAAM,IAAK,OAAA,IAAI,sBAAsB,CAAC,MAAM,CAAC,EAAlC,CAAkC,CAAC,CAAC","sourcesContent":["import { Nullable } from \"@babylonjs/core/types\";\nimport { IGLTFLoaderExtension } from \"../glTFLoaderExtension\";\nimport { GLTFLoader, ArrayItem } from \"../glTFLoader\";\n\nimport { Material } from '@babylonjs/core/Materials/material';\nimport { Mesh } from '@babylonjs/core/Meshes/mesh';\nimport { Node } from '@babylonjs/core/node';\nimport { AbstractMesh } from '@babylonjs/core/Meshes/abstractMesh';\nimport { INode, IMeshPrimitive, IMesh } from '../glTFLoaderInterfaces';\nimport { IKHRMaterialVariants_Mapping, IKHRMaterialVariants_Variant, IKHRMaterialVariants_Variants } from 'babylonjs-gltf2interface';\n\nconst NAME = \"KHR_materials_variants\";\n\ninterface IVariantsMap {\n [key: string]: Array<{ mesh: AbstractMesh, material: Nullable<Material> }>;\n}\n\ninterface IExtensionMetadata {\n lastSelected: Nullable<string | Array<string>>;\n original: Array<{ mesh: AbstractMesh, material: Nullable<Material> }>;\n variants: IVariantsMap;\n}\n\n/**\n * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_variants/README.md)\n */\nexport class KHR_materials_variants implements IGLTFLoaderExtension {\n /**\n * The name of this extension.\n */\n public readonly name = NAME;\n\n /**\n * Defines whether this extension is enabled.\n */\n public enabled: boolean;\n\n private _loader: GLTFLoader;\n\n private _variants?: Array<IKHRMaterialVariants_Variant>;\n\n /** @hidden */\n constructor(loader: GLTFLoader) {\n this._loader = loader;\n this.enabled = this._loader.isExtensionUsed(NAME);\n }\n\n /** @hidden */\n public dispose() {\n (this._loader as any) = null;\n }\n\n /**\n * Gets the list of available variant names for this asset.\n * @param rootMesh The glTF root mesh\n * @returns the list of all the variant names for this model\n */\n public static GetAvailableVariants(rootMesh: Mesh): string[] {\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\n if (!extensionMetadata) {\n return [];\n }\n\n return Object.keys(extensionMetadata.variants);\n }\n\n /**\n * Gets the list of available variant names for this asset.\n * @param rootMesh The glTF root mesh\n * @returns the list of all the variant names for this model\n */\n public getAvailableVariants(rootMesh: Mesh): string[] {\n return KHR_materials_variants.GetAvailableVariants(rootMesh);\n }\n\n /**\n * Select a variant given a variant name or a list of variant names.\n * @param rootMesh The glTF root mesh\n * @param variantName The variant name(s) to select.\n */\n public static SelectVariant(rootMesh: Mesh, variantName: string | string[]): void {\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\n if (!extensionMetadata) {\n throw new Error(`Cannot select variant on a glTF mesh that does not have the ${NAME} extension`);\n }\n\n const select = (variantName: string): void => {\n const entries = extensionMetadata.variants[variantName];\n if (entries) {\n for (const entry of entries) {\n entry.mesh.material = entry.material;\n }\n }\n };\n\n if (variantName instanceof Array) {\n for (const name of variantName) {\n select(name);\n }\n } else {\n select(variantName);\n }\n\n extensionMetadata.lastSelected = variantName;\n }\n\n /**\n * Select a variant given a variant name or a list of variant names.\n * @param rootMesh The glTF root mesh\n * @param variantName The variant name(s) to select.\n */\n public selectVariant(rootMesh: Mesh, variantName: string | string[]): void {\n return KHR_materials_variants.SelectVariant(rootMesh, variantName);\n }\n\n /**\n * Reset back to the original before selecting a variant.\n * @param rootMesh The glTF root mesh\n */\n public static Reset(rootMesh: Mesh): void {\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\n if (!extensionMetadata) {\n throw new Error(`Cannot reset on a glTF mesh that does not have the ${NAME} extension`);\n }\n\n for (const entry of extensionMetadata.original) {\n entry.mesh.material = entry.material;\n }\n\n extensionMetadata.lastSelected = null;\n }\n\n /**\n * Reset back to the original before selecting a variant.\n * @param rootMesh The glTF root mesh\n */\n public reset(rootMesh: Mesh): void {\n return KHR_materials_variants.Reset(rootMesh);\n }\n\n /**\n * Gets the last selected variant name(s) or null if original.\n * @param rootMesh The glTF root mesh\n * @returns The selected variant name(s).\n */\n public static GetLastSelectedVariant(rootMesh: Mesh): Nullable<string | string[]> {\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\n if (!extensionMetadata) {\n throw new Error(`Cannot get the last selected variant on a glTF mesh that does not have the ${NAME} extension`);\n }\n\n return extensionMetadata.lastSelected;\n }\n\n /**\n * Gets the last selected variant name(s) or null if original.\n * @param rootMesh The glTF root mesh\n * @returns The selected variant name(s).\n */\n public getLastSelectedVariant(rootMesh: Mesh): Nullable<string | string[]> {\n return KHR_materials_variants.GetLastSelectedVariant(rootMesh);\n }\n\n private static _GetExtensionMetadata(rootMesh: Nullable<Mesh>): Nullable<IExtensionMetadata> {\n return rootMesh?.metadata?.gltf?.[NAME] || null;\n }\n\n /** @hidden */\n public onLoading(): void {\n const extensions = this._loader.gltf.extensions;\n if (extensions && extensions[this.name]) {\n const extension = extensions[this.name] as IKHRMaterialVariants_Variants;\n this._variants = extension.variants;\n }\n }\n\n /** @hidden */\n public _loadMeshPrimitiveAsync(context: string, name: string, node: INode, mesh: IMesh, primitive: IMeshPrimitive, assign: (babylonMesh: AbstractMesh) => void): Nullable<Promise<AbstractMesh>> {\n return GLTFLoader.LoadExtensionAsync<IKHRMaterialVariants_Mapping, AbstractMesh>(context, primitive, this.name, (extensionContext, extension) => {\n const promises = new Array<Promise<any>>();\n promises.push(this._loader._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, (babylonMesh) => {\n assign(babylonMesh);\n\n if (babylonMesh instanceof Mesh) {\n const babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);\n\n const root = this._loader.rootBabylonMesh;\n const metadata = (root ? root.metadata = root.metadata || {} : {});\n const gltf = (metadata.gltf = metadata.gltf || {});\n const extensionMetadata: IExtensionMetadata = (gltf[NAME] = gltf[NAME] || { lastSelected: null, original: [], variants: {} });\n\n // Store the original material.\n extensionMetadata.original.push({ mesh: babylonMesh, material: babylonMesh.material });\n\n // For each mapping, look at the variants and make a new entry for them.\n for (let mappingIndex = 0; mappingIndex < extension.mappings.length; ++mappingIndex) {\n const mapping = extension.mappings[mappingIndex];\n const material = ArrayItem.Get(`${extensionContext}/mappings/${mappingIndex}/material`, this._loader.gltf.materials, mapping.material);\n promises.push(this._loader._loadMaterialAsync(`#/materials/${mapping.material}`, material, babylonMesh, babylonDrawMode, (babylonMaterial) => {\n for (let mappingVariantIndex = 0; mappingVariantIndex < mapping.variants.length; ++mappingVariantIndex) {\n const variantIndex = mapping.variants[mappingVariantIndex];\n const variant = ArrayItem.Get(`/extensions/${NAME}/variants/${variantIndex}`, this._variants, variantIndex);\n extensionMetadata.variants[variant.name] = extensionMetadata.variants[variant.name] || [];\n extensionMetadata.variants[variant.name].push({\n mesh: babylonMesh,\n material: babylonMaterial\n });\n\n // Replace the target when original mesh is cloned\n babylonMesh.onClonedObservable.add((newOne: Node) => {\n const newMesh = newOne as Mesh;\n let metadata: Nullable<IExtensionMetadata> = null;\n let newRoot: Nullable<Node> = newMesh;\n\n // Find root to get medata\n do {\n newRoot = newRoot!.parent;\n if (!newRoot) {\n return;\n }\n metadata = KHR_materials_variants._GetExtensionMetadata(newRoot as Mesh);\n }\n while (metadata === null);\n\n // Need to clone the metadata on the root (first time only)\n if (root && metadata === KHR_materials_variants._GetExtensionMetadata(root)) {\n // Copy main metadata\n newRoot.metadata = {};\n for (var key in root.metadata) {\n newRoot.metadata[key] = root.metadata[key];\n }\n\n // Copy the gltf metadata\n newRoot.metadata.gltf = [];\n for (var key in root.metadata.gltf) {\n newRoot.metadata.gltf[key] = root.metadata.gltf[key];\n }\n\n // Duplicate the extension specific metadata\n newRoot.metadata.gltf[NAME] = { lastSelected: null, original: [], variants: {} };\n for (var original of metadata.original) {\n newRoot.metadata.gltf[NAME].original.push({\n mesh: original.mesh,\n material: original.material\n });\n }\n for (var key in metadata.variants) {\n if (metadata.variants.hasOwnProperty(key)) {\n newRoot.metadata.gltf[NAME].variants[key] = [];\n for (var variantEntry of metadata.variants[key]) {\n newRoot.metadata.gltf[NAME].variants[key].push({\n mesh: variantEntry.mesh,\n material: variantEntry.material\n });\n }\n }\n }\n\n metadata = newRoot.metadata.gltf[NAME];\n }\n\n // Relocate\n for (var target of metadata!.original) {\n if (target.mesh === babylonMesh) {\n target.mesh = newMesh;\n }\n }\n for (var target of metadata!.variants[variant.name]) {\n if (target.mesh === babylonMesh) {\n target.mesh = newMesh;\n }\n }\n });\n }\n }));\n }\n }\n }));\n return Promise.all(promises).then(([babylonMesh]) => {\n return babylonMesh;\n });\n });\n }\n}\n\nGLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_variants(loader));\n"]}
1
+ {"version":3,"file":"KHR_materials_variants.js","sourceRoot":"","sources":["../../../../../../../lts/loaders/generated/glTF/2.0/Extensions/KHR_materials_variants.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAGtD,OAAO,EAAE,IAAI,EAAE,uCAAyB;AAMxC,IAAM,IAAI,GAAG,wBAAwB,CAAC;AAYtC;;GAEG;AACH;IAeI;;;OAGG;IACH,gCAAY,MAAkB;QAlB9B;;WAEG;QACa,SAAI,GAAG,IAAI,CAAC;QAgBxB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC;IACtD,CAAC;IAED,cAAc;IACP,wCAAO,GAAd;QACK,IAAI,CAAC,OAAe,GAAG,IAAI,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACW,2CAAoB,GAAlC,UAAmC,QAAc;QAC7C,IAAM,iBAAiB,GAAG,IAAI,CAAC,qBAAqB,CAAC,QAAQ,CAAC,CAAC;QAC/D,IAAI,CAAC,iBAAiB,EAAE;YACpB,OAAO,EAAE,CAAC;SACb;QAED,OAAO,MAAM,CAAC,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACnD,CAAC;IAED;;;;OAIG;IACI,qDAAoB,GAA3B,UAA4B,QAAc;QACtC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IAAI,mBAAmB,GAAG,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,mBAAmB;wCAA7F,mBAAmB;6BAyE3B;wBACL,CAAC,CAAC,CACL,CAAC;;oBAjFN,wEAAwE;oBACxE,KAAK,IAAI,YAAY,GAAG,CAAC,EAAE,YAAY,GAAG,SAAS,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,YAAY;gCAA1E,YAAY;qBAiFpB;iBACJ;YACL,CAAC,CAAC,CACL,CAAC;YACF,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,UAAC,EAAa;oBAAZ,WAAW,QAAA;gBAC3C,OAAO,WAAW,CAAC;YACvB,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IACL,6BAAC;AAAD,CAAC,AAtRD,IAsRC;;AAED,UAAU,CAAC,iBAAiB,CAAC,IAAI,EAAE,UAAC,MAAM,IAAK,OAAA,IAAI,sBAAsB,CAAC,MAAM,CAAC,EAAlC,CAAkC,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */\r\nimport type { Nullable } from \"core/types\";\r\nimport type { IGLTFLoaderExtension } from \"../glTFLoaderExtension\";\r\nimport { GLTFLoader, ArrayItem } from \"../glTFLoader\";\r\n\r\nimport type { Material } from \"core/Materials/material\";\r\nimport { Mesh } from \"core/Meshes/mesh\";\r\nimport type { Node } from \"core/node\";\r\nimport type { AbstractMesh } from \"core/Meshes/abstractMesh\";\r\nimport type { INode, IMeshPrimitive, IMesh } from \"../glTFLoaderInterfaces\";\r\nimport type { IKHRMaterialVariants_Mapping, IKHRMaterialVariants_Variant, IKHRMaterialVariants_Variants } from \"babylonjs-gltf2interface\";\r\n\r\nconst NAME = \"KHR_materials_variants\";\r\n\r\ninterface IVariantsMap {\r\n [key: string]: Array<{ mesh: AbstractMesh; material: Nullable<Material> }>;\r\n}\r\n\r\ninterface IExtensionMetadata {\r\n lastSelected: Nullable<string | Array<string>>;\r\n original: Array<{ mesh: AbstractMesh; material: Nullable<Material> }>;\r\n variants: IVariantsMap;\r\n}\r\n\r\n/**\r\n * [Specification](https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_variants/README.md)\r\n */\r\nexport class KHR_materials_variants implements IGLTFLoaderExtension {\r\n /**\r\n * The name of this extension.\r\n */\r\n public readonly name = NAME;\r\n\r\n /**\r\n * Defines whether this extension is enabled.\r\n */\r\n public enabled: boolean;\r\n\r\n private _loader: GLTFLoader;\r\n\r\n private _variants?: Array<IKHRMaterialVariants_Variant>;\r\n\r\n /**\r\n * @param loader\r\n * @hidden\r\n */\r\n constructor(loader: GLTFLoader) {\r\n this._loader = loader;\r\n this.enabled = this._loader.isExtensionUsed(NAME);\r\n }\r\n\r\n /** @hidden */\r\n public dispose() {\r\n (this._loader as any) = null;\r\n }\r\n\r\n /**\r\n * Gets the list of available variant names for this asset.\r\n * @param rootMesh The glTF root mesh\r\n * @returns the list of all the variant names for this model\r\n */\r\n public static GetAvailableVariants(rootMesh: Mesh): string[] {\r\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\r\n if (!extensionMetadata) {\r\n return [];\r\n }\r\n\r\n return Object.keys(extensionMetadata.variants);\r\n }\r\n\r\n /**\r\n * Gets the list of available variant names for this asset.\r\n * @param rootMesh The glTF root mesh\r\n * @returns the list of all the variant names for this model\r\n */\r\n public getAvailableVariants(rootMesh: Mesh): string[] {\r\n return KHR_materials_variants.GetAvailableVariants(rootMesh);\r\n }\r\n\r\n /**\r\n * Select a variant given a variant name or a list of variant names.\r\n * @param rootMesh The glTF root mesh\r\n * @param variantName The variant name(s) to select.\r\n */\r\n public static SelectVariant(rootMesh: Mesh, variantName: string | string[]): void {\r\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\r\n if (!extensionMetadata) {\r\n throw new Error(`Cannot select variant on a glTF mesh that does not have the ${NAME} extension`);\r\n }\r\n\r\n const select = (variantName: string): void => {\r\n const entries = extensionMetadata.variants[variantName];\r\n if (entries) {\r\n for (const entry of entries) {\r\n entry.mesh.material = entry.material;\r\n }\r\n }\r\n };\r\n\r\n if (variantName instanceof Array) {\r\n for (const name of variantName) {\r\n select(name);\r\n }\r\n } else {\r\n select(variantName);\r\n }\r\n\r\n extensionMetadata.lastSelected = variantName;\r\n }\r\n\r\n /**\r\n * Select a variant given a variant name or a list of variant names.\r\n * @param rootMesh The glTF root mesh\r\n * @param variantName The variant name(s) to select.\r\n */\r\n public selectVariant(rootMesh: Mesh, variantName: string | string[]): void {\r\n return KHR_materials_variants.SelectVariant(rootMesh, variantName);\r\n }\r\n\r\n /**\r\n * Reset back to the original before selecting a variant.\r\n * @param rootMesh The glTF root mesh\r\n */\r\n public static Reset(rootMesh: Mesh): void {\r\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\r\n if (!extensionMetadata) {\r\n throw new Error(`Cannot reset on a glTF mesh that does not have the ${NAME} extension`);\r\n }\r\n\r\n for (const entry of extensionMetadata.original) {\r\n entry.mesh.material = entry.material;\r\n }\r\n\r\n extensionMetadata.lastSelected = null;\r\n }\r\n\r\n /**\r\n * Reset back to the original before selecting a variant.\r\n * @param rootMesh The glTF root mesh\r\n */\r\n public reset(rootMesh: Mesh): void {\r\n return KHR_materials_variants.Reset(rootMesh);\r\n }\r\n\r\n /**\r\n * Gets the last selected variant name(s) or null if original.\r\n * @param rootMesh The glTF root mesh\r\n * @returns The selected variant name(s).\r\n */\r\n public static GetLastSelectedVariant(rootMesh: Mesh): Nullable<string | string[]> {\r\n const extensionMetadata = this._GetExtensionMetadata(rootMesh);\r\n if (!extensionMetadata) {\r\n throw new Error(`Cannot get the last selected variant on a glTF mesh that does not have the ${NAME} extension`);\r\n }\r\n\r\n return extensionMetadata.lastSelected;\r\n }\r\n\r\n /**\r\n * Gets the last selected variant name(s) or null if original.\r\n * @param rootMesh The glTF root mesh\r\n * @returns The selected variant name(s).\r\n */\r\n public getLastSelectedVariant(rootMesh: Mesh): Nullable<string | string[]> {\r\n return KHR_materials_variants.GetLastSelectedVariant(rootMesh);\r\n }\r\n\r\n private static _GetExtensionMetadata(rootMesh: Nullable<Mesh>): Nullable<IExtensionMetadata> {\r\n return rootMesh?.metadata?.gltf?.[NAME] || null;\r\n }\r\n\r\n /** @hidden */\r\n public onLoading(): void {\r\n const extensions = this._loader.gltf.extensions;\r\n if (extensions && extensions[this.name]) {\r\n const extension = extensions[this.name] as IKHRMaterialVariants_Variants;\r\n this._variants = extension.variants;\r\n }\r\n }\r\n\r\n /**\r\n * @param context\r\n * @param name\r\n * @param node\r\n * @param mesh\r\n * @param primitive\r\n * @param assign\r\n * @hidden\r\n */\r\n public _loadMeshPrimitiveAsync(\r\n context: string,\r\n name: string,\r\n node: INode,\r\n mesh: IMesh,\r\n primitive: IMeshPrimitive,\r\n assign: (babylonMesh: AbstractMesh) => void\r\n ): Nullable<Promise<AbstractMesh>> {\r\n return GLTFLoader.LoadExtensionAsync<IKHRMaterialVariants_Mapping, AbstractMesh>(context, primitive, this.name, (extensionContext, extension) => {\r\n const promises = new Array<Promise<any>>();\r\n promises.push(\r\n this._loader._loadMeshPrimitiveAsync(context, name, node, mesh, primitive, (babylonMesh) => {\r\n assign(babylonMesh);\r\n\r\n if (babylonMesh instanceof Mesh) {\r\n const babylonDrawMode = GLTFLoader._GetDrawMode(context, primitive.mode);\r\n\r\n const root = this._loader.rootBabylonMesh;\r\n const metadata = root ? (root.metadata = root.metadata || {}) : {};\r\n const gltf = (metadata.gltf = metadata.gltf || {});\r\n const extensionMetadata: IExtensionMetadata = (gltf[NAME] = gltf[NAME] || { lastSelected: null, original: [], variants: {} });\r\n\r\n // Store the original material.\r\n extensionMetadata.original.push({ mesh: babylonMesh, material: babylonMesh.material });\r\n\r\n // For each mapping, look at the variants and make a new entry for them.\r\n for (let mappingIndex = 0; mappingIndex < extension.mappings.length; ++mappingIndex) {\r\n const mapping = extension.mappings[mappingIndex];\r\n const material = ArrayItem.Get(`${extensionContext}/mappings/${mappingIndex}/material`, this._loader.gltf.materials, mapping.material);\r\n promises.push(\r\n this._loader._loadMaterialAsync(`#/materials/${mapping.material}`, material, babylonMesh, babylonDrawMode, (babylonMaterial) => {\r\n for (let mappingVariantIndex = 0; mappingVariantIndex < mapping.variants.length; ++mappingVariantIndex) {\r\n const variantIndex = mapping.variants[mappingVariantIndex];\r\n const variant = ArrayItem.Get(`/extensions/${NAME}/variants/${variantIndex}`, this._variants, variantIndex);\r\n extensionMetadata.variants[variant.name] = extensionMetadata.variants[variant.name] || [];\r\n extensionMetadata.variants[variant.name].push({\r\n mesh: babylonMesh,\r\n material: babylonMaterial,\r\n });\r\n\r\n // Replace the target when original mesh is cloned\r\n babylonMesh.onClonedObservable.add((newOne: Node) => {\r\n const newMesh = newOne as Mesh;\r\n let metadata: Nullable<IExtensionMetadata> = null;\r\n let newRoot: Nullable<Node> = newMesh;\r\n\r\n // Find root to get medata\r\n do {\r\n newRoot = newRoot!.parent;\r\n if (!newRoot) {\r\n return;\r\n }\r\n metadata = KHR_materials_variants._GetExtensionMetadata(newRoot as Mesh);\r\n } while (metadata === null);\r\n\r\n // Need to clone the metadata on the root (first time only)\r\n if (root && metadata === KHR_materials_variants._GetExtensionMetadata(root)) {\r\n // Copy main metadata\r\n newRoot.metadata = {};\r\n for (const key in root.metadata) {\r\n newRoot.metadata[key] = root.metadata[key];\r\n }\r\n\r\n // Copy the gltf metadata\r\n newRoot.metadata.gltf = [];\r\n for (const key in root.metadata.gltf) {\r\n newRoot.metadata.gltf[key] = root.metadata.gltf[key];\r\n }\r\n\r\n // Duplicate the extension specific metadata\r\n newRoot.metadata.gltf[NAME] = { lastSelected: null, original: [], variants: {} };\r\n for (const original of metadata.original) {\r\n newRoot.metadata.gltf[NAME].original.push({\r\n mesh: original.mesh,\r\n material: original.material,\r\n });\r\n }\r\n for (const key in metadata.variants) {\r\n if (Object.prototype.hasOwnProperty.call(metadata.variants, key)) {\r\n newRoot.metadata.gltf[NAME].variants[key] = [];\r\n for (const variantEntry of metadata.variants[key]) {\r\n newRoot.metadata.gltf[NAME].variants[key].push({\r\n mesh: variantEntry.mesh,\r\n material: variantEntry.material,\r\n });\r\n }\r\n }\r\n }\r\n\r\n metadata = newRoot.metadata.gltf[NAME];\r\n }\r\n\r\n // Relocate\r\n for (const target of metadata!.original) {\r\n if (target.mesh === babylonMesh) {\r\n target.mesh = newMesh;\r\n }\r\n }\r\n for (const target of metadata!.variants[variant.name]) {\r\n if (target.mesh === babylonMesh) {\r\n target.mesh = newMesh;\r\n }\r\n }\r\n });\r\n }\r\n })\r\n );\r\n }\r\n }\r\n })\r\n );\r\n return Promise.all(promises).then(([babylonMesh]) => {\r\n return babylonMesh;\r\n });\r\n });\r\n }\r\n}\r\n\r\nGLTFLoader.RegisterExtension(NAME, (loader) => new KHR_materials_variants(loader));\r\n"]}