babylon.objSerializer.js 5.7 KB

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