babylonjs.serializers.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 GLTFExport = /** @class */ (function () {
  145. function GLTFExport() {
  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. * @param glb
  153. */
  154. GLTFExport.GLTF = function (meshes, filename, glb) {
  155. /**
  156. * Creates a buffer view based on teh supplied arguments
  157. * @param bufferIndex
  158. * @param byteOffset
  159. * @param byteLength
  160. */
  161. function createBufferView(bufferIndex, byteOffset, byteLength) {
  162. var bufferview = { buffer: bufferIndex, byteLength: byteLength };
  163. if (byteOffset > 0) {
  164. bufferview.byteOffset = byteOffset;
  165. }
  166. return bufferview;
  167. }
  168. /**
  169. * Creates an accessor based on the supplied arguments
  170. * @param bufferviewIndex
  171. * @param name
  172. * @param type
  173. * @param componentType
  174. * @param count
  175. * @param min
  176. * @param max
  177. */
  178. function createAccessor(bufferviewIndex, name, type, componentType, count, min, max) {
  179. var accessor = { name: name, bufferView: bufferviewIndex, componentType: componentType, count: count, type: type };
  180. if (min) {
  181. accessor.min = min;
  182. }
  183. if (max) {
  184. accessor.max = max;
  185. }
  186. return accessor;
  187. }
  188. /**
  189. * Calculates the minimum and maximum values of an array of floats, based on stride
  190. * @param buff
  191. * @param vertexStart
  192. * @param vertexCount
  193. * @param arrayOffset
  194. * @param stride
  195. */
  196. function calculateMinMax(buff, vertexStart, vertexCount, arrayOffset, stride) {
  197. var min = [Infinity, Infinity, Infinity];
  198. var max = [-Infinity, -Infinity, -Infinity];
  199. var end = vertexStart + vertexCount;
  200. if (vertexCount > 0) {
  201. for (var i = vertexStart; i < end; ++i) {
  202. var index = stride * i;
  203. for (var j = 0; j < stride; ++j) {
  204. if (buff[index] < min[j]) {
  205. min[j] = buff[index];
  206. }
  207. if (buff[index] > max[j]) {
  208. max[j] = buff[index];
  209. }
  210. ++index;
  211. }
  212. }
  213. }
  214. return { min: min, max: max };
  215. }
  216. /**
  217. * Write mesh attribute data to buffer.
  218. * Returns the bytelength of the data.
  219. * @param vertexBufferType
  220. * @param submesh
  221. * @param meshAttributeArray
  222. * @param strideSize
  223. * @param byteOffset
  224. * @param dataBuffer
  225. * @param useRightHandedSystem
  226. */
  227. function writeAttributeData(vertexBufferType, submesh, meshAttributeArray, strideSize, byteOffset, dataBuffer, useRightHandedSystem) {
  228. var byteOff = byteOffset;
  229. var end = submesh.verticesStart + submesh.verticesCount;
  230. var byteLength = 0;
  231. switch (vertexBufferType) {
  232. case BABYLON.VertexBuffer.PositionKind: {
  233. for (var k = submesh.verticesStart; k < end; ++k) {
  234. var index = k * strideSize;
  235. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  236. byteOff += 4;
  237. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  238. byteOff += 4;
  239. if (useRightHandedSystem) {
  240. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  241. }
  242. else {
  243. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  244. }
  245. byteOff += 4;
  246. }
  247. byteLength = submesh.verticesCount * 12;
  248. break;
  249. }
  250. case BABYLON.VertexBuffer.NormalKind: {
  251. for (var k = submesh.verticesStart; k < end; ++k) {
  252. var index = k * strideSize;
  253. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  254. byteOff += 4;
  255. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  256. byteOff += 4;
  257. if (useRightHandedSystem) {
  258. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  259. }
  260. else {
  261. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  262. }
  263. byteOff += 4;
  264. }
  265. byteLength = submesh.verticesCount * 12;
  266. break;
  267. }
  268. case BABYLON.VertexBuffer.TangentKind: {
  269. for (var k = submesh.indexStart; k < end; ++k) {
  270. var index = k * strideSize;
  271. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  272. byteOff += 4;
  273. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  274. byteOff += 4;
  275. if (useRightHandedSystem) {
  276. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  277. }
  278. else {
  279. dataBuffer.setFloat32(byteOff, -meshAttributeArray[index + 2], true);
  280. }
  281. byteOff += 4;
  282. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  283. byteOff += 4;
  284. }
  285. byteLength = submesh.verticesCount * 16;
  286. break;
  287. }
  288. case BABYLON.VertexBuffer.ColorKind: {
  289. for (var k = submesh.verticesStart; k < end; ++k) {
  290. var index = k * strideSize;
  291. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  292. byteOff += 4;
  293. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  294. byteOff += 4;
  295. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 2], true);
  296. byteOff += 4;
  297. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 3], true);
  298. byteOff += 4;
  299. }
  300. byteLength = submesh.verticesCount * 16;
  301. break;
  302. }
  303. case BABYLON.VertexBuffer.UVKind: {
  304. for (var k = submesh.verticesStart; k < end; ++k) {
  305. var index = k * strideSize;
  306. dataBuffer.setFloat32(byteOff, meshAttributeArray[index], true);
  307. byteOff += 4;
  308. dataBuffer.setFloat32(byteOff, meshAttributeArray[index + 1], true);
  309. byteOff += 4;
  310. }
  311. byteLength = submesh.verticesCount * 8;
  312. break;
  313. }
  314. case BABYLON.VertexBuffer.UV2Kind: {
  315. for (var k = submesh.verticesStart; 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. }
  322. byteLength = submesh.verticesCount * 8;
  323. break;
  324. }
  325. default: {
  326. throw new Error("Unsupported vertex buffer type: " + vertexBufferType);
  327. }
  328. }
  329. return byteLength;
  330. }
  331. /**
  332. * Generates a glb file from the json and binary data.
  333. * Returns an object with the glb file name as the key and data as the value.
  334. * @param jsonText
  335. * @param binaryBuffer
  336. * @param glTFPrefix
  337. */
  338. function createGLB(jsonText, binaryBuffer, glTFPrefix) {
  339. var glbFileName = glTFPrefix + '.glb';
  340. var headerLength = 12;
  341. var chunkLengthPrefix = 8;
  342. var jsonLength = jsonText.length;
  343. var jsonRemainder = jsonLength % 4;
  344. var binRemainder = binaryBuffer.byteLength % 4;
  345. var jsonPadding = jsonRemainder === 0 ? jsonRemainder : 4 - jsonRemainder;
  346. var binPadding = binRemainder === 0 ? binRemainder : 4 - binRemainder;
  347. var totalByteLength = headerLength + (2 * chunkLengthPrefix) + jsonLength + jsonPadding + binaryBuffer.byteLength + binPadding;
  348. //header
  349. var headerBuffer = new ArrayBuffer(headerLength);
  350. var headerBufferView = new DataView(headerBuffer);
  351. headerBufferView.setUint32(0, 0x46546C67, true); //glTF
  352. headerBufferView.setUint32(4, 2, true); // version
  353. headerBufferView.setUint32(8, totalByteLength, true); // total bytes in file
  354. //json chunk
  355. var jsonChunkBuffer = new ArrayBuffer(chunkLengthPrefix + jsonLength + jsonPadding);
  356. var jsonChunkBufferView = new DataView(jsonChunkBuffer);
  357. jsonChunkBufferView.setUint32(0, jsonLength + jsonPadding, true);
  358. jsonChunkBufferView.setUint32(4, 0x4E4F534A, true);
  359. //json chunk bytes
  360. var jsonData = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix);
  361. for (var i = 0; i < jsonLength; ++i) {
  362. jsonData[i] = jsonText.charCodeAt(i);
  363. }
  364. //json padding
  365. var jsonPaddingView = new Uint8Array(jsonChunkBuffer, chunkLengthPrefix + jsonLength);
  366. for (var i = 0; i < jsonPadding; ++i) {
  367. jsonPaddingView[i] = 0x20;
  368. }
  369. //binary chunk
  370. var binaryChunkBuffer = new ArrayBuffer(chunkLengthPrefix);
  371. var binaryChunkBufferView = new DataView(binaryChunkBuffer);
  372. binaryChunkBufferView.setUint32(0, binaryBuffer.byteLength, true);
  373. binaryChunkBufferView.setUint32(4, 0x004E4942, true);
  374. // binary padding
  375. var binPaddingBuffer = new ArrayBuffer(binPadding);
  376. var binPaddingView = new Uint8Array(binPaddingBuffer);
  377. for (var i = 0; i < binPadding; ++i) {
  378. binPaddingView[i] = 0;
  379. }
  380. // binary data
  381. var glbFile = new Blob([headerBuffer, jsonChunkBuffer, binaryChunkBuffer, binaryBuffer, binPaddingBuffer], { type: 'application/octet-stream' });
  382. return _a = {},
  383. _a[glbFileName] = glbFile,
  384. _a;
  385. var _a;
  386. }
  387. /**
  388. * Creates a glTF scene based on the array of meshes.
  389. * Returns the the total byte offset.
  390. * @param gltf
  391. * @param totalByteOffset
  392. * @param buffer
  393. * @param dataBuffer
  394. */
  395. function createScene(gltf, totalByteOffset, dataBuffer) {
  396. var scene = { nodes: new Array() };
  397. for (var i = 0; i < meshes.length; ++i) {
  398. // create node to hold translation/rotation/scale and the mesh
  399. var node = { mesh: -1 };
  400. var babylonMesh = meshes[i];
  401. var useRightHandedSystem = babylonMesh.getScene().useRightHandedSystem;
  402. if (!(babylonMesh.position.x === 0 && babylonMesh.position.y === 0 && babylonMesh.position.z === 0)) {
  403. if (useRightHandedSystem) {
  404. node.translation = babylonMesh.position.asArray();
  405. }
  406. else {
  407. node.translation = [babylonMesh.position.x, babylonMesh.position.y, -babylonMesh.position.z];
  408. }
  409. }
  410. if (!(babylonMesh.scaling.x === 1 && babylonMesh.scaling.y === 1 && babylonMesh.scaling.z === 1)) {
  411. if (useRightHandedSystem) {
  412. node.scale = babylonMesh.scaling.asArray();
  413. }
  414. else {
  415. node.scale = [babylonMesh.scaling.x, babylonMesh.scaling.y, -babylonMesh.scaling.z];
  416. }
  417. }
  418. var rotationQuaternion = BABYLON.Quaternion.RotationYawPitchRoll(babylonMesh.rotation.y, babylonMesh.rotation.x, babylonMesh.rotation.z);
  419. if (babylonMesh.rotationQuaternion) {
  420. rotationQuaternion = rotationQuaternion.multiply(babylonMesh.rotationQuaternion);
  421. }
  422. if (!(rotationQuaternion.x === 0 && rotationQuaternion.y === 0 && rotationQuaternion.z === 0 && rotationQuaternion.w === 1)) {
  423. if (useRightHandedSystem) {
  424. node.rotation = rotationQuaternion.asArray();
  425. }
  426. else {
  427. node.rotation = [-rotationQuaternion.x, -rotationQuaternion.y, rotationQuaternion.z, rotationQuaternion.w];
  428. }
  429. }
  430. var positionVertexBuffer = void 0;
  431. var positions = void 0;
  432. var positionVertexBufferOffset = void 0;
  433. var positionStrideSize = void 0;
  434. var normalVertexBuffer = void 0;
  435. var normals = void 0;
  436. var normalStrideSize = void 0;
  437. var tangentVertexBuffer = void 0;
  438. var tangents = void 0;
  439. var tangentStrideSize = void 0;
  440. var colorVertexBuffer = void 0;
  441. var colors = void 0;
  442. var colorStrideSize = void 0;
  443. var texCoord0VertexBuffer = void 0;
  444. var texCoords0 = void 0;
  445. var texCoord0StrideSize = void 0;
  446. var texCoord1VertexBuffer = void 0;
  447. var texCoords1 = void 0;
  448. var texCoord1StrideSize = void 0;
  449. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  450. positionVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.PositionKind);
  451. positions = positionVertexBuffer.getData();
  452. positionVertexBufferOffset = positionVertexBuffer.getOffset();
  453. positionStrideSize = positionVertexBuffer.getStrideSize();
  454. }
  455. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  456. normalVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.NormalKind);
  457. normals = normalVertexBuffer.getData();
  458. normalStrideSize = normalVertexBuffer.getStrideSize();
  459. }
  460. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  461. tangentVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.TangentKind);
  462. tangents = tangentVertexBuffer.getData();
  463. tangentStrideSize = tangentVertexBuffer.getStrideSize();
  464. }
  465. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  466. colorVertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.ColorKind);
  467. colors = colorVertexBuffer.getData();
  468. colorStrideSize = colorVertexBuffer.getStrideSize();
  469. }
  470. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  471. texCoord0VertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.UVKind);
  472. texCoords0 = texCoord0VertexBuffer.getData();
  473. texCoord0StrideSize = texCoord0VertexBuffer.getStrideSize();
  474. }
  475. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  476. texCoord1VertexBuffer = babylonMesh.getVertexBuffer(BABYLON.VertexBuffer.UV2Kind);
  477. texCoords1 = texCoord1VertexBuffer.getData();
  478. texCoord1StrideSize = texCoord1VertexBuffer.getStrideSize();
  479. }
  480. // create mesh
  481. var mesh = { primitives: new Array() };
  482. mesh.primitives = [];
  483. if (babylonMesh.name) {
  484. mesh.name = babylonMesh.name;
  485. }
  486. // go through all mesh primitives (submeshes)
  487. for (var j = 0; j < babylonMesh.subMeshes.length; ++j) {
  488. var submesh = babylonMesh.subMeshes[j];
  489. var meshPrimitive = { attributes: {} };
  490. // Loop through each attribute of the submesh (mesh primitive)
  491. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.PositionKind)) {
  492. if (dataBuffer) {
  493. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.PositionKind, submesh, positions, positionStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  494. }
  495. else {
  496. // Create bufferview
  497. var byteLength = submesh.verticesCount * 12;
  498. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  499. totalByteOffset += byteLength;
  500. gltf.bufferViews.push(bufferview);
  501. // Create accessor
  502. var result = calculateMinMax(positions, submesh.verticesStart, submesh.verticesCount, positionVertexBufferOffset, positionStrideSize);
  503. var accessor = createAccessor(gltf.bufferViews.length - 1, "Position", "VEC3", 5126, submesh.verticesCount, result.min, result.max);
  504. gltf.accessors.push(accessor);
  505. meshPrimitive.attributes.POSITION = gltf.accessors.length - 1;
  506. }
  507. }
  508. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.NormalKind)) {
  509. if (dataBuffer) {
  510. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.NormalKind, submesh, normals, normalStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  511. }
  512. else {
  513. // Create bufferview
  514. var byteLength = submesh.verticesCount * 12;
  515. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  516. totalByteOffset += byteLength;
  517. gltf.bufferViews.push(bufferview);
  518. // Create accessor
  519. var accessor = createAccessor(gltf.bufferViews.length - 1, "Normal", "VEC3", 5126, submesh.verticesCount);
  520. gltf.accessors.push(accessor);
  521. meshPrimitive.attributes.NORMAL = gltf.accessors.length - 1;
  522. }
  523. }
  524. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.TangentKind)) {
  525. if (dataBuffer) {
  526. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.TangentKind, submesh, tangents, tangentStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  527. }
  528. else {
  529. // Create bufferview
  530. var byteLength = submesh.verticesCount * 16;
  531. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  532. totalByteOffset += byteLength;
  533. gltf.bufferViews.push(bufferview);
  534. // Create accessor
  535. var accessor = createAccessor(gltf.bufferViews.length - 1, "Tangent", "VEC4", 5126, submesh.verticesCount);
  536. gltf.accessors.push(accessor);
  537. meshPrimitive.attributes.TANGENT = gltf.accessors.length - 1;
  538. }
  539. }
  540. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.ColorKind)) {
  541. if (dataBuffer) {
  542. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.ColorKind, submesh, colors, colorStrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  543. }
  544. else {
  545. // Create bufferview
  546. var byteLength = submesh.verticesCount * 16;
  547. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  548. totalByteOffset += byteLength;
  549. gltf.bufferViews.push(bufferview);
  550. // Create accessor
  551. var accessor = createAccessor(gltf.bufferViews.length - 1, "Color", "VEC4", 5126, submesh.verticesCount);
  552. gltf.accessors.push(accessor);
  553. meshPrimitive.attributes.COLOR_0 = gltf.accessors.length - 1;
  554. }
  555. }
  556. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UVKind)) {
  557. if (dataBuffer) {
  558. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.UVKind, submesh, texCoords0, texCoord0StrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  559. }
  560. else {
  561. // Create bufferview
  562. var byteLength = submesh.verticesCount * 8;
  563. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  564. totalByteOffset += byteLength;
  565. gltf.bufferViews.push(bufferview);
  566. // Create accessor
  567. var accessor = createAccessor(gltf.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  568. gltf.accessors.push(accessor);
  569. meshPrimitive.attributes.TEXCOORD_0 = gltf.accessors.length - 1;
  570. }
  571. }
  572. if (babylonMesh.isVerticesDataPresent(BABYLON.VertexBuffer.UV2Kind)) {
  573. if (dataBuffer) {
  574. totalByteOffset += writeAttributeData(BABYLON.VertexBuffer.UV2Kind, submesh, texCoords1, texCoord1StrideSize, totalByteOffset, dataBuffer, useRightHandedSystem);
  575. }
  576. else {
  577. // Create bufferview
  578. var byteLength = submesh.verticesCount * 8;
  579. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  580. totalByteOffset += byteLength;
  581. gltf.bufferViews.push(bufferview);
  582. // Create accessor
  583. var accessor = createAccessor(gltf.bufferViews.length - 1, "Texture Coords", "VEC2", 5126, submesh.verticesCount);
  584. gltf.accessors.push(accessor);
  585. meshPrimitive.attributes.TEXCOORD_1 = gltf.accessors.length - 1;
  586. }
  587. }
  588. if (babylonMesh.getTotalIndices() > 0) {
  589. if (dataBuffer) {
  590. var indices = babylonMesh.getIndices();
  591. var start = submesh.indexStart;
  592. var end = submesh.indexCount + start;
  593. var byteOff = totalByteOffset;
  594. for (var k = start; k < end; k = k + 3) {
  595. dataBuffer.setUint32(byteOff, indices[k], true);
  596. byteOff += 4;
  597. dataBuffer.setUint32(byteOff, indices[k + 1], true);
  598. byteOff += 4;
  599. dataBuffer.setUint32(byteOff, indices[k + 2], true);
  600. byteOff += 4;
  601. }
  602. var byteLength = submesh.indexCount * 4;
  603. totalByteOffset += byteLength;
  604. }
  605. else {
  606. // Create bufferview
  607. var indicesCount = submesh.indexCount;
  608. var byteLength = indicesCount * 4;
  609. var bufferview = createBufferView(0, totalByteOffset, byteLength);
  610. totalByteOffset += byteLength;
  611. gltf.bufferViews.push(bufferview);
  612. // Create accessor
  613. var accessor = createAccessor(gltf.bufferViews.length - 1, "Indices", "SCALAR", 5125, indicesCount);
  614. gltf.accessors.push(accessor);
  615. meshPrimitive.indices = gltf.accessors.length - 1;
  616. }
  617. }
  618. if (babylonMesh.material) {
  619. if (!gltf.materials) {
  620. gltf.materials = new Array();
  621. }
  622. meshPrimitive.material = gltf.materials.length - 1;
  623. }
  624. mesh.primitives.push(meshPrimitive);
  625. }
  626. gltf.meshes.push(mesh);
  627. node.mesh = gltf.meshes.length - 1;
  628. gltf.nodes.push(node);
  629. scene.nodes.push(gltf.nodes.length - 1);
  630. }
  631. gltf.scenes.push(scene);
  632. return totalByteOffset;
  633. }
  634. var glTFPrefix = filename.replace(/\.[^/.]+$/, "");
  635. var gltf = {
  636. buffers: new Array(),
  637. bufferViews: new Array(),
  638. asset: { generator: "BabylonJS", version: "2.0" },
  639. meshes: new Array(),
  640. scenes: new Array(),
  641. nodes: new Array(),
  642. accessors: new Array()
  643. };
  644. var totalByteOffset = 0;
  645. var binaryBuffer;
  646. var dataBuffer;
  647. // Create scene. First pass calculates the totalByteOffset.
  648. totalByteOffset = createScene(gltf, totalByteOffset, null);
  649. var buff = { byteLength: totalByteOffset };
  650. if (!glb) {
  651. buff.uri = glTFPrefix + '.bin';
  652. }
  653. gltf.buffers.push(buff);
  654. var text = JSON.stringify(gltf, null, 2);
  655. binaryBuffer = new ArrayBuffer(totalByteOffset);
  656. dataBuffer = new DataView(binaryBuffer);
  657. totalByteOffset = 0;
  658. // Create scene. Second pass generates the binary data
  659. createScene(gltf, totalByteOffset, dataBuffer);
  660. if (glb) {
  661. var glbFile = createGLB(text, binaryBuffer, glTFPrefix);
  662. return glbFile;
  663. }
  664. var glTFFileName = glTFPrefix + '.gltf';
  665. var glTFBinFile = glTFPrefix + '.bin';
  666. var bin = new Blob([binaryBuffer], { type: 'application/octet-stream' });
  667. return _a = {},
  668. _a[glTFFileName] = text,
  669. _a[glTFBinFile] = bin,
  670. _a;
  671. var _a;
  672. };
  673. return GLTFExport;
  674. }());
  675. BABYLON.GLTFExport = GLTFExport;
  676. })(BABYLON || (BABYLON = {}));
  677. //# sourceMappingURL=babylon.glTFSerializer.js.map
  678. (function universalModuleDefinition(root, factory) {
  679. var f = factory();
  680. if (root && root["BABYLON"]) {
  681. return;
  682. }
  683. if(typeof exports === 'object' && typeof module === 'object')
  684. module.exports = f;
  685. else if(typeof define === 'function' && define.amd)
  686. define(["BJSSerializers"], factory);
  687. else if(typeof exports === 'object')
  688. exports["BJSSerializers"] = f;
  689. else {
  690. root["BABYLON"] = f;
  691. }
  692. })(this, function() {
  693. return BABYLON;
  694. });