babylon.glTF2Serializer.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /// <reference path="../../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var GLTFExport = /** @class */ (function () {
  5. function GLTFExport() {
  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. * @param glb
  13. */
  14. GLTFExport.GLTF = function (meshes, filename, glb) {
  15. /**
  16. * Creates a buffer view based on teh supplied arguments
  17. * @param bufferIndex
  18. * @param byteOffset
  19. * @param byteLength
  20. */
  21. function createBufferView(bufferIndex, byteOffset, byteLength) {
  22. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  23. if (byteOffset > 0) {
  24. bufferview.byteOffset = byteOffset;
  25. }
  26. return bufferview;
  27. }
  28. /**
  29. * Creates an accessor based on the supplied arguments
  30. * @param bufferviewIndex
  31. * @param name
  32. * @param type
  33. * @param componentType
  34. * @param count
  35. * @param min
  36. * @param max
  37. */
  38. function createAccessor(bufferviewIndex, name, type, componentType, count, min, max) {
  39. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  40. if (min) {
  41. accessor.min = min;
  42. }
  43. if (max) {
  44. accessor.max = max;
  45. }
  46. return accessor;
  47. }
  48. /**
  49. * Calculates the minimum and maximum values of an array of floats, based on stride
  50. * @param buff
  51. * @param vertexStart
  52. * @param vertexCount
  53. * @param arrayOffset
  54. * @param stride
  55. */
  56. function calculateMinMax(buff, vertexStart, vertexCount, arrayOffset, stride) {
  57. var min = [Infinity, Infinity, Infinity];
  58. var max = [-Infinity, -Infinity, -Infinity];
  59. var end = vertexStart + vertexCount;
  60. if (vertexCount > 0) {
  61. for (var i = vertexStart; i < end; ++i) {
  62. var index = stride * i;
  63. for (var j = 0; j < stride; ++j) {
  64. if (buff[index] < min[j]) {
  65. min[j] = buff[index];
  66. }
  67. if (buff[index] > max[j]) {
  68. max[j] = buff[index];
  69. }
  70. ++index;
  71. }
  72. }
  73. }
  74. return { min: min, max: max };
  75. }
  76. /**
  77. * Write mesh attribute data to buffer.
  78. * Returns the bytelength of the data.
  79. * @param vertexBufferType
  80. * @param submesh
  81. * @param meshAttributeArray
  82. * @param strideSize
  83. * @param byteOffset
  84. * @param dataBuffer
  85. * @param useRightHandedSystem
  86. */
  87. function writeAttributeData(vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem) {
  88. var byteOff = byteOffset;
  89. var end = submesh.verticesStart + submesh.verticesCount;
  90. var byteLength = 0;
  91. switch (vertexBufferType) {
  92. case BABYLON.VertexBuffer.PositionKind: {
  93. for (var k = submesh.verticesStart; k < end; ++k) {
  94. var index = k * strideSize;
  95. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  96. byteOff += 4;
  97. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  98. byteOff += 4;
  99. if (useRightHandedSystem) {
  100. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  101. }
  102. else {
  103. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  104. }
  105. byteOff += 4;
  106. }
  107. byteLength = submesh.verticesCount * 12;
  108. break;
  109. }
  110. case BABYLON.VertexBuffer.NormalKind: {
  111. for (var k = submesh.verticesStart; k < end; ++k) {
  112. var index = k * strideSize;
  113. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  114. byteOff += 4;
  115. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  116. byteOff += 4;
  117. if (useRightHandedSystem) {
  118. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  119. }
  120. else {
  121. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  122. }
  123. byteOff += 4;
  124. }
  125. byteLength = submesh.verticesCount * 12;
  126. break;
  127. }
  128. case BABYLON.VertexBuffer.TangentKind: {
  129. for (var k = submesh.indexStart; k < end; ++k) {
  130. var index = k * strideSize;
  131. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  132. byteOff += 4;
  133. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  134. byteOff += 4;
  135. if (useRightHandedSystem) {
  136. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  137. }
  138. else {
  139. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  140. }
  141. byteOff += 4;
  142. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  143. byteOff += 4;
  144. }
  145. byteLength = submesh.verticesCount * 16;
  146. break;
  147. }
  148. case BABYLON.VertexBuffer.ColorKind: {
  149. for (var k = submesh.verticesStart; k < end; ++k) {
  150. var index = k * strideSize;
  151. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  152. byteOff += 4;
  153. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  154. byteOff += 4;
  155. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  156. byteOff += 4;
  157. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  158. byteOff += 4;
  159. }
  160. byteLength = submesh.verticesCount * 16;
  161. break;
  162. }
  163. case BABYLON.VertexBuffer.UVKind: {
  164. for (var k = submesh.verticesStart; k < end; ++k) {
  165. var index = k * strideSize;
  166. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  167. byteOff += 4;
  168. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  169. byteOff += 4;
  170. }
  171. byteLength = submesh.verticesCount * 8;
  172. break;
  173. }
  174. case BABYLON.VertexBuffer.UV2Kind: {
  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. }
  182. byteLength = submesh.verticesCount * 8;
  183. break;
  184. }
  185. default: {
  186. throw new Error("Unsupported vertex buffer type: " + vertexBufferType);
  187. }
  188. }
  189. return byteLength;
  190. }
  191. /**
  192. * Generates a glb file from the json and binary data.
  193. * Returns an object with the glb file name as the key and data as the value.
  194. * @param jsonText
  195. * @param binaryBuffer
  196. * @param glTFPrefix
  197. */
  198. function createGLB(jsonText, binaryBuffer, glTFPrefix) {
  199. var glbFileName = glTFPrefix + '.glb';
  200. var headerLength = 12;
  201. var chunkLengthPrefix = 8;
  202. var jsonLength = jsonText.length;
  203. var jsonRemainder = jsonLength % 4;
  204. var binRemainder = binaryBuffer.byteLength % 4;
  205. var jsonPadding = jsonRemainder === 0 ? jsonRemainder : 4 - jsonRemainder;
  206. var binPadding = binRemainder === 0 ? binRemainder : 4 - binRemainder;
  207. var totalByteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding;
  208. //header
  209. var headerBuffer = new ArrayBuffer(headerLength);
  210. var headerBufferView = new DataView(headerBuffer);
  211. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  212. headerBufferView.setUint32(4, 2, true); // version
  213. headerBufferView.setUint32(8, totalByteLength, true); // total bytes in file
  214. //json chunk
  215. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  216. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  217. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  218. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  219. //json chunk bytes
  220. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  221. for (var i = 0; i < jsonLength; ++i) {
  222. jsonData[i] = jsonText.charCodeAt(i);
  223. }
  224. //json padding
  225. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  226. for (var i = 0; i < jsonPadding; ++i) {
  227. jsonPaddingView[i] = 0x20;
  228. }
  229. //binary chunk
  230. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  231. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  232. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength, true);
  233. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  234. // binary padding
  235. var binPaddingBuffer = new ArrayBuffer(binPadding);
  236. var binPaddingView = new Uint8Array(binPaddingBuffer);
  237. for (var i = 0; i < binPadding; ++i) {
  238. binPaddingView[i] = 0;
  239. }
  240. // binary data
  241. var glbFile = new Blob([headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer, binPaddingBuffer], { type: 'application/octet-stream' });
  242. return _a = {},
  243. _a[glbFileName] = glbFile,
  244. _a;
  245. var _a;
  246. }
  247. /**
  248. * Creates a glTF scene based on the array of meshes.
  249. * Returns the the total byte offset.
  250. * @param gltf
  251. * @param totalByteOffset
  252. * @param buffer
  253. * @param dataBuffer
  254. */
  255. function createScene(gltf, totalByteOffset, dataBuffer) {
  256. var scene = { nodes: new Array() };
  257. for (var i = 0; i < meshes.length; ++i) {
  258. // create node to hold translation/rotation/scale and the mesh
  259. var node = { mesh: -1 };
  260. var babylonMesh = meshes[i];
  261. var useRightHandedSystem = babylonMesh.getScene().useRightHandedSystem;
  262. if (!(babylonMesh.position.x === 0 && babylonMesh.position.y === 0 && babylonMesh.position.z === 0)) {
  263. if (useRightHandedSystem) {
  264. node.translation = babylonMesh.position.asArray();
  265. }
  266. else {
  267. node.translation = [babylonMesh.position.x, babylonMesh.position.y, -babylonMesh.position.z];
  268. }
  269. }
  270. if (!(babylonMesh.scaling.x === 1 && babylonMesh.scaling.y === 1 && babylonMesh.scaling.z === 1)) {
  271. if (useRightHandedSystem) {
  272. node.scale = babylonMesh.scaling.asArray();
  273. }
  274. else {
  275. node.scale = [babylonMesh.scaling.x, babylonMesh.scaling.y, -babylonMesh.scaling.z];
  276. }
  277. }
  278. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  279. if (babylonMesh.rotationQuaternion) {
  280. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  281. }
  282. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  283. if (useRightHandedSystem) {
  284. node.rotation = rotationQuaternion.asArray();
  285. }
  286. else {
  287. node.rotation = [-rotationQuaternion.x, -rotationQuaternion.y, rotationQuaternion.z, rotationQuaternion.w];
  288. }
  289. }
  290. var positionVertexBuffer = void 0;
  291. var positions = void 0;
  292. var positionVertexBufferOffset = void 0;
  293. var positionStrideSize = void 0;
  294. var normalVertexBuffer = void 0;
  295. var normals = void 0;
  296. var normalStrideSize = void 0;
  297. var tangentVertexBuffer = void 0;
  298. var tangents = void 0;
  299. var tangentStrideSize = void 0;
  300. var colorVertexBuffer = void 0;
  301. var colors = void 0;
  302. var colorStrideSize = void 0;
  303. var texCoord0VertexBuffer = void 0;
  304. var texCoords0 = void 0;
  305. var texCoord0StrideSize = void 0;
  306. var texCoord1VertexBuffer = void 0;
  307. var texCoords1 = void 0;
  308. var texCoord1StrideSize = void 0;
  309. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  310. positionVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.PositionKind);
  311. positions = positionVertexBuffer.getData();
  312. positionVertexBufferOffset = positionVertexBuffer.getOffset();
  313. positionStrideSize = positionVertexBuffer.getStrideSize();
  314. }
  315. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  316. normalVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.NormalKind);
  317. normals = normalVertexBuffer.getData();
  318. normalStrideSize = normalVertexBuffer.getStrideSize();
  319. }
  320. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  321. tangentVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.TangentKind);
  322. tangents = tangentVertexBuffer.getData();
  323. tangentStrideSize = tangentVertexBuffer.getStrideSize();
  324. }
  325. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  326. colorVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.ColorKind);
  327. colors = colorVertexBuffer.getData();
  328. colorStrideSize = colorVertexBuffer.getStrideSize();
  329. }
  330. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  331. texCoord0VertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.UVKind);
  332. texCoords0 = texCoord0VertexBuffer.getData();
  333. texCoord0StrideSize = texCoord0VertexBuffer.getStrideSize();
  334. }
  335. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  336. texCoord1VertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.UV2Kind);
  337. texCoords1 = texCoord1VertexBuffer.getData();
  338. texCoord1StrideSize = texCoord1VertexBuffer.getStrideSize();
  339. }
  340. // create mesh
  341. var mesh = { primitives: new Array() };
  342. mesh.primitives = [];
  343. if (babylonMesh.name) {
  344. mesh.name = babylonMesh.name;
  345. }
  346. // go through all mesh primitives (submeshes)
  347. for (var j = 0; j < babylonMesh.subMeshes.length; ++j) {
  348. var submesh = babylonMesh.subMeshes[j];
  349. var meshPrimitive = { attributes: {} };
  350. // Loop through each attribute of the submesh (mesh primitive)
  351. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  352. if (dataBuffer) {
  353. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.PositionKind, submesh, positions, positionStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  354. }
  355. else {
  356. // Create bufferview
  357. var byteLength = submesh.verticesCount * 12;
  358. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  359. totalByteOffset += byteLength;
  360. gltf.bufferViews.push(bufferview);
  361. // Create accessor
  362. var result = calculateMinMax(positions, submesh.verticesStart, submesh.verticesCount, positionVertexBufferOffset, positionStrideSize);
  363. var accessor = createAccessor(gltf.bufferViews.length - 1, "Position", "VEC3", 5126, submesh.verticesCount, result.min, result.max);
  364. gltf.accessors.push(accessor);
  365. meshPrimitive.attributes.POSITION = gltf.accessors.length - 1;
  366. }
  367. }
  368. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  369. if (dataBuffer) {
  370. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.NormalKind, submesh, normals, normalStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  371. }
  372. else {
  373. // Create bufferview
  374. var byteLength = submesh.verticesCount * 12;
  375. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  376. totalByteOffset += byteLength;
  377. gltf.bufferViews.push(bufferview);
  378. // Create accessor
  379. var accessor = createAccessor(gltf.bufferViews.length - 1, "Normal", "VEC3", 5126, submesh.verticesCount);
  380. gltf.accessors.push(accessor);
  381. meshPrimitive.attributes.NORMAL = gltf.accessors.length - 1;
  382. }
  383. }
  384. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  385. if (dataBuffer) {
  386. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.TangentKind, submesh, tangents, tangentStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  387. }
  388. else {
  389. // Create bufferview
  390. var byteLength = submesh.verticesCount * 16;
  391. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  392. totalByteOffset += byteLength;
  393. gltf.bufferViews.push(bufferview);
  394. // Create accessor
  395. var accessor = createAccessor(gltf.bufferViews.length - 1, "Tangent", "VEC4", 5126, submesh.verticesCount);
  396. gltf.accessors.push(accessor);
  397. meshPrimitive.attributes.TANGENT = gltf.accessors.length - 1;
  398. }
  399. }
  400. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  401. if (dataBuffer) {
  402. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.ColorKind, submesh, colors, colorStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  403. }
  404. else {
  405. // Create bufferview
  406. var byteLength = submesh.verticesCount * 16;
  407. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  408. totalByteOffset += byteLength;
  409. gltf.bufferViews.push(bufferview);
  410. // Create accessor
  411. var accessor = createAccessor(gltf.bufferViews.length - 1, "Color", "VEC4", 5126, submesh.verticesCount);
  412. gltf.accessors.push(accessor);
  413. meshPrimitive.attributes.COLOR_0 = gltf.accessors.length - 1;
  414. }
  415. }
  416. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  417. if (dataBuffer) {
  418. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.UVKind, submesh, texCoords0, texCoord0StrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  419. }
  420. else {
  421. // Create bufferview
  422. var byteLength = submesh.verticesCount * 8;
  423. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  424. totalByteOffset += byteLength;
  425. gltf.bufferViews.push(bufferview);
  426. // Create accessor
  427. var accessor = createAccessor(gltf.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  428. gltf.accessors.push(accessor);
  429. meshPrimitive.attributes.TEXCOORD_0 = gltf.accessors.length - 1;
  430. }
  431. }
  432. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  433. if (dataBuffer) {
  434. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.UV2Kind, submesh, texCoords1, texCoord1StrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  435. }
  436. else {
  437. // Create bufferview
  438. var byteLength = submesh.verticesCount * 8;
  439. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  440. totalByteOffset += byteLength;
  441. gltf.bufferViews.push(bufferview);
  442. // Create accessor
  443. var accessor = createAccessor(gltf.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  444. gltf.accessors.push(accessor);
  445. meshPrimitive.attributes.TEXCOORD_1 = gltf.accessors.length - 1;
  446. }
  447. }
  448. if (babylonMesh.getTotalIndices() > 0) {
  449. if (dataBuffer) {
  450. var indices = babylonMesh.getIndices();
  451. var start = submesh.indexStart;
  452. var end = submesh.indexCount + start;
  453. var byteOff = totalByteOffset;
  454. for (var k = start; k < end; k = k + 3) {
  455. dataBuffer.setUint32(byteOff, indices[k], true);
  456. byteOff += 4;
  457. dataBuffer.setUint32(byteOff, indices[k + 1], true);
  458. byteOff += 4;
  459. dataBuffer.setUint32(byteOff, indices[k + 2], true);
  460. byteOff += 4;
  461. }
  462. var byteLength = submesh.indexCount * 4;
  463. totalByteOffset += byteLength;
  464. }
  465. else {
  466. // Create bufferview
  467. var indicesCount = submesh.indexCount;
  468. var byteLength = indicesCount * 4;
  469. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  470. totalByteOffset += byteLength;
  471. gltf.bufferViews.push(bufferview);
  472. // Create accessor
  473. var accessor = createAccessor(gltf.bufferViews.length - 1, "Indices", "SCALAR", 5125, indicesCount);
  474. gltf.accessors.push(accessor);
  475. meshPrimitive.indices = gltf.accessors.length - 1;
  476. }
  477. }
  478. if (babylonMesh.material) {
  479. if (!gltf.materials) {
  480. gltf.materials = new Array();
  481. }
  482. meshPrimitive.material = gltf.materials.length - 1;
  483. }
  484. mesh.primitives.push(meshPrimitive);
  485. }
  486. gltf.meshes.push(mesh);
  487. node.mesh = gltf.meshes.length - 1;
  488. gltf.nodes.push(node);
  489. scene.nodes.push(gltf.nodes.length - 1);
  490. }
  491. gltf.scenes.push(scene);
  492. return totalByteOffset;
  493. }
  494. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  495. var gltf = {
  496. buffers: new Array(),
  497. bufferViews: new Array(),
  498. asset: { generator: "BabylonJS", version: "2.0" },
  499. meshes: new Array(),
  500. scenes: new Array(),
  501. nodes: new Array(),
  502. accessors: new Array()
  503. };
  504. var totalByteOffset = 0;
  505. var binaryBuffer;
  506. var dataBuffer;
  507. // Create scene. First pass calculates the totalByteOffset.
  508. totalByteOffset = createScene(gltf, totalByteOffset, null);
  509. var buff = { byteLength: totalByteOffset };
  510. if (!glb) {
  511. buff.uri = glTFPrefix + '.bin';
  512. }
  513. gltf.buffers.push(buff);
  514. var text = JSON.stringify(gltf, null, 2);
  515. binaryBuffer = new ArrayBuffer(totalByteOffset);
  516. dataBuffer = new DataView(binaryBuffer);
  517. totalByteOffset = 0;
  518. // Create scene. Second pass generates the binary data
  519. createScene(gltf, totalByteOffset, dataBuffer);
  520. if (glb) {
  521. var glbFile = createGLB(text, binaryBuffer, glTFPrefix);
  522. return glbFile;
  523. }
  524. var glTFFileName = glTFPrefix + '.gltf';
  525. var glTFBinFile = glTFPrefix + '.bin';
  526. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  527. return _a = {},
  528. _a[glTFFileName] = text,
  529. _a[glTFBinFile] = bin,
  530. _a;
  531. var _a;
  532. };
  533. return GLTFExport;
  534. }());
  535. BABYLON.GLTFExport = GLTFExport;
  536. })(BABYLON || (BABYLON = {}));
  537. //# sourceMappingURL=babylon.glTFSerializer.js.map