babylon.objSerializer.ts 6.4 KB

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