babylon.objSerializer.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. export class OBJExport {
  4. //Exports the geometry of a Mesh in .OBJ file format (text)
  5. public static OBJ(mesh: Mesh, materials?: boolean, matlibname?: string): string {
  6. var output = [];
  7. var g = mesh.geometry;
  8. var trunkVerts = g.getVerticesData('position');
  9. var trunkNormals = g.getVerticesData('normal');
  10. var trunkUV = g.getVerticesData('uv');
  11. var trunkFaces = g.getIndices();
  12. if (materials) {
  13. if (!matlibname) {
  14. matlibname = 'mat';
  15. }
  16. output.push("mtllib " + matlibname + ".mtl");
  17. }
  18. for (var i = 0; i < trunkVerts.length; i += 3) {
  19. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  20. }
  21. for (i = 0; i < trunkNormals.length; i += 3) {
  22. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  23. }
  24. for (i = 0; i < trunkUV.length; i += 2) {
  25. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  26. }
  27. //TODO: submeshes (groups)
  28. //TODO: smoothing groups (s 1, s off)
  29. output.push("g gr1");
  30. if (materials) {
  31. output.push("usemtl mat1");
  32. }
  33. for (i = 0; i < trunkFaces.length; i += 3) {
  34. output.push(
  35. "f " + (trunkFaces[i + 2] + 1) + "/" + (trunkFaces[i + 2] + 1) + "/" + (trunkFaces[i + 2] + 1) +
  36. " " + (trunkFaces[i + 1] + 1) + "/" + (trunkFaces[i + 1] + 1) + "/" + (trunkFaces[i + 1] + 1) +
  37. " " + (trunkFaces[i] + 1) + "/" + (trunkFaces[i] + 1) + "/" + (trunkFaces[i] + 1)
  38. );
  39. }
  40. var text = output.join("\n");
  41. return (text);
  42. }
  43. //Exports the material(s) of a mesh in .MTL file format (text)
  44. public static MTL(mesh: Mesh): string {
  45. var output = [];
  46. var m = <StandardMaterial>mesh.material;
  47. output.push("newmtl mat1");
  48. output.push(" Ns " + m.specularPower.toFixed(4));
  49. output.push(" Ni 1.5000");
  50. output.push(" d " + m.alpha.toFixed(4));
  51. output.push(" Tr 0.0000");
  52. output.push(" Tf 1.0000 1.0000 1.0000");
  53. output.push(" illum 2");
  54. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  55. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  56. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  57. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  58. //TODO: uv scale, offset, wrap
  59. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  60. var uvscale = "";
  61. if (m.ambientTexture) {
  62. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  63. }
  64. if (m.diffuseTexture) {
  65. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  66. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  67. }
  68. if (m.specularTexture) {
  69. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  70. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  71. if (m.useGlossinessFromSpecularMapAlpha) {
  72. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  73. }
  74. */
  75. }
  76. /* TODO: emissive texture not in .MAT format (???)
  77. if (m.emissiveTexture) {
  78. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  79. }
  80. */
  81. if (m.bumpTexture) {
  82. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  83. }
  84. if (m.opacityTexture) {
  85. output.push(" map_d " + uvscale + m.opacityTexture.name);
  86. }
  87. var text = output.join("\n");
  88. return (text);
  89. }
  90. }
  91. }