babylonjs.serializers.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. var babylonDependency; try { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("../babylon.max")); } catch (e) { babylonDependency = BABYLON || (typeof require !== 'undefined' && require("babylonjs")); }
  2. var BABYLON = babylonDependency;
  3. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  4. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  5. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  6. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  7. return c > 3 && r && Object.defineProperty(target, key, r), r;
  8. };
  9. var __extends = (this && this.__extends) || (function () {
  10. var extendStatics = Object.setPrototypeOf ||
  11. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  12. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  13. return function (d, b) {
  14. extendStatics(d, b);
  15. function __() { this.constructor = d; }
  16. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  17. };
  18. })();
  19. var BABYLON;
  20. (function (BABYLON) {
  21. var OBJExport = (function () {
  22. function OBJExport() {
  23. }
  24. //Exports the geometrys of a Mesh array in .OBJ file format (text)
  25. OBJExport.OBJ = function (mesh, materials, matlibname, globalposition) {
  26. var output = [];
  27. var v = 1;
  28. if (materials) {
  29. if (!matlibname) {
  30. matlibname = 'mat';
  31. }
  32. output.push("mtllib " + matlibname + ".mtl");
  33. }
  34. for (var j = 0; j < mesh.length; j++) {
  35. output.push("g object" + j);
  36. output.push("o object_" + j);
  37. //Uses the position of the item in the scene, to the file (this back to normal in the end)
  38. if (globalposition) {
  39. var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x, mesh[j].position.y, mesh[j].position.z);
  40. var lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x), -(mesh[j].position.y), -(mesh[j].position.z));
  41. mesh[j].bakeTransformIntoVertices(newMatrix);
  42. }
  43. //TODO: submeshes (groups)
  44. //TODO: smoothing groups (s 1, s off);
  45. if (materials) {
  46. output.push("usemtl " + mesh[j].material.id);
  47. }
  48. var g = mesh[j].geometry;
  49. var trunkVerts = g.getVerticesData('position');
  50. var trunkNormals = g.getVerticesData('normal');
  51. var trunkUV = g.getVerticesData('uv');
  52. var trunkFaces = g.getIndices();
  53. var curV = 0;
  54. for (var i = 0; i < trunkVerts.length; i += 3) {
  55. output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
  56. curV++;
  57. }
  58. for (i = 0; i < trunkNormals.length; i += 3) {
  59. output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
  60. }
  61. for (i = 0; i < trunkUV.length; i += 2) {
  62. output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
  63. }
  64. for (i = 0; i < trunkFaces.length; i += 3) {
  65. output.push("f " + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
  66. " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
  67. " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v));
  68. }
  69. //back de previous matrix, to not change the original mesh in the scene
  70. if (globalposition) {
  71. mesh[j].bakeTransformIntoVertices(lastMatrix);
  72. }
  73. v += curV;
  74. }
  75. var text = output.join("\n");
  76. return (text);
  77. };
  78. //Exports the material(s) of a mesh in .MTL file format (text)
  79. //TODO: Export the materials of mesh array
  80. OBJExport.MTL = function (mesh) {
  81. var output = [];
  82. var m = mesh.material;
  83. output.push("newmtl mat1");
  84. output.push(" Ns " + m.specularPower.toFixed(4));
  85. output.push(" Ni 1.5000");
  86. output.push(" d " + m.alpha.toFixed(4));
  87. output.push(" Tr 0.0000");
  88. output.push(" Tf 1.0000 1.0000 1.0000");
  89. output.push(" illum 2");
  90. output.push(" Ka " + m.ambientColor.r.toFixed(4) + " " + m.ambientColor.g.toFixed(4) + " " + m.ambientColor.b.toFixed(4));
  91. output.push(" Kd " + m.diffuseColor.r.toFixed(4) + " " + m.diffuseColor.g.toFixed(4) + " " + m.diffuseColor.b.toFixed(4));
  92. output.push(" Ks " + m.specularColor.r.toFixed(4) + " " + m.specularColor.g.toFixed(4) + " " + m.specularColor.b.toFixed(4));
  93. output.push(" Ke " + m.emissiveColor.r.toFixed(4) + " " + m.emissiveColor.g.toFixed(4) + " " + m.emissiveColor.b.toFixed(4));
  94. //TODO: uv scale, offset, wrap
  95. //TODO: UV mirrored in Blender? second UV channel? lightMap? reflection textures?
  96. var uvscale = "";
  97. if (m.ambientTexture) {
  98. output.push(" map_Ka " + uvscale + m.ambientTexture.name);
  99. }
  100. if (m.diffuseTexture) {
  101. output.push(" map_Kd " + uvscale + m.diffuseTexture.name);
  102. //TODO: alpha testing, opacity in diffuse texture alpha channel (diffuseTexture.hasAlpha -> map_d)
  103. }
  104. if (m.specularTexture) {
  105. output.push(" map_Ks " + uvscale + m.specularTexture.name);
  106. /* TODO: glossiness = specular highlight component is in alpha channel of specularTexture. (???)
  107. if (m.useGlossinessFromSpecularMapAlpha) {
  108. output.push(" map_Ns "+uvscale + m.specularTexture.name);
  109. }
  110. */
  111. }
  112. /* TODO: emissive texture not in .MAT format (???)
  113. if (m.emissiveTexture) {
  114. output.push(" map_d "+uvscale+m.emissiveTexture.name);
  115. }
  116. */
  117. if (m.bumpTexture) {
  118. output.push(" map_bump -imfchan z " + uvscale + m.bumpTexture.name);
  119. }
  120. if (m.opacityTexture) {
  121. output.push(" map_d " + uvscale + m.opacityTexture.name);
  122. }
  123. var text = output.join("\n");
  124. return (text);
  125. };
  126. return OBJExport;
  127. }());
  128. BABYLON.OBJExport = OBJExport;
  129. })(BABYLON || (BABYLON = {}));
  130. //# sourceMappingURL=babylon.objSerializer.js.map
  131. (function universalModuleDefinition(root, factory) {
  132. if (root && root["BABYLON"]) {
  133. return;
  134. }
  135. if(typeof exports === 'object' && typeof module === 'object')
  136. module.exports = factory();
  137. else if(typeof define === 'function' && define.amd)
  138. define([], factory);
  139. else if(typeof exports === 'object')
  140. exports["BJSSerializers"] = factory();
  141. else {
  142. root["BABYLON"] = factory();
  143. }
  144. })(this, function() {
  145. return BABYLON;
  146. });