babylon.objSerializer.ts 4.9 KB

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