babylonjs.serializers.js 101 KB

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