babylon.objSerializer.js 6.0 KB

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