babylon.glTF2Serializer.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var GLTF2Export = /** @class */ (function () {
  5. function GLTF2Export() {
  6. }
  7. /**
  8. * Exports the geometry of a Mesh array in .gltf file format.
  9. * If glb is set to true, exports as .glb.
  10. * @param meshes
  11. * @param materials
  12. *
  13. * @returns {[fileName: string]: string | Blob} Returns an object with a .gltf, .glb and associates textures
  14. * as keys and their data and paths as values.
  15. */
  16. GLTF2Export.GLTF = function (scene, filename) {
  17. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  18. var gltfGenerator = new BABYLON._GLTF2Exporter(scene);
  19. return gltfGenerator._generateGLTF(glTFPrefix);
  20. };
  21. /**
  22. *
  23. * @param meshes
  24. * @param filename
  25. *
  26. * @returns {[fileName: string]: string | Blob} Returns an object with a .glb filename as key and data as value
  27. */
  28. GLTF2Export.GLB = function (scene, filename) {
  29. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  30. var gltfGenerator = new BABYLON._GLTF2Exporter(scene);
  31. return gltfGenerator._generateGLB(glTFPrefix);
  32. };
  33. return GLTF2Export;
  34. }());
  35. BABYLON.GLTF2Export = GLTF2Export;
  36. })(BABYLON || (BABYLON = {}));
  37. //# sourceMappingURL=babylon.glTFSerializer.js.map
  38. var BABYLON;
  39. (function (BABYLON) {
  40. var _GLTF2Exporter = /** @class */ (function () {
  41. function _GLTF2Exporter(babylonScene) {
  42. this.asset = { generator: "BabylonJS", version: "2.0" };
  43. this.babylonScene = babylonScene;
  44. this.bufferViews = new Array();
  45. this.accessors = new Array();
  46. this.meshes = new Array();
  47. this.scenes = new Array();
  48. this.nodes = new Array();
  49. var totalByteLength = 0;
  50. totalByteLength = this.createScene(this.babylonScene, totalByteLength);
  51. this.totalByteLength = totalByteLength;
  52. }
  53. /**
  54. * Creates a buffer view based on teh supplied arguments
  55. * @param bufferIndex
  56. * @param byteOffset
  57. * @param byteLength
  58. *
  59. * @returns {_IGLTFBufferView}
  60. */
  61. _GLTF2Exporter.prototype.createBufferView = function (bufferIndex, byteOffset, byteLength) {
  62. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  63. if (byteOffset > 0) {
  64. bufferview.byteOffset = byteOffset;
  65. }
  66. return bufferview;
  67. };
  68. /**
  69. * Creates an accessor based on the supplied arguments
  70. * @param bufferviewIndex
  71. * @param name
  72. * @param type
  73. * @param componentType
  74. * @param count
  75. * @param min
  76. * @param max
  77. *
  78. * @returns {_IGLTFAccessor}
  79. */
  80. _GLTF2Exporter.prototype.createAccessor = function (bufferviewIndex, name, type, componentType, count, min, max) {
  81. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  82. if (min) {
  83. accessor.min = min;
  84. }
  85. if (max) {
  86. accessor.max = max;
  87. }
  88. return accessor;
  89. };
  90. /**
  91. * Calculates the minimum and maximum values of an array of floats, based on stride
  92. * @param buff
  93. * @param vertexStart
  94. * @param vertexCount
  95. * @param arrayOffset
  96. * @param stride
  97. *
  98. * @returns {min: number[], max: number[]} min number array and max number array
  99. */
  100. _GLTF2Exporter.prototype.calculateMinMax = function (buff, vertexStart, vertexCount, arrayOffset, stride) {
  101. var min = [Infinity, Infinity, Infinity];
  102. var max = [-Infinity, -Infinity, -Infinity];
  103. var end = vertexStart + vertexCount;
  104. if (vertexCount > 0) {
  105. for (var i = vertexStart; i < end; ++i) {
  106. var index = stride * i;
  107. for (var j = 0; j < stride; ++j) {
  108. if (buff[index] < min[j]) {
  109. min[j] = buff[index];
  110. }
  111. if (buff[index] > max[j]) {
  112. max[j] = buff[index];
  113. }
  114. ++index;
  115. }
  116. }
  117. }
  118. return { min: min, max: max };
  119. };
  120. /**
  121. * Write mesh attribute data to buffer.
  122. * Returns the bytelength of the data.
  123. * @param vertexBufferType
  124. * @param submesh
  125. * @param meshAttributeArray
  126. * @param strideSize
  127. * @param byteOffset
  128. * @param dataBuffer
  129. * @param useRightHandedSystem
  130. *
  131. * @returns {number} byte length
  132. */
  133. _GLTF2Exporter.prototype.writeAttributeData = function (vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem) {
  134. var byteOff = byteOffset;
  135. var end = submesh.verticesStart + submesh.verticesCount;
  136. var byteLength = 0;
  137. switch (vertexBufferType) {
  138. case BABYLON.VertexBuffer.PositionKind: {
  139. for (var k = submesh.verticesStart; k < end; ++k) {
  140. var index = k * strideSize;
  141. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  142. byteOff += 4;
  143. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  144. byteOff += 4;
  145. if (useRightHandedSystem) {
  146. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  147. }
  148. else {
  149. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  150. }
  151. byteOff += 4;
  152. }
  153. byteLength = submesh.verticesCount * 12;
  154. break;
  155. }
  156. case BABYLON.VertexBuffer.NormalKind: {
  157. for (var k = submesh.verticesStart; k < end; ++k) {
  158. var index = k * strideSize;
  159. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  160. byteOff += 4;
  161. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  162. byteOff += 4;
  163. if (useRightHandedSystem) {
  164. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  165. }
  166. else {
  167. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  168. }
  169. byteOff += 4;
  170. }
  171. byteLength = submesh.verticesCount * 12;
  172. break;
  173. }
  174. case BABYLON.VertexBuffer.TangentKind: {
  175. for (var k = submesh.indexStart; 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. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  189. byteOff += 4;
  190. }
  191. byteLength = submesh.verticesCount * 16;
  192. break;
  193. }
  194. case BABYLON.VertexBuffer.ColorKind: {
  195. for (var k = submesh.verticesStart; k < end; ++k) {
  196. var index = k * strideSize;
  197. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  198. byteOff += 4;
  199. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  200. byteOff += 4;
  201. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  202. byteOff += 4;
  203. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  204. byteOff += 4;
  205. }
  206. byteLength = submesh.verticesCount * 16;
  207. break;
  208. }
  209. case BABYLON.VertexBuffer.UVKind: {
  210. for (var k = submesh.verticesStart; k < end; ++k) {
  211. var index = k * strideSize;
  212. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  213. byteOff += 4;
  214. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  215. byteOff += 4;
  216. }
  217. byteLength = submesh.verticesCount * 8;
  218. break;
  219. }
  220. case BABYLON.VertexBuffer.UV2Kind: {
  221. for (var k = submesh.verticesStart; k < end; ++k) {
  222. var index = k * strideSize;
  223. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  224. byteOff += 4;
  225. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  226. byteOff += 4;
  227. }
  228. byteLength = submesh.verticesCount * 8;
  229. break;
  230. }
  231. default: {
  232. throw new Error("Unsupported vertex buffer type: " + vertexBufferType);
  233. }
  234. }
  235. return byteLength;
  236. };
  237. /**
  238. * Generates glTF json data
  239. * @param glb
  240. * @param glTFPrefix
  241. * @param prettyPrint
  242. *
  243. * @returns {string} json data as string
  244. */
  245. _GLTF2Exporter.prototype.generateJSON = function (glb, glTFPrefix, prettyPrint) {
  246. var buffer = { byteLength: this.totalByteLength };
  247. var glTF = {
  248. buffers: [buffer],
  249. asset: this.asset,
  250. meshes: this.meshes,
  251. scenes: this.scenes,
  252. nodes: this.nodes,
  253. bufferViews: this.bufferViews,
  254. accessors: this.accessors
  255. };
  256. if (this.scenes.length > 0) {
  257. glTF.scene = 0;
  258. }
  259. if (!glb) {
  260. buffer.uri = glTFPrefix + ".bin";
  261. }
  262. var jsonText = prettyPrint ? JSON.stringify(glTF, null, 2) : JSON.stringify(glTF);
  263. return jsonText;
  264. };
  265. /**
  266. * Generates data for .gltf and .bin files based on the glTF prefix string
  267. * @param glTFPrefix
  268. *
  269. * @returns {[x: string]: string | Blob} object with glTF json tex filename
  270. * and binary file name as keys and their data as values
  271. */
  272. _GLTF2Exporter.prototype._generateGLTF = function (glTFPrefix) {
  273. var jsonText = this.generateJSON(false, glTFPrefix, true);
  274. var binaryBuffer = this.generateBinary();
  275. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  276. var glTFFileName = glTFPrefix + '.gltf';
  277. var glTFBinFile = glTFPrefix + '.bin';
  278. var container = new BABYLON._GLTFData();
  279. container._glTFFiles[glTFFileName] = jsonText;
  280. container._glTFFiles[glTFBinFile] = bin;
  281. return container;
  282. };
  283. /**
  284. * Creates a binary buffer for glTF
  285. *
  286. * @returns {ArrayBuffer}
  287. */
  288. _GLTF2Exporter.prototype.generateBinary = function () {
  289. var byteOffset = 0;
  290. var binaryBuffer = new ArrayBuffer(this.totalByteLength);
  291. var dataBuffer = new DataView(binaryBuffer);
  292. byteOffset = this.createScene(this.babylonScene, byteOffset, dataBuffer);
  293. return binaryBuffer;
  294. };
  295. /**
  296. * Generates a glb file from the json and binary data.
  297. * Returns an object with the glb file name as the key and data as the value.
  298. * @param jsonText
  299. * @param binaryBuffer
  300. * @param glTFPrefix
  301. *
  302. * @returns {[glbFileName: string]: Blob} object with glb filename as key and data as value
  303. */
  304. _GLTF2Exporter.prototype._generateGLB = function (glTFPrefix) {
  305. var jsonText = this.generateJSON(true);
  306. var binaryBuffer = this.generateBinary();
  307. var glbFileName = glTFPrefix + '.glb';
  308. var headerLength = 12;
  309. var chunkLengthPrefix = 8;
  310. var jsonLength = jsonText.length;
  311. var jsonRemainder = jsonLength % 4;
  312. var binRemainder = binaryBuffer.byteLength % 4;
  313. var jsonPadding = jsonRemainder === 0 ? jsonRemainder : 4 - jsonRemainder;
  314. var binPadding = binRemainder === 0 ? binRemainder : 4 - binRemainder;
  315. var byteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding;
  316. //header
  317. var headerBuffer = new ArrayBuffer(headerLength);
  318. var headerBufferView = new DataView(headerBuffer);
  319. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  320. headerBufferView.setUint32(4, 2, true); // version
  321. headerBufferView.setUint32(8, byteLength, true); // total bytes in file
  322. //json chunk
  323. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  324. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  325. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  326. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  327. //json chunk bytes
  328. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  329. for (var i = 0; i < jsonLength; ++i) {
  330. jsonData[i] = jsonText.charCodeAt(i);
  331. }
  332. //json padding
  333. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  334. for (var i = 0; i < jsonPadding; ++i) {
  335. jsonPaddingView[i] = 0x20;
  336. }
  337. //binary chunk
  338. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  339. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  340. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength, true);
  341. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  342. // binary padding
  343. var binPaddingBuffer = new ArrayBuffer(binPadding);
  344. var binPaddingView = new Uint8Array(binPaddingBuffer);
  345. for (var i = 0; i < binPadding; ++i) {
  346. binPaddingView[i] = 0;
  347. }
  348. // binary data
  349. var glbFile = new Blob([headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer, binPaddingBuffer], { type: 'application/octet-stream' });
  350. var container = new BABYLON._GLTFData();
  351. container._glTFFiles[glbFileName] = glbFile;
  352. return container;
  353. };
  354. /**
  355. * Sets the TRS for each node
  356. * @param node
  357. * @param babylonMesh
  358. * @param useRightHandedSystem
  359. */
  360. _GLTF2Exporter.prototype.setNodeTransformation = function (node, babylonMesh, useRightHandedSystem) {
  361. if (!(babylonMesh.position.x === 0 && babylonMesh.position.y === 0 && babylonMesh.position.z === 0)) {
  362. if (useRightHandedSystem) {
  363. node.translation = babylonMesh.position.asArray();
  364. }
  365. else {
  366. node.translation = [babylonMesh.position.x, babylonMesh.position.y, -babylonMesh.position.z];
  367. }
  368. }
  369. if (!(babylonMesh.scaling.x === 1 && babylonMesh.scaling.y === 1 && babylonMesh.scaling.z === 1)) {
  370. if (useRightHandedSystem) {
  371. node.scale = babylonMesh.scaling.asArray();
  372. }
  373. else {
  374. node.scale = [babylonMesh.scaling.x, babylonMesh.scaling.y, -babylonMesh.scaling.z];
  375. }
  376. }
  377. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  378. if (babylonMesh.rotationQuaternion) {
  379. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  380. }
  381. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  382. if (useRightHandedSystem) {
  383. node.rotation = rotationQuaternion.asArray();
  384. }
  385. else {
  386. node.rotation = [-rotationQuaternion.x, -rotationQuaternion.y, rotationQuaternion.z, rotationQuaternion.w];
  387. }
  388. }
  389. };
  390. /**
  391. * Sets data for the primitive attributes of each submesh
  392. * @param mesh
  393. * @param babylonMesh
  394. * @param byteOffset
  395. * @param useRightHandedSystem
  396. * @param dataBuffer
  397. *
  398. * @returns {number} bytelength of the primitive attributes plus the passed in byteOffset
  399. */
  400. _GLTF2Exporter.prototype.setPrimitiveAttributes = function (mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer) {
  401. // go through all mesh primitives (submeshes)
  402. for (var j = 0; j < babylonMesh.subMeshes.length; ++j) {
  403. var bufferMesh = null;
  404. var submesh = babylonMesh.subMeshes[j];
  405. var meshPrimitive = { attributes: {} };
  406. if (babylonMesh instanceof BABYLON.Mesh) {
  407. bufferMesh = babylonMesh;
  408. }
  409. else if (babylonMesh instanceof BABYLON.InstancedMesh) {
  410. bufferMesh = babylonMesh.sourceMesh;
  411. }
  412. // Loop through each attribute of the submesh (mesh primitive)
  413. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  414. var positionVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.PositionKind);
  415. var positionVertexBufferOffset = positionVertexBuffer.getOffset();
  416. var positions = positionVertexBuffer.getData();
  417. var positionStrideSize = positionVertexBuffer.getStrideSize();
  418. if (dataBuffer) {
  419. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.PositionKind, submesh, positions, positionStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  420. }
  421. else {
  422. // Create bufferview
  423. var byteLength = submesh.verticesCount * 12;
  424. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  425. byteOffset += byteLength;
  426. this.bufferViews.push(bufferview);
  427. // Create accessor
  428. var result = this.calculateMinMax(positions, submesh.verticesStart, submesh.verticesCount, positionVertexBufferOffset, positionStrideSize);
  429. var accessor = this.createAccessor(this.bufferViews.length - 1, "Position", "VEC3", 5126, submesh.verticesCount, result.min, result.max);
  430. this.accessors.push(accessor);
  431. meshPrimitive.attributes.POSITION = this.accessors.length - 1;
  432. }
  433. }
  434. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  435. var normalVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.NormalKind);
  436. var normals = normalVertexBuffer.getData();
  437. var normalStrideSize = normalVertexBuffer.getStrideSize();
  438. if (dataBuffer) {
  439. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.NormalKind, submesh, normals, normalStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  440. }
  441. else {
  442. // Create bufferview
  443. var byteLength = submesh.verticesCount * 12;
  444. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  445. byteOffset += byteLength;
  446. this.bufferViews.push(bufferview);
  447. // Create accessor
  448. var accessor = this.createAccessor(this.bufferViews.length - 1, "Normal", "VEC3", 5126, submesh.verticesCount);
  449. this.accessors.push(accessor);
  450. meshPrimitive.attributes.NORMAL = this.accessors.length - 1;
  451. }
  452. }
  453. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  454. var tangentVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.TangentKind);
  455. var tangents = tangentVertexBuffer.getData();
  456. var tangentStrideSize = tangentVertexBuffer.getStrideSize();
  457. if (dataBuffer) {
  458. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.TangentKind, submesh, tangents, tangentStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  459. }
  460. else {
  461. // Create bufferview
  462. var byteLength = submesh.verticesCount * 16;
  463. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  464. byteOffset += byteLength;
  465. this.bufferViews.push(bufferview);
  466. // Create accessor
  467. var accessor = this.createAccessor(this.bufferViews.length - 1, "Tangent", "VEC4", 5126, submesh.verticesCount);
  468. this.accessors.push(accessor);
  469. meshPrimitive.attributes.TANGENT = this.accessors.length - 1;
  470. }
  471. }
  472. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  473. var colorVertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.ColorKind);
  474. var colors = colorVertexBuffer.getData();
  475. var colorStrideSize = colorVertexBuffer.getStrideSize();
  476. if (dataBuffer) {
  477. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.ColorKind, submesh, colors, colorStrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  478. }
  479. else {
  480. // Create bufferview
  481. var byteLength = submesh.verticesCount * 16;
  482. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  483. byteOffset += byteLength;
  484. this.bufferViews.push(bufferview);
  485. // Create accessor
  486. var accessor = this.createAccessor(this.bufferViews.length - 1, "Color", "VEC4", 5126, submesh.verticesCount);
  487. this.accessors.push(accessor);
  488. meshPrimitive.attributes.COLOR_0 = this.accessors.length - 1;
  489. }
  490. }
  491. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  492. var texCoord0VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UVKind);
  493. var texCoords0 = texCoord0VertexBuffer.getData();
  494. var texCoord0StrideSize = texCoord0VertexBuffer.getStrideSize();
  495. if (dataBuffer) {
  496. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UVKind, submesh, texCoords0, texCoord0StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  497. }
  498. else {
  499. // Create bufferview
  500. var byteLength = submesh.verticesCount * 8;
  501. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  502. byteOffset += byteLength;
  503. this.bufferViews.push(bufferview);
  504. // Create accessor
  505. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  506. this.accessors.push(accessor);
  507. meshPrimitive.attributes.TEXCOORD_0 = this.accessors.length - 1;
  508. }
  509. }
  510. if (bufferMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  511. var texCoord1VertexBuffer = bufferMesh.getVertexBuffer(BABYLON.VertexBuffer.UV2Kind);
  512. var texCoords1 = texCoord1VertexBuffer.getData();
  513. var texCoord1StrideSize = texCoord1VertexBuffer.getStrideSize();
  514. if (dataBuffer) {
  515. byteOffset += this.writeAttributeData(BABYLON.VertexBuffer.UV2Kind, submesh, texCoords1, texCoord1StrideSize, byteOffset, dataBuffer, useRightHandedSystem);
  516. }
  517. else {
  518. // Create bufferview
  519. var byteLength = submesh.verticesCount * 8;
  520. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  521. byteOffset += byteLength;
  522. this.bufferViews.push(bufferview);
  523. // Create accessor
  524. var accessor = this.createAccessor(this.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  525. this.accessors.push(accessor);
  526. meshPrimitive.attributes.TEXCOORD_1 = this.accessors.length - 1;
  527. }
  528. }
  529. if (bufferMesh.getTotalIndices() > 0) {
  530. if (dataBuffer) {
  531. var indices = bufferMesh.getIndices();
  532. var start = submesh.indexStart;
  533. var end = submesh.indexCount + start;
  534. var byteOff = byteOffset;
  535. for (var k = start; k < end; k = k + 3) {
  536. dataBuffer.setUint32(byteOff, indices[k], true);
  537. byteOff += 4;
  538. dataBuffer.setUint32(byteOff, indices[k + 1], true);
  539. byteOff += 4;
  540. dataBuffer.setUint32(byteOff, indices[k + 2], true);
  541. byteOff += 4;
  542. }
  543. var byteLength = submesh.indexCount * 4;
  544. byteOffset += byteLength;
  545. }
  546. else {
  547. // Create bufferview
  548. var indicesCount = submesh.indexCount;
  549. var byteLength = indicesCount * 4;
  550. var bufferview = this.createBufferView(0, byteOffset, byteLength);
  551. byteOffset += byteLength;
  552. this.bufferViews.push(bufferview);
  553. // Create accessor
  554. var accessor = this.createAccessor(this.bufferViews.length - 1, "Indices", "SCALAR", 5125, indicesCount);
  555. this.accessors.push(accessor);
  556. meshPrimitive.indices = this.accessors.length - 1;
  557. }
  558. }
  559. if (bufferMesh.material) {
  560. //TODO: Implement Material
  561. }
  562. mesh.primitives.push(meshPrimitive);
  563. }
  564. return byteOffset;
  565. };
  566. /**
  567. * Creates a glTF scene based on the array of meshes.
  568. * Returns the the total byte offset.
  569. * @param gltf
  570. * @param byteOffset
  571. * @param buffer
  572. * @param dataBuffer
  573. *
  574. * @returns {number} bytelength + byteoffset
  575. */
  576. _GLTF2Exporter.prototype.createScene = function (babylonScene, byteOffset, dataBuffer) {
  577. if (babylonScene.meshes.length > 0) {
  578. var babylonMeshes = babylonScene.meshes;
  579. var scene = { nodes: new Array() };
  580. for (var i = 0; i < babylonMeshes.length; ++i) {
  581. // create node to hold translation/rotation/scale and the mesh
  582. var node = { mesh: -1 };
  583. var babylonMesh = babylonMeshes[i];
  584. var useRightHandedSystem = babylonMesh.getScene().useRightHandedSystem;
  585. // Set transformation
  586. this.setNodeTransformation(node, babylonMesh, useRightHandedSystem);
  587. // create mesh
  588. var mesh = { primitives: new Array() };
  589. mesh.primitives = [];
  590. byteOffset = this.setPrimitiveAttributes(mesh, babylonMesh, byteOffset, useRightHandedSystem, dataBuffer);
  591. // go through all mesh primitives (submeshes)
  592. this.meshes.push(mesh);
  593. node.mesh = this.meshes.length - 1;
  594. if (babylonMesh.name) {
  595. node.name = babylonMesh.name;
  596. }
  597. this.nodes.push(node);
  598. scene.nodes.push(this.nodes.length - 1);
  599. }
  600. this.scenes.push(scene);
  601. }
  602. return byteOffset;
  603. };
  604. return _GLTF2Exporter;
  605. }());
  606. BABYLON._GLTF2Exporter = _GLTF2Exporter;
  607. })(BABYLON || (BABYLON = {}));
  608. //# sourceMappingURL=babylon.glTFExporter.js.map
  609. var BABYLON;
  610. (function (BABYLON) {
  611. /**
  612. * Class for holding and downloading glTF file data
  613. */
  614. var _GLTFData = /** @class */ (function () {
  615. function _GLTFData() {
  616. this._glTFFiles = {};
  617. }
  618. /**
  619. * Downloads glTF data.
  620. */
  621. _GLTFData.prototype.downloadFiles = function () {
  622. /**
  623. * Checks for a matching suffix at the end of a string (for ES5 and lower)
  624. * @param str
  625. * @param suffix
  626. *
  627. * @returns {boolean} indicating whether the suffix matches or not
  628. */
  629. function endsWith(str, suffix) {
  630. return str.indexOf(suffix, str.length - suffix.length) !== -1;
  631. }
  632. for (var key in this._glTFFiles) {
  633. var link = document.createElement('a');
  634. document.body.appendChild(link);
  635. link.setAttribute("type", "hidden");
  636. link.download = key;
  637. var blob = this._glTFFiles[key];
  638. var mimeType = void 0;
  639. if (endsWith(key, ".glb")) {
  640. mimeType = { type: "model/gltf-binary" };
  641. }
  642. else if (endsWith(key, ".bin")) {
  643. mimeType = { type: "application/octet-stream" };
  644. }
  645. else if (endsWith(key, ".gltf")) {
  646. mimeType = { type: "model/gltf+json" };
  647. }
  648. link.href = window.URL.createObjectURL(new Blob([blob], mimeType));
  649. link.click();
  650. }
  651. };
  652. return _GLTFData;
  653. }());
  654. BABYLON._GLTFData = _GLTFData;
  655. })(BABYLON || (BABYLON = {}));
  656. //# sourceMappingURL=babylon.glTFData.js.map