@babylonjs/serializers 5.0.0-beta.8 → 5.0.0-beta.8-snapshot

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/package.json +20 -105
  2. package/OBJ/index.d.ts +0 -1
  3. package/OBJ/index.js +0 -2
  4. package/OBJ/index.js.map +0 -1
  5. package/OBJ/objSerializer.d.ts +0 -21
  6. package/OBJ/objSerializer.js +0 -160
  7. package/OBJ/objSerializer.js.map +0 -1
  8. package/glTF/2.0/Extensions/KHR_lights_punctual.d.ts +0 -38
  9. package/glTF/2.0/Extensions/KHR_lights_punctual.js +0 -174
  10. package/glTF/2.0/Extensions/KHR_lights_punctual.js.map +0 -1
  11. package/glTF/2.0/Extensions/KHR_materials_clearcoat.d.ts +0 -24
  12. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js +0 -91
  13. package/glTF/2.0/Extensions/KHR_materials_clearcoat.js.map +0 -1
  14. package/glTF/2.0/Extensions/KHR_materials_sheen.d.ts +0 -24
  15. package/glTF/2.0/Extensions/KHR_materials_sheen.js +0 -74
  16. package/glTF/2.0/Extensions/KHR_materials_sheen.js.map +0 -1
  17. package/glTF/2.0/Extensions/KHR_materials_unlit.d.ts +0 -21
  18. package/glTF/2.0/Extensions/KHR_materials_unlit.js +0 -52
  19. package/glTF/2.0/Extensions/KHR_materials_unlit.js.map +0 -1
  20. package/glTF/2.0/Extensions/KHR_texture_transform.d.ts +0 -34
  21. package/glTF/2.0/Extensions/KHR_texture_transform.js +0 -137
  22. package/glTF/2.0/Extensions/KHR_texture_transform.js.map +0 -1
  23. package/glTF/2.0/Extensions/index.d.ts +0 -5
  24. package/glTF/2.0/Extensions/index.js +0 -6
  25. package/glTF/2.0/Extensions/index.js.map +0 -1
  26. package/glTF/2.0/glTFAnimation.d.ts +0 -200
  27. package/glTF/2.0/glTFAnimation.js +0 -840
  28. package/glTF/2.0/glTFAnimation.js.map +0 -1
  29. package/glTF/2.0/glTFData.d.ts +0 -19
  30. package/glTF/2.0/glTFData.js +0 -53
  31. package/glTF/2.0/glTFData.js.map +0 -1
  32. package/glTF/2.0/glTFExporter.d.ts +0 -445
  33. package/glTF/2.0/glTFExporter.js +0 -1943
  34. package/glTF/2.0/glTFExporter.js.map +0 -1
  35. package/glTF/2.0/glTFExporterExtension.d.ts +0 -74
  36. package/glTF/2.0/glTFExporterExtension.js +0 -3
  37. package/glTF/2.0/glTFExporterExtension.js.map +0 -1
  38. package/glTF/2.0/glTFMaterialExporter.d.ts +0 -230
  39. package/glTF/2.0/glTFMaterialExporter.js +0 -1143
  40. package/glTF/2.0/glTFMaterialExporter.js.map +0 -1
  41. package/glTF/2.0/glTFSerializer.d.ts +0 -60
  42. package/glTF/2.0/glTFSerializer.js +0 -63
  43. package/glTF/2.0/glTFSerializer.js.map +0 -1
  44. package/glTF/2.0/glTFUtilities.d.ts +0 -96
  45. package/glTF/2.0/glTFUtilities.js +0 -196
  46. package/glTF/2.0/glTFUtilities.js.map +0 -1
  47. package/glTF/2.0/index.d.ts +0 -8
  48. package/glTF/2.0/index.js +0 -9
  49. package/glTF/2.0/index.js.map +0 -1
  50. package/glTF/2.0/shaders/textureTransform.fragment.d.ts +0 -5
  51. package/glTF/2.0/shaders/textureTransform.fragment.js +0 -7
  52. package/glTF/2.0/shaders/textureTransform.fragment.js.map +0 -1
  53. package/glTF/glTFFileExporter.d.ts +0 -20
  54. package/glTF/glTFFileExporter.js +0 -3
  55. package/glTF/glTFFileExporter.js.map +0 -1
  56. package/glTF/index.d.ts +0 -2
  57. package/glTF/index.js +0 -3
  58. package/glTF/index.js.map +0 -1
  59. package/index.d.ts +0 -3
  60. package/index.js.map +0 -1
  61. package/legacy/legacy-glTF2Serializer.d.ts +0 -2
  62. package/legacy/legacy-glTF2Serializer.js +0 -44
  63. package/legacy/legacy-glTF2Serializer.js.map +0 -1
  64. package/legacy/legacy-objSerializer.d.ts +0 -1
  65. package/legacy/legacy-objSerializer.js +0 -13
  66. package/legacy/legacy-objSerializer.js.map +0 -1
  67. package/legacy/legacy-stlSerializer.d.ts +0 -1
  68. package/legacy/legacy-stlSerializer.js +0 -13
  69. package/legacy/legacy-stlSerializer.js.map +0 -1
  70. package/legacy/legacy.d.ts +0 -4
  71. package/legacy/legacy.js +0 -5
  72. package/legacy/legacy.js.map +0 -1
  73. package/readme.md +0 -29
  74. package/stl/index.d.ts +0 -1
  75. package/stl/index.js +0 -2
  76. package/stl/index.js.map +0 -1
  77. package/stl/stlSerializer.d.ts +0 -17
  78. package/stl/stlSerializer.js +0 -108
  79. package/stl/stlSerializer.js.map +0 -1
@@ -1,1143 +0,0 @@
1
- import { __awaiter, __generator } from "tslib";
2
- import { Vector2 } from "@babylonjs/core/Maths/math.vector.js";
3
- import { Color3 } from "@babylonjs/core/Maths/math.color.js";
4
- import { Scalar } from "@babylonjs/core/Maths/math.scalar.js";
5
- import { Tools } from "@babylonjs/core/Misc/tools.js";
6
- import { TextureTools } from "@babylonjs/core/Misc/textureTools.js";
7
- import { Texture } from "@babylonjs/core/Materials/Textures/texture.js";
8
- import { RawTexture } from "@babylonjs/core/Materials/Textures/rawTexture.js";
9
- import { Constants } from '@babylonjs/core/Engines/constants.js';
10
- /**
11
- * Utility methods for working with glTF material conversion properties. This class should only be used internally
12
- * @hidden
13
- */
14
- var _GLTFMaterialExporter = /** @class */ (function () {
15
- function _GLTFMaterialExporter(exporter) {
16
- /**
17
- * Mapping to store textures
18
- */
19
- this._textureMap = {};
20
- this._textureMap = {};
21
- this._exporter = exporter;
22
- }
23
- /**
24
- * Specifies if two colors are approximately equal in value
25
- * @param color1 first color to compare to
26
- * @param color2 second color to compare to
27
- * @param epsilon threshold value
28
- */
29
- _GLTFMaterialExporter.FuzzyEquals = function (color1, color2, epsilon) {
30
- return Scalar.WithinEpsilon(color1.r, color2.r, epsilon) &&
31
- Scalar.WithinEpsilon(color1.g, color2.g, epsilon) &&
32
- Scalar.WithinEpsilon(color1.b, color2.b, epsilon);
33
- };
34
- /**
35
- * Gets the materials from a Babylon scene and converts them to glTF materials
36
- * @param scene babylonjs scene
37
- * @param mimeType texture mime type
38
- * @param images array of images
39
- * @param textures array of textures
40
- * @param materials set of materials
41
- * @param imageData mapping of texture names to base64 textures
42
- * @param hasTextureCoords specifies if texture coordinates are present on the material
43
- */
44
- _GLTFMaterialExporter.prototype._convertMaterialsToGLTFAsync = function (exportMaterials, mimeType, hasTextureCoords) {
45
- var _this = this;
46
- var promises = [];
47
- exportMaterials.forEach(function (material) {
48
- if (material.getClassName() === "StandardMaterial") {
49
- promises.push(_this._convertStandardMaterialAsync(material, mimeType, hasTextureCoords));
50
- }
51
- else if (material.getClassName().indexOf("PBR") !== -1) {
52
- promises.push(_this._convertPBRMaterialAsync(material, mimeType, hasTextureCoords));
53
- }
54
- else {
55
- Tools.Warn("Unsupported material type: ".concat(material.name));
56
- }
57
- });
58
- return Promise.all(promises).then(function () { });
59
- };
60
- /**
61
- * Makes a copy of the glTF material without the texture parameters
62
- * @param originalMaterial original glTF material
63
- * @returns glTF material without texture parameters
64
- */
65
- _GLTFMaterialExporter.prototype._stripTexturesFromMaterial = function (originalMaterial) {
66
- var newMaterial = {};
67
- if (originalMaterial) {
68
- newMaterial.name = originalMaterial.name;
69
- newMaterial.doubleSided = originalMaterial.doubleSided;
70
- newMaterial.alphaMode = originalMaterial.alphaMode;
71
- newMaterial.alphaCutoff = originalMaterial.alphaCutoff;
72
- newMaterial.emissiveFactor = originalMaterial.emissiveFactor;
73
- var originalPBRMetallicRoughness = originalMaterial.pbrMetallicRoughness;
74
- if (originalPBRMetallicRoughness) {
75
- newMaterial.pbrMetallicRoughness = {};
76
- newMaterial.pbrMetallicRoughness.baseColorFactor = originalPBRMetallicRoughness.baseColorFactor;
77
- newMaterial.pbrMetallicRoughness.metallicFactor = originalPBRMetallicRoughness.metallicFactor;
78
- newMaterial.pbrMetallicRoughness.roughnessFactor = originalPBRMetallicRoughness.roughnessFactor;
79
- }
80
- }
81
- return newMaterial;
82
- };
83
- /**
84
- * Specifies if the material has any texture parameters present
85
- * @param material glTF Material
86
- * @returns boolean specifying if texture parameters are present
87
- */
88
- _GLTFMaterialExporter.prototype._hasTexturesPresent = function (material) {
89
- var _a;
90
- if (material.emissiveTexture || material.normalTexture || material.occlusionTexture) {
91
- return true;
92
- }
93
- var pbrMat = material.pbrMetallicRoughness;
94
- if (pbrMat) {
95
- if (pbrMat.baseColorTexture || pbrMat.metallicRoughnessTexture) {
96
- return true;
97
- }
98
- }
99
- if (material.extensions) {
100
- for (var extension in material.extensions) {
101
- var extensionObject = material.extensions[extension];
102
- if (extensionObject) {
103
- return (_a = extensionObject.hasTextures) === null || _a === void 0 ? void 0 : _a.call(extensionObject);
104
- }
105
- }
106
- }
107
- return false;
108
- };
109
- _GLTFMaterialExporter.prototype._getTextureInfo = function (babylonTexture) {
110
- if (babylonTexture) {
111
- var textureUid = babylonTexture.uid;
112
- if (textureUid in this._textureMap) {
113
- return this._textureMap[textureUid];
114
- }
115
- }
116
- return null;
117
- };
118
- /**
119
- * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material
120
- * @param babylonStandardMaterial
121
- * @returns glTF Metallic Roughness Material representation
122
- */
123
- _GLTFMaterialExporter.prototype._convertToGLTFPBRMetallicRoughness = function (babylonStandardMaterial) {
124
- var P0 = new Vector2(0, 1);
125
- var P1 = new Vector2(0, 0.1);
126
- var P2 = new Vector2(0, 0.1);
127
- var P3 = new Vector2(1300, 0.1);
128
- /**
129
- * Given the control points, solve for x based on a given t for a cubic bezier curve
130
- * @param t a value between 0 and 1
131
- * @param p0 first control point
132
- * @param p1 second control point
133
- * @param p2 third control point
134
- * @param p3 fourth control point
135
- * @returns number result of cubic bezier curve at the specified t
136
- */
137
- function _cubicBezierCurve(t, p0, p1, p2, p3) {
138
- return ((1 - t) * (1 - t) * (1 - t) * p0 +
139
- 3 * (1 - t) * (1 - t) * t * p1 +
140
- 3 * (1 - t) * t * t * p2 +
141
- t * t * t * p3);
142
- }
143
- /**
144
- * Evaluates a specified specular power value to determine the appropriate roughness value,
145
- * based on a pre-defined cubic bezier curve with specular on the abscissa axis (x-axis)
146
- * and roughness on the ordinant axis (y-axis)
147
- * @param specularPower specular power of standard material
148
- * @returns Number representing the roughness value
149
- */
150
- function _solveForRoughness(specularPower) {
151
- var t = Math.pow(specularPower / P3.x, 0.333333);
152
- return _cubicBezierCurve(t, P0.y, P1.y, P2.y, P3.y);
153
- }
154
- var diffuse = babylonStandardMaterial.diffuseColor.toLinearSpace().scale(0.5);
155
- var opacity = babylonStandardMaterial.alpha;
156
- var specularPower = Scalar.Clamp(babylonStandardMaterial.specularPower, 0, _GLTFMaterialExporter._MaxSpecularPower);
157
- var roughness = _solveForRoughness(specularPower);
158
- var glTFPbrMetallicRoughness = {
159
- baseColorFactor: [
160
- diffuse.r,
161
- diffuse.g,
162
- diffuse.b,
163
- opacity
164
- ],
165
- metallicFactor: 0,
166
- roughnessFactor: roughness,
167
- };
168
- return glTFPbrMetallicRoughness;
169
- };
170
- /**
171
- * Computes the metallic factor
172
- * @param diffuse diffused value
173
- * @param specular specular value
174
- * @param oneMinusSpecularStrength one minus the specular strength
175
- * @returns metallic value
176
- */
177
- _GLTFMaterialExporter._SolveMetallic = function (diffuse, specular, oneMinusSpecularStrength) {
178
- if (specular < this._DielectricSpecular.r) {
179
- this._DielectricSpecular;
180
- return 0;
181
- }
182
- var a = this._DielectricSpecular.r;
183
- var b = diffuse * oneMinusSpecularStrength / (1.0 - this._DielectricSpecular.r) + specular - 2.0 * this._DielectricSpecular.r;
184
- var c = this._DielectricSpecular.r - specular;
185
- var D = b * b - 4.0 * a * c;
186
- return Scalar.Clamp((-b + Math.sqrt(D)) / (2.0 * a), 0, 1);
187
- };
188
- /**
189
- * Sets the glTF alpha mode to a glTF material from the Babylon Material
190
- * @param glTFMaterial glTF material
191
- * @param babylonMaterial Babylon material
192
- */
193
- _GLTFMaterialExporter._SetAlphaMode = function (glTFMaterial, babylonMaterial) {
194
- if (babylonMaterial.needAlphaBlending()) {
195
- glTFMaterial.alphaMode = "BLEND" /* BLEND */;
196
- }
197
- else if (babylonMaterial.needAlphaTesting()) {
198
- glTFMaterial.alphaMode = "MASK" /* MASK */;
199
- glTFMaterial.alphaCutoff = babylonMaterial.alphaCutOff;
200
- }
201
- };
202
- /**
203
- * Converts a Babylon Standard Material to a glTF Material
204
- * @param babylonStandardMaterial BJS Standard Material
205
- * @param mimeType mime type to use for the textures
206
- * @param images array of glTF image interfaces
207
- * @param textures array of glTF texture interfaces
208
- * @param materials array of glTF material interfaces
209
- * @param imageData map of image file name to data
210
- * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
211
- */
212
- _GLTFMaterialExporter.prototype._convertStandardMaterialAsync = function (babylonStandardMaterial, mimeType, hasTextureCoords) {
213
- var materialMap = this._exporter._materialMap;
214
- var materials = this._exporter._materials;
215
- var promises = [];
216
- var glTFPbrMetallicRoughness = this._convertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
217
- var glTFMaterial = { name: babylonStandardMaterial.name };
218
- if (babylonStandardMaterial.backFaceCulling != null && !babylonStandardMaterial.backFaceCulling) {
219
- if (!babylonStandardMaterial.twoSidedLighting) {
220
- Tools.Warn(babylonStandardMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
221
- }
222
- glTFMaterial.doubleSided = true;
223
- }
224
- if (hasTextureCoords) {
225
- if (babylonStandardMaterial.diffuseTexture) {
226
- promises.push(this._exportTextureAsync(babylonStandardMaterial.diffuseTexture, mimeType).then(function (glTFTexture) {
227
- if (glTFTexture) {
228
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
229
- }
230
- }));
231
- }
232
- if (babylonStandardMaterial.bumpTexture) {
233
- promises.push(this._exportTextureAsync(babylonStandardMaterial.bumpTexture, mimeType).then(function (glTFTexture) {
234
- if (glTFTexture) {
235
- glTFMaterial.normalTexture = glTFTexture;
236
- if (babylonStandardMaterial.bumpTexture != null && babylonStandardMaterial.bumpTexture.level !== 1) {
237
- glTFMaterial.normalTexture.scale = babylonStandardMaterial.bumpTexture.level;
238
- }
239
- }
240
- }));
241
- }
242
- if (babylonStandardMaterial.emissiveTexture) {
243
- glTFMaterial.emissiveFactor = [1.0, 1.0, 1.0];
244
- promises.push(this._exportTextureAsync(babylonStandardMaterial.emissiveTexture, mimeType).then(function (glTFEmissiveTexture) {
245
- if (glTFEmissiveTexture) {
246
- glTFMaterial.emissiveTexture = glTFEmissiveTexture;
247
- }
248
- }));
249
- }
250
- if (babylonStandardMaterial.ambientTexture) {
251
- promises.push(this._exportTextureAsync(babylonStandardMaterial.ambientTexture, mimeType).then(function (glTFTexture) {
252
- if (glTFTexture) {
253
- var occlusionTexture = {
254
- index: glTFTexture.index
255
- };
256
- glTFMaterial.occlusionTexture = occlusionTexture;
257
- occlusionTexture.strength = 1.0;
258
- }
259
- }));
260
- }
261
- }
262
- if (babylonStandardMaterial.alpha < 1.0 || babylonStandardMaterial.opacityTexture) {
263
- if (babylonStandardMaterial.alphaMode === Constants.ALPHA_COMBINE) {
264
- glTFMaterial.alphaMode = "BLEND" /* BLEND */;
265
- }
266
- else {
267
- Tools.Warn(babylonStandardMaterial.name + ": glTF 2.0 does not support alpha mode: " + babylonStandardMaterial.alphaMode.toString());
268
- }
269
- }
270
- if (babylonStandardMaterial.emissiveColor && !_GLTFMaterialExporter.FuzzyEquals(babylonStandardMaterial.emissiveColor, Color3.Black(), _GLTFMaterialExporter._Epsilon)) {
271
- glTFMaterial.emissiveFactor = babylonStandardMaterial.emissiveColor.asArray();
272
- }
273
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
274
- _GLTFMaterialExporter._SetAlphaMode(glTFMaterial, babylonStandardMaterial);
275
- materials.push(glTFMaterial);
276
- materialMap[babylonStandardMaterial.uniqueId] = materials.length - 1;
277
- return this._finishMaterial(promises, glTFMaterial, babylonStandardMaterial, mimeType);
278
- };
279
- _GLTFMaterialExporter.prototype._finishMaterial = function (promises, glTFMaterial, babylonMaterial, mimeType) {
280
- var _this = this;
281
- return Promise.all(promises).then(function () {
282
- var textures = _this._exporter._extensionsPostExportMaterialAdditionalTextures("exportMaterial", glTFMaterial, babylonMaterial);
283
- var tasks = null;
284
- for (var _i = 0, textures_1 = textures; _i < textures_1.length; _i++) {
285
- var texture = textures_1[_i];
286
- if (!tasks) {
287
- tasks = [];
288
- }
289
- tasks.push(_this._exportTextureAsync(texture, mimeType));
290
- }
291
- if (!tasks) {
292
- tasks = [Promise.resolve(null)];
293
- }
294
- return Promise.all(tasks).then(function () {
295
- var extensionWork = _this._exporter._extensionsPostExportMaterialAsync("exportMaterial", glTFMaterial, babylonMaterial);
296
- if (!extensionWork) {
297
- return glTFMaterial;
298
- }
299
- return extensionWork.then(function () { return glTFMaterial; });
300
- });
301
- });
302
- };
303
- /**
304
- * Converts a Babylon PBR Metallic Roughness Material to a glTF Material
305
- * @param babylonPBRMetalRoughMaterial BJS PBR Metallic Roughness Material
306
- * @param mimeType mime type to use for the textures
307
- * @param images array of glTF image interfaces
308
- * @param textures array of glTF texture interfaces
309
- * @param materials array of glTF material interfaces
310
- * @param imageData map of image file name to data
311
- * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
312
- */
313
- _GLTFMaterialExporter.prototype._convertPBRMetallicRoughnessMaterialAsync = function (babylonPBRMetalRoughMaterial, mimeType, hasTextureCoords) {
314
- var materialMap = this._exporter._materialMap;
315
- var materials = this._exporter._materials;
316
- var promises = [];
317
- var glTFPbrMetallicRoughness = {};
318
- if (babylonPBRMetalRoughMaterial.baseColor) {
319
- glTFPbrMetallicRoughness.baseColorFactor = [
320
- babylonPBRMetalRoughMaterial.baseColor.r,
321
- babylonPBRMetalRoughMaterial.baseColor.g,
322
- babylonPBRMetalRoughMaterial.baseColor.b,
323
- babylonPBRMetalRoughMaterial.alpha
324
- ];
325
- }
326
- if (babylonPBRMetalRoughMaterial.metallic != null && babylonPBRMetalRoughMaterial.metallic !== 1) {
327
- glTFPbrMetallicRoughness.metallicFactor = babylonPBRMetalRoughMaterial.metallic;
328
- }
329
- if (babylonPBRMetalRoughMaterial.roughness != null && babylonPBRMetalRoughMaterial.roughness !== 1) {
330
- glTFPbrMetallicRoughness.roughnessFactor = babylonPBRMetalRoughMaterial.roughness;
331
- }
332
- var glTFMaterial = {
333
- name: babylonPBRMetalRoughMaterial.name
334
- };
335
- if (babylonPBRMetalRoughMaterial.doubleSided) {
336
- glTFMaterial.doubleSided = babylonPBRMetalRoughMaterial.doubleSided;
337
- }
338
- _GLTFMaterialExporter._SetAlphaMode(glTFMaterial, babylonPBRMetalRoughMaterial);
339
- if (hasTextureCoords) {
340
- if (babylonPBRMetalRoughMaterial.baseTexture != null) {
341
- promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.baseTexture, mimeType).then(function (glTFTexture) {
342
- if (glTFTexture) {
343
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
344
- }
345
- }));
346
- }
347
- if (babylonPBRMetalRoughMaterial.normalTexture) {
348
- promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.normalTexture, mimeType).then(function (glTFTexture) {
349
- if (glTFTexture) {
350
- glTFMaterial.normalTexture = glTFTexture;
351
- if (babylonPBRMetalRoughMaterial.normalTexture.level !== 1) {
352
- glTFMaterial.normalTexture.scale = babylonPBRMetalRoughMaterial.normalTexture.level;
353
- }
354
- }
355
- }));
356
- }
357
- if (babylonPBRMetalRoughMaterial.occlusionTexture) {
358
- promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.occlusionTexture, mimeType).then(function (glTFTexture) {
359
- if (glTFTexture) {
360
- glTFMaterial.occlusionTexture = glTFTexture;
361
- if (babylonPBRMetalRoughMaterial.occlusionStrength != null) {
362
- glTFMaterial.occlusionTexture.strength = babylonPBRMetalRoughMaterial.occlusionStrength;
363
- }
364
- }
365
- }));
366
- }
367
- if (babylonPBRMetalRoughMaterial.emissiveTexture) {
368
- promises.push(this._exportTextureAsync(babylonPBRMetalRoughMaterial.emissiveTexture, mimeType).then(function (glTFTexture) {
369
- if (glTFTexture) {
370
- glTFMaterial.emissiveTexture = glTFTexture;
371
- }
372
- }));
373
- }
374
- }
375
- if (_GLTFMaterialExporter.FuzzyEquals(babylonPBRMetalRoughMaterial.emissiveColor, Color3.Black(), _GLTFMaterialExporter._Epsilon)) {
376
- glTFMaterial.emissiveFactor = babylonPBRMetalRoughMaterial.emissiveColor.asArray();
377
- }
378
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
379
- materials.push(glTFMaterial);
380
- materialMap[babylonPBRMetalRoughMaterial.uniqueId] = materials.length - 1;
381
- return this._finishMaterial(promises, glTFMaterial, babylonPBRMetalRoughMaterial, mimeType);
382
- };
383
- /**
384
- * Converts an image typed array buffer to a base64 image
385
- * @param buffer typed array buffer
386
- * @param width width of the image
387
- * @param height height of the image
388
- * @param mimeType mimetype of the image
389
- * @returns base64 image string
390
- */
391
- _GLTFMaterialExporter.prototype._createBase64FromCanvasAsync = function (buffer, width, height, mimeType) {
392
- var _this = this;
393
- return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
394
- var textureType, hostingScene, engine, tempTexture, data, base64;
395
- return __generator(this, function (_a) {
396
- switch (_a.label) {
397
- case 0:
398
- textureType = Constants.TEXTURETYPE_UNSIGNED_INT;
399
- hostingScene = this._exporter._babylonScene;
400
- engine = hostingScene.getEngine();
401
- tempTexture = engine.createRawTexture(buffer, width, height, Constants.TEXTUREFORMAT_RGBA, false, true, Texture.NEAREST_SAMPLINGMODE, null, textureType);
402
- return [4 /*yield*/, TextureTools.ApplyPostProcess("pass", tempTexture, hostingScene, textureType, Constants.TEXTURE_NEAREST_SAMPLINGMODE, Constants.TEXTUREFORMAT_RGBA)];
403
- case 1:
404
- _a.sent();
405
- return [4 /*yield*/, engine._readTexturePixels(tempTexture, width, height)];
406
- case 2:
407
- data = _a.sent();
408
- return [4 /*yield*/, Tools.DumpDataAsync(width, height, data, mimeType, undefined, true, false)];
409
- case 3:
410
- base64 = _a.sent();
411
- resolve(base64);
412
- return [2 /*return*/];
413
- }
414
- });
415
- }); });
416
- };
417
- /**
418
- * Generates a white texture based on the specified width and height
419
- * @param width width of the texture in pixels
420
- * @param height height of the texture in pixels
421
- * @param scene babylonjs scene
422
- * @returns white texture
423
- */
424
- _GLTFMaterialExporter.prototype._createWhiteTexture = function (width, height, scene) {
425
- var data = new Uint8Array(width * height * 4);
426
- for (var i = 0; i < data.length; i = i + 4) {
427
- data[i] = data[i + 1] = data[i + 2] = data[i + 3] = 0xFF;
428
- }
429
- var rawTexture = RawTexture.CreateRGBATexture(data, width, height, scene);
430
- return rawTexture;
431
- };
432
- /**
433
- * Resizes the two source textures to the same dimensions. If a texture is null, a default white texture is generated. If both textures are null, returns null
434
- * @param texture1 first texture to resize
435
- * @param texture2 second texture to resize
436
- * @param scene babylonjs scene
437
- * @returns resized textures or null
438
- */
439
- _GLTFMaterialExporter.prototype._resizeTexturesToSameDimensions = function (texture1, texture2, scene) {
440
- var texture1Size = texture1 ? texture1.getSize() : { width: 0, height: 0 };
441
- var texture2Size = texture2 ? texture2.getSize() : { width: 0, height: 0 };
442
- var resizedTexture1;
443
- var resizedTexture2;
444
- if (texture1Size.width < texture2Size.width) {
445
- if (texture1 && texture1 instanceof Texture) {
446
- resizedTexture1 = TextureTools.CreateResizedCopy(texture1, texture2Size.width, texture2Size.height, true);
447
- }
448
- else {
449
- resizedTexture1 = this._createWhiteTexture(texture2Size.width, texture2Size.height, scene);
450
- }
451
- resizedTexture2 = texture2;
452
- }
453
- else if (texture1Size.width > texture2Size.width) {
454
- if (texture2 && texture2 instanceof Texture) {
455
- resizedTexture2 = TextureTools.CreateResizedCopy(texture2, texture1Size.width, texture1Size.height, true);
456
- }
457
- else {
458
- resizedTexture2 = this._createWhiteTexture(texture1Size.width, texture1Size.height, scene);
459
- }
460
- resizedTexture1 = texture1;
461
- }
462
- else {
463
- resizedTexture1 = texture1;
464
- resizedTexture2 = texture2;
465
- }
466
- return {
467
- "texture1": resizedTexture1,
468
- "texture2": resizedTexture2
469
- };
470
- };
471
- /**
472
- * Converts an array of pixels to a Float32Array
473
- * Throws an error if the pixel format is not supported
474
- * @param pixels - array buffer containing pixel values
475
- * @returns Float32 of pixels
476
- */
477
- _GLTFMaterialExporter.prototype._convertPixelArrayToFloat32 = function (pixels) {
478
- if (pixels instanceof Uint8Array) {
479
- var length_1 = pixels.length;
480
- var buffer = new Float32Array(pixels.length);
481
- for (var i = 0; i < length_1; ++i) {
482
- buffer[i] = pixels[i] / 255;
483
- }
484
- return buffer;
485
- }
486
- else if (pixels instanceof Float32Array) {
487
- return pixels;
488
- }
489
- else {
490
- throw new Error('Unsupported pixel format!');
491
- }
492
- };
493
- /**
494
- * Convert Specular Glossiness Textures to Metallic Roughness
495
- * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
496
- * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
497
- * @param diffuseTexture texture used to store diffuse information
498
- * @param specularGlossinessTexture texture used to store specular and glossiness information
499
- * @param factors specular glossiness material factors
500
- * @param mimeType the mime type to use for the texture
501
- * @returns pbr metallic roughness interface or null
502
- */
503
- _GLTFMaterialExporter.prototype._convertSpecularGlossinessTexturesToMetallicRoughnessAsync = function (diffuseTexture, specularGlossinessTexture, factors, mimeType) {
504
- var _a;
505
- return __awaiter(this, void 0, void 0, function () {
506
- var promises, scene, resizedTextures, diffuseSize, diffuseBuffer, specularGlossinessBuffer, width, height, diffusePixels, specularPixels, byteLength, metallicRoughnessBuffer, baseColorBuffer, strideSize, maxBaseColor, maxMetallic, maxRoughness, h, w, offset, diffuseColor, specularColor, glossiness, specularGlossiness, metallicRoughness, metallicRoughnessFactors_1, writeOutMetallicRoughnessTexture, writeOutBaseColorTexture, h, w, destinationOffset, linearBaseColorPixel, sRGBBaseColorPixel, metallicRoughnessPixel, promise, promise;
507
- return __generator(this, function (_b) {
508
- switch (_b.label) {
509
- case 0:
510
- promises = [];
511
- if (!(diffuseTexture || specularGlossinessTexture)) {
512
- return [2 /*return*/, Promise.reject('_ConvertSpecularGlosinessTexturesToMetallicRoughness: diffuse and specular glossiness textures are not defined!')];
513
- }
514
- scene = diffuseTexture ? diffuseTexture.getScene() : specularGlossinessTexture ? specularGlossinessTexture.getScene() : null;
515
- if (!scene) return [3 /*break*/, 3];
516
- resizedTextures = this._resizeTexturesToSameDimensions(diffuseTexture, specularGlossinessTexture, scene);
517
- diffuseSize = (_a = resizedTextures.texture1) === null || _a === void 0 ? void 0 : _a.getSize();
518
- diffuseBuffer = void 0;
519
- specularGlossinessBuffer = void 0;
520
- width = diffuseSize.width;
521
- height = diffuseSize.height;
522
- return [4 /*yield*/, resizedTextures.texture1.readPixels()];
523
- case 1:
524
- diffusePixels = _b.sent();
525
- return [4 /*yield*/, resizedTextures.texture2.readPixels()];
526
- case 2:
527
- specularPixels = _b.sent();
528
- if (diffusePixels) {
529
- diffuseBuffer = this._convertPixelArrayToFloat32(diffusePixels);
530
- }
531
- else {
532
- return [2 /*return*/, Promise.reject("Failed to retrieve pixels from diffuse texture!")];
533
- }
534
- if (specularPixels) {
535
- specularGlossinessBuffer = this._convertPixelArrayToFloat32(specularPixels);
536
- }
537
- else {
538
- return [2 /*return*/, Promise.reject("Failed to retrieve pixels from specular glossiness texture!")];
539
- }
540
- byteLength = specularGlossinessBuffer.byteLength;
541
- metallicRoughnessBuffer = new Uint8Array(byteLength);
542
- baseColorBuffer = new Uint8Array(byteLength);
543
- strideSize = 4;
544
- maxBaseColor = Color3.Black();
545
- maxMetallic = 0;
546
- maxRoughness = 0;
547
- for (h = 0; h < height; ++h) {
548
- for (w = 0; w < width; ++w) {
549
- offset = (width * h + w) * strideSize;
550
- diffuseColor = new Color3(diffuseBuffer[offset], diffuseBuffer[offset + 1], diffuseBuffer[offset + 2]).toLinearSpace().multiply(factors.diffuseColor);
551
- specularColor = new Color3(specularGlossinessBuffer[offset], specularGlossinessBuffer[offset + 1], specularGlossinessBuffer[offset + 2]).toLinearSpace().multiply(factors.specularColor);
552
- glossiness = (specularGlossinessBuffer[offset + 3]) * factors.glossiness;
553
- specularGlossiness = {
554
- diffuseColor: diffuseColor,
555
- specularColor: specularColor,
556
- glossiness: glossiness
557
- };
558
- metallicRoughness = this._convertSpecularGlossinessToMetallicRoughness(specularGlossiness);
559
- maxBaseColor.r = Math.max(maxBaseColor.r, metallicRoughness.baseColor.r);
560
- maxBaseColor.g = Math.max(maxBaseColor.g, metallicRoughness.baseColor.g);
561
- maxBaseColor.b = Math.max(maxBaseColor.b, metallicRoughness.baseColor.b);
562
- maxMetallic = Math.max(maxMetallic, metallicRoughness.metallic);
563
- maxRoughness = Math.max(maxRoughness, metallicRoughness.roughness);
564
- baseColorBuffer[offset] = metallicRoughness.baseColor.r * 255;
565
- baseColorBuffer[offset + 1] = metallicRoughness.baseColor.g * 255;
566
- baseColorBuffer[offset + 2] = metallicRoughness.baseColor.b * 255;
567
- baseColorBuffer[offset + 3] = resizedTextures.texture1.hasAlpha ? diffuseBuffer[offset + 3] * 255 : 255;
568
- metallicRoughnessBuffer[offset] = 0;
569
- metallicRoughnessBuffer[offset + 1] = metallicRoughness.roughness * 255;
570
- metallicRoughnessBuffer[offset + 2] = metallicRoughness.metallic * 255;
571
- metallicRoughnessBuffer[offset + 3] = 255;
572
- }
573
- }
574
- metallicRoughnessFactors_1 = {
575
- baseColor: maxBaseColor,
576
- metallic: maxMetallic,
577
- roughness: maxRoughness
578
- };
579
- writeOutMetallicRoughnessTexture = false;
580
- writeOutBaseColorTexture = false;
581
- for (h = 0; h < height; ++h) {
582
- for (w = 0; w < width; ++w) {
583
- destinationOffset = (width * h + w) * strideSize;
584
- baseColorBuffer[destinationOffset] /= metallicRoughnessFactors_1.baseColor.r > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.r : 1;
585
- baseColorBuffer[destinationOffset + 1] /= metallicRoughnessFactors_1.baseColor.g > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.g : 1;
586
- baseColorBuffer[destinationOffset + 2] /= metallicRoughnessFactors_1.baseColor.b > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.baseColor.b : 1;
587
- linearBaseColorPixel = Color3.FromInts(baseColorBuffer[destinationOffset], baseColorBuffer[destinationOffset + 1], baseColorBuffer[destinationOffset + 2]);
588
- sRGBBaseColorPixel = linearBaseColorPixel.toGammaSpace();
589
- baseColorBuffer[destinationOffset] = sRGBBaseColorPixel.r * 255;
590
- baseColorBuffer[destinationOffset + 1] = sRGBBaseColorPixel.g * 255;
591
- baseColorBuffer[destinationOffset + 2] = sRGBBaseColorPixel.b * 255;
592
- if (!_GLTFMaterialExporter.FuzzyEquals(sRGBBaseColorPixel, Color3.White(), _GLTFMaterialExporter._Epsilon)) {
593
- writeOutBaseColorTexture = true;
594
- }
595
- metallicRoughnessBuffer[destinationOffset + 1] /= metallicRoughnessFactors_1.roughness > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.roughness : 1;
596
- metallicRoughnessBuffer[destinationOffset + 2] /= metallicRoughnessFactors_1.metallic > _GLTFMaterialExporter._Epsilon ? metallicRoughnessFactors_1.metallic : 1;
597
- metallicRoughnessPixel = Color3.FromInts(255, metallicRoughnessBuffer[destinationOffset + 1], metallicRoughnessBuffer[destinationOffset + 2]);
598
- if (!_GLTFMaterialExporter.FuzzyEquals(metallicRoughnessPixel, Color3.White(), _GLTFMaterialExporter._Epsilon)) {
599
- writeOutMetallicRoughnessTexture = true;
600
- }
601
- }
602
- }
603
- if (writeOutMetallicRoughnessTexture) {
604
- promise = this._createBase64FromCanvasAsync(metallicRoughnessBuffer, width, height, mimeType).then(function (metallicRoughnessBase64) {
605
- metallicRoughnessFactors_1.metallicRoughnessTextureBase64 = metallicRoughnessBase64;
606
- });
607
- promises.push(promise);
608
- }
609
- if (writeOutBaseColorTexture) {
610
- promise = this._createBase64FromCanvasAsync(baseColorBuffer, width, height, mimeType).then(function (baseColorBase64) {
611
- metallicRoughnessFactors_1.baseColorTextureBase64 = baseColorBase64;
612
- });
613
- promises.push(promise);
614
- }
615
- return [2 /*return*/, Promise.all(promises).then(function () {
616
- return metallicRoughnessFactors_1;
617
- })];
618
- case 3: return [2 /*return*/, Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!")];
619
- }
620
- });
621
- });
622
- };
623
- /**
624
- * Converts specular glossiness material properties to metallic roughness
625
- * @param specularGlossiness interface with specular glossiness material properties
626
- * @returns interface with metallic roughness material properties
627
- */
628
- _GLTFMaterialExporter.prototype._convertSpecularGlossinessToMetallicRoughness = function (specularGlossiness) {
629
- var diffusePerceivedBrightness = this._getPerceivedBrightness(specularGlossiness.diffuseColor);
630
- var specularPerceivedBrightness = this._getPerceivedBrightness(specularGlossiness.specularColor);
631
- var oneMinusSpecularStrength = 1 - this._getMaxComponent(specularGlossiness.specularColor);
632
- var metallic = _GLTFMaterialExporter._SolveMetallic(diffusePerceivedBrightness, specularPerceivedBrightness, oneMinusSpecularStrength);
633
- var baseColorFromDiffuse = specularGlossiness.diffuseColor.scale(oneMinusSpecularStrength / (1.0 - _GLTFMaterialExporter._DielectricSpecular.r) / Math.max(1 - metallic, _GLTFMaterialExporter._Epsilon));
634
- var baseColorFromSpecular = specularGlossiness.specularColor.subtract(_GLTFMaterialExporter._DielectricSpecular.scale(1 - metallic)).scale(1 / Math.max(metallic, _GLTFMaterialExporter._Epsilon));
635
- var baseColor = Color3.Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic);
636
- baseColor = baseColor.clampToRef(0, 1, baseColor);
637
- var metallicRoughness = {
638
- baseColor: baseColor,
639
- metallic: metallic,
640
- roughness: 1 - specularGlossiness.glossiness
641
- };
642
- return metallicRoughness;
643
- };
644
- /**
645
- * Calculates the surface reflectance, independent of lighting conditions
646
- * @param color Color source to calculate brightness from
647
- * @returns number representing the perceived brightness, or zero if color is undefined
648
- */
649
- _GLTFMaterialExporter.prototype._getPerceivedBrightness = function (color) {
650
- if (color) {
651
- return Math.sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
652
- }
653
- return 0;
654
- };
655
- /**
656
- * Returns the maximum color component value
657
- * @param color
658
- * @returns maximum color component value, or zero if color is null or undefined
659
- */
660
- _GLTFMaterialExporter.prototype._getMaxComponent = function (color) {
661
- if (color) {
662
- return Math.max(color.r, Math.max(color.g, color.b));
663
- }
664
- return 0;
665
- };
666
- /**
667
- * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors
668
- * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
669
- * @param mimeType mime type to use for the textures
670
- * @param images array of glTF image interfaces
671
- * @param textures array of glTF texture interfaces
672
- * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
673
- * @param imageData map of image file name to data
674
- * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
675
- * @returns glTF PBR Metallic Roughness factors
676
- */
677
- _GLTFMaterialExporter.prototype._convertMetalRoughFactorsToMetallicRoughnessAsync = function (babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords) {
678
- var promises = [];
679
- var baseColor = babylonPBRMaterial._albedoColor;
680
- var metallic = babylonPBRMaterial._metallic;
681
- var roughness = babylonPBRMaterial._roughness;
682
- var metallicRoughness = {
683
- baseColor: baseColor,
684
- metallic: metallic,
685
- roughness: roughness
686
- };
687
- if (hasTextureCoords) {
688
- var albedoTexture = babylonPBRMaterial._albedoTexture;
689
- if (albedoTexture) {
690
- promises.push(this._exportTextureAsync(babylonPBRMaterial._albedoTexture, mimeType).then(function (glTFTexture) {
691
- if (glTFTexture) {
692
- glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
693
- }
694
- }));
695
- }
696
- var metallicTexture = babylonPBRMaterial._metallicTexture;
697
- if (metallicTexture) {
698
- promises.push(this._exportTextureAsync(metallicTexture, mimeType).then(function (glTFTexture) {
699
- if (glTFTexture) {
700
- glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
701
- }
702
- }));
703
- }
704
- }
705
- return Promise.all(promises).then(function () {
706
- return metallicRoughness;
707
- });
708
- };
709
- _GLTFMaterialExporter.prototype._getGLTFTextureSampler = function (texture) {
710
- var sampler = this._getGLTFTextureWrapModesSampler(texture);
711
- var samplingMode = texture instanceof Texture ? texture.samplingMode : null;
712
- if (samplingMode != null) {
713
- switch (samplingMode) {
714
- case Texture.LINEAR_LINEAR: {
715
- sampler.magFilter = 9729 /* LINEAR */;
716
- sampler.minFilter = 9729 /* LINEAR */;
717
- break;
718
- }
719
- case Texture.LINEAR_NEAREST: {
720
- sampler.magFilter = 9729 /* LINEAR */;
721
- sampler.minFilter = 9728 /* NEAREST */;
722
- break;
723
- }
724
- case Texture.NEAREST_LINEAR: {
725
- sampler.magFilter = 9728 /* NEAREST */;
726
- sampler.minFilter = 9729 /* LINEAR */;
727
- break;
728
- }
729
- case Texture.NEAREST_LINEAR_MIPLINEAR: {
730
- sampler.magFilter = 9728 /* NEAREST */;
731
- sampler.minFilter = 9987 /* LINEAR_MIPMAP_LINEAR */;
732
- break;
733
- }
734
- case Texture.NEAREST_NEAREST: {
735
- sampler.magFilter = 9728 /* NEAREST */;
736
- sampler.minFilter = 9728 /* NEAREST */;
737
- break;
738
- }
739
- case Texture.NEAREST_LINEAR_MIPNEAREST: {
740
- sampler.magFilter = 9728 /* NEAREST */;
741
- sampler.minFilter = 9985 /* LINEAR_MIPMAP_NEAREST */;
742
- break;
743
- }
744
- case Texture.LINEAR_NEAREST_MIPNEAREST: {
745
- sampler.magFilter = 9729 /* LINEAR */;
746
- sampler.minFilter = 9984 /* NEAREST_MIPMAP_NEAREST */;
747
- break;
748
- }
749
- case Texture.LINEAR_NEAREST_MIPLINEAR: {
750
- sampler.magFilter = 9729 /* LINEAR */;
751
- sampler.minFilter = 9986 /* NEAREST_MIPMAP_LINEAR */;
752
- break;
753
- }
754
- case Texture.NEAREST_NEAREST_MIPLINEAR: {
755
- sampler.magFilter = 9728 /* NEAREST */;
756
- sampler.minFilter = 9986 /* NEAREST_MIPMAP_LINEAR */;
757
- break;
758
- }
759
- case Texture.LINEAR_LINEAR_MIPLINEAR: {
760
- sampler.magFilter = 9729 /* LINEAR */;
761
- sampler.minFilter = 9987 /* LINEAR_MIPMAP_LINEAR */;
762
- break;
763
- }
764
- case Texture.LINEAR_LINEAR_MIPNEAREST: {
765
- sampler.magFilter = 9729 /* LINEAR */;
766
- sampler.minFilter = 9985 /* LINEAR_MIPMAP_NEAREST */;
767
- break;
768
- }
769
- case Texture.NEAREST_NEAREST_MIPNEAREST: {
770
- sampler.magFilter = 9728 /* NEAREST */;
771
- sampler.minFilter = 9984 /* NEAREST_MIPMAP_NEAREST */;
772
- break;
773
- }
774
- }
775
- }
776
- return sampler;
777
- };
778
- _GLTFMaterialExporter.prototype._getGLTFTextureWrapMode = function (wrapMode) {
779
- switch (wrapMode) {
780
- case Texture.WRAP_ADDRESSMODE: {
781
- return 10497 /* REPEAT */;
782
- }
783
- case Texture.CLAMP_ADDRESSMODE: {
784
- return 33071 /* CLAMP_TO_EDGE */;
785
- }
786
- case Texture.MIRROR_ADDRESSMODE: {
787
- return 33648 /* MIRRORED_REPEAT */;
788
- }
789
- default: {
790
- Tools.Error("Unsupported Texture Wrap Mode ".concat(wrapMode, "!"));
791
- return 10497 /* REPEAT */;
792
- }
793
- }
794
- };
795
- _GLTFMaterialExporter.prototype._getGLTFTextureWrapModesSampler = function (texture) {
796
- var wrapS = this._getGLTFTextureWrapMode(texture instanceof Texture ? texture.wrapU : Texture.WRAP_ADDRESSMODE);
797
- var wrapT = this._getGLTFTextureWrapMode(texture instanceof Texture ? texture.wrapV : Texture.WRAP_ADDRESSMODE);
798
- if (wrapS === 10497 /* REPEAT */ && wrapT === 10497 /* REPEAT */) { // default wrapping mode in glTF, so omitting
799
- return {};
800
- }
801
- return { wrapS: wrapS, wrapT: wrapT };
802
- };
803
- /**
804
- * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors
805
- * @param babylonPBRMaterial BJS PBR Metallic Roughness Material
806
- * @param mimeType mime type to use for the textures
807
- * @param images array of glTF image interfaces
808
- * @param textures array of glTF texture interfaces
809
- * @param glTFPbrMetallicRoughness glTF PBR Metallic Roughness interface
810
- * @param imageData map of image file name to data
811
- * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
812
- * @returns glTF PBR Metallic Roughness factors
813
- */
814
- _GLTFMaterialExporter.prototype._convertSpecGlossFactorsToMetallicRoughnessAsync = function (babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords) {
815
- var _this = this;
816
- return Promise.resolve().then(function () {
817
- var samplers = _this._exporter._samplers;
818
- var textures = _this._exporter._textures;
819
- var diffuseColor = babylonPBRMaterial._albedoColor;
820
- var specularColor = babylonPBRMaterial._reflectivityColor;
821
- var glossiness = babylonPBRMaterial._microSurface;
822
- var specGloss = {
823
- diffuseColor: diffuseColor,
824
- specularColor: specularColor,
825
- glossiness: glossiness,
826
- };
827
- var samplerIndex = null;
828
- var albedoTexture = babylonPBRMaterial._albedoTexture;
829
- var reflectivityTexture = babylonPBRMaterial._reflectivityTexture;
830
- if (albedoTexture) {
831
- var sampler = _this._getGLTFTextureSampler(albedoTexture);
832
- if (sampler.magFilter != null && sampler.minFilter != null && sampler.wrapS != null && sampler.wrapT != null) {
833
- samplers.push(sampler);
834
- samplerIndex = samplers.length - 1;
835
- }
836
- }
837
- var useMicrosurfaceFromReflectivityMapAlpha = babylonPBRMaterial._useMicroSurfaceFromReflectivityMapAlpha;
838
- if (reflectivityTexture && !useMicrosurfaceFromReflectivityMapAlpha) {
839
- return Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported");
840
- }
841
- if ((albedoTexture || reflectivityTexture) && hasTextureCoords) {
842
- return _this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(albedoTexture, reflectivityTexture, specGloss, mimeType).then(function (metallicRoughnessFactors) {
843
- if (metallicRoughnessFactors.baseColorTextureBase64) {
844
- var glTFBaseColorTexture = _this._getTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, albedoTexture ? albedoTexture.coordinatesIndex : null, samplerIndex);
845
- if (glTFBaseColorTexture) {
846
- glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
847
- }
848
- }
849
- if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
850
- var glTFMRColorTexture = _this._getTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, reflectivityTexture ? reflectivityTexture.coordinatesIndex : null, samplerIndex);
851
- if (glTFMRColorTexture) {
852
- glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
853
- }
854
- }
855
- return metallicRoughnessFactors;
856
- });
857
- }
858
- else {
859
- return _this._convertSpecularGlossinessToMetallicRoughness(specGloss);
860
- }
861
- });
862
- };
863
- /**
864
- * Converts a Babylon PBR Base Material to a glTF Material
865
- * @param babylonPBRMaterial BJS PBR Base Material
866
- * @param mimeType mime type to use for the textures
867
- * @param images array of glTF image interfaces
868
- * @param textures array of glTF texture interfaces
869
- * @param materials array of glTF material interfaces
870
- * @param imageData map of image file name to data
871
- * @param hasTextureCoords specifies if texture coordinates are present on the submesh to determine if textures should be applied
872
- */
873
- _GLTFMaterialExporter.prototype._convertPBRMaterialAsync = function (babylonPBRMaterial, mimeType, hasTextureCoords) {
874
- var _this = this;
875
- var glTFPbrMetallicRoughness = {};
876
- var glTFMaterial = {
877
- name: babylonPBRMaterial.name
878
- };
879
- var useMetallicRoughness = babylonPBRMaterial.isMetallicWorkflow();
880
- if (useMetallicRoughness) {
881
- var albedoColor = babylonPBRMaterial._albedoColor;
882
- var alpha = babylonPBRMaterial.alpha;
883
- if (albedoColor) {
884
- glTFPbrMetallicRoughness.baseColorFactor = [
885
- albedoColor.r,
886
- albedoColor.g,
887
- albedoColor.b,
888
- alpha
889
- ];
890
- }
891
- return this._convertMetalRoughFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(function (metallicRoughness) {
892
- return _this.setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
893
- });
894
- }
895
- else {
896
- return this._convertSpecGlossFactorsToMetallicRoughnessAsync(babylonPBRMaterial, mimeType, glTFPbrMetallicRoughness, hasTextureCoords).then(function (metallicRoughness) {
897
- return _this.setMetallicRoughnessPbrMaterial(metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords);
898
- });
899
- }
900
- };
901
- _GLTFMaterialExporter.prototype.setMetallicRoughnessPbrMaterial = function (metallicRoughness, babylonPBRMaterial, glTFMaterial, glTFPbrMetallicRoughness, mimeType, hasTextureCoords) {
902
- var materialMap = this._exporter._materialMap;
903
- var materials = this._exporter._materials;
904
- var promises = [];
905
- if (metallicRoughness) {
906
- _GLTFMaterialExporter._SetAlphaMode(glTFMaterial, babylonPBRMaterial);
907
- if (!(_GLTFMaterialExporter.FuzzyEquals(metallicRoughness.baseColor, Color3.White(), _GLTFMaterialExporter._Epsilon) && babylonPBRMaterial.alpha >= _GLTFMaterialExporter._Epsilon)) {
908
- glTFPbrMetallicRoughness.baseColorFactor = [
909
- metallicRoughness.baseColor.r,
910
- metallicRoughness.baseColor.g,
911
- metallicRoughness.baseColor.b,
912
- babylonPBRMaterial.alpha
913
- ];
914
- }
915
- if (metallicRoughness.metallic != null && metallicRoughness.metallic !== 1) {
916
- glTFPbrMetallicRoughness.metallicFactor = metallicRoughness.metallic;
917
- }
918
- if (metallicRoughness.roughness != null && metallicRoughness.roughness !== 1) {
919
- glTFPbrMetallicRoughness.roughnessFactor = metallicRoughness.roughness;
920
- }
921
- if (babylonPBRMaterial.backFaceCulling != null && !babylonPBRMaterial.backFaceCulling) {
922
- if (!babylonPBRMaterial._twoSidedLighting) {
923
- Tools.Warn(babylonPBRMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
924
- }
925
- glTFMaterial.doubleSided = true;
926
- }
927
- if (hasTextureCoords) {
928
- var bumpTexture_1 = babylonPBRMaterial._bumpTexture;
929
- if (bumpTexture_1) {
930
- var promise = this._exportTextureAsync(bumpTexture_1, mimeType).then(function (glTFTexture) {
931
- if (glTFTexture) {
932
- glTFMaterial.normalTexture = glTFTexture;
933
- if (bumpTexture_1.level !== 1) {
934
- glTFMaterial.normalTexture.scale = bumpTexture_1.level;
935
- }
936
- }
937
- });
938
- promises.push(promise);
939
- }
940
- var ambientTexture = babylonPBRMaterial._ambientTexture;
941
- if (ambientTexture) {
942
- var promise = this._exportTextureAsync(ambientTexture, mimeType).then(function (glTFTexture) {
943
- if (glTFTexture) {
944
- var occlusionTexture = {
945
- index: glTFTexture.index,
946
- texCoord: glTFTexture.texCoord
947
- };
948
- glTFMaterial.occlusionTexture = occlusionTexture;
949
- var ambientTextureStrength = babylonPBRMaterial._ambientTextureStrength;
950
- if (ambientTextureStrength) {
951
- occlusionTexture.strength = ambientTextureStrength;
952
- }
953
- }
954
- });
955
- promises.push(promise);
956
- }
957
- var emissiveTexture = babylonPBRMaterial._emissiveTexture;
958
- if (emissiveTexture) {
959
- var promise = this._exportTextureAsync(emissiveTexture, mimeType).then(function (glTFTexture) {
960
- if (glTFTexture) {
961
- glTFMaterial.emissiveTexture = glTFTexture;
962
- }
963
- });
964
- promises.push(promise);
965
- }
966
- }
967
- var emissiveColor = babylonPBRMaterial._emissiveColor;
968
- if (!_GLTFMaterialExporter.FuzzyEquals(emissiveColor, Color3.Black(), _GLTFMaterialExporter._Epsilon)) {
969
- glTFMaterial.emissiveFactor = emissiveColor.asArray();
970
- }
971
- glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
972
- materials.push(glTFMaterial);
973
- materialMap[babylonPBRMaterial.uniqueId] = materials.length - 1;
974
- }
975
- return this._finishMaterial(promises, glTFMaterial, babylonPBRMaterial, mimeType);
976
- };
977
- _GLTFMaterialExporter.prototype.getPixelsFromTexture = function (babylonTexture) {
978
- var pixels = babylonTexture.textureType === Constants.TEXTURETYPE_UNSIGNED_INT ? babylonTexture.readPixels() : babylonTexture.readPixels();
979
- return pixels;
980
- };
981
- /**
982
- * Extracts a texture from a Babylon texture into file data and glTF data
983
- * @param babylonTexture Babylon texture to extract
984
- * @param mimeType Mime Type of the babylonTexture
985
- * @return glTF texture info, or null if the texture format is not supported
986
- */
987
- _GLTFMaterialExporter.prototype._exportTextureAsync = function (babylonTexture, mimeType) {
988
- var _this = this;
989
- var extensionPromise = this._exporter._extensionsPreExportTextureAsync("exporter", babylonTexture, mimeType);
990
- if (!extensionPromise) {
991
- return this._exportTextureInfoAsync(babylonTexture, mimeType);
992
- }
993
- return extensionPromise.then(function (texture) {
994
- if (!texture) {
995
- return _this._exportTextureInfoAsync(babylonTexture, mimeType);
996
- }
997
- return _this._exportTextureInfoAsync(texture, mimeType);
998
- });
999
- };
1000
- _GLTFMaterialExporter.prototype._exportTextureInfoAsync = function (babylonTexture, mimeType) {
1001
- var _this = this;
1002
- return Promise.resolve().then(function () { return __awaiter(_this, void 0, void 0, function () {
1003
- var textureUid, pixels, samplers, sampler, samplerIndex_1, foundSamplerIndex, i, s, size;
1004
- var _this = this;
1005
- return __generator(this, function (_a) {
1006
- switch (_a.label) {
1007
- case 0:
1008
- textureUid = babylonTexture.uid;
1009
- if (!(textureUid in this._textureMap)) return [3 /*break*/, 1];
1010
- return [2 /*return*/, this._textureMap[textureUid]];
1011
- case 1: return [4 /*yield*/, this.getPixelsFromTexture(babylonTexture)];
1012
- case 2:
1013
- pixels = _a.sent();
1014
- if (!pixels) {
1015
- return [2 /*return*/, null];
1016
- }
1017
- samplers = this._exporter._samplers;
1018
- sampler = this._getGLTFTextureSampler(babylonTexture);
1019
- samplerIndex_1 = null;
1020
- foundSamplerIndex = null;
1021
- for (i = 0; i < samplers.length; ++i) {
1022
- s = samplers[i];
1023
- if (s.minFilter === sampler.minFilter && s.magFilter === sampler.magFilter &&
1024
- s.wrapS === sampler.wrapS && s.wrapT === sampler.wrapT) {
1025
- foundSamplerIndex = i;
1026
- break;
1027
- }
1028
- }
1029
- if (foundSamplerIndex == null) {
1030
- samplers.push(sampler);
1031
- samplerIndex_1 = samplers.length - 1;
1032
- }
1033
- else {
1034
- samplerIndex_1 = foundSamplerIndex;
1035
- }
1036
- size = babylonTexture.getSize();
1037
- // Preserve texture mime type if defined
1038
- if (babylonTexture.mimeType) {
1039
- switch (babylonTexture.mimeType) {
1040
- case "image/jpeg":
1041
- mimeType = "image/jpeg" /* JPEG */;
1042
- break;
1043
- case "image/png":
1044
- mimeType = "image/png" /* PNG */;
1045
- break;
1046
- }
1047
- }
1048
- return [2 /*return*/, this._createBase64FromCanvasAsync(pixels, size.width, size.height, mimeType).then(function (base64Data) {
1049
- var textureInfo = _this._getTextureInfoFromBase64(base64Data, babylonTexture.name.replace(/\.\/|\/|\.\\|\\/g, "_"), mimeType, babylonTexture.coordinatesIndex, samplerIndex_1);
1050
- if (textureInfo) {
1051
- _this._textureMap[textureUid] = textureInfo;
1052
- _this._exporter._extensionsPostExportTextures("linkTextureInfo", textureInfo, babylonTexture);
1053
- }
1054
- return textureInfo;
1055
- })];
1056
- }
1057
- });
1058
- }); });
1059
- };
1060
- /**
1061
- * Builds a texture from base64 string
1062
- * @param base64Texture base64 texture string
1063
- * @param baseTextureName Name to use for the texture
1064
- * @param mimeType image mime type for the texture
1065
- * @param images array of images
1066
- * @param textures array of textures
1067
- * @param imageData map of image data
1068
- * @returns glTF texture info, or null if the texture format is not supported
1069
- */
1070
- _GLTFMaterialExporter.prototype._getTextureInfoFromBase64 = function (base64Texture, baseTextureName, mimeType, texCoordIndex, samplerIndex) {
1071
- var textures = this._exporter._textures;
1072
- var images = this._exporter._images;
1073
- var imageData = this._exporter._imageData;
1074
- var textureInfo = null;
1075
- var glTFTexture = {
1076
- source: images.length,
1077
- name: baseTextureName
1078
- };
1079
- if (samplerIndex != null) {
1080
- glTFTexture.sampler = samplerIndex;
1081
- }
1082
- var binStr = atob(base64Texture.split(',')[1]);
1083
- var arrBuff = new ArrayBuffer(binStr.length);
1084
- var arr = new Uint8Array(arrBuff);
1085
- for (var i = 0, length_2 = binStr.length; i < length_2; ++i) {
1086
- arr[i] = binStr.charCodeAt(i);
1087
- }
1088
- var imageValues = { data: arr, mimeType: mimeType };
1089
- var extension = mimeType === "image/jpeg" /* JPEG */ ? '.jpeg' : '.png';
1090
- var textureName = baseTextureName + extension;
1091
- var originalTextureName = textureName;
1092
- if (textureName in imageData) {
1093
- textureName = "".concat(baseTextureName, "_").concat(Tools.RandomId()).concat(extension);
1094
- }
1095
- imageData[textureName] = imageValues;
1096
- if (mimeType === "image/jpeg" /* JPEG */ || mimeType === "image/png" /* PNG */) {
1097
- var glTFImage = {
1098
- name: baseTextureName,
1099
- uri: textureName
1100
- };
1101
- var foundIndex = null;
1102
- for (var i = 0; i < images.length; ++i) {
1103
- if (images[i].uri === originalTextureName) {
1104
- foundIndex = i;
1105
- break;
1106
- }
1107
- }
1108
- if (foundIndex == null) {
1109
- images.push(glTFImage);
1110
- glTFTexture.source = images.length - 1;
1111
- }
1112
- else {
1113
- glTFTexture.source = foundIndex;
1114
- }
1115
- textures.push(glTFTexture);
1116
- textureInfo = {
1117
- index: textures.length - 1
1118
- };
1119
- if (texCoordIndex != null) {
1120
- textureInfo.texCoord = texCoordIndex;
1121
- }
1122
- }
1123
- else {
1124
- Tools.Error("Unsupported texture mime type ".concat(mimeType));
1125
- }
1126
- return textureInfo;
1127
- };
1128
- /**
1129
- * Represents the dielectric specular values for R, G and B
1130
- */
1131
- _GLTFMaterialExporter._DielectricSpecular = new Color3(0.04, 0.04, 0.04);
1132
- /**
1133
- * Allows the maximum specular power to be defined for material calculations
1134
- */
1135
- _GLTFMaterialExporter._MaxSpecularPower = 1024;
1136
- /**
1137
- * Numeric tolerance value
1138
- */
1139
- _GLTFMaterialExporter._Epsilon = 1e-6;
1140
- return _GLTFMaterialExporter;
1141
- }());
1142
- export { _GLTFMaterialExporter };
1143
- //# sourceMappingURL=glTFMaterialExporter.js.map