@babylonjs/loaders 5.0.0-rc.7 → 5.0.1
Sign up to get free protection for your applications and to get access to all the features.
- package/OBJ/index.d.ts +4 -4
- package/OBJ/index.js +4 -4
- package/OBJ/mtlFileLoader.d.ts +41 -41
- package/OBJ/mtlFileLoader.js +232 -232
- package/OBJ/mtlFileLoader.js.map +1 -1
- package/OBJ/objFileLoader.d.ts +126 -126
- package/OBJ/objFileLoader.js +296 -296
- package/OBJ/objFileLoader.js.map +1 -1
- package/OBJ/objLoadingOptions.d.ts +43 -43
- package/OBJ/objLoadingOptions.js +1 -1
- package/OBJ/objLoadingOptions.js.map +1 -1
- package/OBJ/solidParser.d.ts +154 -154
- package/OBJ/solidParser.js +693 -693
- package/OBJ/solidParser.js.map +1 -1
- package/STL/index.d.ts +1 -1
- package/STL/index.js +1 -1
- package/STL/stlFileLoader.d.ts +64 -64
- package/STL/stlFileLoader.js +233 -233
- package/STL/stlFileLoader.js.map +1 -1
- package/glTF/1.0/glTFBinaryExtension.d.ts +13 -13
- package/glTF/1.0/glTFBinaryExtension.js +60 -60
- package/glTF/1.0/glTFBinaryExtension.js.map +1 -1
- package/glTF/1.0/glTFLoader.d.ts +136 -136
- package/glTF/1.0/glTFLoader.js +1814 -1815
- package/glTF/1.0/glTFLoader.js.map +1 -1
- package/glTF/1.0/glTFLoaderInterfaces.d.ts +412 -412
- package/glTF/1.0/glTFLoaderInterfaces.js +95 -95
- package/glTF/1.0/glTFLoaderInterfaces.js.map +1 -1
- package/glTF/1.0/glTFLoaderUtils.d.ts +63 -62
- package/glTF/1.0/glTFLoaderUtils.js +250 -250
- package/glTF/1.0/glTFLoaderUtils.js.map +1 -1
- package/glTF/1.0/glTFMaterialsCommonExtension.d.ts +10 -10
- package/glTF/1.0/glTFMaterialsCommonExtension.js +129 -129
- package/glTF/1.0/glTFMaterialsCommonExtension.js.map +1 -1
- package/glTF/1.0/index.d.ts +5 -5
- package/glTF/1.0/index.js +5 -5
- package/glTF/2.0/Extensions/EXT_lights_image_based.d.ts +43 -43
- package/glTF/2.0/Extensions/EXT_lights_image_based.js +117 -117
- package/glTF/2.0/Extensions/EXT_lights_image_based.js.map +1 -1
- package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.d.ts +36 -36
- package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js +88 -88
- package/glTF/2.0/Extensions/EXT_mesh_gpu_instancing.js.map +1 -1
- package/glTF/2.0/Extensions/EXT_meshopt_compression.d.ts +34 -34
- package/glTF/2.0/Extensions/EXT_meshopt_compression.js +49 -49
- package/glTF/2.0/Extensions/EXT_meshopt_compression.js.map +1 -1
- package/glTF/2.0/Extensions/EXT_texture_webp.d.ts +29 -29
- package/glTF/2.0/Extensions/EXT_texture_webp.js +40 -40
- package/glTF/2.0/Extensions/EXT_texture_webp.js.map +1 -1
- package/glTF/2.0/Extensions/ExtrasAsMetadata.d.ts +50 -50
- package/glTF/2.0/Extensions/ExtrasAsMetadata.js +73 -73
- package/glTF/2.0/Extensions/ExtrasAsMetadata.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_draco_mesh_compression.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js +112 -112
- package/glTF/2.0/Extensions/KHR_draco_mesh_compression.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +36 -36
- package/glTF/2.0/Extensions/KHR_lights_punctual.js +89 -89
- package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +94 -94
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_emissive_strength.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js +53 -53
- package/glTF/2.0/Extensions/KHR_materials_emissive_strength.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_ior.d.ts +42 -42
- package/glTF/2.0/Extensions/KHR_materials_ior.js +62 -62
- package/glTF/2.0/Extensions/KHR_materials_ior.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js +80 -80
- package/glTF/2.0/Extensions/KHR_materials_pbrSpecularGlossiness.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_sheen.js +84 -84
- package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_specular.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_specular.js +74 -74
- package/glTF/2.0/Extensions/KHR_materials_specular.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_translucency.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_translucency.js +83 -83
- package/glTF/2.0/Extensions/KHR_materials_translucency.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_transmission.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_transmission.js +288 -288
- package/glTF/2.0/Extensions/KHR_materials_transmission.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +38 -38
- package/glTF/2.0/Extensions/KHR_materials_unlit.js +73 -73
- package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_variants.d.ts +87 -87
- package/glTF/2.0/Extensions/KHR_materials_variants.js +254 -254
- package/glTF/2.0/Extensions/KHR_materials_variants.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_volume.d.ts +39 -39
- package/glTF/2.0/Extensions/KHR_materials_volume.js +85 -85
- package/glTF/2.0/Extensions/KHR_materials_volume.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_mesh_quantization.d.ts +22 -22
- package/glTF/2.0/Extensions/KHR_mesh_quantization.js +23 -23
- package/glTF/2.0/Extensions/KHR_mesh_quantization.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_texture_basisu.d.ts +29 -29
- package/glTF/2.0/Extensions/KHR_texture_basisu.js +40 -40
- package/glTF/2.0/Extensions/KHR_texture_basisu.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +33 -33
- package/glTF/2.0/Extensions/KHR_texture_transform.js +61 -61
- package/glTF/2.0/Extensions/KHR_texture_transform.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_xmp_json_ld.d.ts +32 -32
- package/glTF/2.0/Extensions/KHR_xmp_json_ld.js +49 -49
- package/glTF/2.0/Extensions/KHR_xmp_json_ld.js.map +1 -1
- package/glTF/2.0/Extensions/MSFT_audio_emitter.d.ts +54 -54
- package/glTF/2.0/Extensions/MSFT_audio_emitter.js +240 -240
- package/glTF/2.0/Extensions/MSFT_audio_emitter.js.map +1 -1
- package/glTF/2.0/Extensions/MSFT_lod.d.ts +109 -109
- package/glTF/2.0/Extensions/MSFT_lod.js +369 -369
- package/glTF/2.0/Extensions/MSFT_lod.js.map +1 -1
- package/glTF/2.0/Extensions/MSFT_minecraftMesh.d.ts +14 -14
- package/glTF/2.0/Extensions/MSFT_minecraftMesh.js +36 -36
- package/glTF/2.0/Extensions/MSFT_minecraftMesh.js.map +1 -1
- package/glTF/2.0/Extensions/MSFT_sRGBFactors.d.ts +14 -14
- package/glTF/2.0/Extensions/MSFT_sRGBFactors.js +36 -36
- package/glTF/2.0/Extensions/MSFT_sRGBFactors.js.map +1 -1
- package/glTF/2.0/Extensions/index.d.ts +26 -26
- package/glTF/2.0/Extensions/index.js +26 -26
- package/glTF/2.0/glTFLoader.d.ts +406 -405
- package/glTF/2.0/glTFLoader.js +2366 -2366
- package/glTF/2.0/glTFLoader.js.map +1 -1
- package/glTF/2.0/glTFLoaderExtension.d.ts +152 -152
- package/glTF/2.0/glTFLoaderExtension.js +1 -1
- package/glTF/2.0/glTFLoaderExtension.js.map +1 -1
- package/glTF/2.0/glTFLoaderInterfaces.d.ts +211 -211
- package/glTF/2.0/glTFLoaderInterfaces.js +1 -1
- package/glTF/2.0/glTFLoaderInterfaces.js.map +1 -1
- package/glTF/2.0/index.d.ts +4 -4
- package/glTF/2.0/index.js +5 -4
- package/glTF/2.0/index.js.map +1 -1
- package/glTF/glTFFileLoader.d.ts +433 -433
- package/glTF/glTFFileLoader.js +927 -927
- package/glTF/glTFFileLoader.js.map +1 -1
- package/glTF/glTFValidation.d.ts +29 -29
- package/glTF/glTFValidation.js +123 -123
- package/glTF/glTFValidation.js.map +1 -1
- package/glTF/index.d.ts +5 -5
- package/glTF/index.js +6 -5
- package/glTF/index.js.map +1 -1
- package/index.d.ts +3 -3
- package/index.js +4 -3
- package/index.js.map +1 -1
- package/legacy/legacy-glTF.d.ts +2 -2
- package/legacy/legacy-glTF.js +18 -18
- package/legacy/legacy-glTF.js.map +1 -1
- package/legacy/legacy-glTF1.d.ts +2 -2
- package/legacy/legacy-glTF1.js +15 -14
- package/legacy/legacy-glTF1.js.map +1 -1
- package/legacy/legacy-glTF1FileLoader.d.ts +2 -2
- package/legacy/legacy-glTF1FileLoader.js +3 -3
- package/legacy/legacy-glTF1FileLoader.js.map +1 -1
- package/legacy/legacy-glTF2.d.ts +2 -2
- package/legacy/legacy-glTF2.js +33 -32
- package/legacy/legacy-glTF2.js.map +1 -1
- package/legacy/legacy-glTF2FileLoader.d.ts +2 -2
- package/legacy/legacy-glTF2FileLoader.js +3 -3
- package/legacy/legacy-glTF2FileLoader.js.map +1 -1
- package/legacy/legacy-glTFFileLoader.d.ts +3 -3
- package/legacy/legacy-glTFFileLoader.js +4 -4
- package/legacy/legacy-glTFFileLoader.js.map +1 -1
- package/legacy/legacy-objFileLoader.d.ts +1 -1
- package/legacy/legacy-objFileLoader.js +13 -12
- package/legacy/legacy-objFileLoader.js.map +1 -1
- package/legacy/legacy-stlFileLoader.d.ts +1 -1
- package/legacy/legacy-stlFileLoader.js +13 -12
- package/legacy/legacy-stlFileLoader.js.map +1 -1
- package/legacy/legacy.d.ts +6 -6
- package/legacy/legacy.js +8 -7
- package/legacy/legacy.js.map +1 -1
- package/license.md +71 -0
- package/package.json +25 -6
@@ -1,255 +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
|
-
/**
|
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); });
|
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
|
+
/**
|
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); });
|
255
255
|
//# sourceMappingURL=KHR_materials_variants.js.map
|
@@ -1 +1 @@
|
|
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,g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AAsB,CAAC,MAAM,CAAC,EAAlC,CAAkC,CAAC,CAAC","sourcesContent":["/* eslint-disable @typescript-eslint/naming-convention */\r\nimport { Nullable } from \"core/types\";\r\nimport { IGLTFLoaderExtension } from \"../glTFLoaderExtension\";\r\nimport { GLTFLoader, ArrayItem } from \"../glTFLoader\";\r\n\r\nimport { Material } from \"core/Materials/material\";\r\nimport { Mesh } from \"core/Meshes/mesh\";\r\nimport { Node } from \"core/node\";\r\nimport { AbstractMesh } from \"core/Meshes/abstractMesh\";\r\nimport { INode, IMeshPrimitive, IMesh } from \"../glTFLoaderInterfaces\";\r\nimport { 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"]}
|
1
|
+
{"version":3,"file":"KHR_materials_variants.js","sourceRoot":"","sources":["../../../../../../../lts/loaders/generated/glTF/2.0/Extensions/KHR_materials_variants.ts"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C,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"]}
|