babylonjs.serializers.js 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. var __extends = (this && this.__extends) || (function () {
  8. var extendStatics = Object.setPrototypeOf ||
  9. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  10. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  11. return function (d, b) {
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. (function universalModuleDefinition(root, factory) {
  18. if(typeof exports === 'object' && typeof module === 'object')
  19. module.exports = factory(require("babylonjs"));
  20. else if(typeof define === 'function' && define.amd)
  21. define("babylonjs-serializers", ["babylonjs"], factory);
  22. else if(typeof exports === 'object')
  23. exports["babylonjs-serializers"] = factory(require("babylonjs"));
  24. else {
  25. root["BABYLON"] = factory(root["BABYLON"]);
  26. }
  27. })(this, function(BABYLON) {
  28. "use strict";
  29. var BABYLON;
  30. (function (BABYLON) {
  31. var OBJExport = /** @class */ (function () {
  32. function OBJExport() {
  33. }
  34. //Exports the geometrys of a Mesh array in .OBJ file format (text)
  35. OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
  36. var output = [];
  37. var v = 1;
  38. if (materials) {
  39. if (!matlibname) {
  40. matlibname = 'mat';
  41. }
  42. output.push("mtllib " + matlibname + ".mtl");
  43. }
  44. for (var j = 0; j < mesh.length; j++) {
  45. output.push("g object" + j);
  46. output.push("o object_" + j);
  47. //Uses the position of the item in the scene, to the file (this back to normal in the end)
  48. var lastMatrix = null;
  49. if (globalposition) {
  50. var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
  51. lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
  52. mesh[j].bakeTransformIntoVertices(newMatrix);
  53. }
  54. //TODO: submeshes (groups)
  55. //TODO: smoothing groups (s 1, s off);
  56. if (materials) {
  57. var mat = mesh[j].material;
  58. if (mat) {
  59. output.push("usemtl " + mat.id);
  60. }
  61. }
  62. var g = mesh[j].geometry;
  63. if (!g) {
  64. continue;
  65. }
  66. var trunkVerts = g.getVerticesData('position');
  67. var trunkNormals = g.getVerticesData('normal');
  68. var trunkUV = g.getVerticesData('uv');
  69. var trunkFaces = g.getIndices();
  70. var curV = 0;
  71. if (!trunkVerts || !trunkNormals || !trunkUV || !trunkFaces) {
  72. continue;
  73. }
  74. for (var i = 0; i < trunkVerts.length; i += 3) {
  75. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  76. curV++;
  77. }
  78. for (i = 0; i < trunkNormals.length; i += 3) {
  79. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  80. }
  81. for (i = 0; i < trunkUV.length; i += 2) {
  82. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  83. }
  84. for (i = 0; i < trunkFaces.length; i += 3) {
  85. output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
  86. " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
  87. " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
  88. }
  89. //back de previous matrix, to not change the original mesh in the scene
  90. if (globalposition && lastMatrix) {
  91. mesh[j].bakeTransformIntoVertices(lastMatrix);
  92. }
  93. v += curV;
  94. }
  95. var text = output.join("\n");
  96. return (text);
  97. };
  98. //Exports the material(s) of a mesh in .MTL file format (text)
  99. //TODO: Export the materials of mesh array
  100. OBJExport.MTL = function (mesh) {
  101. var output = [];
  102. var m = mesh.material;
  103. output.push("newmtl mat1");
  104. output.push(" Ns " + m.specularPower.toFixed(4));
  105. output.push(" Ni 1.5000");
  106. output.push(" d " + m.alpha.toFixed(4));
  107. output.push(" Tr 0.0000");
  108. output.push(" Tf 1.0000 1.0000 1.0000");
  109. output.push(" illum 2");
  110. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  111. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  112. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  113. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  114. //TODO: uv scale, offset, wrap
  115. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  116. var uvscale = "";
  117. if (m.ambientTexture) {
  118. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  119. }
  120. if (m.diffuseTexture) {
  121. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  122. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  123. }
  124. if (m.specularTexture) {
  125. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  126. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  127. if (m.useGlossinessFromSpecularMapAlpha) {
  128. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  129. }
  130. */
  131. }
  132. /* TODO: emissive texture not in .MAT format (???)
  133. if (m.emissiveTexture) {
  134. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  135. }
  136. */
  137. if (m.bumpTexture) {
  138. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  139. }
  140. if (m.opacityTexture) {
  141. output.push(" map_d " + uvscale + m.opacityTexture.name);
  142. }
  143. var text = output.join("\n");
  144. return (text);
  145. };
  146. return OBJExport;
  147. }());
  148. BABYLON.OBJExport = OBJExport;
  149. })(BABYLON || (BABYLON = {}));
  150. //# sourceMappingURL=babylon.objSerializer.js.map
  151. "use strict";
  152. var BABYLON;
  153. (function (BABYLON) {
  154. ;
  155. /**
  156. * Class for generating glTF data from a Babylon scene.
  157. */
  158. var GLTF2Export = /** @class */ (function () {
  159. function GLTF2Export() {
  160. }
  161. /**
  162. * Exports the geometry of the scene to .gltf file format.
  163. * @param scene - Babylon scene with scene hierarchy information.
  164. * @param filePrefix - File prefix to use when generating the glTF file.
  165. * @param options - Exporter options.
  166. * @returns - Returns an object with a .gltf file and associates texture names
  167. * as keys and their data and paths as values.
  168. */
  169. GLTF2Export.GLTF = function (scene, filePrefix, options) {
  170. var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  171. var gltfGenerator = new BABYLON.GLTF2._Exporter(scene, options);
  172. if (scene.isReady) {
  173. return gltfGenerator._generateGLTF(glTFPrefix);
  174. }
  175. else {
  176. throw new Error("glTF Serializer: Scene is not ready!");
  177. }
  178. };
  179. /**
  180. * Exports the geometry of the scene to .glb file format.
  181. * @param scene - Babylon scene with scene hierarchy information.
  182. * @param filePrefix - File prefix to use when generating glb file.
  183. * @param options - Exporter options.
  184. * @returns - Returns an object with a .glb filename as key and data as value
  185. */
  186. GLTF2Export.GLB = function (scene, filePrefix, options) {
  187. var glTFPrefix = filePrefix.replace(/\.[^/.]+$/, "");
  188. var gltfGenerator = new BABYLON.GLTF2._Exporter(scene, options);
  189. if (scene.isReady) {
  190. return gltfGenerator._generateGLB(glTFPrefix);
  191. }
  192. else {
  193. throw new Error("glTF Serializer: Scene is not ready!");
  194. }
  195. };
  196. return GLTF2Export;
  197. }());
  198. BABYLON.GLTF2Export = GLTF2Export;
  199. })(BABYLON || (BABYLON = {}));
  200. //# sourceMappingURL=babylon.glTFSerializer.js.map
  201. "use strict";
  202. /**
  203. * Module for the Babylon glTF 2.0 exporter. Should ONLY be used internally.
  204. * @ignore - capitalization of GLTF2 module.
  205. */
  206. var BABYLON;
  207. (function (BABYLON) {
  208. var GLTF2;
  209. (function (GLTF2) {
  210. /**
  211. * Converts Babylon Scene into glTF 2.0.
  212. */
  213. var _Exporter = /** @class */ (function () {
  214. /**
  215. * Creates a glTF Exporter instance, which can accept optional exporter options.
  216. * @param babylonScene - Babylon scene object
  217. * @param options - Options to modify the behavior of the exporter.
  218. */
  219. function _Exporter(babylonScene, options) {
  220. this.asset = { generator: "BabylonJS", version: "2.0" };
  221. this.babylonScene = babylonScene;
  222. this.bufferViews = new Array();
  223. this.accessors = new Array();
  224. this.meshes = new Array();
  225. this.scenes = new Array();
  226. this.nodes = new Array();
  227. this.images = new Array();
  228. this.materials = new Array();
  229. this.textures = new Array();
  230. this.imageData = {};
  231. this.convertToRightHandedSystem = this.babylonScene.useRightHandedSystem ? false : true;
  232. if (options) {
  233. this.options = options;
  234. }
  235. }
  236. /**
  237. * Creates a buffer view based on teh supplied arguments
  238. * @param bufferIndex - index value of the specified buffer
  239. * @param byteOffset - byte offset value
  240. * @param byteLength - byte length of the bufferView
  241. * @param byteStride - byte distance between conequential elements.
  242. * @param name - name of the buffer view
  243. * @returns - bufferView for glTF
  244. */
  245. _Exporter.prototype.createBufferView = function (bufferIndex, byteOffset, byteLength, byteStride, name) {
  246. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  247. if (byteOffset) {
  248. bufferview.byteOffset = byteOffset;
  249. }
  250. if (name) {
  251. bufferview.name = name;
  252. }
  253. if (byteStride) {
  254. bufferview.byteStride = byteStride;
  255. }
  256. return bufferview;
  257. };
  258. /**
  259. * Creates an accessor based on the supplied arguments
  260. * @param bufferviewIndex - The index of the bufferview referenced by this accessor.
  261. * @param name - The name of the accessor.
  262. * @param type - The type of the accessor.
  263. * @param componentType - The datatype of components in the attribute.
  264. * @param count - The number of attributes referenced by this accessor.
  265. * @param byteOffset - The offset relative to the start of the bufferView in bytes.
  266. * @param min - Minimum value of each component in this attribute.
  267. * @param max - Maximum value of each component in this attribute.
  268. * @returns - accessor for glTF
  269. */
  270. _Exporter.prototype.createAccessor = function (bufferviewIndex, name, type, componentType, count, byteOffset, min, max) {
  271. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  272. if (min) {
  273. accessor.min = min;
  274. }
  275. if (max) {
  276. accessor.max = max;
  277. }
  278. if (byteOffset) {
  279. accessor.byteOffset = byteOffset;
  280. }
  281. return accessor;
  282. };
  283. /**
  284. * Calculates the minimum and maximum values of an array of position floats.
  285. * @param positions - Positions array of a mesh.
  286. * @param vertexStart - Starting vertex offset to calculate min and max values.
  287. * @param vertexCount - Number of vertices to check for min and max values.
  288. * @returns - min number array and max number array.
  289. */
  290. _Exporter.prototype.calculateMinMaxPositions = function (positions, vertexStart, vertexCount) {
  291. var min = [Infinity, Infinity, Infinity];
  292. var max = [-Infinity, -Infinity, -Infinity];
  293. var positionStrideSize = 3;
  294. var end = vertexStart + vertexCount;
  295. if (vertexCount) {
  296. for (var i = vertexStart; i < end; ++i) {
  297. var indexOffset = positionStrideSize * i;
  298. var position = BABYLON.Vector3.FromArray(positions, indexOffset);
  299. var vector = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(position).asArray() : position.asArray();
  300. for (var j = 0; j < positionStrideSize; ++j) {
  301. var num = vector[j];
  302. if (num < min[j]) {
  303. min[j] = num;
  304. }
  305. if (num > max[j]) {
  306. max[j] = num;
  307. }
  308. ++indexOffset;
  309. }
  310. }
  311. }
  312. return { min: min, max: max };
  313. };
  314. /**
  315. * Converts a vector3 array to right-handed.
  316. * @param vector - vector3 Array to convert to right-handed.
  317. * @returns - right-handed Vector3 array.
  318. */
  319. _Exporter.GetRightHandedVector3 = function (vector) {
  320. return new BABYLON.Vector3(vector.x, vector.y, -vector.z);
  321. };
  322. /**
  323. * Converts a vector4 array to right-handed.
  324. * @param vector - vector4 Array to convert to right-handed.
  325. * @returns - right-handed vector4 array.
  326. */
  327. _Exporter.GetRightHandedVector4 = function (vector) {
  328. return new BABYLON.Vector4(vector.x, vector.y, -vector.z, -vector.w);
  329. };
  330. /**
  331. * Converts a quaternion to right-handed.
  332. * @param quaternion - Source quaternion to convert to right-handed.
  333. */
  334. _Exporter.GetRightHandedQuaternion = function (quaternion) {
  335. return new BABYLON.Quaternion(-quaternion.x, -quaternion.y, quaternion.z, quaternion.w);
  336. };
  337. /**
  338. * Writes mesh attribute data to a data buffer.
  339. * Returns the bytelength of the data.
  340. * @param vertexBufferKind - Indicates what kind of vertex data is being passed in.
  341. * @param meshAttributeArray - Array containing the attribute data.
  342. * @param strideSize - Represents the offset between consecutive attributes
  343. * @param byteOffset - The offset to start counting bytes from.
  344. * @param dataBuffer - The buffer to write the binary data to.
  345. * @returns - Byte length of the attribute data.
  346. */
  347. _Exporter.prototype.writeAttributeData = function (vertexBufferKind, meshAttributeArray, strideSize, vertexBufferOffset, byteOffset, dataBuffer) {
  348. var byteOff = byteOffset;
  349. var end = meshAttributeArray.length / strideSize;
  350. var byteLength = 0;
  351. for (var k = 0; k < end; ++k) {
  352. var index = k * strideSize;
  353. var vector = [];
  354. if (vertexBufferKind === BABYLON.VertexBuffer.PositionKind || vertexBufferKind === BABYLON.VertexBuffer.NormalKind) {
  355. var vertexData = BABYLON.Vector3.FromArray(meshAttributeArray, index);
  356. vector = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(vertexData).asArray() : vertexData.asArray();
  357. }
  358. else if (vertexBufferKind === BABYLON.VertexBuffer.TangentKind || vertexBufferKind === BABYLON.VertexBuffer.ColorKind) {
  359. var vertexData = BABYLON.Vector4.FromArray(meshAttributeArray, index);
  360. vector = (this.convertToRightHandedSystem && !(vertexBufferKind === BABYLON.VertexBuffer.ColorKind)) ? _Exporter.GetRightHandedVector4(vertexData).asArray() : vertexData.asArray();
  361. }
  362. else if (vertexBufferKind === BABYLON.VertexBuffer.UVKind || vertexBufferKind === BABYLON.VertexBuffer.UV2Kind) {
  363. vector = [meshAttributeArray[index], meshAttributeArray[index + 1]];
  364. }
  365. else {
  366. BABYLON.Tools.Warn("Unsupported Vertex Buffer Type: " + vertexBufferKind);
  367. }
  368. for (var i = 0; i < vector.length; ++i) {
  369. dataBuffer.setFloat32(byteOff, vector[i], true);
  370. byteOff += 4;
  371. }
  372. }
  373. byteLength = meshAttributeArray.length * 4;
  374. return byteLength;
  375. };
  376. /**
  377. * Generates glTF json data
  378. * @param shouldUseGlb - Indicates whether the json should be written for a glb file.
  379. * @param glTFPrefix - Text to use when prefixing a glTF file.
  380. * @param prettyPrint - Indicates whether the json file should be pretty printed (true) or not (false).
  381. * @returns - json data as string
  382. */
  383. _Exporter.prototype.generateJSON = function (shouldUseGlb, glTFPrefix, prettyPrint) {
  384. var buffer = { byteLength: this.totalByteLength };
  385. var glTF = {
  386. asset: this.asset
  387. };
  388. if (buffer.byteLength) {
  389. glTF.buffers = [buffer];
  390. }
  391. if (this.nodes && this.nodes.length) {
  392. glTF.nodes = this.nodes;
  393. }
  394. if (this.meshes && this.meshes.length) {
  395. glTF.meshes = this.meshes;
  396. }
  397. if (this.scenes && this.scenes.length) {
  398. glTF.scenes = this.scenes;
  399. glTF.scene = 0;
  400. }
  401. if (this.bufferViews && this.bufferViews.length) {
  402. glTF.bufferViews = this.bufferViews;
  403. }
  404. if (this.accessors && this.accessors.length) {
  405. glTF.accessors = this.accessors;
  406. }
  407. if (this.materials && this.materials.length) {
  408. glTF.materials = this.materials;
  409. }
  410. if (this.textures && this.textures.length) {
  411. glTF.textures = this.textures;
  412. }
  413. if (this.images && this.images.length) {
  414. if (!shouldUseGlb) {
  415. glTF.images = this.images;
  416. }
  417. else {
  418. glTF.images = [];
  419. // Replace uri with bufferview and mime type for glb
  420. var imageLength = this.images.length;
  421. var byteOffset = this.totalByteLength;
  422. for (var i = 0; i < imageLength; ++i) {
  423. var image = this.images[i];
  424. if (image.uri) {
  425. var imageData = this.imageData[image.uri];
  426. var imageName = image.uri.split('.')[0] + " image";
  427. var bufferView = this.createBufferView(0, byteOffset, imageData.data.length, undefined, imageName);
  428. byteOffset += imageData.data.buffer.byteLength;
  429. this.bufferViews.push(bufferView);
  430. image.bufferView = this.bufferViews.length - 1;
  431. image.name = imageName;
  432. image.mimeType = imageData.mimeType;
  433. image.uri = undefined;
  434. glTF.images.push(image);
  435. }
  436. }
  437. buffer.byteLength = byteOffset;
  438. }
  439. }
  440. if (!shouldUseGlb) {
  441. buffer.uri = glTFPrefix + ".bin";
  442. }
  443. var jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
  444. return jsonText;
  445. };
  446. /**
  447. * Generates data for .gltf and .bin files based on the glTF prefix string
  448. * @param glTFPrefix - Text to use when prefixing a glTF file.
  449. * @returns - GLTFData with glTF file data.
  450. */
  451. _Exporter.prototype._generateGLTF = function (glTFPrefix) {
  452. var binaryBuffer = this.generateBinary();
  453. var jsonText = this.generateJSON(false, glTFPrefix, true);
  454. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  455. var glTFFileName = glTFPrefix + '.gltf';
  456. var glTFBinFile = glTFPrefix + '.bin';
  457. var container = new BABYLON._GLTFData();
  458. container.glTFFiles[glTFFileName] = jsonText;
  459. container.glTFFiles[glTFBinFile] = bin;
  460. if (this.imageData) {
  461. for (var image in this.imageData) {
  462. container.glTFFiles[image] = new Blob([this.imageData[image].data], { type: this.imageData[image].mimeType });
  463. }
  464. }
  465. return container;
  466. };
  467. /**
  468. * Creates a binary buffer for glTF
  469. * @returns - array buffer for binary data
  470. */
  471. _Exporter.prototype.generateBinary = function () {
  472. var byteOffset = 0;
  473. byteOffset = this.createScene(this.babylonScene, byteOffset);
  474. return this.binaryBuffer;
  475. };
  476. /**
  477. * Pads the number to a multiple of 4
  478. * @param num - number to pad
  479. * @returns - padded number
  480. */
  481. _Exporter.prototype._getPadding = function (num) {
  482. var remainder = num % 4;
  483. var padding = remainder === 0 ? remainder : 4 - remainder;
  484. return padding;
  485. };
  486. /**
  487. * Generates a glb file from the json and binary data.
  488. * Returns an object with the glb file name as the key and data as the value.
  489. * @param glTFPrefix
  490. * @returns - object with glb filename as key and data as value
  491. */
  492. _Exporter.prototype._generateGLB = function (glTFPrefix) {
  493. var binaryBuffer = this.generateBinary();
  494. var jsonText = this.generateJSON(true);
  495. var glbFileName = glTFPrefix + '.glb';
  496. var headerLength = 12;
  497. var chunkLengthPrefix = 8;
  498. var jsonLength = jsonText.length;
  499. var imageByteLength = 0;
  500. for (var key in this.imageData) {
  501. imageByteLength += this.imageData[key].data.byteLength;
  502. }
  503. var jsonPadding = this._getPadding(jsonLength);
  504. var binPadding = this._getPadding(binaryBuffer.byteLength);
  505. var imagePadding = this._getPadding(imageByteLength);
  506. var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding + imageByteLength + imagePadding;
  507. //header
  508. var headerBuffer = new ArrayBuffer(headerLength);
  509. var headerBufferView = new DataView(headerBuffer);
  510. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  511. headerBufferView.setUint32(4, 2, true); // version
  512. headerBufferView.setUint32(8, byteLength, true); // total bytes in file
  513. //json chunk
  514. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  515. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  516. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  517. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  518. //json chunk bytes
  519. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  520. for (var i = 0; i < jsonLength; ++i) {
  521. jsonData[i] = jsonText.charCodeAt(i);
  522. }
  523. //json padding
  524. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  525. for (var i = 0; i < jsonPadding; ++i) {
  526. jsonPaddingView[i] = 0x20;
  527. }
  528. //binary chunk
  529. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  530. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  531. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength + imageByteLength + imagePadding, true);
  532. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  533. // binary padding
  534. var binPaddingBuffer = new ArrayBuffer(binPadding);
  535. var binPaddingView = new Uint8Array(binPaddingBuffer);
  536. for (var i = 0; i < binPadding; ++i) {
  537. binPaddingView[i] = 0;
  538. }
  539. var imagePaddingBuffer = new ArrayBuffer(imagePadding);
  540. var imagePaddingView = new Uint8Array(imagePaddingBuffer);
  541. for (var i = 0; i < imagePadding; ++i) {
  542. imagePaddingView[i] = 0;
  543. }
  544. var glbData = [headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer];
  545. // binary data
  546. for (var key in this.imageData) {
  547. glbData.push(this.imageData[key].data.buffer);
  548. }
  549. glbData.push(binPaddingBuffer);
  550. glbData.push(imagePaddingBuffer);
  551. var glbFile = new Blob(glbData, { type: 'application/octet-stream' });
  552. var container = new BABYLON._GLTFData();
  553. container.glTFFiles[glbFileName] = glbFile;
  554. return container;
  555. };
  556. /**
  557. * Sets the TRS for each node
  558. * @param node - glTF Node for storing the transformation data.
  559. * @param babylonMesh - Babylon mesh used as the source for the transformation data.
  560. */
  561. _Exporter.prototype.setNodeTransformation = function (node, babylonMesh) {
  562. if (!babylonMesh.position.equalsToFloats(0, 0, 0)) {
  563. node.translation = this.convertToRightHandedSystem ? _Exporter.GetRightHandedVector3(babylonMesh.position).asArray() : babylonMesh.position.asArray();
  564. }
  565. if (!babylonMesh.scaling.equalsToFloats(1, 1, 1)) {
  566. node.scale = babylonMesh.scaling.asArray();
  567. }
  568. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  569. if (babylonMesh.rotationQuaternion) {
  570. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  571. }
  572. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  573. node.rotation = this.convertToRightHandedSystem ? _Exporter.GetRightHandedQuaternion(rotationQuaternion).asArray() : rotationQuaternion.asArray();
  574. }
  575. };
  576. /**
  577. * Creates a bufferview based on the vertices type for the Babylon mesh
  578. * @param kind - Indicates the type of vertices data.
  579. * @param babylonMesh - The Babylon mesh to get the vertices data from.
  580. * @param byteOffset - The offset from the buffer to start indexing from.
  581. * @param dataBuffer - The buffer to write the bufferview data to.
  582. * @returns bytelength of the bufferview data.
  583. */
  584. _Exporter.prototype.createBufferViewKind = function (kind, babylonMesh, byteOffset, dataBuffer) {
  585. var bufferMesh = null;
  586. var byteLength = 0;
  587. if (babylonMesh instanceof BABYLON.Mesh) {
  588. bufferMesh = babylonMesh;
  589. }
  590. else if (babylonMesh instanceof BABYLON.InstancedMesh) {
  591. bufferMesh = babylonMesh.sourceMesh;
  592. }
  593. if (bufferMesh) {
  594. var vertexBuffer = null;
  595. var vertexBufferOffset = null;
  596. var vertexData = null;
  597. var vertexStrideSize = null;
  598. if (bufferMesh.isVerticesDataPresent(kind)) {
  599. vertexBuffer = bufferMesh.getVertexBuffer(kind);
  600. if (vertexBuffer) {
  601. vertexBufferOffset = vertexBuffer.getOffset();
  602. vertexData = vertexBuffer.getData();
  603. if (vertexData) {
  604. vertexStrideSize = vertexBuffer.getStrideSize();
  605. if (dataBuffer && vertexData) {
  606. byteLength = this.writeAttributeData(kind, vertexData, vertexStrideSize, vertexBufferOffset, byteOffset, dataBuffer);
  607. byteOffset += byteLength;
  608. }
  609. else {
  610. byteLength = vertexData.length * 4;
  611. var bufferView = this.createBufferView(0, byteOffset, byteLength, vertexStrideSize * 4, kind + " - " + bufferMesh.name);
  612. byteOffset += byteLength;
  613. this.bufferViews.push(bufferView);
  614. }
  615. }
  616. }
  617. }
  618. }
  619. return byteLength;
  620. };
  621. /**
  622. * Sets data for the primitive attributes of each submesh
  623. * @param mesh - glTF Mesh object to store the primitive attribute information.
  624. * @param babylonMesh - Babylon mesh to get the primitive attribute data from.
  625. * @param byteOffset - The offset in bytes of the buffer data.
  626. * @param dataBuffer - Buffer to write the attribute data to.
  627. * @returns - bytelength of the primitive attributes plus the passed in byteOffset.
  628. */
  629. _Exporter.prototype.setPrimitiveAttributes = function (mesh, babylonMesh, byteOffset, dataBuffer) {
  630. var bufferMesh = null;
  631. if (babylonMesh instanceof BABYLON.Mesh) {
  632. bufferMesh = babylonMesh;
  633. }
  634. else if (babylonMesh instanceof BABYLON.InstancedMesh) {
  635. bufferMesh = babylonMesh.sourceMesh;
  636. }
  637. var attributeData = [
  638. { kind: BABYLON.VertexBuffer.PositionKind, accessorType: "VEC3" /* VEC3 */ },
  639. { kind: BABYLON.VertexBuffer.NormalKind, accessorType: "VEC3" /* VEC3 */ },
  640. { kind: BABYLON.VertexBuffer.ColorKind, accessorType: "VEC4" /* VEC4 */ },
  641. { kind: BABYLON.VertexBuffer.TangentKind, accessorType: "VEC4" /* VEC4 */ },
  642. { kind: BABYLON.VertexBuffer.UVKind, accessorType: "VEC2" /* VEC2 */ },
  643. { kind: BABYLON.VertexBuffer.UV2Kind, accessorType: "VEC2" /* VEC2 */ },
  644. ];
  645. var indexBufferViewIndex = null;
  646. if (bufferMesh) {
  647. // For each BabylonMesh, create bufferviews for each 'kind'
  648. for (var _i = 0, attributeData_1 = attributeData; _i < attributeData_1.length; _i++) {
  649. var attribute = attributeData_1[_i];
  650. var attributeKind = attribute.kind;
  651. if (bufferMesh.isVerticesDataPresent(attributeKind)) {
  652. byteOffset += this.createBufferViewKind(attributeKind, babylonMesh, byteOffset, dataBuffer);
  653. attribute.bufferViewIndex = this.bufferViews.length - 1;
  654. }
  655. }
  656. if (bufferMesh.getTotalIndices()) {
  657. var indices = bufferMesh.getIndices();
  658. if (indices) {
  659. if (dataBuffer) {
  660. var end = indices.length;
  661. var byteOff = byteOffset;
  662. for (var k = 0; k < end; ++k) {
  663. dataBuffer.setUint32(byteOff, indices[k], true);
  664. byteOff += 4;
  665. }
  666. byteOffset = byteOff;
  667. }
  668. else {
  669. var byteLength = indices.length * 4;
  670. var bufferView = this.createBufferView(0, byteOffset, byteLength, undefined, "Indices - " + bufferMesh.name);
  671. byteOffset += byteLength;
  672. this.bufferViews.push(bufferView);
  673. indexBufferViewIndex = this.bufferViews.length - 1;
  674. }
  675. }
  676. }
  677. if (babylonMesh.subMeshes) {
  678. var uvCoordsPresent = false;
  679. // go through all mesh primitives (submeshes)
  680. for (var _a = 0, _b = babylonMesh.subMeshes; _a < _b.length; _a++) {
  681. var submesh = _b[_a];
  682. var meshPrimitive = { attributes: {} };
  683. // Create a bufferview storing all the positions
  684. if (!dataBuffer) {
  685. for (var _c = 0, attributeData_2 = attributeData; _c < attributeData_2.length; _c++) {
  686. var attribute = attributeData_2[_c];
  687. var attributeKind = attribute.kind;
  688. if (bufferMesh.isVerticesDataPresent(attributeKind)) {
  689. var vertexBuffer = bufferMesh.getVertexBuffer(attributeKind);
  690. if (vertexBuffer) {
  691. var bufferData = vertexBuffer.getData();
  692. if (bufferData) {
  693. var strideSize = vertexBuffer.getStrideSize();
  694. var minMax = void 0;
  695. var min = null;
  696. var max = null;
  697. var bufferViewIndex = attribute.bufferViewIndex;
  698. if (bufferViewIndex != undefined) {
  699. if (attributeKind == BABYLON.VertexBuffer.PositionKind) {
  700. minMax = this.calculateMinMaxPositions(bufferData, 0, bufferData.length / strideSize);
  701. min = minMax.min;
  702. max = minMax.max;
  703. }
  704. var accessor = this.createAccessor(bufferViewIndex, attributeKind + " - " + babylonMesh.name, attribute.accessorType, 5126 /* FLOAT */, bufferData.length / strideSize, 0, min, max);
  705. this.accessors.push(accessor);
  706. switch (attributeKind) {
  707. case BABYLON.VertexBuffer.PositionKind: {
  708. meshPrimitive.attributes.POSITION = this.accessors.length - 1;
  709. break;
  710. }
  711. case BABYLON.VertexBuffer.NormalKind: {
  712. meshPrimitive.attributes.NORMAL = this.accessors.length - 1;
  713. break;
  714. }
  715. case BABYLON.VertexBuffer.ColorKind: {
  716. meshPrimitive.attributes.COLOR_0 = this.accessors.length - 1;
  717. break;
  718. }
  719. case BABYLON.VertexBuffer.TangentKind: {
  720. meshPrimitive.attributes.TANGENT = this.accessors.length - 1;
  721. break;
  722. }
  723. case BABYLON.VertexBuffer.UVKind: {
  724. meshPrimitive.attributes.TEXCOORD_0 = this.accessors.length - 1;
  725. uvCoordsPresent = true;
  726. break;
  727. }
  728. case BABYLON.VertexBuffer.UV2Kind: {
  729. meshPrimitive.attributes.TEXCOORD_1 = this.accessors.length - 1;
  730. uvCoordsPresent = true;
  731. break;
  732. }
  733. default: {
  734. BABYLON.Tools.Warn("Unsupported Vertex Buffer Type: " + attributeKind);
  735. }
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. if (indexBufferViewIndex) {
  743. // Create accessor
  744. var accessor = this.createAccessor(indexBufferViewIndex, "indices - " + babylonMesh.name, "SCALAR" /* SCALAR */, 5125 /* UNSIGNED_INT */, submesh.indexCount, submesh.indexStart * 4, null, null);
  745. this.accessors.push(accessor);
  746. meshPrimitive.indices = this.accessors.length - 1;
  747. }
  748. }
  749. if (bufferMesh.material) {
  750. var materialIndex = null;
  751. if (bufferMesh.material instanceof BABYLON.StandardMaterial || bufferMesh.material instanceof BABYLON.PBRMetallicRoughnessMaterial || bufferMesh.material instanceof BABYLON.PBRMaterial) {
  752. materialIndex = babylonMesh.getScene().materials.indexOf(bufferMesh.material);
  753. }
  754. else if (bufferMesh.material instanceof BABYLON.MultiMaterial) {
  755. var babylonMultiMaterial = bufferMesh.material;
  756. var material = babylonMultiMaterial.subMaterials[submesh.materialIndex];
  757. if (material) {
  758. materialIndex = babylonMesh.getScene().materials.indexOf(material);
  759. }
  760. }
  761. else {
  762. BABYLON.Tools.Warn("Material type " + bufferMesh.material.getClassName() + " for material " + bufferMesh.material.name + " is not yet implemented in glTF serializer.");
  763. }
  764. if (materialIndex != null && Object.keys(meshPrimitive.attributes).length > 0) {
  765. if (uvCoordsPresent || !GLTF2._GLTFMaterial._HasTexturesPresent(this.materials[materialIndex])) {
  766. meshPrimitive.material = materialIndex;
  767. }
  768. else {
  769. // If no texture coordinate information is present, make a copy of the material without the textures to be glTF compliant.
  770. var newMat = GLTF2._GLTFMaterial._StripTexturesFromMaterial(this.materials[materialIndex]);
  771. this.materials.push(newMat);
  772. meshPrimitive.material = this.materials.length - 1;
  773. }
  774. }
  775. }
  776. mesh.primitives.push(meshPrimitive);
  777. }
  778. }
  779. }
  780. return byteOffset;
  781. };
  782. /**
  783. * Creates a glTF scene based on the array of meshes.
  784. * Returns the the total byte offset.
  785. * @param babylonScene - Babylon scene to get the mesh data from.
  786. * @param byteOffset - Offset to start from in bytes.
  787. * @returns bytelength + byteoffset
  788. */
  789. _Exporter.prototype.createScene = function (babylonScene, byteOffset) {
  790. if (babylonScene.meshes.length) {
  791. var babylonMeshes = babylonScene.meshes;
  792. var scene = { nodes: new Array() };
  793. GLTF2._GLTFMaterial._ConvertMaterialsToGLTF(babylonScene.materials, "image/png" /* PNG */, this.images, this.textures, this.materials, this.imageData, true);
  794. var result = this.createNodeMap(babylonScene, byteOffset);
  795. this.nodeMap = result.nodeMap;
  796. this.totalByteLength = result.byteOffset;
  797. this.binaryBuffer = new ArrayBuffer(this.totalByteLength);
  798. var dataBuffer = new DataView(this.binaryBuffer);
  799. for (var i = 0; i < babylonMeshes.length; ++i) {
  800. var babylonMesh = babylonMeshes[i];
  801. // Build Hierarchy with the node map.
  802. var glTFNodeIndex = this.nodeMap[babylonMesh.uniqueId];
  803. var glTFNode = this.nodes[glTFNodeIndex];
  804. if (!babylonMesh.parent) {
  805. if (this.options &&
  806. this.options.shouldExportMesh != undefined &&
  807. !this.options.shouldExportMesh(babylonMesh)) {
  808. BABYLON.Tools.Log("Omitting " + babylonMesh.name + " from scene.");
  809. }
  810. else {
  811. scene.nodes.push(glTFNodeIndex);
  812. }
  813. }
  814. var directDescendents = babylonMesh.getDescendants(true);
  815. if (!glTFNode.children && directDescendents && directDescendents.length) {
  816. glTFNode.children = [];
  817. for (var _i = 0, directDescendents_1 = directDescendents; _i < directDescendents_1.length; _i++) {
  818. var descendent = directDescendents_1[_i];
  819. glTFNode.children.push(this.nodeMap[descendent.uniqueId]);
  820. }
  821. }
  822. var mesh = { primitives: new Array() };
  823. byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, dataBuffer);
  824. }
  825. this.scenes.push(scene);
  826. }
  827. return byteOffset;
  828. };
  829. /**
  830. * Creates a mapping of Node unique id to node index
  831. * @param scene - Babylon Scene.
  832. * @param byteOffset - The initial byte offset.
  833. * @returns - Node mapping of unique id to index.
  834. */
  835. _Exporter.prototype.createNodeMap = function (scene, byteOffset) {
  836. var nodeMap = {};
  837. for (var _i = 0, _a = scene.meshes; _i < _a.length; _i++) {
  838. var babylonMesh = _a[_i];
  839. var result = this.createNode(babylonMesh, byteOffset, null);
  840. this.nodes.push(result.node);
  841. nodeMap[babylonMesh.uniqueId] = this.nodes.length - 1;
  842. byteOffset = result.byteOffset;
  843. }
  844. return { nodeMap: nodeMap, byteOffset: byteOffset };
  845. };
  846. /**
  847. * Creates a glTF node from a Babylon mesh.
  848. * @param babylonMesh - Source Babylon mesh.
  849. * @param byteOffset - The initial byte offset.
  850. * @param dataBuffer - Buffer for storing geometry data.
  851. * @returns - Object containing an INode and byteoffset.
  852. */
  853. _Exporter.prototype.createNode = function (babylonMesh, byteOffset, dataBuffer) {
  854. // create node to hold translation/rotation/scale and the mesh
  855. var node = {};
  856. if (babylonMesh.name) {
  857. node.name = babylonMesh.name;
  858. }
  859. // Set transformation
  860. this.setNodeTransformation(node, babylonMesh);
  861. // create mesh
  862. var mesh = { primitives: new Array() };
  863. mesh.primitives = [];
  864. byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, dataBuffer);
  865. if (mesh.primitives.length) {
  866. this.meshes.push(mesh);
  867. node.mesh = this.meshes.length - 1;
  868. }
  869. return { node: node, byteOffset: byteOffset };
  870. };
  871. return _Exporter;
  872. }());
  873. GLTF2._Exporter = _Exporter;
  874. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  875. })(BABYLON || (BABYLON = {}));
  876. //# sourceMappingURL=babylon.glTFExporter.js.map
  877. "use strict";
  878. var BABYLON;
  879. (function (BABYLON) {
  880. /**
  881. * Class for holding and downloading glTF file data
  882. */
  883. var _GLTFData = /** @class */ (function () {
  884. /**
  885. * Initializes the glTF file object.
  886. */
  887. function _GLTFData() {
  888. this.glTFFiles = {};
  889. }
  890. /**
  891. * Downloads the glTF data as files based on their names and data.
  892. */
  893. _GLTFData.prototype.downloadFiles = function () {
  894. /**
  895. * Checks for a matching suffix at the end of a string (for ES5 and lower).
  896. * @param str - Source string.
  897. * @param suffix - Suffix to search for in the source string.
  898. * @returns - Boolean indicating whether the suffix was found (true) or not (false).
  899. */
  900. function endsWith(str, suffix) {
  901. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  902. }
  903. for (var key in this.glTFFiles) {
  904. var link = document.createElement('a');
  905. document.body.appendChild(link);
  906. link.setAttribute("type", "hidden");
  907. link.download = key;
  908. var blob = this.glTFFiles[key];
  909. var mimeType = void 0;
  910. if (endsWith(key, ".glb")) {
  911. mimeType = { type: "model/gltf-binary" };
  912. }
  913. else if (endsWith(key, ".bin")) {
  914. mimeType = { type: "application/octet-stream" };
  915. }
  916. else if (endsWith(key, ".gltf")) {
  917. mimeType = { type: "model/gltf+json" };
  918. }
  919. else if (endsWith(key, ".jpeg" || ".jpg")) {
  920. mimeType = { type: "image/jpeg" /* JPEG */ };
  921. }
  922. else if (endsWith(key, ".png")) {
  923. mimeType = { type: "image/png" /* PNG */ };
  924. }
  925. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  926. link.click();
  927. }
  928. };
  929. return _GLTFData;
  930. }());
  931. BABYLON._GLTFData = _GLTFData;
  932. })(BABYLON || (BABYLON = {}));
  933. //# sourceMappingURL=babylon.glTFData.js.map
  934. "use strict";
  935. var BABYLON;
  936. (function (BABYLON) {
  937. var GLTF2;
  938. (function (GLTF2) {
  939. /**
  940. * Utility methods for working with glTF material conversion properties. This class should only be used internally.
  941. */
  942. var _GLTFMaterial = /** @class */ (function () {
  943. function _GLTFMaterial() {
  944. }
  945. /**
  946. * Specifies if two colors are approximately equal in value.
  947. * @param color1 - first color to compare to.
  948. * @param color2 - second color to compare to.
  949. * @param epsilon - threshold value
  950. */
  951. _GLTFMaterial.FuzzyEquals = function (color1, color2, epsilon) {
  952. return BABYLON.Scalar.WithinEpsilon(color1.r, color2.r, epsilon) &&
  953. BABYLON.Scalar.WithinEpsilon(color1.g, color2.g, epsilon) &&
  954. BABYLON.Scalar.WithinEpsilon(color1.b, color2.b, epsilon);
  955. };
  956. /**
  957. * Gets the materials from a Babylon scene and converts them to glTF materials.
  958. * @param scene - babylonjs scene.
  959. * @param mimeType - texture mime type.
  960. * @param images - array of images.
  961. * @param textures - array of textures.
  962. * @param materials - array of materials.
  963. * @param imageData - mapping of texture names to base64 textures
  964. * @param hasTextureCoords - specifies if texture coordinates are present on the material.
  965. */
  966. _GLTFMaterial._ConvertMaterialsToGLTF = function (babylonMaterials, mimeType, images, textures, materials, imageData, hasTextureCoords) {
  967. for (var i = 0; i < babylonMaterials.length; ++i) {
  968. var babylonMaterial = babylonMaterials[i];
  969. if (babylonMaterial instanceof BABYLON.StandardMaterial) {
  970. _GLTFMaterial._ConvertStandardMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
  971. }
  972. else if (babylonMaterial instanceof BABYLON.PBRMetallicRoughnessMaterial) {
  973. _GLTFMaterial._ConvertPBRMetallicRoughnessMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
  974. }
  975. else if (babylonMaterial instanceof BABYLON.PBRMaterial) {
  976. _GLTFMaterial._ConvertPBRMaterial(babylonMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords);
  977. }
  978. else {
  979. throw new Error("Unsupported material type: " + babylonMaterial.name);
  980. }
  981. }
  982. };
  983. /**
  984. * Makes a copy of the glTF material without the texture parameters.
  985. * @param originalMaterial - original glTF material.
  986. * @returns glTF material without texture parameters
  987. */
  988. _GLTFMaterial._StripTexturesFromMaterial = function (originalMaterial) {
  989. var newMaterial = {};
  990. if (originalMaterial) {
  991. newMaterial.name = originalMaterial.name;
  992. newMaterial.doubleSided = originalMaterial.doubleSided;
  993. newMaterial.alphaMode = originalMaterial.alphaMode;
  994. newMaterial.alphaCutoff = originalMaterial.alphaCutoff;
  995. newMaterial.emissiveFactor = originalMaterial.emissiveFactor;
  996. var originalPBRMetallicRoughness = originalMaterial.pbrMetallicRoughness;
  997. if (originalPBRMetallicRoughness) {
  998. newMaterial.pbrMetallicRoughness = {};
  999. newMaterial.pbrMetallicRoughness.baseColorFactor = originalPBRMetallicRoughness.baseColorFactor;
  1000. newMaterial.pbrMetallicRoughness.metallicFactor = originalPBRMetallicRoughness.metallicFactor;
  1001. newMaterial.pbrMetallicRoughness.roughnessFactor = originalPBRMetallicRoughness.roughnessFactor;
  1002. }
  1003. }
  1004. return newMaterial;
  1005. };
  1006. /**
  1007. * Specifies if the material has any texture parameters present.
  1008. * @param material - glTF Material.
  1009. * @returns boolean specifying if texture parameters are present
  1010. */
  1011. _GLTFMaterial._HasTexturesPresent = function (material) {
  1012. if (material.emissiveTexture || material.normalTexture || material.occlusionTexture) {
  1013. return true;
  1014. }
  1015. var pbrMat = material.pbrMetallicRoughness;
  1016. if (pbrMat) {
  1017. if (pbrMat.baseColorTexture || pbrMat.metallicRoughnessTexture) {
  1018. return true;
  1019. }
  1020. }
  1021. return false;
  1022. };
  1023. /**
  1024. * Converts a Babylon StandardMaterial to a glTF Metallic Roughness Material.
  1025. * @param babylonStandardMaterial
  1026. * @returns - glTF Metallic Roughness Material representation
  1027. */
  1028. _GLTFMaterial._ConvertToGLTFPBRMetallicRoughness = function (babylonStandardMaterial) {
  1029. var P0 = new BABYLON.Vector2(0, 1);
  1030. var P1 = new BABYLON.Vector2(0, 0.1);
  1031. var P2 = new BABYLON.Vector2(0, 0.1);
  1032. var P3 = new BABYLON.Vector2(1300, 0.1);
  1033. /**
  1034. * Given the control points, solve for x based on a given t for a cubic bezier curve.
  1035. * @param t - a value between 0 and 1.
  1036. * @param p0 - first control point.
  1037. * @param p1 - second control point.
  1038. * @param p2 - third control point.
  1039. * @param p3 - fourth control point.
  1040. * @returns - number result of cubic bezier curve at the specified t.
  1041. */
  1042. function _cubicBezierCurve(t, p0, p1, p2, p3) {
  1043. return ((1 - t) * (1 - t) * (1 - t) * p0 +
  1044. 3 * (1 - t) * (1 - t) * t * p1 +
  1045. 3 * (1 - t) * t * t * p2 +
  1046. t * t * t * p3);
  1047. }
  1048. /**
  1049. * Evaluates a specified specular power value to determine the appropriate roughness value,
  1050. * based on a pre-defined cubic bezier curve with specular on the abscissa axis (x-axis)
  1051. * and roughness on the ordinant axis (y-axis).
  1052. * @param specularPower - specular power of standard material.
  1053. * @returns - Number representing the roughness value.
  1054. */
  1055. function _solveForRoughness(specularPower) {
  1056. var t = Math.pow(specularPower / P3.x, 0.333333);
  1057. return _cubicBezierCurve(t, P0.y, P1.y, P2.y, P3.y);
  1058. }
  1059. var diffuse = babylonStandardMaterial.diffuseColor.toLinearSpace().scale(0.5);
  1060. var opacity = babylonStandardMaterial.alpha;
  1061. var specularPower = BABYLON.Scalar.Clamp(babylonStandardMaterial.specularPower, 0, this._maxSpecularPower);
  1062. var roughness = _solveForRoughness(specularPower);
  1063. var glTFPbrMetallicRoughness = {
  1064. baseColorFactor: [
  1065. diffuse.r,
  1066. diffuse.g,
  1067. diffuse.b,
  1068. opacity
  1069. ],
  1070. metallicFactor: 0,
  1071. roughnessFactor: roughness,
  1072. };
  1073. return glTFPbrMetallicRoughness;
  1074. };
  1075. /**
  1076. * Computes the metallic factor
  1077. * @param diffuse - diffused value
  1078. * @param specular - specular value
  1079. * @param oneMinusSpecularStrength - one minus the specular strength
  1080. * @returns - metallic value
  1081. */
  1082. _GLTFMaterial._SolveMetallic = function (diffuse, specular, oneMinusSpecularStrength) {
  1083. if (specular < _GLTFMaterial._dielectricSpecular.r) {
  1084. _GLTFMaterial._dielectricSpecular;
  1085. return 0;
  1086. }
  1087. var a = _GLTFMaterial._dielectricSpecular.r;
  1088. var b = diffuse * oneMinusSpecularStrength / (1.0 - _GLTFMaterial._dielectricSpecular.r) + specular - 2.0 * _GLTFMaterial._dielectricSpecular.r;
  1089. var c = _GLTFMaterial._dielectricSpecular.r - specular;
  1090. var D = b * b - 4.0 * a * c;
  1091. return BABYLON.Scalar.Clamp((-b + Math.sqrt(D)) / (2.0 * a), 0, 1);
  1092. };
  1093. /**
  1094. * Gets the glTF alpha mode from the Babylon Material
  1095. * @param babylonMaterial - Babylon Material
  1096. * @returns - The Babylon alpha mode value
  1097. */
  1098. _GLTFMaterial._GetAlphaMode = function (babylonMaterial) {
  1099. if (babylonMaterial instanceof BABYLON.StandardMaterial) {
  1100. var babylonStandardMaterial = babylonMaterial;
  1101. if ((babylonStandardMaterial.alpha != 1.0) ||
  1102. (babylonStandardMaterial.diffuseTexture != null && babylonStandardMaterial.diffuseTexture.hasAlpha) ||
  1103. (babylonStandardMaterial.opacityTexture != null)) {
  1104. return "BLEND" /* BLEND */;
  1105. }
  1106. else {
  1107. return "OPAQUE" /* OPAQUE */;
  1108. }
  1109. }
  1110. else if (babylonMaterial instanceof BABYLON.PBRMetallicRoughnessMaterial) {
  1111. var babylonPBRMetallicRoughness = babylonMaterial;
  1112. switch (babylonPBRMetallicRoughness.transparencyMode) {
  1113. case BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE: {
  1114. return "OPAQUE" /* OPAQUE */;
  1115. }
  1116. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND: {
  1117. return "BLEND" /* BLEND */;
  1118. }
  1119. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST: {
  1120. return "MASK" /* MASK */;
  1121. }
  1122. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND: {
  1123. BABYLON.Tools.Warn(babylonMaterial.name + ": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead.");
  1124. return "BLEND" /* BLEND */;
  1125. }
  1126. default: {
  1127. throw new Error("Unsupported alpha mode " + babylonPBRMetallicRoughness.transparencyMode);
  1128. }
  1129. }
  1130. }
  1131. else if (babylonMaterial instanceof BABYLON.PBRMaterial) {
  1132. var babylonPBRMaterial = babylonMaterial;
  1133. switch (babylonPBRMaterial.transparencyMode) {
  1134. case BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE: {
  1135. return "OPAQUE" /* OPAQUE */;
  1136. }
  1137. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND: {
  1138. return "BLEND" /* BLEND */;
  1139. }
  1140. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST: {
  1141. return "MASK" /* MASK */;
  1142. }
  1143. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND: {
  1144. BABYLON.Tools.Warn(babylonMaterial.name + ": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead.");
  1145. return "BLEND" /* BLEND */;
  1146. }
  1147. default: {
  1148. throw new Error("Unsupported alpha mode " + babylonPBRMaterial.transparencyMode);
  1149. }
  1150. }
  1151. }
  1152. else {
  1153. throw new Error("Unsupported Babylon material type");
  1154. }
  1155. };
  1156. /**
  1157. * Converts a Babylon Standard Material to a glTF Material.
  1158. * @param babylonStandardMaterial - BJS Standard Material.
  1159. * @param mimeType - mime type to use for the textures.
  1160. * @param images - array of glTF image interfaces.
  1161. * @param textures - array of glTF texture interfaces.
  1162. * @param materials - array of glTF material interfaces.
  1163. * @param imageData - map of image file name to data.
  1164. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  1165. */
  1166. _GLTFMaterial._ConvertStandardMaterial = function (babylonStandardMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
  1167. BABYLON.Tools.Warn(babylonStandardMaterial.name + ": Standard Material is currently not fully supported/implemented in glTF serializer");
  1168. var glTFPbrMetallicRoughness = _GLTFMaterial._ConvertToGLTFPBRMetallicRoughness(babylonStandardMaterial);
  1169. var glTFMaterial = { name: babylonStandardMaterial.name };
  1170. if (babylonStandardMaterial.backFaceCulling != null && !babylonStandardMaterial.backFaceCulling) {
  1171. if (!babylonStandardMaterial.twoSidedLighting) {
  1172. BABYLON.Tools.Warn(babylonStandardMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
  1173. }
  1174. glTFMaterial.doubleSided = true;
  1175. }
  1176. if (hasTextureCoords) {
  1177. if (babylonStandardMaterial.diffuseTexture) {
  1178. var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.diffuseTexture, mimeType, images, textures, imageData);
  1179. if (glTFTexture != null) {
  1180. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  1181. }
  1182. }
  1183. if (babylonStandardMaterial.bumpTexture) {
  1184. var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.bumpTexture, mimeType, images, textures, imageData);
  1185. if (glTFTexture) {
  1186. glTFMaterial.normalTexture = glTFTexture;
  1187. }
  1188. }
  1189. if (babylonStandardMaterial.emissiveTexture) {
  1190. var glTFEmissiveTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.emissiveTexture, mimeType, images, textures, imageData);
  1191. if (glTFEmissiveTexture) {
  1192. glTFMaterial.emissiveTexture = glTFEmissiveTexture;
  1193. }
  1194. glTFMaterial.emissiveFactor = [1.0, 1.0, 1.0];
  1195. }
  1196. if (babylonStandardMaterial.ambientTexture) {
  1197. var glTFTexture = _GLTFMaterial._ExportTexture(babylonStandardMaterial.ambientTexture, mimeType, images, textures, imageData);
  1198. if (glTFTexture) {
  1199. var occlusionTexture = {
  1200. index: glTFTexture.index
  1201. };
  1202. glTFMaterial.occlusionTexture = occlusionTexture;
  1203. occlusionTexture.strength = 1.0;
  1204. }
  1205. }
  1206. }
  1207. if (babylonStandardMaterial.alpha < 1.0 || babylonStandardMaterial.opacityTexture) {
  1208. if (babylonStandardMaterial.alphaMode === BABYLON.Engine.ALPHA_COMBINE) {
  1209. glTFMaterial.alphaMode = "BLEND" /* BLEND */;
  1210. }
  1211. else {
  1212. BABYLON.Tools.Warn(babylonStandardMaterial.name + ": glTF 2.0 does not support alpha mode: " + babylonStandardMaterial.alphaMode.toString());
  1213. }
  1214. }
  1215. if (babylonStandardMaterial.emissiveColor && !this.FuzzyEquals(babylonStandardMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
  1216. glTFMaterial.emissiveFactor = babylonStandardMaterial.emissiveColor.asArray();
  1217. }
  1218. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  1219. materials.push(glTFMaterial);
  1220. };
  1221. /**
  1222. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
  1223. * @param babylonPBRMetalRoughMaterial - BJS PBR Metallic Roughness Material.
  1224. * @param mimeType - mime type to use for the textures.
  1225. * @param images - array of glTF image interfaces.
  1226. * @param textures - array of glTF texture interfaces.
  1227. * @param materials - array of glTF material interfaces.
  1228. * @param imageData - map of image file name to data.
  1229. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  1230. */
  1231. _GLTFMaterial._ConvertPBRMetallicRoughnessMaterial = function (babylonPBRMetalRoughMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
  1232. var glTFPbrMetallicRoughness = {};
  1233. if (babylonPBRMetalRoughMaterial.baseColor) {
  1234. glTFPbrMetallicRoughness.baseColorFactor = [
  1235. babylonPBRMetalRoughMaterial.baseColor.r,
  1236. babylonPBRMetalRoughMaterial.baseColor.g,
  1237. babylonPBRMetalRoughMaterial.baseColor.b,
  1238. babylonPBRMetalRoughMaterial.alpha
  1239. ];
  1240. }
  1241. if (babylonPBRMetalRoughMaterial.metallic != null && babylonPBRMetalRoughMaterial.metallic !== 1) {
  1242. glTFPbrMetallicRoughness.metallicFactor = babylonPBRMetalRoughMaterial.metallic;
  1243. }
  1244. if (babylonPBRMetalRoughMaterial.roughness != null && babylonPBRMetalRoughMaterial.roughness !== 1) {
  1245. glTFPbrMetallicRoughness.roughnessFactor = babylonPBRMetalRoughMaterial.roughness;
  1246. }
  1247. var glTFMaterial = {
  1248. name: babylonPBRMetalRoughMaterial.name
  1249. };
  1250. if (babylonPBRMetalRoughMaterial.doubleSided) {
  1251. glTFMaterial.doubleSided = babylonPBRMetalRoughMaterial.doubleSided;
  1252. }
  1253. if (hasTextureCoords) {
  1254. if (babylonPBRMetalRoughMaterial.baseTexture != null) {
  1255. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.baseTexture, mimeType, images, textures, imageData);
  1256. if (glTFTexture != null) {
  1257. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  1258. }
  1259. }
  1260. if (babylonPBRMetalRoughMaterial.normalTexture) {
  1261. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.normalTexture, mimeType, images, textures, imageData);
  1262. if (glTFTexture) {
  1263. glTFMaterial.normalTexture = glTFTexture;
  1264. }
  1265. }
  1266. if (babylonPBRMetalRoughMaterial.occlusionTexture) {
  1267. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.occlusionTexture, mimeType, images, textures, imageData);
  1268. if (glTFTexture) {
  1269. glTFMaterial.occlusionTexture = glTFTexture;
  1270. if (babylonPBRMetalRoughMaterial.occlusionStrength != null) {
  1271. glTFMaterial.occlusionTexture.strength = babylonPBRMetalRoughMaterial.occlusionStrength;
  1272. }
  1273. }
  1274. }
  1275. if (babylonPBRMetalRoughMaterial.emissiveTexture) {
  1276. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMetalRoughMaterial.emissiveTexture, mimeType, images, textures, imageData);
  1277. if (glTFTexture != null) {
  1278. glTFMaterial.emissiveTexture = glTFTexture;
  1279. }
  1280. }
  1281. }
  1282. if (this.FuzzyEquals(babylonPBRMetalRoughMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
  1283. glTFMaterial.emissiveFactor = babylonPBRMetalRoughMaterial.emissiveColor.asArray();
  1284. }
  1285. if (babylonPBRMetalRoughMaterial.transparencyMode != null) {
  1286. var alphaMode = _GLTFMaterial._GetAlphaMode(babylonPBRMetalRoughMaterial);
  1287. if (alphaMode !== "OPAQUE" /* OPAQUE */) {
  1288. glTFMaterial.alphaMode = alphaMode;
  1289. if (alphaMode === "BLEND" /* BLEND */) {
  1290. glTFMaterial.alphaCutoff = babylonPBRMetalRoughMaterial.alphaCutOff;
  1291. }
  1292. }
  1293. }
  1294. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  1295. materials.push(glTFMaterial);
  1296. };
  1297. /**
  1298. * Converts an image typed array buffer to a base64 image.
  1299. * @param buffer - typed array buffer.
  1300. * @param width - width of the image.
  1301. * @param height - height of the image.
  1302. * @param mimeType - mimetype of the image.
  1303. * @returns - base64 image string.
  1304. */
  1305. _GLTFMaterial._CreateBase64FromCanvas = function (buffer, width, height, mimeType) {
  1306. var imageCanvas = document.createElement('canvas');
  1307. imageCanvas.id = "WriteCanvas";
  1308. var ctx = imageCanvas.getContext('2d');
  1309. imageCanvas.width = width;
  1310. imageCanvas.height = height;
  1311. var imgData = ctx.createImageData(width, height);
  1312. imgData.data.set(buffer);
  1313. ctx.putImageData(imgData, 0, 0);
  1314. return imageCanvas.toDataURL(mimeType);
  1315. };
  1316. /**
  1317. * Generates a white texture based on the specified width and height.
  1318. * @param width - width of the texture in pixels.
  1319. * @param height - height of the texture in pixels.
  1320. * @param scene - babylonjs scene.
  1321. * @returns - white texture.
  1322. */
  1323. _GLTFMaterial._CreateWhiteTexture = function (width, height, scene) {
  1324. var data = new Uint8Array(width * height * 4);
  1325. for (var i = 0; i < data.length; ++i) {
  1326. data[i] = 255;
  1327. }
  1328. var rawTexture = BABYLON.RawTexture.CreateRGBATexture(data, width, height, scene);
  1329. return rawTexture;
  1330. };
  1331. /**
  1332. * 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.
  1333. * @param texture1 - first texture to resize.
  1334. * @param texture2 - second texture to resize.
  1335. * @param scene - babylonjs scene.
  1336. * @returns resized textures or null.
  1337. */
  1338. _GLTFMaterial._ResizeTexturesToSameDimensions = function (texture1, texture2, scene) {
  1339. var texture1Size = texture1 ? texture1.getSize() : { width: 0, height: 0 };
  1340. var texture2Size = texture2 ? texture2.getSize() : { width: 0, height: 0 };
  1341. var resizedTexture1;
  1342. var resizedTexture2;
  1343. if (texture1Size.width < texture2Size.width) {
  1344. if (texture1) {
  1345. resizedTexture1 = BABYLON.TextureTools.CreateResizedCopy(texture1, texture2Size.width, texture2Size.height, true);
  1346. }
  1347. else {
  1348. resizedTexture1 = this._CreateWhiteTexture(texture2Size.width, texture2Size.height, scene);
  1349. }
  1350. resizedTexture2 = texture2;
  1351. }
  1352. else if (texture1Size.width > texture2Size.width) {
  1353. if (texture2) {
  1354. resizedTexture2 = BABYLON.TextureTools.CreateResizedCopy(texture2, texture1Size.width, texture1Size.height, true);
  1355. }
  1356. else {
  1357. resizedTexture2 = this._CreateWhiteTexture(texture1Size.width, texture1Size.height, scene);
  1358. }
  1359. resizedTexture1 = texture1;
  1360. }
  1361. else {
  1362. resizedTexture1 = texture1;
  1363. resizedTexture2 = texture2;
  1364. }
  1365. return {
  1366. "texture1": resizedTexture1,
  1367. "texture2": resizedTexture2
  1368. };
  1369. };
  1370. /**
  1371. * Convert Specular Glossiness Textures to Metallic Roughness.
  1372. * See link below for info on the material conversions from PBR Metallic/Roughness and Specular/Glossiness
  1373. * @link https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness/examples/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
  1374. * @param diffuseTexture - texture used to store diffuse information.
  1375. * @param specularGlossinessTexture - texture used to store specular and glossiness information.
  1376. * @param factors - specular glossiness material factors.
  1377. * @param mimeType - the mime type to use for the texture.
  1378. * @returns pbr metallic roughness interface or null.
  1379. */
  1380. _GLTFMaterial._ConvertSpecularGlossinessTexturesToMetallicRoughness = function (diffuseTexture, specularGlossinessTexture, factors, mimeType) {
  1381. if (!(diffuseTexture || specularGlossinessTexture)) {
  1382. return null;
  1383. }
  1384. var scene = diffuseTexture ? diffuseTexture.getScene() : specularGlossinessTexture.getScene();
  1385. if (!scene) {
  1386. throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!");
  1387. }
  1388. var resizedTextures = this._ResizeTexturesToSameDimensions(diffuseTexture, specularGlossinessTexture, scene);
  1389. var diffuseSize = resizedTextures.texture1.getSize();
  1390. var diffuseBuffer;
  1391. var specularGlossinessBuffer;
  1392. var width = diffuseSize.width;
  1393. var height = diffuseSize.height;
  1394. var pixels = (resizedTextures.texture1.readPixels());
  1395. if (pixels instanceof Uint8Array) {
  1396. diffuseBuffer = (resizedTextures.texture1.readPixels());
  1397. }
  1398. else {
  1399. throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: " + resizedTextures.texture1.name);
  1400. }
  1401. pixels = resizedTextures.texture2.readPixels();
  1402. if (pixels instanceof Uint8Array) {
  1403. specularGlossinessBuffer = (resizedTextures.texture2.readPixels());
  1404. }
  1405. else {
  1406. throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: " + resizedTextures.texture2.name);
  1407. }
  1408. var byteLength = specularGlossinessBuffer.byteLength;
  1409. var metallicRoughnessBuffer = new Uint8Array(byteLength);
  1410. var baseColorBuffer = new Uint8Array(byteLength);
  1411. var strideSize = 4;
  1412. var maxBaseColor = BABYLON.Color3.Black();
  1413. var maxMetallic = 0;
  1414. var maxRoughness = 0;
  1415. for (var h = 0; h < height; ++h) {
  1416. for (var w = 0; w < width; ++w) {
  1417. var offset = (width * h + w) * strideSize;
  1418. var diffuseColor = BABYLON.Color3.FromInts(diffuseBuffer[offset], diffuseBuffer[offset + 1], diffuseBuffer[offset + 2]).toLinearSpace().multiply(factors.diffuseColor);
  1419. var specularColor = BABYLON.Color3.FromInts(specularGlossinessBuffer[offset], specularGlossinessBuffer[offset + 1], specularGlossinessBuffer[offset + 2]).toLinearSpace().multiply(factors.specularColor);
  1420. var glossiness = (specularGlossinessBuffer[offset + 3] / 255) * factors.glossiness;
  1421. var specularGlossiness = {
  1422. diffuseColor: diffuseColor,
  1423. specularColor: specularColor,
  1424. glossiness: glossiness
  1425. };
  1426. var metallicRoughness = this._ConvertSpecularGlossinessToMetallicRoughness(specularGlossiness);
  1427. maxBaseColor.r = Math.max(maxBaseColor.r, metallicRoughness.baseColor.r);
  1428. maxBaseColor.g = Math.max(maxBaseColor.g, metallicRoughness.baseColor.g);
  1429. maxBaseColor.b = Math.max(maxBaseColor.b, metallicRoughness.baseColor.b);
  1430. maxMetallic = Math.max(maxMetallic, metallicRoughness.metallic);
  1431. maxRoughness = Math.max(maxRoughness, metallicRoughness.roughness);
  1432. baseColorBuffer[offset] = metallicRoughness.baseColor.r * 255;
  1433. baseColorBuffer[offset + 1] = metallicRoughness.baseColor.g * 255;
  1434. baseColorBuffer[offset + 2] = metallicRoughness.baseColor.b * 255;
  1435. baseColorBuffer[offset + 3] = resizedTextures.texture1.hasAlpha ? diffuseBuffer[offset + 3] : 255;
  1436. metallicRoughnessBuffer[offset] = 0;
  1437. metallicRoughnessBuffer[offset + 1] = metallicRoughness.roughness * 255;
  1438. metallicRoughnessBuffer[offset + 2] = metallicRoughness.metallic * 255;
  1439. metallicRoughnessBuffer[offset + 3] = 255;
  1440. }
  1441. }
  1442. // Retrieves the metallic roughness factors from the maximum texture values.
  1443. var metallicRoughnessFactors = {
  1444. baseColor: maxBaseColor,
  1445. metallic: maxMetallic,
  1446. roughness: maxRoughness
  1447. };
  1448. var writeOutMetallicRoughnessTexture = false;
  1449. var writeOutBaseColorTexture = false;
  1450. for (var h = 0; h < height; ++h) {
  1451. for (var w = 0; w < width; ++w) {
  1452. var destinationOffset = (width * h + w) * strideSize;
  1453. baseColorBuffer[destinationOffset] /= metallicRoughnessFactors.baseColor.r > this._epsilon ? metallicRoughnessFactors.baseColor.r : 1;
  1454. baseColorBuffer[destinationOffset + 1] /= metallicRoughnessFactors.baseColor.g > this._epsilon ? metallicRoughnessFactors.baseColor.g : 1;
  1455. baseColorBuffer[destinationOffset + 2] /= metallicRoughnessFactors.baseColor.b > this._epsilon ? metallicRoughnessFactors.baseColor.b : 1;
  1456. var linearBaseColorPixel = BABYLON.Color3.FromInts(baseColorBuffer[destinationOffset], baseColorBuffer[destinationOffset + 1], baseColorBuffer[destinationOffset + 2]);
  1457. var sRGBBaseColorPixel = linearBaseColorPixel.toGammaSpace();
  1458. baseColorBuffer[destinationOffset] = sRGBBaseColorPixel.r * 255;
  1459. baseColorBuffer[destinationOffset + 1] = sRGBBaseColorPixel.g * 255;
  1460. baseColorBuffer[destinationOffset + 2] = sRGBBaseColorPixel.b * 255;
  1461. if (!this.FuzzyEquals(sRGBBaseColorPixel, BABYLON.Color3.White(), this._epsilon)) {
  1462. writeOutBaseColorTexture = true;
  1463. }
  1464. metallicRoughnessBuffer[destinationOffset + 1] /= metallicRoughnessFactors.roughness > this._epsilon ? metallicRoughnessFactors.roughness : 1;
  1465. metallicRoughnessBuffer[destinationOffset + 2] /= metallicRoughnessFactors.metallic > this._epsilon ? metallicRoughnessFactors.metallic : 1;
  1466. var metallicRoughnessPixel = BABYLON.Color3.FromInts(255, metallicRoughnessBuffer[destinationOffset + 1], metallicRoughnessBuffer[destinationOffset + 2]);
  1467. if (!this.FuzzyEquals(metallicRoughnessPixel, BABYLON.Color3.White(), this._epsilon)) {
  1468. writeOutMetallicRoughnessTexture = true;
  1469. }
  1470. }
  1471. }
  1472. if (writeOutMetallicRoughnessTexture) {
  1473. var metallicRoughnessBase64 = this._CreateBase64FromCanvas(metallicRoughnessBuffer, width, height, mimeType);
  1474. metallicRoughnessFactors.metallicRoughnessTextureBase64 = metallicRoughnessBase64;
  1475. }
  1476. if (writeOutBaseColorTexture) {
  1477. var baseColorBase64 = this._CreateBase64FromCanvas(baseColorBuffer, width, height, mimeType);
  1478. metallicRoughnessFactors.baseColorTextureBase64 = baseColorBase64;
  1479. }
  1480. return metallicRoughnessFactors;
  1481. };
  1482. /**
  1483. * Converts specular glossiness material properties to metallic roughness.
  1484. * @param specularGlossiness - interface with specular glossiness material properties.
  1485. * @returns - interface with metallic roughness material properties.
  1486. */
  1487. _GLTFMaterial._ConvertSpecularGlossinessToMetallicRoughness = function (specularGlossiness) {
  1488. var diffusePerceivedBrightness = _GLTFMaterial._GetPerceivedBrightness(specularGlossiness.diffuseColor);
  1489. var specularPerceivedBrightness = _GLTFMaterial._GetPerceivedBrightness(specularGlossiness.specularColor);
  1490. var oneMinusSpecularStrength = 1 - _GLTFMaterial._GetMaxComponent(specularGlossiness.specularColor);
  1491. var metallic = _GLTFMaterial._SolveMetallic(diffusePerceivedBrightness, specularPerceivedBrightness, oneMinusSpecularStrength);
  1492. var baseColorFromDiffuse = specularGlossiness.diffuseColor.scale(oneMinusSpecularStrength / (1.0 - this._dielectricSpecular.r) / Math.max(1 - metallic, this._epsilon));
  1493. var baseColorFromSpecular = specularGlossiness.specularColor.subtract(this._dielectricSpecular.scale(1 - metallic)).scale(1 / Math.max(metallic, this._epsilon));
  1494. var baseColor = BABYLON.Color3.Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic);
  1495. baseColor = baseColor.clampToRef(0, 1, baseColor);
  1496. var metallicRoughness = {
  1497. baseColor: baseColor,
  1498. metallic: metallic,
  1499. roughness: 1 - specularGlossiness.glossiness
  1500. };
  1501. return metallicRoughness;
  1502. };
  1503. /**
  1504. * Calculates the surface reflectance, independent of lighting conditions.
  1505. * @param color - Color source to calculate brightness from.
  1506. * @returns number representing the perceived brightness, or zero if color is undefined.
  1507. */
  1508. _GLTFMaterial._GetPerceivedBrightness = function (color) {
  1509. if (color) {
  1510. return Math.sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
  1511. }
  1512. return 0;
  1513. };
  1514. /**
  1515. * Returns the maximum color component value.
  1516. * @param color
  1517. * @returns maximum color component value, or zero if color is null or undefined.
  1518. */
  1519. _GLTFMaterial._GetMaxComponent = function (color) {
  1520. if (color) {
  1521. return Math.max(color.r, Math.max(color.g, color.b));
  1522. }
  1523. return 0;
  1524. };
  1525. /**
  1526. * Convert a PBRMaterial (Metallic/Roughness) to Metallic Roughness factors.
  1527. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  1528. * @param mimeType - mime type to use for the textures.
  1529. * @param images - array of glTF image interfaces.
  1530. * @param textures - array of glTF texture interfaces.
  1531. * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
  1532. * @param imageData - map of image file name to data.
  1533. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  1534. * @returns - glTF PBR Metallic Roughness factors.
  1535. */
  1536. _GLTFMaterial._ConvertMetalRoughFactorsToMetallicRoughness = function (babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords) {
  1537. var metallicRoughness = {
  1538. baseColor: babylonPBRMaterial.albedoColor,
  1539. metallic: babylonPBRMaterial.metallic,
  1540. roughness: babylonPBRMaterial.roughness
  1541. };
  1542. if (hasTextureCoords) {
  1543. if (babylonPBRMaterial.albedoTexture) {
  1544. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.albedoTexture, mimeType, images, textures, imageData);
  1545. if (glTFTexture) {
  1546. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  1547. }
  1548. }
  1549. if (babylonPBRMaterial.metallicTexture) {
  1550. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.metallicTexture, mimeType, images, textures, imageData);
  1551. if (glTFTexture != null) {
  1552. glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFTexture;
  1553. }
  1554. }
  1555. }
  1556. return metallicRoughness;
  1557. };
  1558. /**
  1559. * Convert a PBRMaterial (Specular/Glossiness) to Metallic Roughness factors.
  1560. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  1561. * @param mimeType - mime type to use for the textures.
  1562. * @param images - array of glTF image interfaces.
  1563. * @param textures - array of glTF texture interfaces.
  1564. * @param glTFPbrMetallicRoughness - glTF PBR Metallic Roughness interface.
  1565. * @param imageData - map of image file name to data.
  1566. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  1567. * @returns - glTF PBR Metallic Roughness factors.
  1568. */
  1569. _GLTFMaterial._ConvertSpecGlossFactorsToMetallicRoughness = function (babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords) {
  1570. var specGloss = {
  1571. diffuseColor: babylonPBRMaterial.albedoColor || BABYLON.Color3.White(),
  1572. specularColor: babylonPBRMaterial.reflectivityColor || BABYLON.Color3.White(),
  1573. glossiness: babylonPBRMaterial.microSurface || 1,
  1574. };
  1575. if (babylonPBRMaterial.reflectivityTexture && !babylonPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha) {
  1576. throw new Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture currently not supported");
  1577. }
  1578. var metallicRoughnessFactors = this._ConvertSpecularGlossinessTexturesToMetallicRoughness(babylonPBRMaterial.albedoTexture, babylonPBRMaterial.reflectivityTexture, specGloss, mimeType);
  1579. if (!metallicRoughnessFactors) {
  1580. metallicRoughnessFactors = this._ConvertSpecularGlossinessToMetallicRoughness(specGloss);
  1581. }
  1582. else {
  1583. if (hasTextureCoords) {
  1584. if (metallicRoughnessFactors.baseColorTextureBase64) {
  1585. var glTFBaseColorTexture = _GLTFMaterial._GetTextureInfoFromBase64(metallicRoughnessFactors.baseColorTextureBase64, "bjsBaseColorTexture_" + (textures.length) + ".png", mimeType, images, textures, imageData);
  1586. if (glTFBaseColorTexture != null) {
  1587. glTFPbrMetallicRoughness.baseColorTexture = glTFBaseColorTexture;
  1588. }
  1589. }
  1590. if (metallicRoughnessFactors.metallicRoughnessTextureBase64) {
  1591. var glTFMRColorTexture = _GLTFMaterial._GetTextureInfoFromBase64(metallicRoughnessFactors.metallicRoughnessTextureBase64, "bjsMetallicRoughnessTexture_" + (textures.length) + ".png", mimeType, images, textures, imageData);
  1592. if (glTFMRColorTexture != null) {
  1593. glTFPbrMetallicRoughness.metallicRoughnessTexture = glTFMRColorTexture;
  1594. }
  1595. }
  1596. }
  1597. }
  1598. return metallicRoughnessFactors;
  1599. };
  1600. /**
  1601. * Converts a Babylon PBR Metallic Roughness Material to a glTF Material.
  1602. * @param babylonPBRMaterial - BJS PBR Metallic Roughness Material.
  1603. * @param mimeType - mime type to use for the textures.
  1604. * @param images - array of glTF image interfaces.
  1605. * @param textures - array of glTF texture interfaces.
  1606. * @param materials - array of glTF material interfaces.
  1607. * @param imageData - map of image file name to data.
  1608. * @param hasTextureCoords - specifies if texture coordinates are present on the submesh to determine if textures should be applied.
  1609. */
  1610. _GLTFMaterial._ConvertPBRMaterial = function (babylonPBRMaterial, mimeType, images, textures, materials, imageData, hasTextureCoords) {
  1611. var glTFPbrMetallicRoughness = {};
  1612. var metallicRoughness;
  1613. var glTFMaterial = {
  1614. name: babylonPBRMaterial.name
  1615. };
  1616. var useMetallicRoughness = babylonPBRMaterial.isMetallicWorkflow();
  1617. if (useMetallicRoughness) {
  1618. metallicRoughness = this._ConvertMetalRoughFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  1619. }
  1620. else {
  1621. metallicRoughness = this._ConvertSpecGlossFactorsToMetallicRoughness(babylonPBRMaterial, mimeType, images, textures, glTFPbrMetallicRoughness, imageData, hasTextureCoords);
  1622. }
  1623. if (!(this.FuzzyEquals(metallicRoughness.baseColor, BABYLON.Color3.White(), this._epsilon) && babylonPBRMaterial.alpha >= this._epsilon)) {
  1624. glTFPbrMetallicRoughness.baseColorFactor = [
  1625. metallicRoughness.baseColor.r,
  1626. metallicRoughness.baseColor.g,
  1627. metallicRoughness.baseColor.b,
  1628. babylonPBRMaterial.alpha
  1629. ];
  1630. }
  1631. if (metallicRoughness.metallic != null && metallicRoughness.metallic !== 1) {
  1632. glTFPbrMetallicRoughness.metallicFactor = metallicRoughness.metallic;
  1633. }
  1634. if (metallicRoughness.roughness != null && metallicRoughness.roughness !== 1) {
  1635. glTFPbrMetallicRoughness.roughnessFactor = metallicRoughness.roughness;
  1636. }
  1637. if (babylonPBRMaterial.backFaceCulling != null && !babylonPBRMaterial.backFaceCulling) {
  1638. if (!babylonPBRMaterial.twoSidedLighting) {
  1639. BABYLON.Tools.Warn(babylonPBRMaterial.name + ": Back-face culling enabled and two-sided lighting disabled is not supported in glTF.");
  1640. }
  1641. glTFMaterial.doubleSided = true;
  1642. }
  1643. if (hasTextureCoords) {
  1644. if (babylonPBRMaterial.bumpTexture) {
  1645. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.bumpTexture, mimeType, images, textures, imageData);
  1646. if (glTFTexture) {
  1647. glTFMaterial.normalTexture = glTFTexture;
  1648. }
  1649. }
  1650. if (babylonPBRMaterial.ambientTexture) {
  1651. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.ambientTexture, mimeType, images, textures, imageData);
  1652. if (glTFTexture) {
  1653. var occlusionTexture = {
  1654. index: glTFTexture.index
  1655. };
  1656. glTFMaterial.occlusionTexture = occlusionTexture;
  1657. if (babylonPBRMaterial.ambientTextureStrength) {
  1658. occlusionTexture.strength = babylonPBRMaterial.ambientTextureStrength;
  1659. }
  1660. }
  1661. }
  1662. if (babylonPBRMaterial.emissiveTexture) {
  1663. var glTFTexture = _GLTFMaterial._ExportTexture(babylonPBRMaterial.emissiveTexture, mimeType, images, textures, imageData);
  1664. if (glTFTexture != null) {
  1665. glTFMaterial.emissiveTexture = glTFTexture;
  1666. }
  1667. }
  1668. }
  1669. if (!this.FuzzyEquals(babylonPBRMaterial.emissiveColor, BABYLON.Color3.Black(), this._epsilon)) {
  1670. glTFMaterial.emissiveFactor = babylonPBRMaterial.emissiveColor.asArray();
  1671. }
  1672. if (babylonPBRMaterial.transparencyMode != null) {
  1673. var alphaMode = _GLTFMaterial._GetAlphaMode(babylonPBRMaterial);
  1674. if (alphaMode !== "OPAQUE" /* OPAQUE */) {
  1675. glTFMaterial.alphaMode = alphaMode;
  1676. if (alphaMode === "BLEND" /* BLEND */) {
  1677. glTFMaterial.alphaCutoff = babylonPBRMaterial.alphaCutOff;
  1678. }
  1679. }
  1680. }
  1681. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  1682. materials.push(glTFMaterial);
  1683. };
  1684. /**
  1685. * Extracts a texture from a Babylon texture into file data and glTF data.
  1686. * @param babylonTexture - Babylon texture to extract.
  1687. * @param mimeType - Mime Type of the babylonTexture.
  1688. * @param images - Array of glTF images.
  1689. * @param textures - Array of glTF textures.
  1690. * @param imageData - map of image file name and data.
  1691. * @return - glTF texture info, or null if the texture format is not supported.
  1692. */
  1693. _GLTFMaterial._ExportTexture = function (babylonTexture, mimeType, images, textures, imageData) {
  1694. var textureName = "texture_" + (textures.length - 1).toString();
  1695. var textureData = babylonTexture.getInternalTexture();
  1696. if (textureData != null) {
  1697. textureName = textureData.url || textureName;
  1698. }
  1699. textureName = BABYLON.Tools.GetFilename(textureName);
  1700. var baseFile = textureName.split('.')[0];
  1701. var extension = "";
  1702. if (mimeType === "image/jpeg" /* JPEG */) {
  1703. extension = ".jpg";
  1704. }
  1705. else if (mimeType === "image/png" /* PNG */) {
  1706. extension = ".png";
  1707. }
  1708. else {
  1709. throw new Error("Unsupported mime type " + mimeType);
  1710. }
  1711. textureName = baseFile + extension;
  1712. var pixels = babylonTexture.readPixels();
  1713. var size = babylonTexture.getSize();
  1714. var base64Data = this._CreateBase64FromCanvas(pixels, size.width, size.height, mimeType);
  1715. return this._GetTextureInfoFromBase64(base64Data, textureName, mimeType, images, textures, imageData);
  1716. };
  1717. /**
  1718. * Builds a texture from base64 string.
  1719. * @param base64Texture - base64 texture string.
  1720. * @param textureName - Name to use for the texture.
  1721. * @param mimeType - image mime type for the texture.
  1722. * @param images - array of images.
  1723. * @param textures - array of textures.
  1724. * @param imageData - map of image data.
  1725. * @returns - glTF texture info, or null if the texture format is not supported.
  1726. */
  1727. _GLTFMaterial._GetTextureInfoFromBase64 = function (base64Texture, textureName, mimeType, images, textures, imageData) {
  1728. var textureInfo = null;
  1729. var glTFTexture = {
  1730. source: images.length
  1731. };
  1732. var binStr = atob(base64Texture.split(',')[1]);
  1733. var arr = new Uint8Array(binStr.length);
  1734. for (var i = 0; i < binStr.length; ++i) {
  1735. arr[i] = binStr.charCodeAt(i);
  1736. }
  1737. var imageValues = { data: arr, mimeType: mimeType };
  1738. imageData[textureName] = imageValues;
  1739. if (mimeType === "image/jpeg" /* JPEG */ || mimeType === "image/png" /* PNG */) {
  1740. var glTFImage = {
  1741. uri: textureName
  1742. };
  1743. var foundIndex = -1;
  1744. for (var i = 0; i < images.length; ++i) {
  1745. if (images[i].uri === textureName) {
  1746. foundIndex = i;
  1747. break;
  1748. }
  1749. }
  1750. if (foundIndex === -1) {
  1751. images.push(glTFImage);
  1752. glTFTexture.source = images.length - 1;
  1753. textures.push({
  1754. source: images.length - 1
  1755. });
  1756. textureInfo = {
  1757. index: images.length - 1
  1758. };
  1759. }
  1760. else {
  1761. glTFTexture.source = foundIndex;
  1762. textureInfo = {
  1763. index: foundIndex
  1764. };
  1765. }
  1766. }
  1767. return textureInfo;
  1768. };
  1769. /**
  1770. * Represents the dielectric specular values for R, G and B.
  1771. */
  1772. _GLTFMaterial._dielectricSpecular = new BABYLON.Color3(0.04, 0.04, 0.04);
  1773. /**
  1774. * Allows the maximum specular power to be defined for material calculations.
  1775. */
  1776. _GLTFMaterial._maxSpecularPower = 1024;
  1777. /**
  1778. * Numeric tolerance value
  1779. */
  1780. _GLTFMaterial._epsilon = 1e-6;
  1781. return _GLTFMaterial;
  1782. }());
  1783. GLTF2._GLTFMaterial = _GLTFMaterial;
  1784. })(GLTF2 = BABYLON.GLTF2 || (BABYLON.GLTF2 = {}));
  1785. })(BABYLON || (BABYLON = {}));
  1786. //# sourceMappingURL=babylon.glTFMaterial.js.map
  1787. return BABYLON;
  1788. });