babylon.objSerializer.js 4.2 KB

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