babylon.objSerializer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var OBJExport = /** @class */ (function () {
  5. function OBJExport() {
  6. }
  7. //Exports the geometry of a Mesh array in .OBJ file format (text)
  8. OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
  9. var output = [];
  10. var v = 1;
  11. if (materials) {
  12. if (!matlibname) {
  13. matlibname = 'mat';
  14. }
  15. output.push("mtllib " + matlibname + ".mtl");
  16. }
  17. for (var j = 0; j < mesh.length; j++) {
  18. output.push("g object" + j);
  19. output.push("o object_" + j);
  20. //Uses the position of the item in the scene, to the file (this back to normal in the end)
  21. var lastMatrix = null;
  22. if (globalposition) {
  23. var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
  24. lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
  25. mesh[j].bakeTransformIntoVertices(newMatrix);
  26. }
  27. //TODO: submeshes (groups)
  28. //TODO: smoothing groups (s 1, s off);
  29. if (materials) {
  30. var mat = mesh[j].material;
  31. if (mat) {
  32. output.push("usemtl " + mat.id);
  33. }
  34. }
  35. var g = mesh[j].geometry;
  36. if (!g) {
  37. BABYLON.Tools.Warn("No geometry is present on the mesh");
  38. continue;
  39. }
  40. var trunkVerts = g.getVerticesData('position');
  41. var trunkNormals = g.getVerticesData('normal');
  42. var trunkUV = g.getVerticesData('uv');
  43. var trunkFaces = g.getIndices();
  44. var curV = 0;
  45. if (!trunkVerts || !trunkFaces) {
  46. BABYLON.Tools.Warn("There are no position vertices or indices on the mesh!");
  47. continue;
  48. }
  49. for (var i = 0; i < trunkVerts.length; i += 3) {
  50. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  51. curV++;
  52. }
  53. if (trunkNormals != null) {
  54. for (i = 0; i < trunkNormals.length; i += 3) {
  55. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  56. }
  57. }
  58. if (trunkUV != null) {
  59. for (i = 0; i < trunkUV.length; i += 2) {
  60. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  61. }
  62. }
  63. for (i = 0; i < trunkFaces.length; i += 3) {
  64. var indices = [String(trunkFaces[i + 2] + v), String(trunkFaces[i + 1] + v), String(trunkFaces[i] + v)];
  65. var blanks = ["", "", ""];
  66. var facePositions = indices;
  67. var faceUVs = trunkUV != null ? indices : blanks;
  68. var faceNormals = trunkNormals != null ? indices : blanks;
  69. output.push("f " + facePositions[0] + "/" + faceUVs[0] + "/" + faceNormals[0] +
  70. " " + facePositions[1] + "/" + faceUVs[1] + "/" + faceNormals[1] +
  71. " " + facePositions[2] + "/" + faceUVs[2] + "/" + faceNormals[2]);
  72. }
  73. //back de previous matrix, to not change the original mesh in the scene
  74. if (globalposition && lastMatrix) {
  75. mesh[j].bakeTransformIntoVertices(lastMatrix);
  76. }
  77. v += curV;
  78. }
  79. var text = output.join("\n");
  80. return (text);
  81. };
  82. //Exports the material(s) of a mesh in .MTL file format (text)
  83. //TODO: Export the materials of mesh array
  84. OBJExport.MTL = function (mesh) {
  85. var output = [];
  86. var m = mesh.material;
  87. output.push("newmtl mat1");
  88. output.push(" Ns " + m.specularPower.toFixed(4));
  89. output.push(" Ni 1.5000");
  90. output.push(" d " + m.alpha.toFixed(4));
  91. output.push(" Tr 0.0000");
  92. output.push(" Tf 1.0000 1.0000 1.0000");
  93. output.push(" illum 2");
  94. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  95. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  96. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  97. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  98. //TODO: uv scale, offset, wrap
  99. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  100. var uvscale = "";
  101. if (m.ambientTexture) {
  102. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  103. }
  104. if (m.diffuseTexture) {
  105. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  106. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  107. }
  108. if (m.specularTexture) {
  109. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  110. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  111. if (m.useGlossinessFromSpecularMapAlpha) {
  112. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  113. }
  114. */
  115. }
  116. /* TODO: emissive texture not in .MAT format (???)
  117. if (m.emissiveTexture) {
  118. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  119. }
  120. */
  121. if (m.bumpTexture) {
  122. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  123. }
  124. if (m.opacityTexture) {
  125. output.push(" map_d " + uvscale + m.opacityTexture.name);
  126. }
  127. var text = output.join("\n");
  128. return (text);
  129. };
  130. return OBJExport;
  131. }());
  132. BABYLON.OBJExport = OBJExport;
  133. })(BABYLON || (BABYLON = {}));
  134. //# sourceMappingURL=babylon.objSerializer.js.map