babylonjs.serializers.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. var globalObject = (typeof global !== 'undefined') ? global : ((typeof window !== 'undefined') ? window : this);
  2. var babylonDependency = (globalObject && globalObject.BABYLON) || BABYLON || (typeof require !== 'undefined' && require("babylonjs"));
  3. var BABYLON = babylonDependency;
  4. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  5. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  6. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  7. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  8. return c > 3 && r && Object.defineProperty(target, key, r), r;
  9. };
  10. var __extends = (this && this.__extends) || (function () {
  11. var extendStatics = Object.setPrototypeOf ||
  12. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  13. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  14. return function (d, b) {
  15. extendStatics(d, b);
  16. function __() { this.constructor = d; }
  17. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  18. };
  19. })();
  20. var BABYLON;
  21. (function (BABYLON) {
  22. var OBJExport = /** @class */ (function () {
  23. function OBJExport() {
  24. }
  25. //Exports the geometrys of a Mesh array in .OBJ file format (text)
  26. OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
  27. var output = [];
  28. var v = 1;
  29. if (materials) {
  30. if (!matlibname) {
  31. matlibname = 'mat';
  32. }
  33. output.push("mtllib " + matlibname + ".mtl");
  34. }
  35. for (var j = 0; j < mesh.length; j++) {
  36. output.push("g object" + j);
  37. output.push("o object_" + j);
  38. //Uses the position of the item in the scene, to the file (this back to normal in the end)
  39. var lastMatrix = null;
  40. if (globalposition) {
  41. var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
  42. lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
  43. mesh[j].bakeTransformIntoVertices(newMatrix);
  44. }
  45. //TODO: submeshes (groups)
  46. //TODO: smoothing groups (s 1, s off);
  47. if (materials) {
  48. var mat = mesh[j].material;
  49. if (mat) {
  50. output.push("usemtl " + mat.id);
  51. }
  52. }
  53. var g = mesh[j].geometry;
  54. if (!g) {
  55. continue;
  56. }
  57. var trunkVerts = g.getVerticesData('position');
  58. var trunkNormals = g.getVerticesData('normal');
  59. var trunkUV = g.getVerticesData('uv');
  60. var trunkFaces = g.getIndices();
  61. var curV = 0;
  62. if (!trunkVerts || !trunkNormals || !trunkUV || !trunkFaces) {
  63. continue;
  64. }
  65. for (var i = 0; i < trunkVerts.length; i += 3) {
  66. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  67. curV++;
  68. }
  69. for (i = 0; i < trunkNormals.length; i += 3) {
  70. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  71. }
  72. for (i = 0; i < trunkUV.length; i += 2) {
  73. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  74. }
  75. for (i = 0; i < trunkFaces.length; i += 3) {
  76. output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
  77. " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
  78. " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
  79. }
  80. //back de previous matrix, to not change the original mesh in the scene
  81. if (globalposition && lastMatrix) {
  82. mesh[j].bakeTransformIntoVertices(lastMatrix);
  83. }
  84. v += curV;
  85. }
  86. var text = output.join("\n");
  87. return (text);
  88. };
  89. //Exports the material(s) of a mesh in .MTL file format (text)
  90. //TODO: Export the materials of mesh array
  91. OBJExport.MTL = function (mesh) {
  92. var output = [];
  93. var m = mesh.material;
  94. output.push("newmtl mat1");
  95. output.push(" Ns " + m.specularPower.toFixed(4));
  96. output.push(" Ni 1.5000");
  97. output.push(" d " + m.alpha.toFixed(4));
  98. output.push(" Tr 0.0000");
  99. output.push(" Tf 1.0000 1.0000 1.0000");
  100. output.push(" illum 2");
  101. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  102. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  103. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  104. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  105. //TODO: uv scale, offset, wrap
  106. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  107. var uvscale = "";
  108. if (m.ambientTexture) {
  109. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  110. }
  111. if (m.diffuseTexture) {
  112. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  113. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  114. }
  115. if (m.specularTexture) {
  116. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  117. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  118. if (m.useGlossinessFromSpecularMapAlpha) {
  119. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  120. }
  121. */
  122. }
  123. /* TODO: emissive texture not in .MAT format (???)
  124. if (m.emissiveTexture) {
  125. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  126. }
  127. */
  128. if (m.bumpTexture) {
  129. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  130. }
  131. if (m.opacityTexture) {
  132. output.push(" map_d " + uvscale + m.opacityTexture.name);
  133. }
  134. var text = output.join("\n");
  135. return (text);
  136. };
  137. return OBJExport;
  138. }());
  139. BABYLON.OBJExport = OBJExport;
  140. })(BABYLON || (BABYLON = {}));
  141. //# sourceMappingURL=babylon.objSerializer.js.map
  142. var BABYLON;
  143. (function (BABYLON) {
  144. var GLTF2Export = /** @class */ (function () {
  145. function GLTF2Export() {
  146. }
  147. /**
  148. * Exports the geometry of a Mesh array in .gltf file format.
  149. * If glb is set to true, exports as .glb.
  150. * @param meshes
  151. * @param materials
  152. *
  153. * @returns {[fileName: string]: string | Blob} Returns an object with a .gltf, .glb and associates textures
  154. * as keys and their data and paths as values.
  155. */
  156. GLTF2Export.GLTF = function (scene, filename) {
  157. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  158. var gltfGenerator = new BABYLON._GLTF2Exporter(scene);
  159. return gltfGenerator._generateGLTF(glTFPrefix);
  160. };
  161. /**
  162. *
  163. * @param meshes
  164. * @param filename
  165. *
  166. * @returns {[fileName: string]: string | Blob} Returns an object with a .glb filename as key and data as value
  167. */
  168. GLTF2Export.GLB = function (scene, filename) {
  169. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  170. var gltfGenerator = new BABYLON._GLTF2Exporter(scene);
  171. return gltfGenerator._generateGLB(glTFPrefix);
  172. };
  173. return GLTF2Export;
  174. }());
  175. BABYLON.GLTF2Export = GLTF2Export;
  176. })(BABYLON || (BABYLON = {}));
  177. //# sourceMappingURL=babylon.glTFSerializer.js.map
  178. var BABYLON;
  179. (function (BABYLON) {
  180. var _GLTF2Exporter = /** @class */ (function () {
  181. function _GLTF2Exporter(babylonScene) {
  182. this.asset = { generator: "BabylonJS", version: "2.0" };
  183. this.babylonScene = babylonScene;
  184. this.bufferViews = new Array();
  185. this.accessors = new Array();
  186. this.meshes = new Array();
  187. this.scenes = new Array();
  188. this.nodes = new Array();
  189. var totalByteLength = 0;
  190. totalByteLength = this.createScene(this.babylonScene, totalByteLength);
  191. this.totalByteLength = totalByteLength;
  192. }
  193. /**
  194. * Creates a buffer view based on teh supplied arguments
  195. * @param bufferIndex
  196. * @param byteOffset
  197. * @param byteLength
  198. *
  199. * @returns {_IGLTFBufferView}
  200. */
  201. _GLTF2Exporter.prototype.createBufferView = function (bufferIndex, byteOffset, byteLength) {
  202. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  203. if (byteOffset > 0) {
  204. bufferview.byteOffset = byteOffset;
  205. }
  206. return bufferview;
  207. };
  208. /**
  209. * Creates an accessor based on the supplied arguments
  210. * @param bufferviewIndex
  211. * @param name
  212. * @param type
  213. * @param componentType
  214. * @param count
  215. * @param min
  216. * @param max
  217. *
  218. * @returns {_IGLTFAccessor}
  219. */
  220. _GLTF2Exporter.prototype.createAccessor = function (bufferviewIndex, name, type, componentType, count, min, max) {
  221. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  222. if (min) {
  223. accessor.min = min;
  224. }
  225. if (max) {
  226. accessor.max = max;
  227. }
  228. return accessor;
  229. };
  230. /**
  231. * Calculates the minimum and maximum values of an array of floats, based on stride
  232. * @param buff
  233. * @param vertexStart
  234. * @param vertexCount
  235. * @param arrayOffset
  236. * @param stride
  237. *
  238. * @returns {min: number[], max: number[]} min number array and max number array
  239. */
  240. _GLTF2Exporter.prototype.calculateMinMax = function (buff, vertexStart, vertexCount, arrayOffset, stride) {
  241. var min = [Infinity, Infinity, Infinity];
  242. var max = [-Infinity, -Infinity, -Infinity];
  243. var end = vertexStart + vertexCount;
  244. if (vertexCount > 0) {
  245. for (var i = vertexStart; i < end; ++i) {
  246. var index = stride * i;
  247. for (var j = 0; j < stride; ++j) {
  248. if (buff[index] < min[j]) {
  249. min[j] = buff[index];
  250. }
  251. if (buff[index] > max[j]) {
  252. max[j] = buff[index];
  253. }
  254. ++index;
  255. }
  256. }
  257. }
  258. return { min: min, max: max };
  259. };
  260. /**
  261. * Write mesh attribute data to buffer.
  262. * Returns the bytelength of the data.
  263. * @param vertexBufferType
  264. * @param submesh
  265. * @param meshAttributeArray
  266. * @param strideSize
  267. * @param byteOffset
  268. * @param dataBuffer
  269. * @param useRightHandedSystem
  270. *
  271. * @returns {number} byte length
  272. */
  273. _GLTF2Exporter.prototype.writeAttributeData = function (vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem) {
  274. var byteOff = byteOffset;
  275. var end = submesh.verticesStart + submesh.verticesCount;
  276. var byteLength = 0;
  277. switch (vertexBufferType) {
  278. case BABYLON.VertexBuffer.PositionKind: {
  279. for (var k = submesh.verticesStart; k < end; ++k) {
  280. var index = k * strideSize;
  281. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  282. byteOff += 4;
  283. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  284. byteOff += 4;
  285. if (useRightHandedSystem) {
  286. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  287. }
  288. else {
  289. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  290. }
  291. byteOff += 4;
  292. }
  293. byteLength = submesh.verticesCount * 12;
  294. break;
  295. }
  296. case BABYLON.VertexBuffer.NormalKind: {
  297. for (var k = submesh.verticesStart; k < end; ++k) {
  298. var index = k * strideSize;
  299. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  300. byteOff += 4;
  301. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  302. byteOff += 4;
  303. if (useRightHandedSystem) {
  304. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  305. }
  306. else {
  307. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  308. }
  309. byteOff += 4;
  310. }
  311. byteLength = submesh.verticesCount * 12;
  312. break;
  313. }
  314. case BABYLON.VertexBuffer.TangentKind: {
  315. for (var k = submesh.indexStart; k < end; ++k) {
  316. var index = k * strideSize;
  317. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  318. byteOff += 4;
  319. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  320. byteOff += 4;
  321. if (useRightHandedSystem) {
  322. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  323. }
  324. else {
  325. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  326. }
  327. byteOff += 4;
  328. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  329. byteOff += 4;
  330. }
  331. byteLength = submesh.verticesCount * 16;
  332. break;
  333. }
  334. case BABYLON.VertexBuffer.ColorKind: {
  335. for (var k = submesh.verticesStart; k < end; ++k) {
  336. var index = k * strideSize;
  337. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  338. byteOff += 4;
  339. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  340. byteOff += 4;
  341. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  342. byteOff += 4;
  343. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  344. byteOff += 4;
  345. }
  346. byteLength = submesh.verticesCount * 16;
  347. break;
  348. }
  349. case BABYLON.VertexBuffer.UVKind: {
  350. for (var k = submesh.verticesStart; k < end; ++k) {
  351. var index = k * strideSize;
  352. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  353. byteOff += 4;
  354. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  355. byteOff += 4;
  356. }
  357. byteLength = submesh.verticesCount * 8;
  358. break;
  359. }
  360. case BABYLON.VertexBuffer.UV2Kind: {
  361. for (var k = submesh.verticesStart; k < end; ++k) {
  362. var index = k * strideSize;
  363. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  364. byteOff += 4;
  365. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  366. byteOff += 4;
  367. }
  368. byteLength = submesh.verticesCount * 8;
  369. break;
  370. }
  371. default: {
  372. throw new Error("Unsupported vertex buffer type: " + vertexBufferType);
  373. }
  374. }
  375. return byteLength;
  376. };
  377. /**
  378. * Generates glTF json data
  379. * @param glb
  380. * @param glTFPrefix
  381. * @param prettyPrint
  382. *
  383. * @returns {string} json data as string
  384. */
  385. _GLTF2Exporter.prototype.generateJSON = function (glb, glTFPrefix, prettyPrint) {
  386. var buffer = { byteLength: this.totalByteLength };
  387. var glTF = {
  388. buffers: [buffer],
  389. asset: this.asset,
  390. meshes: this.meshes,
  391. scenes: this.scenes,
  392. nodes: this.nodes,
  393. bufferViews: this.bufferViews,
  394. accessors: this.accessors
  395. };
  396. if (this.scenes.length > 0) {
  397. glTF.scene = 0;
  398. }
  399. if (!glb) {
  400. buffer.uri = glTFPrefix + ".bin";
  401. }
  402. var jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
  403. return jsonText;
  404. };
  405. /**
  406. * Generates data for .gltf and .bin files based on the glTF prefix string
  407. * @param glTFPrefix
  408. *
  409. * @returns {[x: string]: string | Blob} object with glTF json tex filename
  410. * and binary file name as keys and their data as values
  411. */
  412. _GLTF2Exporter.prototype._generateGLTF = function (glTFPrefix) {
  413. var jsonText = this.generateJSON(false, glTFPrefix, true);
  414. var binaryBuffer = this.generateBinary();
  415. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  416. var glTFFileName = glTFPrefix + '.gltf';
  417. var glTFBinFile = glTFPrefix + '.bin';
  418. var container = new BABYLON._GLTFData();
  419. container._glTFFiles[glTFFileName] = jsonText;
  420. container._glTFFiles[glTFBinFile] = bin;
  421. return container;
  422. };
  423. /**
  424. * Creates a binary buffer for glTF
  425. *
  426. * @returns {ArrayBuffer}
  427. */
  428. _GLTF2Exporter.prototype.generateBinary = function () {
  429. var byteOffset = 0;
  430. var binaryBuffer = new ArrayBuffer(this.totalByteLength);
  431. var dataBuffer = new DataView(binaryBuffer);
  432. byteOffset = this.createScene(this.babylonScene, byteOffset, dataBuffer);
  433. return binaryBuffer;
  434. };
  435. /**
  436. * Generates a glb file from the json and binary data.
  437. * Returns an object with the glb file name as the key and data as the value.
  438. * @param jsonText
  439. * @param binaryBuffer
  440. * @param glTFPrefix
  441. *
  442. * @returns {[glbFileName: string]: Blob} object with glb filename as key and data as value
  443. */
  444. _GLTF2Exporter.prototype._generateGLB = function (glTFPrefix) {
  445. var jsonText = this.generateJSON(true);
  446. var binaryBuffer = this.generateBinary();
  447. var glbFileName = glTFPrefix + '.glb';
  448. var headerLength = 12;
  449. var chunkLengthPrefix = 8;
  450. var jsonLength = jsonText.length;
  451. var jsonRemainder = jsonLength % 4;
  452. var binRemainder = binaryBuffer.byteLength % 4;
  453. var jsonPadding = jsonRemainder === 0 ? jsonRemainder : 4 - jsonRemainder;
  454. var binPadding = binRemainder === 0 ? binRemainder : 4 - binRemainder;
  455. var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding;
  456. //header
  457. var headerBuffer = new ArrayBuffer(headerLength);
  458. var headerBufferView = new DataView(headerBuffer);
  459. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  460. headerBufferView.setUint32(4, 2, true); // version
  461. headerBufferView.setUint32(8, byteLength, true); // total bytes in file
  462. //json chunk
  463. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  464. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  465. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  466. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  467. //json chunk bytes
  468. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  469. for (var i = 0; i < jsonLength; ++i) {
  470. jsonData[i] = jsonText.charCodeAt(i);
  471. }
  472. //json padding
  473. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  474. for (var i = 0; i < jsonPadding; ++i) {
  475. jsonPaddingView[i] = 0x20;
  476. }
  477. //binary chunk
  478. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  479. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  480. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength, true);
  481. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  482. // binary padding
  483. var binPaddingBuffer = new ArrayBuffer(binPadding);
  484. var binPaddingView = new Uint8Array(binPaddingBuffer);
  485. for (var i = 0; i < binPadding; ++i) {
  486. binPaddingView[i] = 0;
  487. }
  488. // binary data
  489. var glbFile = new Blob([headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer, binPaddingBuffer], { type: 'application/octet-stream' });
  490. var container = new BABYLON._GLTFData();
  491. container._glTFFiles[glbFileName] = glbFile;
  492. return container;
  493. };
  494. /**
  495. * Sets the TRS for each node
  496. * @param node
  497. * @param babylonMesh
  498. * @param useRightHandedSystem
  499. */
  500. _GLTF2Exporter.prototype.setNodeTransformation = function (node, babylonMesh, useRightHandedSystem) {
  501. if (!(babylonMesh.position.x === 0 && babylonMesh.position.y === 0 && babylonMesh.position.z === 0)) {
  502. if (useRightHandedSystem) {
  503. node.translation = babylonMesh.position.asArray();
  504. }
  505. else {
  506. node.translation = [babylonMesh.position.x, babylonMesh.position.y, -babylonMesh.position.z];
  507. }
  508. }
  509. if (!(babylonMesh.scaling.x === 1 && babylonMesh.scaling.y === 1 && babylonMesh.scaling.z === 1)) {
  510. if (useRightHandedSystem) {
  511. node.scale = babylonMesh.scaling.asArray();
  512. }
  513. else {
  514. node.scale = [babylonMesh.scaling.x, babylonMesh.scaling.y, -babylonMesh.scaling.z];
  515. }
  516. }
  517. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  518. if (babylonMesh.rotationQuaternion) {
  519. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  520. }
  521. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  522. if (useRightHandedSystem) {
  523. node.rotation = rotationQuaternion.asArray();
  524. }
  525. else {
  526. node.rotation = [-rotationQuaternion.x, -rotationQuaternion.y, rotationQuaternion.z, rotationQuaternion.w];
  527. }
  528. }
  529. };
  530. /**
  531. * Sets data for the primitive attributes of each submesh
  532. * @param mesh
  533. * @param babylonMesh
  534. * @param byteOffset
  535. * @param useRightHandedSystem
  536. * @param dataBuffer
  537. *
  538. * @returns {number} bytelength of the primitive attributes plus the passed in byteOffset
  539. */
  540. _GLTF2Exporter.prototype.setPrimitiveAttributes = function (mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer) {
  541. // go through all mesh primitives (submeshes)
  542. for (var j = 0; j < babylonMesh.subMeshes.length; ++j) {
  543. var bufferMesh = null;
  544. var submesh = babylonMesh.subMeshes[j];
  545. var meshPrimitive = { attributes: {} };
  546. if (babylonMesh instanceof BABYLON.Mesh) {
  547. bufferMesh = babylonMesh;
  548. }
  549. else if (babylonMesh instanceof BABYLON.InstancedMesh) {
  550. bufferMesh = babylonMesh.sourceMesh;
  551. }
  552. // Loop through each attribute of the submesh (mesh primitive)
  553. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  554. var positionVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.PositionKind);
  555. var positionVertexBufferOffset = positionVertexBuffer.getOffset();
  556. var positions = positionVertexBuffer.getData();
  557. var positionStrideSize = positionVertexBuffer.getStrideSize();
  558. if (dataBuffer) {
  559. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.PositionKind, submesh, positions, positionStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  560. }
  561. else {
  562. // Create bufferview
  563. var byteLength = submesh.verticesCount * 12;
  564. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  565. byteOffset += byteLength;
  566. this.bufferViews.push(bufferview);
  567. // Create accessor
  568. var result = this.calculateMinMax(positions, submesh.verticesStart, submesh.verticesCount, positionVertexBufferOffset, positionStrideSize);
  569. var accessor = this.createAccessor(this.bufferViews.length - 1, "Position", "VEC3", 5126, submesh.verticesCount, result.min, result.max);
  570. this.accessors.push(accessor);
  571. meshPrimitive.attributes.POSITION = this.accessors.length - 1;
  572. }
  573. }
  574. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  575. var normalVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.NormalKind);
  576. var normals = normalVertexBuffer.getData();
  577. var normalStrideSize = normalVertexBuffer.getStrideSize();
  578. if (dataBuffer) {
  579. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.NormalKind, submesh, normals, normalStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  580. }
  581. else {
  582. // Create bufferview
  583. var byteLength = submesh.verticesCount * 12;
  584. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  585. byteOffset += byteLength;
  586. this.bufferViews.push(bufferview);
  587. // Create accessor
  588. var accessor = this.createAccessor(this.bufferViews.length - 1, "Normal", "VEC3", 5126, submesh.verticesCount);
  589. this.accessors.push(accessor);
  590. meshPrimitive.attributes.NORMAL = this.accessors.length - 1;
  591. }
  592. }
  593. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  594. var tangentVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.TangentKind);
  595. var tangents = tangentVertexBuffer.getData();
  596. var tangentStrideSize = tangentVertexBuffer.getStrideSize();
  597. if (dataBuffer) {
  598. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.TangentKind, submesh, tangents, tangentStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  599. }
  600. else {
  601. // Create bufferview
  602. var byteLength = submesh.verticesCount * 16;
  603. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  604. byteOffset += byteLength;
  605. this.bufferViews.push(bufferview);
  606. // Create accessor
  607. var accessor = this.createAccessor(this.bufferViews.length - 1, "Tangent", "VEC4", 5126, submesh.verticesCount);
  608. this.accessors.push(accessor);
  609. meshPrimitive.attributes.TANGENT = this.accessors.length - 1;
  610. }
  611. }
  612. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  613. var colorVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.ColorKind);
  614. var colors = colorVertexBuffer.getData();
  615. var colorStrideSize = colorVertexBuffer.getStrideSize();
  616. if (dataBuffer) {
  617. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.ColorKind, submesh, colors, colorStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  618. }
  619. else {
  620. // Create bufferview
  621. var byteLength = submesh.verticesCount * 16;
  622. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  623. byteOffset += byteLength;
  624. this.bufferViews.push(bufferview);
  625. // Create accessor
  626. var accessor = this.createAccessor(this.bufferViews.length - 1, "Color", "VEC4", 5126, submesh.verticesCount);
  627. this.accessors.push(accessor);
  628. meshPrimitive.attributes.COLOR_0 = this.accessors.length - 1;
  629. }
  630. }
  631. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  632. var texCoord0VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UVKind);
  633. var texCoords0 = texCoord0VertexBuffer.getData();
  634. var texCoord0StrideSize = texCoord0VertexBuffer.getStrideSize();
  635. if (dataBuffer) {
  636. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UVKind, submesh, texCoords0, texCoord0StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  637. }
  638. else {
  639. // Create bufferview
  640. var byteLength = submesh.verticesCount * 8;
  641. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  642. byteOffset += byteLength;
  643. this.bufferViews.push(bufferview);
  644. // Create accessor
  645. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  646. this.accessors.push(accessor);
  647. meshPrimitive.attributes.TEXCOORD_0 = this.accessors.length - 1;
  648. }
  649. }
  650. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  651. var texCoord1VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UV2Kind);
  652. var texCoords1 = texCoord1VertexBuffer.getData();
  653. var texCoord1StrideSize = texCoord1VertexBuffer.getStrideSize();
  654. if (dataBuffer) {
  655. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UV2Kind, submesh, texCoords1, texCoord1StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  656. }
  657. else {
  658. // Create bufferview
  659. var byteLength = submesh.verticesCount * 8;
  660. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  661. byteOffset += byteLength;
  662. this.bufferViews.push(bufferview);
  663. // Create accessor
  664. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  665. this.accessors.push(accessor);
  666. meshPrimitive.attributes.TEXCOORD_1 = this.accessors.length - 1;
  667. }
  668. }
  669. if (bufferMesh.getTotalIndices() > 0) {
  670. if (dataBuffer) {
  671. var indices = bufferMesh.getIndices();
  672. var start = submesh.indexStart;
  673. var end = submesh.indexCount + start;
  674. var byteOff = byteOffset;
  675. for (var k = start; k < end; k = k + 3) {
  676. dataBuffer.setUint32(byteOff, indices[k], true);
  677. byteOff += 4;
  678. dataBuffer.setUint32(byteOff, indices[k + 1], true);
  679. byteOff += 4;
  680. dataBuffer.setUint32(byteOff, indices[k + 2], true);
  681. byteOff += 4;
  682. }
  683. var byteLength = submesh.indexCount * 4;
  684. byteOffset += byteLength;
  685. }
  686. else {
  687. // Create bufferview
  688. var indicesCount = submesh.indexCount;
  689. var byteLength = indicesCount * 4;
  690. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  691. byteOffset += byteLength;
  692. this.bufferViews.push(bufferview);
  693. // Create accessor
  694. var accessor = this.createAccessor(this.bufferViews.length - 1, "Indices", "SCALAR", 5125, indicesCount);
  695. this.accessors.push(accessor);
  696. meshPrimitive.indices = this.accessors.length - 1;
  697. }
  698. }
  699. if (bufferMesh.material) {
  700. //TODO: Implement Material
  701. }
  702. mesh.primitives.push(meshPrimitive);
  703. }
  704. return byteOffset;
  705. };
  706. /**
  707. * Creates a glTF scene based on the array of meshes.
  708. * Returns the the total byte offset.
  709. * @param gltf
  710. * @param byteOffset
  711. * @param buffer
  712. * @param dataBuffer
  713. *
  714. * @returns {number} bytelength + byteoffset
  715. */
  716. _GLTF2Exporter.prototype.createScene = function (babylonScene, byteOffset, dataBuffer) {
  717. if (babylonScene.meshes.length > 0) {
  718. var babylonMeshes = babylonScene.meshes;
  719. var scene = { nodes: new Array() };
  720. for (var i = 0; i < babylonMeshes.length; ++i) {
  721. // create node to hold translation/rotation/scale and the mesh
  722. var node = { mesh: -1 };
  723. var babylonMesh = babylonMeshes[i];
  724. var useRightHandedSystem = babylonMesh.getScene().useRightHandedSystem;
  725. // Set transformation
  726. this.setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  727. // create mesh
  728. var mesh = { primitives: new Array() };
  729. mesh.primitives = [];
  730. byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  731. // go through all mesh primitives (submeshes)
  732. this.meshes.push(mesh);
  733. node.mesh = this.meshes.length - 1;
  734. if (babylonMesh.name) {
  735. node.name = babylonMesh.name;
  736. }
  737. this.nodes.push(node);
  738. scene.nodes.push(this.nodes.length - 1);
  739. }
  740. this.scenes.push(scene);
  741. }
  742. return byteOffset;
  743. };
  744. return _GLTF2Exporter;
  745. }());
  746. BABYLON._GLTF2Exporter = _GLTF2Exporter;
  747. })(BABYLON || (BABYLON = {}));
  748. //# sourceMappingURL=babylon.glTFExporter.js.map
  749. var BABYLON;
  750. (function (BABYLON) {
  751. /**
  752. * Class for holding and downloading glTF file data
  753. */
  754. var _GLTFData = /** @class */ (function () {
  755. function _GLTFData() {
  756. this._glTFFiles = {};
  757. }
  758. /**
  759. * Downloads glTF data.
  760. */
  761. _GLTFData.prototype.downloadFiles = function () {
  762. /**
  763. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  764. * @param str
  765. * @param suffix
  766. *
  767. * @returns {boolean} indicating whether the suffix matches or not
  768. */
  769. function endsWith(str, suffix) {
  770. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  771. }
  772. for (var key in this._glTFFiles) {
  773. var link = document.createElement('a');
  774. document.body.appendChild(link);
  775. link.setAttribute("type", "hidden");
  776. link.download = key;
  777. var blob = this._glTFFiles[key];
  778. var mimeType = void 0;
  779. if (endsWith(key, ".glb")) {
  780. mimeType = { type: "model/gltf-binary" };
  781. }
  782. else if (endsWith(key, ".bin")) {
  783. mimeType = { type: "application/octet-stream" };
  784. }
  785. else if (endsWith(key, ".gltf")) {
  786. mimeType = { type: "model/gltf+json" };
  787. }
  788. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  789. link.click();
  790. }
  791. };
  792. return _GLTFData;
  793. }());
  794. BABYLON._GLTFData = _GLTFData;
  795. })(BABYLON || (BABYLON = {}));
  796. //# sourceMappingURL=babylon.glTFData.js.map
  797. (function universalModuleDefinition(root, factory) {
  798. var f = factory();
  799. if (root && root["BABYLON"]) {
  800. return;
  801. }
  802. if(typeof exports === 'object' && typeof module === 'object')
  803. module.exports = f;
  804. else if(typeof define === 'function' && define.amd)
  805. define(["BJSSerializers"], factory);
  806. else if(typeof exports === 'object')
  807. exports["BJSSerializers"] = f;
  808. else {
  809. root["BABYLON"] = f;
  810. }
  811. })(this, function() {
  812. return BABYLON;
  813. });