babylonjs.serializers.js 101 KB

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