babylon.objSerializer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 geometrys 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. continue;
  38. }
  39. var trunkVerts = g.getVerticesData('position');
  40. var trunkNormals = g.getVerticesData('normal');
  41. var trunkUV = g.getVerticesData('uv');
  42. var trunkFaces = g.getIndices();
  43. var curV = 0;
  44. if (!trunkVerts || !trunkNormals || !trunkUV || !trunkFaces) {
  45. continue;
  46. }
  47. for (var i = 0; i < trunkVerts.length; i += 3) {
  48. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  49. curV++;
  50. }
  51. for (i = 0; i < trunkNormals.length; i += 3) {
  52. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  53. }
  54. for (i = 0; i < trunkUV.length; i += 2) {
  55. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  56. }
  57. for (i = 0; i < trunkFaces.length; i += 3) {
  58. output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
  59. " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
  60. " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
  61. }
  62. //back de previous matrix, to not change the original mesh in the scene
  63. if (globalposition && lastMatrix) {
  64. mesh[j].bakeTransformIntoVertices(lastMatrix);
  65. }
  66. v += curV;
  67. }
  68. var text = output.join("\n");
  69. return (text);
  70. };
  71. //Exports the material(s) of a mesh in .MTL file format (text)
  72. //TODO: Export the materials of mesh array
  73. OBJExport.MTL = function (mesh) {
  74. var output = [];
  75. var m = mesh.material;
  76. output.push("newmtl mat1");
  77. output.push(" Ns " + m.specularPower.toFixed(4));
  78. output.push(" Ni 1.5000");
  79. output.push(" d " + m.alpha.toFixed(4));
  80. output.push(" Tr 0.0000");
  81. output.push(" Tf 1.0000 1.0000 1.0000");
  82. output.push(" illum 2");
  83. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  84. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  85. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  86. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  87. //TODO: uv scale, offset, wrap
  88. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  89. var uvscale = "";
  90. if (m.ambientTexture) {
  91. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  92. }
  93. if (m.diffuseTexture) {
  94. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  95. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  96. }
  97. if (m.specularTexture) {
  98. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  99. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  100. if (m.useGlossinessFromSpecularMapAlpha) {
  101. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  102. }
  103. */
  104. }
  105. /* TODO: emissive texture not in .MAT format (???)
  106. if (m.emissiveTexture) {
  107. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  108. }
  109. */
  110. if (m.bumpTexture) {
  111. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  112. }
  113. if (m.opacityTexture) {
  114. output.push(" map_d " + uvscale + m.opacityTexture.name);
  115. }
  116. var text = output.join("\n");
  117. return (text);
  118. };
  119. return OBJExport;
  120. }());
  121. BABYLON.OBJExport = OBJExport;
  122. })(BABYLON || (BABYLON = {}));
  123. //# sourceMappingURL=babylon.objSerializer.js.map