babylon.glTF2Serializer.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. ;
  5. var GLTF2Export = /** @class */ (function () {
  6. function GLTF2Export() {
  7. }
  8. /**
  9. * Exports the geometry of a Mesh array in .gltf file format.
  10. * @param meshes
  11. * @param materials
  12. * @param options
  13. *
  14. * @returns - Returns an object with a .gltf, .glb and associates textures
  15. * as keys and their data and paths as values.
  16. */
  17. GLTF2Export.GLTF = function (scene, filename, options) {
  18. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  19. var gltfGenerator = new BABYLON._GLTF2Exporter(scene, options);
  20. if (scene.isReady) {
  21. return gltfGenerator._generateGLTF(glTFPrefix);
  22. }
  23. else {
  24. throw new Error("glTF Serializer: Scene is not ready!");
  25. }
  26. };
  27. /**
  28. *
  29. * @param meshes
  30. * @param filename
  31. *
  32. * @returns - Returns an object with a .glb filename as key and data as value
  33. */
  34. GLTF2Export.GLB = function (scene, filename, options) {
  35. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  36. var gltfGenerator = new BABYLON._GLTF2Exporter(scene, options);
  37. if (scene.isReady) {
  38. return gltfGenerator._generateGLB(glTFPrefix);
  39. }
  40. else {
  41. throw new Error("glTF Serializer: Scene is not ready!");
  42. }
  43. };
  44. return GLTF2Export;
  45. }());
  46. BABYLON.GLTF2Export = GLTF2Export;
  47. })(BABYLON || (BABYLON = {}));
  48. //# sourceMappingURL=babylon.glTFSerializer.js.map
  49. var BABYLON;
  50. (function (BABYLON) {
  51. /**
  52. * glTF Image mimetype enum
  53. */
  54. var _EGLTFImageMimeTypeEnum;
  55. (function (_EGLTFImageMimeTypeEnum) {
  56. _EGLTFImageMimeTypeEnum["PNG"] = "image/png";
  57. _EGLTFImageMimeTypeEnum["JPG"] = "image/jpeg";
  58. })(_EGLTFImageMimeTypeEnum || (_EGLTFImageMimeTypeEnum = {}));
  59. /**
  60. * glTF Alpha Mode Enum
  61. */
  62. var _EGLTFAlphaModeEnum;
  63. (function (_EGLTFAlphaModeEnum) {
  64. _EGLTFAlphaModeEnum["OPAQUE"] = "OPAQUE";
  65. _EGLTFAlphaModeEnum["MASK"] = "MASK";
  66. _EGLTFAlphaModeEnum["BLEND"] = "BLEND";
  67. })(_EGLTFAlphaModeEnum = BABYLON._EGLTFAlphaModeEnum || (BABYLON._EGLTFAlphaModeEnum = {}));
  68. /**
  69. * Converts Babylon Scene into glTF 2.0
  70. */
  71. var _GLTF2Exporter = /** @class */ (function () {
  72. function _GLTF2Exporter(babylonScene, options) {
  73. this.asset = { generator: "BabylonJS", version: "2.0" };
  74. this.babylonScene = babylonScene;
  75. this.bufferViews = new Array();
  76. this.accessors = new Array();
  77. this.meshes = new Array();
  78. this.scenes = new Array();
  79. this.nodes = new Array();
  80. this.images = new Array();
  81. this.materials = new Array();
  82. this.imageData = {};
  83. if (options !== undefined) {
  84. this.options = options;
  85. }
  86. var totalByteLength = 0;
  87. totalByteLength = this.createScene(this.babylonScene, totalByteLength);
  88. this.totalByteLength = totalByteLength;
  89. }
  90. /**
  91. * Creates a buffer view based on teh supplied arguments
  92. * @param {number} bufferIndex - index value of the specified buffer
  93. * @param {number} byteOffset - byte offset value
  94. * @param {number} byteLength - byte length of the bufferView
  95. * @returns - bufferView for glTF
  96. */
  97. _GLTF2Exporter.prototype.createBufferView = function (bufferIndex, byteOffset, byteLength, name) {
  98. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  99. if (byteOffset > 0) {
  100. bufferview.byteOffset = byteOffset;
  101. }
  102. if (name) {
  103. bufferview.name = name;
  104. }
  105. return bufferview;
  106. };
  107. /**
  108. * Creates an accessor based on the supplied arguments
  109. * @param bufferviewIndex
  110. * @param name
  111. * @param type
  112. * @param componentType
  113. * @param count
  114. * @param min
  115. * @param max
  116. * @returns - accessor for glTF
  117. */
  118. _GLTF2Exporter.prototype.createAccessor = function (bufferviewIndex, name, type, componentType, count, min, max) {
  119. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  120. if (min) {
  121. accessor.min = min;
  122. }
  123. if (max) {
  124. accessor.max = max;
  125. }
  126. return accessor;
  127. };
  128. /**
  129. * Calculates the minimum and maximum values of an array of floats, based on stride
  130. * @param buff
  131. * @param vertexStart
  132. * @param vertexCount
  133. * @param arrayOffset
  134. * @param stride
  135. * @returns - min number array and max number array
  136. */
  137. _GLTF2Exporter.prototype.calculateMinMax = function (buff, vertexStart, vertexCount, arrayOffset, stride) {
  138. var min = [Infinity, Infinity, Infinity];
  139. var max = [-Infinity, -Infinity, -Infinity];
  140. var end = vertexStart + vertexCount;
  141. if (vertexCount > 0) {
  142. for (var i = vertexStart; i < end; ++i) {
  143. var index = stride * i;
  144. for (var j = 0; j < stride; ++j) {
  145. if (buff[index] < min[j]) {
  146. min[j] = buff[index];
  147. }
  148. if (buff[index] > max[j]) {
  149. max[j] = buff[index];
  150. }
  151. ++index;
  152. }
  153. }
  154. }
  155. return { min: min, max: max };
  156. };
  157. /**
  158. * Write mesh attribute data to buffer.
  159. * Returns the bytelength of the data.
  160. * @param vertexBufferType
  161. * @param submesh
  162. * @param meshAttributeArray
  163. * @param strideSize
  164. * @param byteOffset
  165. * @param dataBuffer
  166. * @param useRightHandedSystem
  167. * @returns - byte length
  168. */
  169. _GLTF2Exporter.prototype.writeAttributeData = function (vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem) {
  170. var byteOff = byteOffset;
  171. var end = submesh.verticesStart + submesh.verticesCount;
  172. var byteLength = 0;
  173. switch (vertexBufferType) {
  174. case BABYLON.VertexBuffer.PositionKind: {
  175. for (var k = submesh.verticesStart; k < end; ++k) {
  176. var index = k * strideSize;
  177. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  178. byteOff += 4;
  179. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  180. byteOff += 4;
  181. if (useRightHandedSystem) {
  182. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  183. }
  184. else {
  185. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  186. }
  187. byteOff += 4;
  188. }
  189. byteLength = submesh.verticesCount * 12;
  190. break;
  191. }
  192. case BABYLON.VertexBuffer.NormalKind: {
  193. for (var k = submesh.verticesStart; k < end; ++k) {
  194. var index = k * strideSize;
  195. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  196. byteOff += 4;
  197. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  198. byteOff += 4;
  199. if (useRightHandedSystem) {
  200. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  201. }
  202. else {
  203. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  204. }
  205. byteOff += 4;
  206. }
  207. byteLength = submesh.verticesCount * 12;
  208. break;
  209. }
  210. case BABYLON.VertexBuffer.TangentKind: {
  211. for (var k = submesh.indexStart; k < end; ++k) {
  212. var index = k * strideSize;
  213. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  214. byteOff += 4;
  215. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  216. byteOff += 4;
  217. if (useRightHandedSystem) {
  218. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  219. }
  220. else {
  221. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  222. }
  223. byteOff += 4;
  224. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  225. byteOff += 4;
  226. }
  227. byteLength = submesh.verticesCount * 16;
  228. break;
  229. }
  230. case BABYLON.VertexBuffer.ColorKind: {
  231. for (var k = submesh.verticesStart; k < end; ++k) {
  232. var index = k * strideSize;
  233. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  234. byteOff += 4;
  235. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  236. byteOff += 4;
  237. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  238. byteOff += 4;
  239. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  240. byteOff += 4;
  241. }
  242. byteLength = submesh.verticesCount * 16;
  243. break;
  244. }
  245. case BABYLON.VertexBuffer.UVKind: {
  246. for (var k = submesh.verticesStart; k < end; ++k) {
  247. var index = k * strideSize;
  248. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  249. byteOff += 4;
  250. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  251. byteOff += 4;
  252. }
  253. byteLength = submesh.verticesCount * 8;
  254. break;
  255. }
  256. case BABYLON.VertexBuffer.UV2Kind: {
  257. for (var k = submesh.verticesStart; k < end; ++k) {
  258. var index = k * strideSize;
  259. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  260. byteOff += 4;
  261. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  262. byteOff += 4;
  263. }
  264. byteLength = submesh.verticesCount * 8;
  265. break;
  266. }
  267. default: {
  268. throw new Error("Unsupported vertex buffer type: " + vertexBufferType);
  269. }
  270. }
  271. return byteLength;
  272. };
  273. /**
  274. * Generates glTF json data
  275. * @param glb
  276. * @param glTFPrefix
  277. * @param prettyPrint
  278. * @returns - json data as string
  279. */
  280. _GLTF2Exporter.prototype.generateJSON = function (glb, glTFPrefix, prettyPrint) {
  281. var buffer = { byteLength: this.totalByteLength };
  282. var glTF = {
  283. asset: this.asset
  284. };
  285. if (buffer.byteLength > 0) {
  286. glTF.buffers = [buffer];
  287. }
  288. if (this.nodes && this.nodes.length !== 0) {
  289. glTF.nodes = this.nodes;
  290. }
  291. if (this.meshes && this.meshes.length !== 0) {
  292. glTF.meshes = this.meshes;
  293. }
  294. if (this.scenes && this.scenes.length !== 0) {
  295. glTF.scenes = this.scenes;
  296. glTF.scene = 0;
  297. }
  298. if (this.bufferViews && this.bufferViews.length !== 0) {
  299. glTF.bufferViews = this.bufferViews;
  300. }
  301. if (this.accessors && this.accessors.length !== 0) {
  302. glTF.accessors = this.accessors;
  303. }
  304. if (this.materials && this.materials.length !== 0) {
  305. glTF.materials = this.materials;
  306. }
  307. if (this.textures && this.textures.length !== 0) {
  308. glTF.textures = this.textures;
  309. }
  310. if (this.images && this.images.length !== 0) {
  311. if (!glb) {
  312. glTF.images = this.images;
  313. }
  314. else {
  315. glTF.images = [];
  316. // Replace uri with bufferview and mime type for glb
  317. var imageLength = this.images.length;
  318. var byteOffset = this.totalByteLength;
  319. for (var i = 0; i < imageLength; ++i) {
  320. var image = this.images[i];
  321. if (image.uri !== undefined) {
  322. var imageData = this.imageData[image.uri];
  323. var imageName = image.uri.split('.')[0] + " image";
  324. var bufferView = this.createBufferView(0, byteOffset, imageData.data.length, imageName);
  325. byteOffset += imageData.data.buffer.byteLength;
  326. this.bufferViews.push(bufferView);
  327. image.bufferView = this.bufferViews.length - 1;
  328. image.name = imageName;
  329. image.mimeType = imageData.mimeType;
  330. image.uri = undefined;
  331. glTF.images.push(image);
  332. }
  333. }
  334. buffer.byteLength = byteOffset;
  335. }
  336. }
  337. if (!glb) {
  338. buffer.uri = glTFPrefix + ".bin";
  339. }
  340. var jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
  341. return jsonText;
  342. };
  343. /**
  344. * Generates data for .gltf and .bin files based on the glTF prefix string
  345. * @param glTFPrefix
  346. * @returns - object with glTF json tex filename
  347. * and binary file name as keys and their data as values
  348. */
  349. _GLTF2Exporter.prototype._generateGLTF = function (glTFPrefix) {
  350. var jsonText = this.generateJSON(false, glTFPrefix, true);
  351. var binaryBuffer = this.generateBinary();
  352. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  353. var glTFFileName = glTFPrefix + '.gltf';
  354. var glTFBinFile = glTFPrefix + '.bin';
  355. var container = new BABYLON._GLTFData();
  356. container.glTFFiles[glTFFileName] = jsonText;
  357. container.glTFFiles[glTFBinFile] = bin;
  358. if (this.imageData !== null) {
  359. for (var image in this.imageData) {
  360. container.glTFFiles[image] = new Blob([this.imageData[image].data], { type: this.imageData[image].mimeType });
  361. }
  362. }
  363. return container;
  364. };
  365. /**
  366. * Creates a binary buffer for glTF
  367. * @returns - array buffer for binary data
  368. */
  369. _GLTF2Exporter.prototype.generateBinary = function () {
  370. var byteOffset = 0;
  371. var binaryBuffer = new ArrayBuffer(this.totalByteLength);
  372. var dataBuffer = new DataView(binaryBuffer);
  373. byteOffset = this.createScene(this.babylonScene, byteOffset, dataBuffer);
  374. return binaryBuffer;
  375. };
  376. /**
  377. * Pads the number to a power of 4
  378. * @param num - number to pad
  379. * @returns - padded number
  380. */
  381. _GLTF2Exporter.prototype._getPadding = function (num) {
  382. var remainder = num % 4;
  383. var padding = remainder === 0 ? remainder : 4 - remainder;
  384. return padding;
  385. };
  386. /**
  387. * Generates a glb file from the json and binary data.
  388. * Returns an object with the glb file name as the key and data as the value.
  389. * @param jsonText
  390. * @param binaryBuffer
  391. * @param glTFPrefix
  392. * @returns - object with glb filename as key and data as value
  393. */
  394. _GLTF2Exporter.prototype._generateGLB = function (glTFPrefix) {
  395. var jsonText = this.generateJSON(true);
  396. var binaryBuffer = this.generateBinary();
  397. var glbFileName = glTFPrefix + '.glb';
  398. var headerLength = 12;
  399. var chunkLengthPrefix = 8;
  400. var jsonLength = jsonText.length;
  401. var imageByteLength = 0;
  402. for (var key in this.imageData) {
  403. imageByteLength += this.imageData[key].data.byteLength;
  404. }
  405. var jsonPadding = this._getPadding(jsonLength);
  406. var binPadding = this._getPadding(binaryBuffer.byteLength);
  407. var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding + imageByteLength;
  408. //header
  409. var headerBuffer = new ArrayBuffer(headerLength);
  410. var headerBufferView = new DataView(headerBuffer);
  411. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  412. headerBufferView.setUint32(4, 2, true); // version
  413. headerBufferView.setUint32(8, byteLength, true); // total bytes in file
  414. //json chunk
  415. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  416. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  417. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  418. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  419. //json chunk bytes
  420. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  421. for (var i = 0; i < jsonLength; ++i) {
  422. jsonData[i] = jsonText.charCodeAt(i);
  423. }
  424. //json padding
  425. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  426. for (var i = 0; i < jsonPadding; ++i) {
  427. jsonPaddingView[i] = 0x20;
  428. }
  429. //binary chunk
  430. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  431. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  432. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength + imageByteLength, true);
  433. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  434. // binary padding
  435. var binPaddingBuffer = new ArrayBuffer(binPadding);
  436. var binPaddingView = new Uint8Array(binPaddingBuffer);
  437. for (var i = 0; i < binPadding; ++i) {
  438. binPaddingView[i] = 0;
  439. }
  440. var glbData = [headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer];
  441. // binary data
  442. for (var key in this.imageData) {
  443. glbData.push(this.imageData[key].data.buffer);
  444. }
  445. glbData.push(binPaddingBuffer);
  446. var glbFile = new Blob(glbData, { type: 'application/octet-stream' });
  447. var container = new BABYLON._GLTFData();
  448. container.glTFFiles[glbFileName] = glbFile;
  449. return container;
  450. };
  451. /**
  452. * Sets the TRS for each node
  453. * @param node
  454. * @param babylonMesh
  455. * @param useRightHandedSystem
  456. */
  457. _GLTF2Exporter.prototype.setNodeTransformation = function (node, babylonMesh, useRightHandedSystem) {
  458. if (!(babylonMesh.position.x === 0 && babylonMesh.position.y === 0 && babylonMesh.position.z === 0)) {
  459. if (useRightHandedSystem) {
  460. node.translation = babylonMesh.position.asArray();
  461. }
  462. else {
  463. node.translation = [babylonMesh.position.x, babylonMesh.position.y, -babylonMesh.position.z];
  464. }
  465. }
  466. if (!(babylonMesh.scaling.x === 1 && babylonMesh.scaling.y === 1 && babylonMesh.scaling.z === 1)) {
  467. if (useRightHandedSystem) {
  468. node.scale = babylonMesh.scaling.asArray();
  469. }
  470. else {
  471. node.scale = [babylonMesh.scaling.x, babylonMesh.scaling.y, -babylonMesh.scaling.z];
  472. }
  473. }
  474. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  475. if (babylonMesh.rotationQuaternion) {
  476. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  477. }
  478. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  479. if (useRightHandedSystem) {
  480. node.rotation = rotationQuaternion.asArray();
  481. }
  482. else {
  483. node.rotation = [-rotationQuaternion.x, -rotationQuaternion.y, rotationQuaternion.z, rotationQuaternion.w];
  484. }
  485. }
  486. };
  487. /**
  488. *
  489. * @param babylonTexture
  490. * @return - glTF texture, or null if the texture format is not supported
  491. */
  492. _GLTF2Exporter.prototype.exportTexture = function (babylonTexture, mimeType) {
  493. if (mimeType === void 0) { mimeType = _EGLTFImageMimeTypeEnum.JPG; }
  494. var textureInfo = null;
  495. var glTFTexture;
  496. glTFTexture = {
  497. source: this.images.length
  498. };
  499. var textureName = babylonTexture.getInternalTexture().url;
  500. if (textureName.search('/') !== -1) {
  501. var splitFilename = textureName.split('/');
  502. textureName = splitFilename[splitFilename.length - 1];
  503. var basefile = textureName.split('.')[0];
  504. var extension = textureName.split('.')[1];
  505. if (mimeType === _EGLTFImageMimeTypeEnum.JPG) {
  506. extension = ".jpg";
  507. }
  508. else if (mimeType === _EGLTFImageMimeTypeEnum.PNG) {
  509. extension = ".png";
  510. }
  511. else {
  512. throw new Error("Unsupported mime type " + mimeType);
  513. }
  514. textureName = basefile + extension;
  515. }
  516. var pixels = babylonTexture.readPixels();
  517. var imageCanvas = document.createElement('canvas');
  518. imageCanvas.id = "ImageCanvas";
  519. var ctx = imageCanvas.getContext('2d');
  520. var size = babylonTexture.getSize();
  521. imageCanvas.width = size.width;
  522. imageCanvas.height = size.height;
  523. var imgData = ctx.createImageData(size.width, size.height);
  524. imgData.data.set(pixels);
  525. ctx.putImageData(imgData, 0, 0);
  526. var base64Data = imageCanvas.toDataURL(mimeType);
  527. var binStr = atob(base64Data.split(',')[1]);
  528. var arr = new Uint8Array(binStr.length);
  529. for (var i = 0; i < binStr.length; ++i) {
  530. arr[i] = binStr.charCodeAt(i);
  531. }
  532. var imageValues = { data: arr, mimeType: mimeType };
  533. this.imageData[textureName] = imageValues;
  534. if (mimeType === _EGLTFImageMimeTypeEnum.JPG) {
  535. var glTFImage = {
  536. uri: textureName
  537. };
  538. var foundIndex = -1;
  539. for (var i = 0; i < this.images.length; ++i) {
  540. if (this.images[i].uri === textureName) {
  541. foundIndex = i;
  542. break;
  543. }
  544. }
  545. if (foundIndex === -1) {
  546. this.images.push(glTFImage);
  547. glTFTexture.source = this.images.length - 1;
  548. this.textures.push({
  549. source: this.images.length - 1
  550. });
  551. textureInfo = {
  552. index: this.images.length - 1
  553. };
  554. }
  555. else {
  556. glTFTexture.source = foundIndex;
  557. textureInfo = {
  558. index: foundIndex
  559. };
  560. }
  561. }
  562. return textureInfo;
  563. };
  564. /**
  565. * Sets data for the primitive attributes of each submesh
  566. * @param mesh
  567. * @param babylonMesh
  568. * @param byteOffset
  569. * @param useRightHandedSystem
  570. * @param dataBuffer
  571. * @returns - bytelength of the primitive attributes plus the passed in byteOffset
  572. */
  573. _GLTF2Exporter.prototype.setPrimitiveAttributes = function (mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer) {
  574. // go through all mesh primitives (submeshes)
  575. for (var j = 0; j < babylonMesh.subMeshes.length; ++j) {
  576. var bufferMesh = null;
  577. var submesh = babylonMesh.subMeshes[j];
  578. var meshPrimitive = { attributes: {} };
  579. if (babylonMesh instanceof BABYLON.Mesh) {
  580. bufferMesh = babylonMesh;
  581. }
  582. else if (babylonMesh instanceof BABYLON.InstancedMesh) {
  583. bufferMesh = babylonMesh.sourceMesh;
  584. }
  585. // Loop through each attribute of the submesh (mesh primitive)
  586. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  587. var positionVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.PositionKind);
  588. var positionVertexBufferOffset = positionVertexBuffer.getOffset();
  589. var positions = positionVertexBuffer.getData();
  590. var positionStrideSize = positionVertexBuffer.getStrideSize();
  591. if (dataBuffer) {
  592. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.PositionKind, submesh, positions, positionStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  593. }
  594. else {
  595. // Create bufferview
  596. var byteLength = submesh.verticesCount * 12;
  597. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Positions");
  598. byteOffset += byteLength;
  599. this.bufferViews.push(bufferview);
  600. // Create accessor
  601. var result = this.calculateMinMax(positions, submesh.verticesStart, submesh.verticesCount, positionVertexBufferOffset, positionStrideSize);
  602. var accessor = this.createAccessor(this.bufferViews.length - 1, "Position", "VEC3", 5126, submesh.verticesCount, result.min, result.max);
  603. this.accessors.push(accessor);
  604. meshPrimitive.attributes.POSITION = this.accessors.length - 1;
  605. }
  606. }
  607. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  608. var normalVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.NormalKind);
  609. var normals = normalVertexBuffer.getData();
  610. var normalStrideSize = normalVertexBuffer.getStrideSize();
  611. if (dataBuffer) {
  612. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.NormalKind, submesh, normals, normalStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  613. }
  614. else {
  615. // Create bufferview
  616. var byteLength = submesh.verticesCount * 12;
  617. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Normals");
  618. byteOffset += byteLength;
  619. this.bufferViews.push(bufferview);
  620. // Create accessor
  621. var accessor = this.createAccessor(this.bufferViews.length - 1, "Normal", "VEC3", 5126, submesh.verticesCount);
  622. this.accessors.push(accessor);
  623. meshPrimitive.attributes.NORMAL = this.accessors.length - 1;
  624. }
  625. }
  626. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  627. var tangentVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.TangentKind);
  628. var tangents = tangentVertexBuffer.getData();
  629. var tangentStrideSize = tangentVertexBuffer.getStrideSize();
  630. if (dataBuffer) {
  631. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.TangentKind, submesh, tangents, tangentStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  632. }
  633. else {
  634. // Create bufferview
  635. var byteLength = submesh.verticesCount * 16;
  636. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Tangents");
  637. byteOffset += byteLength;
  638. this.bufferViews.push(bufferview);
  639. // Create accessor
  640. var accessor = this.createAccessor(this.bufferViews.length - 1, "Tangent", "VEC4", 5126, submesh.verticesCount);
  641. this.accessors.push(accessor);
  642. meshPrimitive.attributes.TANGENT = this.accessors.length - 1;
  643. }
  644. }
  645. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  646. var colorVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.ColorKind);
  647. var colors = colorVertexBuffer.getData();
  648. var colorStrideSize = colorVertexBuffer.getStrideSize();
  649. if (dataBuffer) {
  650. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.ColorKind, submesh, colors, colorStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  651. }
  652. else {
  653. // Create bufferview
  654. var byteLength = submesh.verticesCount * 16;
  655. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Colors");
  656. byteOffset += byteLength;
  657. this.bufferViews.push(bufferview);
  658. // Create accessor
  659. var accessor = this.createAccessor(this.bufferViews.length - 1, "Color", "VEC4", 5126, submesh.verticesCount);
  660. this.accessors.push(accessor);
  661. meshPrimitive.attributes.COLOR_0 = this.accessors.length - 1;
  662. }
  663. }
  664. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  665. var texCoord0VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UVKind);
  666. var texCoords0 = texCoord0VertexBuffer.getData();
  667. var texCoord0StrideSize = texCoord0VertexBuffer.getStrideSize();
  668. if (dataBuffer) {
  669. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UVKind, submesh, texCoords0, texCoord0StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  670. }
  671. else {
  672. // Create bufferview
  673. var byteLength = submesh.verticesCount * 8;
  674. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Texture Coords0");
  675. byteOffset += byteLength;
  676. this.bufferViews.push(bufferview);
  677. // Create accessor
  678. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  679. this.accessors.push(accessor);
  680. meshPrimitive.attributes.TEXCOORD_0 = this.accessors.length - 1;
  681. }
  682. }
  683. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  684. var texCoord1VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UV2Kind);
  685. var texCoords1 = texCoord1VertexBuffer.getData();
  686. var texCoord1StrideSize = texCoord1VertexBuffer.getStrideSize();
  687. if (dataBuffer) {
  688. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UV2Kind, submesh, texCoords1, texCoord1StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  689. }
  690. else {
  691. // Create bufferview
  692. var byteLength = submesh.verticesCount * 8;
  693. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Texture Coords 1");
  694. byteOffset += byteLength;
  695. this.bufferViews.push(bufferview);
  696. // Create accessor
  697. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  698. this.accessors.push(accessor);
  699. meshPrimitive.attributes.TEXCOORD_1 = this.accessors.length - 1;
  700. }
  701. }
  702. if (bufferMesh.getTotalIndices() > 0) {
  703. if (dataBuffer) {
  704. var indices = bufferMesh.getIndices();
  705. var start = submesh.indexStart;
  706. var end = submesh.indexCount + start;
  707. var byteOff = byteOffset;
  708. for (var k = start; k < end; k = k + 3) {
  709. dataBuffer.setUint32(byteOff, indices[k], true);
  710. byteOff += 4;
  711. dataBuffer.setUint32(byteOff, indices[k + 1], true);
  712. byteOff += 4;
  713. dataBuffer.setUint32(byteOff, indices[k + 2], true);
  714. byteOff += 4;
  715. }
  716. var byteLength = submesh.indexCount * 4;
  717. byteOffset += byteLength;
  718. }
  719. else {
  720. // Create bufferview
  721. var indicesCount = submesh.indexCount;
  722. var byteLength = indicesCount * 4;
  723. var bufferview = this.createBufferView(0, byteOffset, byteLength, "Indices");
  724. byteOffset += byteLength;
  725. this.bufferViews.push(bufferview);
  726. // Create accessor
  727. var accessor = this.createAccessor(this.bufferViews.length - 1, "Indices", "SCALAR", 5125, indicesCount);
  728. this.accessors.push(accessor);
  729. meshPrimitive.indices = this.accessors.length - 1;
  730. }
  731. }
  732. if (bufferMesh.material) {
  733. if (bufferMesh.material instanceof BABYLON.StandardMaterial) {
  734. var babylonStandardMaterial = bufferMesh.material;
  735. var glTFMaterial = { name: babylonStandardMaterial.name };
  736. if (!babylonStandardMaterial.backFaceCulling) {
  737. glTFMaterial.doubleSided = true;
  738. }
  739. if (babylonStandardMaterial.bumpTexture) {
  740. var glTFTexture = this.exportTexture(babylonStandardMaterial.bumpTexture);
  741. if (glTFTexture) {
  742. glTFMaterial.normalTexture = glTFTexture;
  743. }
  744. }
  745. if (babylonStandardMaterial.emissiveTexture) {
  746. var glTFEmissiveTexture = this.exportTexture(babylonStandardMaterial.emissiveTexture);
  747. if (glTFEmissiveTexture) {
  748. glTFMaterial.emissiveTexture = glTFEmissiveTexture;
  749. }
  750. glTFMaterial.emissiveFactor = [1.0, 1.0, 1.0];
  751. }
  752. if (babylonStandardMaterial.ambientTexture) {
  753. var glTFOcclusionTexture = this.exportTexture(babylonStandardMaterial.ambientTexture);
  754. if (glTFOcclusionTexture) {
  755. glTFMaterial.occlusionTexture = glTFOcclusionTexture;
  756. }
  757. }
  758. // Spec Gloss
  759. var babylonSpecularGlossiness = {
  760. diffuse: babylonStandardMaterial.diffuseColor,
  761. opacity: babylonStandardMaterial.alpha,
  762. specular: babylonStandardMaterial.specularColor || BABYLON.Color3.Black(),
  763. glossiness: babylonStandardMaterial.specularPower / 256
  764. };
  765. if (babylonStandardMaterial.specularTexture) {
  766. }
  767. var babylonMetallicRoughness = BABYLON._GLTFMaterial.ConvertToMetallicRoughness(babylonSpecularGlossiness);
  768. var glTFPbrMetallicRoughness = {
  769. baseColorFactor: [
  770. babylonMetallicRoughness.baseColor.r,
  771. babylonMetallicRoughness.baseColor.g,
  772. babylonMetallicRoughness.baseColor.b,
  773. babylonMetallicRoughness.opacity
  774. ],
  775. metallicFactor: babylonMetallicRoughness.metallic,
  776. roughnessFactor: babylonMetallicRoughness.roughness
  777. };
  778. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  779. // TODO: Handle Textures
  780. this.materials.push(glTFMaterial);
  781. meshPrimitive.material = this.materials.length - 1;
  782. }
  783. else if (bufferMesh.material instanceof BABYLON.PBRMetallicRoughnessMaterial) {
  784. if (!this.textures) {
  785. this.textures = new Array();
  786. }
  787. var babylonPBRMaterial = bufferMesh.material;
  788. var glTFPbrMetallicRoughness = {};
  789. if (babylonPBRMaterial.baseColor) {
  790. glTFPbrMetallicRoughness.baseColorFactor = [
  791. babylonPBRMaterial.baseColor.r,
  792. babylonPBRMaterial.baseColor.g,
  793. babylonPBRMaterial.baseColor.b,
  794. babylonPBRMaterial.alpha
  795. ];
  796. }
  797. if (babylonPBRMaterial.baseTexture !== undefined) {
  798. var glTFTexture = this.exportTexture(babylonPBRMaterial.baseTexture);
  799. if (glTFTexture !== null) {
  800. glTFPbrMetallicRoughness.baseColorTexture = glTFTexture;
  801. }
  802. glTFPbrMetallicRoughness.baseColorTexture;
  803. }
  804. if (babylonPBRMaterial.metallic !== undefined) {
  805. glTFPbrMetallicRoughness.metallicFactor = babylonPBRMaterial.metallic;
  806. }
  807. if (babylonPBRMaterial.roughness !== undefined) {
  808. glTFPbrMetallicRoughness.roughnessFactor = babylonPBRMaterial.roughness;
  809. }
  810. var glTFMaterial = {
  811. name: babylonPBRMaterial.name
  812. };
  813. if (babylonPBRMaterial.doubleSided) {
  814. glTFMaterial.doubleSided = babylonPBRMaterial.doubleSided;
  815. }
  816. if (babylonPBRMaterial.normalTexture) {
  817. var glTFTexture = this.exportTexture(babylonPBRMaterial.normalTexture);
  818. if (glTFTexture) {
  819. glTFMaterial.normalTexture = glTFTexture;
  820. }
  821. }
  822. if (babylonPBRMaterial.occlusionTexture) {
  823. var glTFTexture = this.exportTexture(babylonPBRMaterial.occlusionTexture);
  824. if (glTFTexture) {
  825. glTFMaterial.occlusionTexture = glTFTexture;
  826. if (babylonPBRMaterial.occlusionStrength !== undefined) {
  827. glTFMaterial.occlusionTexture.strength = babylonPBRMaterial.occlusionStrength;
  828. }
  829. }
  830. }
  831. if (babylonPBRMaterial.emissiveTexture) {
  832. var glTFTexture = this.exportTexture(babylonPBRMaterial.emissiveTexture);
  833. if (glTFTexture !== null) {
  834. glTFMaterial.emissiveTexture = glTFTexture;
  835. }
  836. }
  837. if (!babylonPBRMaterial.emissiveColor.equals(new BABYLON.Color3(0.0, 0.0, 0.0))) {
  838. glTFMaterial.emissiveFactor = babylonPBRMaterial.emissiveColor.asArray();
  839. }
  840. if (babylonPBRMaterial.transparencyMode) {
  841. var alphaMode = BABYLON._GLTFMaterial.GetAlphaMode(babylonPBRMaterial);
  842. if (alphaMode !== _EGLTFAlphaModeEnum.OPAQUE) {
  843. glTFMaterial.alphaMode = alphaMode;
  844. if (alphaMode === _EGLTFAlphaModeEnum.BLEND) {
  845. glTFMaterial.alphaCutoff = babylonPBRMaterial.alphaCutOff;
  846. }
  847. }
  848. }
  849. glTFMaterial.pbrMetallicRoughness = glTFPbrMetallicRoughness;
  850. // TODO: Handle Textures
  851. this.materials.push(glTFMaterial);
  852. meshPrimitive.material = this.materials.length - 1;
  853. }
  854. }
  855. mesh.primitives.push(meshPrimitive);
  856. }
  857. return byteOffset;
  858. };
  859. /**
  860. * Creates a glTF scene based on the array of meshes.
  861. * Returns the the total byte offset.
  862. * @param gltf
  863. * @param byteOffset
  864. * @param buffer
  865. * @param dataBuffer
  866. * @returns bytelength + byteoffset
  867. */
  868. _GLTF2Exporter.prototype.createScene = function (babylonScene, byteOffset, dataBuffer) {
  869. if (babylonScene.meshes.length > 0) {
  870. var babylonMeshes = babylonScene.meshes;
  871. var scene = { nodes: new Array() };
  872. for (var i = 0; i < babylonMeshes.length; ++i) {
  873. if (this.options &&
  874. this.options.shouldExportMesh !== undefined &&
  875. !this.options.shouldExportMesh(babylonMeshes[i])) {
  876. continue;
  877. }
  878. else {
  879. // create node to hold translation/rotation/scale and the mesh
  880. var node = { mesh: -1 };
  881. var babylonMesh = babylonMeshes[i];
  882. var useRightHandedSystem = babylonMesh.getScene().useRightHandedSystem;
  883. // Set transformation
  884. this.setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  885. // create mesh
  886. var mesh = { primitives: new Array() };
  887. mesh.primitives = [];
  888. byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  889. // go through all mesh primitives (submeshes)
  890. this.meshes.push(mesh);
  891. node.mesh = this.meshes.length - 1;
  892. if (babylonMesh.name) {
  893. node.name = babylonMesh.name;
  894. }
  895. this.nodes.push(node);
  896. scene.nodes.push(this.nodes.length - 1);
  897. }
  898. }
  899. this.scenes.push(scene);
  900. }
  901. return byteOffset;
  902. };
  903. return _GLTF2Exporter;
  904. }());
  905. BABYLON._GLTF2Exporter = _GLTF2Exporter;
  906. })(BABYLON || (BABYLON = {}));
  907. //# sourceMappingURL=babylon.glTFExporter.js.map
  908. var BABYLON;
  909. (function (BABYLON) {
  910. /**
  911. * Class for holding and downloading glTF file data
  912. */
  913. var _GLTFData = /** @class */ (function () {
  914. function _GLTFData() {
  915. this.glTFFiles = {};
  916. }
  917. /**
  918. * Downloads glTF data.
  919. */
  920. _GLTFData.prototype.downloadFiles = function () {
  921. /**
  922. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  923. * @param str
  924. * @param suffix
  925. * @returns - indicating whether the suffix matches or not
  926. */
  927. function endsWith(str, suffix) {
  928. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  929. }
  930. for (var key in this.glTFFiles) {
  931. var link = document.createElement('a');
  932. document.body.appendChild(link);
  933. link.setAttribute("type", "hidden");
  934. link.download = key;
  935. var blob = this.glTFFiles[key];
  936. var mimeType = void 0;
  937. if (endsWith(key, ".glb")) {
  938. mimeType = { type: "model/gltf-binary" };
  939. }
  940. else if (endsWith(key, ".bin")) {
  941. mimeType = { type: "application/octet-stream" };
  942. }
  943. else if (endsWith(key, ".gltf")) {
  944. mimeType = { type: "model/gltf+json" };
  945. }
  946. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  947. link.click();
  948. }
  949. };
  950. return _GLTFData;
  951. }());
  952. BABYLON._GLTFData = _GLTFData;
  953. })(BABYLON || (BABYLON = {}));
  954. //# sourceMappingURL=babylon.glTFData.js.map
  955. var BABYLON;
  956. (function (BABYLON) {
  957. /**
  958. * Utility methods for working with glTF material conversion properties
  959. */
  960. var _GLTFMaterial = /** @class */ (function () {
  961. function _GLTFMaterial() {
  962. }
  963. /**
  964. * Converts Specular Glossiness to Metallic Roughness
  965. * @param babylonSpecularGlossiness - Babylon specular glossiness parameters
  966. * @returns - Babylon metallic roughness values
  967. */
  968. _GLTFMaterial.ConvertToMetallicRoughness = function (babylonSpecularGlossiness) {
  969. var diffuse = babylonSpecularGlossiness.diffuse;
  970. var opacity = babylonSpecularGlossiness.opacity;
  971. var specular = babylonSpecularGlossiness.specular;
  972. var glossiness = babylonSpecularGlossiness.glossiness;
  973. var oneMinusSpecularStrength = 1 - Math.max(specular.r, Math.max(specular.g, specular.b));
  974. var diffusePerceivedBrightness = _GLTFMaterial.PerceivedBrightness(diffuse);
  975. var specularPerceivedBrightness = _GLTFMaterial.PerceivedBrightness(specular);
  976. var metallic = _GLTFMaterial.SolveMetallic(diffusePerceivedBrightness, specularPerceivedBrightness, oneMinusSpecularStrength);
  977. var diffuseScaleFactor = oneMinusSpecularStrength / (1 - this.dielectricSpecular.r) / Math.max(1 - metallic, this.epsilon);
  978. var baseColorFromDiffuse = diffuse.scale(diffuseScaleFactor);
  979. var baseColorFromSpecular = specular.subtract(this.dielectricSpecular.scale(1 - metallic)).scale(1 / Math.max(metallic, this.epsilon));
  980. var lerpColor = BABYLON.Color3.Lerp(baseColorFromDiffuse, baseColorFromSpecular, metallic * metallic);
  981. var baseColor = new BABYLON.Color3();
  982. lerpColor.clampToRef(0, 1, baseColor);
  983. var babylonMetallicRoughness = {
  984. baseColor: baseColor,
  985. opacity: opacity,
  986. metallic: metallic,
  987. roughness: 1.0 - glossiness
  988. };
  989. return babylonMetallicRoughness;
  990. };
  991. /**
  992. * Returns the perceived brightness value based on the provided color
  993. * @param color - color used in calculating the perceived brightness
  994. * @returns - perceived brightness value
  995. */
  996. _GLTFMaterial.PerceivedBrightness = function (color) {
  997. return Math.sqrt(0.299 * color.r * color.r + 0.587 * color.g * color.g + 0.114 * color.b * color.b);
  998. };
  999. /**
  1000. * Computes the metallic factor
  1001. * @param diffuse - diffused value
  1002. * @param specular - specular value
  1003. * @param oneMinusSpecularStrength - one minus the specular strength
  1004. * @returns - metallic value
  1005. */
  1006. _GLTFMaterial.SolveMetallic = function (diffuse, specular, oneMinusSpecularStrength) {
  1007. if (specular < this.dielectricSpecular.r) {
  1008. return 0;
  1009. }
  1010. var a = this.dielectricSpecular.r;
  1011. var b = diffuse * oneMinusSpecularStrength / (1.0 - this.dielectricSpecular.r) + specular - 2.0 * this.dielectricSpecular.r;
  1012. var c = this.dielectricSpecular.r - specular;
  1013. var D = b * b - 4.0 * a * c;
  1014. return BABYLON.Scalar.Clamp((-b + Math.sqrt(D)) / (2.0 * a));
  1015. };
  1016. /**
  1017. * Gets the glTF alpha mode from the Babylon Material
  1018. * @param babylonMaterial - Babylon Material
  1019. * @returns - The Babylon alpha mode value
  1020. */
  1021. _GLTFMaterial.GetAlphaMode = function (babylonMaterial) {
  1022. if (babylonMaterial instanceof BABYLON.StandardMaterial) {
  1023. var babylonStandardMaterial = babylonMaterial;
  1024. if ((babylonStandardMaterial.alpha != 1.0) ||
  1025. (babylonStandardMaterial.diffuseTexture != null && babylonStandardMaterial.diffuseTexture.hasAlpha) ||
  1026. (babylonStandardMaterial.opacityTexture != null)) {
  1027. return BABYLON._EGLTFAlphaModeEnum.BLEND;
  1028. }
  1029. else {
  1030. return BABYLON._EGLTFAlphaModeEnum.OPAQUE;
  1031. }
  1032. }
  1033. else if (babylonMaterial instanceof BABYLON.PBRMetallicRoughnessMaterial) {
  1034. var babylonPBRMetallicRoughness = babylonMaterial;
  1035. switch (babylonPBRMetallicRoughness.transparencyMode) {
  1036. case BABYLON.PBRMaterial.PBRMATERIAL_OPAQUE: {
  1037. return BABYLON._EGLTFAlphaModeEnum.OPAQUE;
  1038. }
  1039. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHABLEND: {
  1040. return BABYLON._EGLTFAlphaModeEnum.BLEND;
  1041. }
  1042. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATEST: {
  1043. return BABYLON._EGLTFAlphaModeEnum.MASK;
  1044. }
  1045. case BABYLON.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND: {
  1046. console.warn("GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead.");
  1047. return BABYLON._EGLTFAlphaModeEnum.BLEND;
  1048. }
  1049. default: {
  1050. throw new Error("Unsupported alpha mode " + babylonPBRMetallicRoughness.transparencyMode);
  1051. }
  1052. }
  1053. }
  1054. else {
  1055. throw new Error("Unsupported Babylon material type");
  1056. }
  1057. };
  1058. _GLTFMaterial.dielectricSpecular = new BABYLON.Color3(0.04, 0.04, 0.04);
  1059. _GLTFMaterial.epsilon = 1e-6;
  1060. return _GLTFMaterial;
  1061. }());
  1062. BABYLON._GLTFMaterial = _GLTFMaterial;
  1063. })(BABYLON || (BABYLON = {}));
  1064. //# sourceMappingURL=babylon.glTFMaterial.js.map