@babylonjs/serializers 8.34.0 → 8.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/glTF/2.0/Extensions/KHR_materials_anisotropy.js +11 -5
- package/glTF/2.0/Extensions/KHR_materials_anisotropy.js.map +1 -1
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +8 -0
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +136 -13
- package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +1 -1
- package/glTF/2.0/Extensions/{KHR_materials_clearcoat_darkening.d.ts → KHR_materials_coat.d.ts} +10 -2
- package/glTF/2.0/Extensions/KHR_materials_coat.js +383 -0
- package/glTF/2.0/Extensions/KHR_materials_coat.js.map +1 -0
- package/glTF/2.0/Extensions/{KHR_materials_clearcoat_color.d.ts → KHR_materials_fuzz.d.ts} +13 -5
- package/glTF/2.0/Extensions/KHR_materials_fuzz.js +194 -0
- package/glTF/2.0/Extensions/KHR_materials_fuzz.js.map +1 -0
- package/glTF/2.0/Extensions/index.d.ts +2 -4
- package/glTF/2.0/Extensions/index.js +2 -4
- package/glTF/2.0/Extensions/index.js.map +1 -1
- package/glTF/2.0/glTFExporter.d.ts +2 -5
- package/glTF/2.0/glTFExporter.js +1 -1
- package/glTF/2.0/glTFExporter.js.map +1 -1
- package/glTF/2.0/glTFMaterialExporter.d.ts +1 -3
- package/glTF/2.0/glTFMaterialExporter.js +40 -31
- package/glTF/2.0/glTFMaterialExporter.js.map +1 -1
- package/package.json +3 -3
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_anisotropy.d.ts +0 -32
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_anisotropy.js +0 -152
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_anisotropy.js.map +0 -1
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_color.js +0 -101
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_color.js.map +0 -1
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_darkening.js +0 -70
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_darkening.js.map +0 -1
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_ior.d.ts +0 -26
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_ior.js +0 -60
- package/glTF/2.0/Extensions/KHR_materials_clearcoat_ior.js.map +0 -1
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { GLTFExporter } from "../glTFExporter.js";
|
|
2
|
+
import { PBRBaseMaterial } from "@babylonjs/core/Materials/PBR/pbrBaseMaterial.js";
|
|
3
|
+
import { OpenPBRMaterial } from "@babylonjs/core/Materials/PBR/openpbrMaterial.js";
|
|
4
|
+
import { MergeTexturesAsync, CreateRGBAConfiguration, CreateTextureInput, CreateConstantInput } from "@babylonjs/core/Materials/Textures/textureMerger.js";
|
|
5
|
+
import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
|
|
6
|
+
const NAME = "KHR_materials_coat";
|
|
7
|
+
// Convert OpenPBR anisotropy values to glTF-compatible values
|
|
8
|
+
function OpenpbrAnisotropyStrengthToGltf(baseRoughness, anisotropy) {
|
|
9
|
+
const baseAlpha = baseRoughness * baseRoughness;
|
|
10
|
+
const roughnessT = baseAlpha * Math.sqrt(2.0 / (1.0 + (1 - anisotropy) * (1 - anisotropy)));
|
|
11
|
+
const roughnessB = (1 - anisotropy) * roughnessT;
|
|
12
|
+
const newBaseRoughness = Math.sqrt(roughnessB);
|
|
13
|
+
const newAnisotropyStrength = Math.min(Math.sqrt((roughnessT - baseAlpha) / Math.max(1.0 - baseAlpha, 0.0001)), 1.0);
|
|
14
|
+
return { newBaseRoughness, newAnisotropyStrength };
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Generate a unique ID for the merged anisotropy textures based on the internal texture data.
|
|
18
|
+
* This is used for caching merged textures.
|
|
19
|
+
* @param babylonMaterial Source OpenPBR material
|
|
20
|
+
* @returns A unique ID string for the merged anisotropy textures
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
function GetAnisoTextureId(babylonMaterial) {
|
|
24
|
+
const anisoStrengthTexture = babylonMaterial.coatRoughnessAnisotropyTexture;
|
|
25
|
+
const tangentTexture = babylonMaterial.geometryCoatTangentTexture;
|
|
26
|
+
const strengthId = anisoStrengthTexture && anisoStrengthTexture.getInternalTexture() ? anisoStrengthTexture.getInternalTexture().uniqueId : "NoStrength";
|
|
27
|
+
const tangentId = tangentTexture && tangentTexture.getInternalTexture() ? tangentTexture.getInternalTexture().uniqueId : "NoTangent";
|
|
28
|
+
return `${strengthId}_${tangentId}`;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Generate a unique ID for the merged coat textures based on the internal texture data.
|
|
32
|
+
* This is used for caching merged textures.
|
|
33
|
+
* @param babylonMaterial Source OpenPBR material
|
|
34
|
+
* @returns A unique ID string for the merged coat textures
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function GetCoatTextureId(babylonMaterial) {
|
|
38
|
+
const coatTexture = babylonMaterial.coatWeightTexture;
|
|
39
|
+
const coatId = coatTexture && coatTexture.getInternalTexture() ? coatTexture.getInternalTexture().uniqueId : "NoCoat";
|
|
40
|
+
const coatRoughnessTexture = babylonMaterial.coatRoughnessTexture;
|
|
41
|
+
const roughnessId = coatRoughnessTexture && coatRoughnessTexture.getInternalTexture() ? coatRoughnessTexture.getInternalTexture().uniqueId : "NoRoughness";
|
|
42
|
+
return `${coatId}_${roughnessId}`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a new texture with the anisotropy data merged together for export.
|
|
46
|
+
* @param babylonMaterial The source OpenPBR material
|
|
47
|
+
* @returns A new texture with the merged anisotropy data
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
async function CreateMergedAnisotropyTexture(babylonMaterial) {
|
|
51
|
+
const scene = babylonMaterial.getScene();
|
|
52
|
+
const anisoStrengthTexture = babylonMaterial.coatRoughnessAnisotropyTexture;
|
|
53
|
+
const tangentTexture = babylonMaterial.geometryCoatTangentTexture;
|
|
54
|
+
// If we don't have any textures, we don't need to generate anything.
|
|
55
|
+
if (!(anisoStrengthTexture || tangentTexture)) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return await MergeTexturesAsync("AnisotropyTexture", CreateRGBAConfiguration(tangentTexture ? CreateTextureInput(tangentTexture, 0) : CreateConstantInput(1.0), // tangent x from red channel
|
|
59
|
+
tangentTexture ? CreateTextureInput(tangentTexture, 1) : CreateConstantInput(0.0), // tangent y from green channel
|
|
60
|
+
anisoStrengthTexture ? CreateTextureInput(anisoStrengthTexture, 0) : CreateConstantInput(1.0) // Anisotropy from red channel
|
|
61
|
+
), scene);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Using the coat weight and coat roughness textures, create a merged internal texture that can be used
|
|
65
|
+
* for multiple textures (with potentially different transforms) on export.
|
|
66
|
+
* @param babylonMaterial The source OpenPBR material
|
|
67
|
+
* @returns A new, internal texture with the coat weight in the red channel and coat roughness in the green channel
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
async function CreateMergedCoatInternalTextureAsync(babylonMaterial) {
|
|
71
|
+
const scene = babylonMaterial.getScene();
|
|
72
|
+
const coatTexture = babylonMaterial.coatWeightTexture;
|
|
73
|
+
const coatRoughnessTexture = babylonMaterial.coatRoughnessTexture;
|
|
74
|
+
// If we don't have any textures, we don't need to generate anything.
|
|
75
|
+
if (!(coatTexture || coatRoughnessTexture)) {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
const texture = await MergeTexturesAsync("CoatTexture", CreateRGBAConfiguration(coatTexture ? CreateTextureInput(coatTexture, 0) : CreateConstantInput(1.0), // coat weight from red channel
|
|
79
|
+
// coat roughness goes in the green channel but may come from red or green channels in the source
|
|
80
|
+
coatRoughnessTexture ? CreateTextureInput(coatRoughnessTexture, babylonMaterial._useCoatRoughnessFromGreenChannel ? 1 : 0) : CreateConstantInput(1.0)), scene);
|
|
81
|
+
return texture.getInternalTexture();
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a temporary texture based on the source texture.
|
|
85
|
+
* @param internalTexture The source internal texture
|
|
86
|
+
* @param sourceTexture The source of the new texture's name, and sampler info
|
|
87
|
+
* @returns The new texture
|
|
88
|
+
*/
|
|
89
|
+
function CreateTempTexture(internalTexture, sourceTexture) {
|
|
90
|
+
const tempTexture = new Texture(sourceTexture.name, sourceTexture.getScene());
|
|
91
|
+
tempTexture._texture = internalTexture;
|
|
92
|
+
tempTexture.coordinatesIndex = sourceTexture.coordinatesIndex;
|
|
93
|
+
if (sourceTexture instanceof Texture) {
|
|
94
|
+
tempTexture.uOffset = sourceTexture.uOffset;
|
|
95
|
+
tempTexture.vOffset = sourceTexture.vOffset;
|
|
96
|
+
tempTexture.uScale = sourceTexture.uScale;
|
|
97
|
+
tempTexture.vScale = sourceTexture.vScale;
|
|
98
|
+
tempTexture.wAng = sourceTexture.wAng;
|
|
99
|
+
}
|
|
100
|
+
tempTexture.wrapU = sourceTexture.wrapU;
|
|
101
|
+
tempTexture.wrapV = sourceTexture.wrapV;
|
|
102
|
+
return tempTexture;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
108
|
+
export class KHR_materials_coat {
|
|
109
|
+
constructor(exporter) {
|
|
110
|
+
/** Name of this extension */
|
|
111
|
+
this.name = NAME;
|
|
112
|
+
/** Defines whether this extension is enabled */
|
|
113
|
+
this.enabled = true;
|
|
114
|
+
/** Defines whether this extension is required */
|
|
115
|
+
this.required = false;
|
|
116
|
+
this._wasUsed = false;
|
|
117
|
+
/**
|
|
118
|
+
* Cache that holds temporary merged textures created during export
|
|
119
|
+
*/
|
|
120
|
+
this._mergedTexturesMap = {};
|
|
121
|
+
/**
|
|
122
|
+
* Cache that holds internal textures of merged textures created during export
|
|
123
|
+
*/
|
|
124
|
+
this._cachedInternalTexturesMap = {};
|
|
125
|
+
this._exporter = exporter;
|
|
126
|
+
}
|
|
127
|
+
dispose() {
|
|
128
|
+
for (const key of Object.keys(this._mergedTexturesMap)) {
|
|
129
|
+
const texture = this._mergedTexturesMap[key];
|
|
130
|
+
texture.dispose();
|
|
131
|
+
}
|
|
132
|
+
this._mergedTexturesMap = {};
|
|
133
|
+
for (const key of Object.keys(this._cachedInternalTexturesMap)) {
|
|
134
|
+
const internalTexture = this._cachedInternalTexturesMap[key];
|
|
135
|
+
internalTexture.dispose();
|
|
136
|
+
}
|
|
137
|
+
this._cachedInternalTexturesMap = {};
|
|
138
|
+
}
|
|
139
|
+
/** @internal */
|
|
140
|
+
get wasUsed() {
|
|
141
|
+
return this._wasUsed;
|
|
142
|
+
}
|
|
143
|
+
async postExportMaterialAdditionalTexturesAsync(context, node, babylonMaterial) {
|
|
144
|
+
const additionalTextures = [];
|
|
145
|
+
if (babylonMaterial instanceof PBRBaseMaterial) {
|
|
146
|
+
if (babylonMaterial.clearCoat.isEnabled) {
|
|
147
|
+
if (babylonMaterial.clearCoat.texture) {
|
|
148
|
+
additionalTextures.push(babylonMaterial.clearCoat.texture);
|
|
149
|
+
}
|
|
150
|
+
if (!babylonMaterial.clearCoat.useRoughnessFromMainTexture && babylonMaterial.clearCoat.textureRoughness) {
|
|
151
|
+
additionalTextures.push(babylonMaterial.clearCoat.textureRoughness);
|
|
152
|
+
}
|
|
153
|
+
if (babylonMaterial.clearCoat.bumpTexture) {
|
|
154
|
+
additionalTextures.push(babylonMaterial.clearCoat.bumpTexture);
|
|
155
|
+
}
|
|
156
|
+
if (babylonMaterial.clearCoat.tintTexture) {
|
|
157
|
+
additionalTextures.push(babylonMaterial.clearCoat.tintTexture);
|
|
158
|
+
}
|
|
159
|
+
return additionalTextures;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else if (babylonMaterial instanceof OpenPBRMaterial) {
|
|
163
|
+
if (babylonMaterial.coatWeight > 0) {
|
|
164
|
+
// We will merge the coat_weight and coat_roughness textures, if needed.
|
|
165
|
+
// However, we want to retain the original texture's transforms and sampling info.
|
|
166
|
+
let coatNeedsMerge = false;
|
|
167
|
+
if (babylonMaterial.coatWeightTexture) {
|
|
168
|
+
// If we don't have a coat_roughness texture or if the coat_weight and coat_roughness
|
|
169
|
+
// textures are already merged, export them as-is.
|
|
170
|
+
if (!babylonMaterial.coatRoughnessTexture) {
|
|
171
|
+
additionalTextures.push(babylonMaterial.coatWeightTexture);
|
|
172
|
+
}
|
|
173
|
+
else if (babylonMaterial._useCoatRoughnessFromGreenChannel &&
|
|
174
|
+
babylonMaterial.coatWeightTexture.getInternalTexture() === babylonMaterial.coatRoughnessTexture.getInternalTexture()) {
|
|
175
|
+
additionalTextures.push(babylonMaterial.coatWeightTexture);
|
|
176
|
+
additionalTextures.push(babylonMaterial.coatRoughnessTexture);
|
|
177
|
+
}
|
|
178
|
+
else {
|
|
179
|
+
coatNeedsMerge = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else if (babylonMaterial.coatRoughnessTexture) {
|
|
183
|
+
if (babylonMaterial._useCoatRoughnessFromGreenChannel) {
|
|
184
|
+
additionalTextures.push(babylonMaterial.coatRoughnessTexture);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
coatNeedsMerge = true;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (coatNeedsMerge) {
|
|
191
|
+
// Merge the two textures together but retain the transforms for each.
|
|
192
|
+
// We do this by caching the internal texture that is created during the merge,
|
|
193
|
+
// and then creating temporary textures that use that internal texture but
|
|
194
|
+
// have the original texture's transforms/sampling info.
|
|
195
|
+
const texId = GetCoatTextureId(babylonMaterial);
|
|
196
|
+
if (!this._cachedInternalTexturesMap[texId]) {
|
|
197
|
+
const internalTexture = await CreateMergedCoatInternalTextureAsync(babylonMaterial);
|
|
198
|
+
if (internalTexture) {
|
|
199
|
+
this._cachedInternalTexturesMap[texId] = internalTexture;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (this._cachedInternalTexturesMap[texId]) {
|
|
203
|
+
if (babylonMaterial.coatWeightTexture) {
|
|
204
|
+
this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId] = CreateTempTexture(this._cachedInternalTexturesMap[texId], babylonMaterial.coatWeightTexture);
|
|
205
|
+
additionalTextures.push(this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId]);
|
|
206
|
+
}
|
|
207
|
+
if (babylonMaterial.coatRoughnessTexture) {
|
|
208
|
+
this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId] = CreateTempTexture(this._cachedInternalTexturesMap[texId], babylonMaterial.coatRoughnessTexture);
|
|
209
|
+
additionalTextures.push(this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId]);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (babylonMaterial.geometryCoatNormalTexture) {
|
|
214
|
+
additionalTextures.push(babylonMaterial.geometryCoatNormalTexture);
|
|
215
|
+
}
|
|
216
|
+
if (babylonMaterial.coatColorTexture) {
|
|
217
|
+
additionalTextures.push(babylonMaterial.coatColorTexture);
|
|
218
|
+
}
|
|
219
|
+
if (babylonMaterial.coatRoughnessAnisotropy > 0) {
|
|
220
|
+
const texId = GetAnisoTextureId(babylonMaterial);
|
|
221
|
+
if (this._mergedTexturesMap[texId]) {
|
|
222
|
+
additionalTextures.push(this._mergedTexturesMap[texId]);
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
const anisoTexture = await CreateMergedAnisotropyTexture(babylonMaterial);
|
|
226
|
+
if (anisoTexture) {
|
|
227
|
+
additionalTextures.push(anisoTexture);
|
|
228
|
+
this._mergedTexturesMap[texId] = anisoTexture;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return additionalTextures;
|
|
232
|
+
}
|
|
233
|
+
return additionalTextures;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return [];
|
|
237
|
+
}
|
|
238
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
239
|
+
postExportMaterialAsync(context, node, babylonMaterial) {
|
|
240
|
+
return new Promise((resolve) => {
|
|
241
|
+
var _a;
|
|
242
|
+
if (babylonMaterial instanceof PBRBaseMaterial) {
|
|
243
|
+
if (!babylonMaterial.clearCoat.isEnabled) {
|
|
244
|
+
resolve(node);
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
this._wasUsed = true;
|
|
248
|
+
node.extensions = node.extensions || {};
|
|
249
|
+
const coatTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);
|
|
250
|
+
let coatTextureRoughnessInfo;
|
|
251
|
+
if (babylonMaterial.clearCoat.useRoughnessFromMainTexture) {
|
|
252
|
+
coatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
coatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.textureRoughness);
|
|
256
|
+
}
|
|
257
|
+
let coatColorTextureInfo;
|
|
258
|
+
if (babylonMaterial.clearCoat.isTintEnabled) {
|
|
259
|
+
coatColorTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.tintTexture);
|
|
260
|
+
}
|
|
261
|
+
const coatNormalTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.bumpTexture);
|
|
262
|
+
const coatIor = babylonMaterial.clearCoat.indexOfRefraction;
|
|
263
|
+
const coatInfo = {
|
|
264
|
+
coatFactor: babylonMaterial.clearCoat.intensity,
|
|
265
|
+
coatTexture: coatTextureInfo ?? undefined,
|
|
266
|
+
coatRoughnessFactor: babylonMaterial.clearCoat.roughness,
|
|
267
|
+
coatRoughnessTexture: coatTextureRoughnessInfo ?? undefined,
|
|
268
|
+
coatNormalTexture: coatNormalTextureInfo ?? undefined,
|
|
269
|
+
coatColorFactor: babylonMaterial.clearCoat.tintColor.asArray(),
|
|
270
|
+
coatColorTexture: coatColorTextureInfo ?? undefined,
|
|
271
|
+
coatIor: coatIor !== 1.5 ? coatIor : undefined,
|
|
272
|
+
};
|
|
273
|
+
if (coatInfo.coatTexture !== null || coatInfo.coatRoughnessTexture !== null || coatInfo.coatRoughnessTexture !== null || coatInfo.coatColorTexture !== null) {
|
|
274
|
+
this._exporter._materialNeedsUVsSet.add(babylonMaterial);
|
|
275
|
+
}
|
|
276
|
+
node.extensions[NAME] = coatInfo;
|
|
277
|
+
}
|
|
278
|
+
else if (babylonMaterial instanceof OpenPBRMaterial) {
|
|
279
|
+
if (babylonMaterial.coatWeight == 0.0) {
|
|
280
|
+
resolve(node);
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
this._wasUsed = true;
|
|
284
|
+
node.extensions = node.extensions || {};
|
|
285
|
+
let coatWeightTexture = null;
|
|
286
|
+
let coatTextureInfo;
|
|
287
|
+
if (babylonMaterial.coatWeightTexture) {
|
|
288
|
+
if (this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId]) {
|
|
289
|
+
coatWeightTexture = this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId];
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
coatWeightTexture = babylonMaterial.coatWeightTexture;
|
|
293
|
+
}
|
|
294
|
+
coatTextureInfo = this._exporter._materialExporter.getTextureInfo(coatWeightTexture);
|
|
295
|
+
}
|
|
296
|
+
let coatRoughnessTexture = null;
|
|
297
|
+
let coatRoughnessTextureInfo;
|
|
298
|
+
if (babylonMaterial.coatRoughnessTexture) {
|
|
299
|
+
if (this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId]) {
|
|
300
|
+
coatRoughnessTexture = this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId];
|
|
301
|
+
}
|
|
302
|
+
else {
|
|
303
|
+
coatRoughnessTexture = babylonMaterial.coatRoughnessTexture;
|
|
304
|
+
}
|
|
305
|
+
coatRoughnessTextureInfo = this._exporter._materialExporter.getTextureInfo(coatRoughnessTexture);
|
|
306
|
+
}
|
|
307
|
+
const coatNormalTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.geometryCoatNormalTexture);
|
|
308
|
+
const coatColorTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.coatColorTexture);
|
|
309
|
+
const coatIor = babylonMaterial.coatIor;
|
|
310
|
+
const coatDarkeningFactor = babylonMaterial.coatDarkening;
|
|
311
|
+
// Check if we can convert from OpenPBR anisotropy to glTF anisotropy
|
|
312
|
+
// Conversion involves both specular roughness and anisotropic roughness changes so,
|
|
313
|
+
// if there are textures for either, we can't reliably convert due to there potentially
|
|
314
|
+
// being different mappings between the textures.
|
|
315
|
+
const roughnessTexture = babylonMaterial.coatRoughnessTexture;
|
|
316
|
+
const texId = GetAnisoTextureId(babylonMaterial);
|
|
317
|
+
const mergedAnisoTexture = this._mergedTexturesMap[texId];
|
|
318
|
+
let coatAnisotropyStrength = 0;
|
|
319
|
+
let coatAnisotropyRotation = 0;
|
|
320
|
+
let coatAnisotropyTexture = undefined;
|
|
321
|
+
if (babylonMaterial.coatRoughnessAnisotropy > 0.0) {
|
|
322
|
+
// If no textures are being used, we'll always output glTF-style anisotropy.
|
|
323
|
+
// If using OpenPBR anisotropy, convert the constants. Otherwise, just export what we have.
|
|
324
|
+
if (!roughnessTexture && !mergedAnisoTexture) {
|
|
325
|
+
// Convert constants
|
|
326
|
+
let newBaseRoughness = babylonMaterial.coatRoughness;
|
|
327
|
+
let newAnisotropyStrength = babylonMaterial.coatRoughnessAnisotropy;
|
|
328
|
+
if (!babylonMaterial._useGltfStyleAnisotropy) {
|
|
329
|
+
const newParams = OpenpbrAnisotropyStrengthToGltf(babylonMaterial.coatRoughness, babylonMaterial.coatRoughnessAnisotropy);
|
|
330
|
+
newBaseRoughness = newParams.newBaseRoughness;
|
|
331
|
+
newAnisotropyStrength = newParams.newAnisotropyStrength;
|
|
332
|
+
}
|
|
333
|
+
if (node.pbrMetallicRoughness) {
|
|
334
|
+
node.pbrMetallicRoughness.roughnessFactor = newBaseRoughness;
|
|
335
|
+
}
|
|
336
|
+
coatAnisotropyStrength = newAnisotropyStrength;
|
|
337
|
+
coatAnisotropyRotation = babylonMaterial.geometryCoatTangentAngle + Math.PI * 0.5;
|
|
338
|
+
coatAnisotropyTexture = undefined;
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
const mergedAnisoTextureInfo = mergedAnisoTexture ? this._exporter._materialExporter.getTextureInfo(mergedAnisoTexture) : null;
|
|
342
|
+
coatAnisotropyStrength = babylonMaterial.coatRoughnessAnisotropy;
|
|
343
|
+
coatAnisotropyRotation = babylonMaterial.geometryCoatTangentAngle;
|
|
344
|
+
coatAnisotropyTexture = mergedAnisoTextureInfo ? mergedAnisoTextureInfo : undefined;
|
|
345
|
+
}
|
|
346
|
+
if (!babylonMaterial._useGltfStyleAnisotropy) {
|
|
347
|
+
// Enable OpenPBR extension on this material.
|
|
348
|
+
node.extensions["KHR_materials_openpbr"] = {};
|
|
349
|
+
(_a = this._exporter._glTF).extensionsUsed || (_a.extensionsUsed = []);
|
|
350
|
+
if (this._exporter._glTF.extensionsUsed.indexOf("KHR_materials_openpbr") === -1) {
|
|
351
|
+
this._exporter._glTF.extensionsUsed.push("KHR_materials_openpbr");
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
const coatInfo = {
|
|
356
|
+
coatFactor: babylonMaterial.coatWeight,
|
|
357
|
+
coatTexture: coatTextureInfo ?? undefined,
|
|
358
|
+
coatRoughnessFactor: babylonMaterial.coatRoughness,
|
|
359
|
+
coatRoughnessTexture: coatRoughnessTextureInfo ?? undefined,
|
|
360
|
+
coatNormalTexture: coatNormalTextureInfo ?? undefined,
|
|
361
|
+
coatColorFactor: babylonMaterial.coatColor.asArray(),
|
|
362
|
+
coatColorTexture: coatColorTextureInfo ?? undefined,
|
|
363
|
+
coatIor: coatIor !== 1.5 ? coatIor : undefined,
|
|
364
|
+
coatDarkeningFactor: coatDarkeningFactor !== 1.0 ? coatDarkeningFactor : undefined,
|
|
365
|
+
coatAnisotropyRotation: coatAnisotropyRotation,
|
|
366
|
+
coatAnisotropyStrength: coatAnisotropyStrength,
|
|
367
|
+
coatAnisotropyTexture: coatAnisotropyTexture,
|
|
368
|
+
};
|
|
369
|
+
if (coatInfo.coatTexture !== null ||
|
|
370
|
+
coatInfo.coatRoughnessTexture !== null ||
|
|
371
|
+
coatInfo.coatRoughnessTexture !== null ||
|
|
372
|
+
coatInfo.coatAnisotropyTexture !== null ||
|
|
373
|
+
coatInfo.coatColorTexture !== null) {
|
|
374
|
+
this._exporter._materialNeedsUVsSet.add(babylonMaterial);
|
|
375
|
+
}
|
|
376
|
+
node.extensions[NAME] = coatInfo;
|
|
377
|
+
}
|
|
378
|
+
resolve(node);
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
GLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_coat(exporter));
|
|
383
|
+
//# sourceMappingURL=KHR_materials_coat.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KHR_materials_coat.js","sourceRoot":"","sources":["../../../../../../dev/serializers/src/glTF/2.0/Extensions/KHR_materials_coat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAE/C,OAAO,EAAE,eAAe,EAAE,yDAA2C;AAErE,OAAO,EAAE,eAAe,EAAE,yDAA2C;AACrE,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,4DAA8C;AAC7I,OAAO,EAAE,OAAO,EAAE,sDAAwC;AAI1D,MAAM,IAAI,GAAG,oBAAoB,CAAC;AAElC,8DAA8D;AAC9D,SAAS,+BAA+B,C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mB,EAAE,eAAe,CAAC,aAAa;oBAClD,oBAAoB,EAAE,wBAAwB,IAAI,SAAS;oBAC3D,iBAAiB,EAAE,qBAAqB,IAAI,SAAS;oBACrD,eAAe,EAAE,eAAe,CAAC,SAAS,CAAC,OAAO,EAAE;oBACpD,gBAAgB,EAAE,oBAAoB,IAAI,SAAS;oBACnD,OAAO,EAAE,OAAO,KAAK,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS;oBAC9C,mBAAmB,EAAE,mBAAmB,KAAK,GAAG,CAAC,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,SAAS;oBAClF,sBAAsB,EAAE,sBAAsB;oBAC9C,sBAAsB,EAAE,sBAAsB;oBAC9C,qBAAqB,EAAE,qBAAqB;iBAC/C,CAAC;gBAEF,IACI,QAAQ,CAAC,WAAW,KAAK,IAAI;oBAC7B,QAAQ,CAAC,oBAAoB,KAAK,IAAI;oBACtC,QAAQ,CAAC,oBAAoB,KAAK,IAAI;oBACtC,QAAQ,CAAC,qBAAqB,KAAK,IAAI;oBACvC,QAAQ,CAAC,gBAAgB,KAAK,IAAI,EACpC,CAAC;oBACC,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;gBAC7D,CAAC;gBAED,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;YACrC,CAAC;YACD,OAAO,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AAED,YAAY,CAAC,iBAAiB,CAAC,IAAI,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,kBAAkB,CAAC,QAAQ,CAAC,CAAC,CAAC","sourcesContent":["import type { IMaterial, IKHRMaterialsCoat } from \"babylonjs-gltf2interface\";\r\nimport type { IGLTFExporterExtensionV2 } from \"../glTFExporterExtension\";\r\nimport { GLTFExporter } from \"../glTFExporter\";\r\nimport type { Material } from \"core/Materials/material\";\r\nimport { PBRBaseMaterial } from \"core/Materials/PBR/pbrBaseMaterial\";\r\nimport type { BaseTexture } from \"core/Materials/Textures/baseTexture\";\r\nimport { OpenPBRMaterial } from \"core/Materials/PBR/openpbrMaterial\";\r\nimport { MergeTexturesAsync, CreateRGBAConfiguration, CreateTextureInput, CreateConstantInput } from \"core/Materials/Textures/textureMerger\";\r\nimport { Texture } from \"core/Materials/Textures/texture\";\r\nimport type { Nullable } from \"core/types\";\r\nimport type { InternalTexture } from \"core/Materials/Textures/internalTexture\";\r\n\r\nconst NAME = \"KHR_materials_coat\";\r\n\r\n// Convert OpenPBR anisotropy values to glTF-compatible values\r\nfunction OpenpbrAnisotropyStrengthToGltf(baseRoughness: number, anisotropy: number) {\r\n const baseAlpha = baseRoughness * baseRoughness;\r\n const roughnessT = baseAlpha * Math.sqrt(2.0 / (1.0 + (1 - anisotropy) * (1 - anisotropy)));\r\n const roughnessB = (1 - anisotropy) * roughnessT;\r\n const newBaseRoughness = Math.sqrt(roughnessB);\r\n const newAnisotropyStrength = Math.min(Math.sqrt((roughnessT - baseAlpha) / Math.max(1.0 - baseAlpha, 0.0001)), 1.0);\r\n\r\n return { newBaseRoughness, newAnisotropyStrength };\r\n}\r\n\r\n/**\r\n * Generate a unique ID for the merged anisotropy textures based on the internal texture data.\r\n * This is used for caching merged textures.\r\n * @param babylonMaterial Source OpenPBR material\r\n * @returns A unique ID string for the merged anisotropy textures\r\n * @internal\r\n */\r\nfunction GetAnisoTextureId(babylonMaterial: OpenPBRMaterial): string {\r\n const anisoStrengthTexture: Nullable<BaseTexture> = babylonMaterial.coatRoughnessAnisotropyTexture;\r\n const tangentTexture = babylonMaterial.geometryCoatTangentTexture;\r\n const strengthId = anisoStrengthTexture && anisoStrengthTexture.getInternalTexture() ? anisoStrengthTexture!.getInternalTexture()!.uniqueId : \"NoStrength\";\r\n const tangentId = tangentTexture && tangentTexture.getInternalTexture() ? tangentTexture!.getInternalTexture()!.uniqueId : \"NoTangent\";\r\n return `${strengthId}_${tangentId}`;\r\n}\r\n\r\n/**\r\n * Generate a unique ID for the merged coat textures based on the internal texture data.\r\n * This is used for caching merged textures.\r\n * @param babylonMaterial Source OpenPBR material\r\n * @returns A unique ID string for the merged coat textures\r\n * @internal\r\n */\r\nfunction GetCoatTextureId(babylonMaterial: OpenPBRMaterial): string {\r\n const coatTexture: Nullable<BaseTexture> = babylonMaterial.coatWeightTexture;\r\n const coatId = coatTexture && coatTexture.getInternalTexture() ? coatTexture!.getInternalTexture()!.uniqueId : \"NoCoat\";\r\n const coatRoughnessTexture: Nullable<BaseTexture> = babylonMaterial.coatRoughnessTexture;\r\n const roughnessId = coatRoughnessTexture && coatRoughnessTexture.getInternalTexture() ? coatRoughnessTexture!.getInternalTexture()!.uniqueId : \"NoRoughness\";\r\n return `${coatId}_${roughnessId}`;\r\n}\r\n\r\n/**\r\n * Creates a new texture with the anisotropy data merged together for export.\r\n * @param babylonMaterial The source OpenPBR material\r\n * @returns A new texture with the merged anisotropy data\r\n * @internal\r\n */\r\nasync function CreateMergedAnisotropyTexture(babylonMaterial: OpenPBRMaterial): Promise<Nullable<BaseTexture>> {\r\n const scene = babylonMaterial.getScene();\r\n\r\n const anisoStrengthTexture: Nullable<BaseTexture> = babylonMaterial.coatRoughnessAnisotropyTexture;\r\n const tangentTexture = babylonMaterial.geometryCoatTangentTexture;\r\n\r\n // If we don't have any textures, we don't need to generate anything.\r\n if (!(anisoStrengthTexture || tangentTexture)) {\r\n return null;\r\n }\r\n\r\n return await MergeTexturesAsync(\r\n \"AnisotropyTexture\",\r\n CreateRGBAConfiguration(\r\n tangentTexture ? CreateTextureInput(tangentTexture, 0) : CreateConstantInput(1.0), // tangent x from red channel\r\n tangentTexture ? CreateTextureInput(tangentTexture, 1) : CreateConstantInput(0.0), // tangent y from green channel\r\n anisoStrengthTexture ? CreateTextureInput(anisoStrengthTexture, 0) : CreateConstantInput(1.0) // Anisotropy from red channel\r\n ),\r\n scene\r\n );\r\n}\r\n\r\n/**\r\n * Using the coat weight and coat roughness textures, create a merged internal texture that can be used\r\n * for multiple textures (with potentially different transforms) on export.\r\n * @param babylonMaterial The source OpenPBR material\r\n * @returns A new, internal texture with the coat weight in the red channel and coat roughness in the green channel\r\n * @internal\r\n */\r\nasync function CreateMergedCoatInternalTextureAsync(babylonMaterial: OpenPBRMaterial): Promise<Nullable<InternalTexture>> {\r\n const scene = babylonMaterial.getScene();\r\n const coatTexture: Nullable<BaseTexture> = babylonMaterial.coatWeightTexture;\r\n const coatRoughnessTexture: Nullable<BaseTexture> = babylonMaterial.coatRoughnessTexture;\r\n // If we don't have any textures, we don't need to generate anything.\r\n if (!(coatTexture || coatRoughnessTexture)) {\r\n return null;\r\n }\r\n\r\n const texture = await MergeTexturesAsync(\r\n \"CoatTexture\",\r\n CreateRGBAConfiguration(\r\n coatTexture ? CreateTextureInput(coatTexture, 0) : CreateConstantInput(1.0), // coat weight from red channel\r\n // coat roughness goes in the green channel but may come from red or green channels in the source\r\n coatRoughnessTexture ? CreateTextureInput(coatRoughnessTexture, babylonMaterial._useCoatRoughnessFromGreenChannel ? 1 : 0) : CreateConstantInput(1.0)\r\n ),\r\n scene\r\n );\r\n\r\n return texture.getInternalTexture();\r\n}\r\n\r\n/**\r\n * Creates a temporary texture based on the source texture.\r\n * @param internalTexture The source internal texture\r\n * @param sourceTexture The source of the new texture's name, and sampler info\r\n * @returns The new texture\r\n */\r\nfunction CreateTempTexture(internalTexture: InternalTexture, sourceTexture: BaseTexture): Texture {\r\n const tempTexture = new Texture(sourceTexture.name, sourceTexture.getScene());\r\n tempTexture._texture = internalTexture;\r\n tempTexture.coordinatesIndex = sourceTexture.coordinatesIndex;\r\n if (sourceTexture instanceof Texture) {\r\n tempTexture.uOffset = sourceTexture.uOffset;\r\n tempTexture.vOffset = sourceTexture.vOffset;\r\n tempTexture.uScale = sourceTexture.uScale;\r\n tempTexture.vScale = sourceTexture.vScale;\r\n tempTexture.wAng = sourceTexture.wAng;\r\n }\r\n tempTexture.wrapU = sourceTexture.wrapU;\r\n tempTexture.wrapV = sourceTexture.wrapV;\r\n return tempTexture;\r\n}\r\n\r\n/**\r\n * @internal\r\n */\r\n// eslint-disable-next-line @typescript-eslint/naming-convention\r\nexport class KHR_materials_coat implements IGLTFExporterExtensionV2 {\r\n /** Name of this extension */\r\n public readonly name = NAME;\r\n\r\n /** Defines whether this extension is enabled */\r\n public enabled = true;\r\n\r\n /** Defines whether this extension is required */\r\n public required = false;\r\n\r\n private _exporter: GLTFExporter;\r\n\r\n private _wasUsed = false;\r\n\r\n constructor(exporter: GLTFExporter) {\r\n this._exporter = exporter;\r\n }\r\n\r\n /**\r\n * Cache that holds temporary merged textures created during export\r\n */\r\n private _mergedTexturesMap: Record<string, BaseTexture> = {};\r\n\r\n /**\r\n * Cache that holds internal textures of merged textures created during export\r\n */\r\n private _cachedInternalTexturesMap: Record<string, InternalTexture> = {};\r\n\r\n public dispose() {\r\n for (const key of Object.keys(this._mergedTexturesMap)) {\r\n const texture = this._mergedTexturesMap[key];\r\n texture.dispose();\r\n }\r\n this._mergedTexturesMap = {};\r\n for (const key of Object.keys(this._cachedInternalTexturesMap)) {\r\n const internalTexture = this._cachedInternalTexturesMap[key];\r\n internalTexture.dispose();\r\n }\r\n this._cachedInternalTexturesMap = {};\r\n }\r\n\r\n /** @internal */\r\n public get wasUsed() {\r\n return this._wasUsed;\r\n }\r\n\r\n public async postExportMaterialAdditionalTexturesAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<BaseTexture[]> {\r\n const additionalTextures: BaseTexture[] = [];\r\n if (babylonMaterial instanceof PBRBaseMaterial) {\r\n if (babylonMaterial.clearCoat.isEnabled) {\r\n if (babylonMaterial.clearCoat.texture) {\r\n additionalTextures.push(babylonMaterial.clearCoat.texture);\r\n }\r\n if (!babylonMaterial.clearCoat.useRoughnessFromMainTexture && babylonMaterial.clearCoat.textureRoughness) {\r\n additionalTextures.push(babylonMaterial.clearCoat.textureRoughness);\r\n }\r\n if (babylonMaterial.clearCoat.bumpTexture) {\r\n additionalTextures.push(babylonMaterial.clearCoat.bumpTexture);\r\n }\r\n if (babylonMaterial.clearCoat.tintTexture) {\r\n additionalTextures.push(babylonMaterial.clearCoat.tintTexture);\r\n }\r\n return additionalTextures;\r\n }\r\n } else if (babylonMaterial instanceof OpenPBRMaterial) {\r\n if (babylonMaterial.coatWeight > 0) {\r\n // We will merge the coat_weight and coat_roughness textures, if needed.\r\n // However, we want to retain the original texture's transforms and sampling info.\r\n let coatNeedsMerge = false;\r\n if (babylonMaterial.coatWeightTexture) {\r\n // If we don't have a coat_roughness texture or if the coat_weight and coat_roughness\r\n // textures are already merged, export them as-is.\r\n if (!babylonMaterial.coatRoughnessTexture) {\r\n additionalTextures.push(babylonMaterial.coatWeightTexture);\r\n } else if (\r\n babylonMaterial._useCoatRoughnessFromGreenChannel &&\r\n babylonMaterial.coatWeightTexture.getInternalTexture() === babylonMaterial.coatRoughnessTexture.getInternalTexture()\r\n ) {\r\n additionalTextures.push(babylonMaterial.coatWeightTexture);\r\n additionalTextures.push(babylonMaterial.coatRoughnessTexture);\r\n } else {\r\n coatNeedsMerge = true;\r\n }\r\n } else if (babylonMaterial.coatRoughnessTexture) {\r\n if (babylonMaterial._useCoatRoughnessFromGreenChannel) {\r\n additionalTextures.push(babylonMaterial.coatRoughnessTexture);\r\n } else {\r\n coatNeedsMerge = true;\r\n }\r\n }\r\n if (coatNeedsMerge) {\r\n // Merge the two textures together but retain the transforms for each.\r\n // We do this by caching the internal texture that is created during the merge,\r\n // and then creating temporary textures that use that internal texture but\r\n // have the original texture's transforms/sampling info.\r\n const texId = GetCoatTextureId(babylonMaterial);\r\n if (!this._cachedInternalTexturesMap[texId]) {\r\n const internalTexture = await CreateMergedCoatInternalTextureAsync(babylonMaterial);\r\n if (internalTexture) {\r\n this._cachedInternalTexturesMap[texId] = internalTexture;\r\n }\r\n }\r\n if (this._cachedInternalTexturesMap[texId]) {\r\n if (babylonMaterial.coatWeightTexture) {\r\n this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId] = CreateTempTexture(\r\n this._cachedInternalTexturesMap[texId],\r\n babylonMaterial.coatWeightTexture\r\n );\r\n additionalTextures.push(this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId]);\r\n }\r\n if (babylonMaterial.coatRoughnessTexture) {\r\n this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId] = CreateTempTexture(\r\n this._cachedInternalTexturesMap[texId],\r\n babylonMaterial.coatRoughnessTexture\r\n );\r\n additionalTextures.push(this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId]);\r\n }\r\n }\r\n }\r\n\r\n if (babylonMaterial.geometryCoatNormalTexture) {\r\n additionalTextures.push(babylonMaterial.geometryCoatNormalTexture);\r\n }\r\n\r\n if (babylonMaterial.coatColorTexture) {\r\n additionalTextures.push(babylonMaterial.coatColorTexture);\r\n }\r\n if (babylonMaterial.coatRoughnessAnisotropy > 0) {\r\n const texId = GetAnisoTextureId(babylonMaterial);\r\n if (this._mergedTexturesMap[texId]) {\r\n additionalTextures.push(this._mergedTexturesMap[texId]);\r\n } else {\r\n const anisoTexture = await CreateMergedAnisotropyTexture(babylonMaterial);\r\n if (anisoTexture) {\r\n additionalTextures.push(anisoTexture);\r\n this._mergedTexturesMap[texId] = anisoTexture;\r\n }\r\n }\r\n return additionalTextures;\r\n }\r\n return additionalTextures;\r\n }\r\n }\r\n\r\n return [];\r\n }\r\n\r\n // eslint-disable-next-line no-restricted-syntax\r\n public postExportMaterialAsync?(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial> {\r\n return new Promise((resolve) => {\r\n if (babylonMaterial instanceof PBRBaseMaterial) {\r\n if (!babylonMaterial.clearCoat.isEnabled) {\r\n resolve(node);\r\n return;\r\n }\r\n\r\n this._wasUsed = true;\r\n\r\n node.extensions = node.extensions || {};\r\n\r\n const coatTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);\r\n let coatTextureRoughnessInfo;\r\n if (babylonMaterial.clearCoat.useRoughnessFromMainTexture) {\r\n coatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.texture);\r\n } else {\r\n coatTextureRoughnessInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.textureRoughness);\r\n }\r\n\r\n let coatColorTextureInfo;\r\n if (babylonMaterial.clearCoat.isTintEnabled) {\r\n coatColorTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.tintTexture);\r\n }\r\n\r\n const coatNormalTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.clearCoat.bumpTexture);\r\n const coatIor: number = babylonMaterial.clearCoat.indexOfRefraction;\r\n\r\n const coatInfo: IKHRMaterialsCoat = {\r\n coatFactor: babylonMaterial.clearCoat.intensity,\r\n coatTexture: coatTextureInfo ?? undefined,\r\n coatRoughnessFactor: babylonMaterial.clearCoat.roughness,\r\n coatRoughnessTexture: coatTextureRoughnessInfo ?? undefined,\r\n coatNormalTexture: coatNormalTextureInfo ?? undefined,\r\n coatColorFactor: babylonMaterial.clearCoat.tintColor.asArray(),\r\n coatColorTexture: coatColorTextureInfo ?? undefined,\r\n coatIor: coatIor !== 1.5 ? coatIor : undefined,\r\n };\r\n\r\n if (coatInfo.coatTexture !== null || coatInfo.coatRoughnessTexture !== null || coatInfo.coatRoughnessTexture !== null || coatInfo.coatColorTexture !== null) {\r\n this._exporter._materialNeedsUVsSet.add(babylonMaterial);\r\n }\r\n\r\n node.extensions[NAME] = coatInfo;\r\n } else if (babylonMaterial instanceof OpenPBRMaterial) {\r\n if (babylonMaterial.coatWeight == 0.0) {\r\n resolve(node);\r\n return;\r\n }\r\n\r\n this._wasUsed = true;\r\n\r\n node.extensions = node.extensions || {};\r\n\r\n let coatWeightTexture: Nullable<BaseTexture> = null;\r\n let coatTextureInfo;\r\n if (babylonMaterial.coatWeightTexture) {\r\n if (this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId]) {\r\n coatWeightTexture = this._mergedTexturesMap[babylonMaterial.coatWeightTexture.uniqueId];\r\n } else {\r\n coatWeightTexture = babylonMaterial.coatWeightTexture;\r\n }\r\n coatTextureInfo = this._exporter._materialExporter.getTextureInfo(coatWeightTexture);\r\n }\r\n\r\n let coatRoughnessTexture: Nullable<BaseTexture> = null;\r\n let coatRoughnessTextureInfo;\r\n if (babylonMaterial.coatRoughnessTexture) {\r\n if (this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId]) {\r\n coatRoughnessTexture = this._mergedTexturesMap[babylonMaterial.coatRoughnessTexture.uniqueId];\r\n } else {\r\n coatRoughnessTexture = babylonMaterial.coatRoughnessTexture;\r\n }\r\n coatRoughnessTextureInfo = this._exporter._materialExporter.getTextureInfo(coatRoughnessTexture);\r\n }\r\n\r\n const coatNormalTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.geometryCoatNormalTexture);\r\n const coatColorTextureInfo = this._exporter._materialExporter.getTextureInfo(babylonMaterial.coatColorTexture);\r\n const coatIor: number = babylonMaterial.coatIor;\r\n const coatDarkeningFactor = babylonMaterial.coatDarkening;\r\n\r\n // Check if we can convert from OpenPBR anisotropy to glTF anisotropy\r\n // Conversion involves both specular roughness and anisotropic roughness changes so,\r\n // if there are textures for either, we can't reliably convert due to there potentially\r\n // being different mappings between the textures.\r\n const roughnessTexture: Nullable<BaseTexture> = babylonMaterial.coatRoughnessTexture;\r\n const texId = GetAnisoTextureId(babylonMaterial);\r\n const mergedAnisoTexture = this._mergedTexturesMap[texId];\r\n let coatAnisotropyStrength = 0;\r\n let coatAnisotropyRotation = 0;\r\n let coatAnisotropyTexture = undefined;\r\n if (babylonMaterial.coatRoughnessAnisotropy > 0.0) {\r\n // If no textures are being used, we'll always output glTF-style anisotropy.\r\n // If using OpenPBR anisotropy, convert the constants. Otherwise, just export what we have.\r\n if (!roughnessTexture && !mergedAnisoTexture) {\r\n // Convert constants\r\n let newBaseRoughness = babylonMaterial.coatRoughness;\r\n let newAnisotropyStrength = babylonMaterial.coatRoughnessAnisotropy;\r\n if (!babylonMaterial._useGltfStyleAnisotropy) {\r\n const newParams = OpenpbrAnisotropyStrengthToGltf(babylonMaterial.coatRoughness, babylonMaterial.coatRoughnessAnisotropy);\r\n newBaseRoughness = newParams.newBaseRoughness;\r\n newAnisotropyStrength = newParams.newAnisotropyStrength;\r\n }\r\n if (node.pbrMetallicRoughness) {\r\n node.pbrMetallicRoughness.roughnessFactor = newBaseRoughness;\r\n }\r\n coatAnisotropyStrength = newAnisotropyStrength;\r\n coatAnisotropyRotation = babylonMaterial.geometryCoatTangentAngle + Math.PI * 0.5;\r\n coatAnisotropyTexture = undefined;\r\n } else {\r\n const mergedAnisoTextureInfo = mergedAnisoTexture ? this._exporter._materialExporter.getTextureInfo(mergedAnisoTexture) : null;\r\n\r\n coatAnisotropyStrength = babylonMaterial.coatRoughnessAnisotropy;\r\n coatAnisotropyRotation = babylonMaterial.geometryCoatTangentAngle;\r\n coatAnisotropyTexture = mergedAnisoTextureInfo ? mergedAnisoTextureInfo : undefined;\r\n }\r\n\r\n if (!babylonMaterial._useGltfStyleAnisotropy) {\r\n // Enable OpenPBR extension on this material.\r\n node.extensions![\"KHR_materials_openpbr\"] = {};\r\n this._exporter._glTF.extensionsUsed ||= [];\r\n if (this._exporter._glTF.extensionsUsed.indexOf(\"KHR_materials_openpbr\") === -1) {\r\n this._exporter._glTF.extensionsUsed.push(\"KHR_materials_openpbr\");\r\n }\r\n }\r\n }\r\n\r\n const coatInfo: IKHRMaterialsCoat = {\r\n coatFactor: babylonMaterial.coatWeight,\r\n coatTexture: coatTextureInfo ?? undefined,\r\n coatRoughnessFactor: babylonMaterial.coatRoughness,\r\n coatRoughnessTexture: coatRoughnessTextureInfo ?? undefined,\r\n coatNormalTexture: coatNormalTextureInfo ?? undefined,\r\n coatColorFactor: babylonMaterial.coatColor.asArray(),\r\n coatColorTexture: coatColorTextureInfo ?? undefined,\r\n coatIor: coatIor !== 1.5 ? coatIor : undefined,\r\n coatDarkeningFactor: coatDarkeningFactor !== 1.0 ? coatDarkeningFactor : undefined,\r\n coatAnisotropyRotation: coatAnisotropyRotation,\r\n coatAnisotropyStrength: coatAnisotropyStrength,\r\n coatAnisotropyTexture: coatAnisotropyTexture,\r\n };\r\n\r\n if (\r\n coatInfo.coatTexture !== null ||\r\n coatInfo.coatRoughnessTexture !== null ||\r\n coatInfo.coatRoughnessTexture !== null ||\r\n coatInfo.coatAnisotropyTexture !== null ||\r\n coatInfo.coatColorTexture !== null\r\n ) {\r\n this._exporter._materialNeedsUVsSet.add(babylonMaterial);\r\n }\r\n\r\n node.extensions[NAME] = coatInfo;\r\n }\r\n resolve(node);\r\n });\r\n }\r\n}\r\n\r\nGLTFExporter.RegisterExtension(NAME, (exporter) => new KHR_materials_coat(exporter));\r\n"]}
|
|
@@ -6,19 +6,27 @@ import type { BaseTexture } from "@babylonjs/core/Materials/Textures/baseTexture
|
|
|
6
6
|
/**
|
|
7
7
|
* @internal
|
|
8
8
|
*/
|
|
9
|
-
export declare class
|
|
9
|
+
export declare class KHR_materials_fuzz implements IGLTFExporterExtensionV2 {
|
|
10
10
|
/** Name of this extension */
|
|
11
|
-
readonly name = "
|
|
11
|
+
readonly name = "KHR_materials_fuzz";
|
|
12
12
|
/** Defines whether this extension is enabled */
|
|
13
13
|
enabled: boolean;
|
|
14
14
|
/** Defines whether this extension is required */
|
|
15
15
|
required: boolean;
|
|
16
|
-
private _exporter;
|
|
17
16
|
private _wasUsed;
|
|
17
|
+
private _exporter;
|
|
18
18
|
constructor(exporter: GLTFExporter);
|
|
19
|
+
/**
|
|
20
|
+
* Cache that holds temporary merged textures created during export
|
|
21
|
+
*/
|
|
22
|
+
private _mergedTexturesMap;
|
|
23
|
+
/**
|
|
24
|
+
* Cache that holds internal textures of merged textures created during export
|
|
25
|
+
*/
|
|
26
|
+
private _cachedInternalTexturesMap;
|
|
19
27
|
dispose(): void;
|
|
20
28
|
/** @internal */
|
|
21
29
|
get wasUsed(): boolean;
|
|
22
|
-
postExportMaterialAdditionalTexturesAsync
|
|
23
|
-
postExportMaterialAsync
|
|
30
|
+
postExportMaterialAdditionalTexturesAsync(context: string, node: IMaterial, babylonMaterial: Material): Promise<BaseTexture[]>;
|
|
31
|
+
postExportMaterialAsync(context: string, node: IMaterial, babylonMaterial: Material): Promise<IMaterial>;
|
|
24
32
|
}
|