babylon.objSerializer.ts 5.9 KB

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