babylon.glTF2Serializer.js 31 KB

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