Browse Source

Export multiple meshes and use de global position

is possible export multiple meshes to obj, fot that reason i create a loop to get all meshes in array.
Felipe Baltazar 8 years ago
parent
commit
b683beced1
1 changed files with 40 additions and 22 deletions
  1. 40 22
      serializers/src/OBJ/babylon.objSerializer.ts

+ 40 - 22
serializers/src/OBJ/babylon.objSerializer.ts

@@ -2,22 +2,41 @@
 
 
 module BABYLON {
 module BABYLON {
  export class OBJExport {
  export class OBJExport {
-        //Exports the geometry of a Mesh in .OBJ file format (text)
-        public static OBJ(mesh: Mesh, materials?: boolean, matlibname?: string): string {
-            var output = [];
-            var g = mesh.geometry;
+        //Exports the geometrys of a Mesh array in .OBJ file format (text)
+        public static OBJ(mesh: Mesh[], materials?: boolean, matlibname?: string, globalposition?:boolean): string {
+        var output = [];
+        var v = 1;
+         if (materials) {
+         if (!matlibname) {
+           matlibname = 'mat';
+          }
+          output.push("mtllib " + matlibname + ".mtl");
+          }
+        for (var j = 0; j < mesh.length; j++) {
+            
+            output.push("g object" + j);
+            output.push("o object_" + j);
+         if(globalposition){
+         var newMatrix = BABYLON.Matrix.Translation(mesh[j].position.x,mesh[j].position.y,mesh[j].position.z);
+	        var lastMatrix = BABYLON.Matrix.Translation(-(mesh[j].position.x),-(mesh[j].position.y),-(mesh[j].position.z));
+         mesh.bakeTransformIntoVertices(newMatrix); 
+         }
+         
+             //TODO: submeshes (groups)
+            //TODO: smoothing groups (s 1, s off);
+            if (materials) {
+            output.push("usemtl " + mesh[j].material.id);
+            }
+            var g = mesh[j].geometry;
             var trunkVerts = g.getVerticesData('position');
             var trunkVerts = g.getVerticesData('position');
             var trunkNormals = g.getVerticesData('normal');
             var trunkNormals = g.getVerticesData('normal');
             var trunkUV = g.getVerticesData('uv');
             var trunkUV = g.getVerticesData('uv');
             var trunkFaces = g.getIndices();
             var trunkFaces = g.getIndices();
-            if (materials) {
-                if (!matlibname) {
-                    matlibname = 'mat';
-                }
-                output.push("mtllib " + matlibname + ".mtl");
-            }
+            var curV = 0;
+
             for (var i = 0; i < trunkVerts.length; i += 3) {
             for (var i = 0; i < trunkVerts.length; i += 3) {
                 output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
                 output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
+                curV++;
             }
             }
             for (i = 0; i < trunkNormals.length; i += 3) {
             for (i = 0; i < trunkNormals.length; i += 3) {
                 output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
                 output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
@@ -25,26 +44,25 @@ module BABYLON {
             for (i = 0; i < trunkUV.length; i += 2) {
             for (i = 0; i < trunkUV.length; i += 2) {
                 output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
                 output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
             }
             }
-
-            //TODO: submeshes (groups)
-            //TODO: smoothing groups (s 1, s off)
-
-            output.push("g gr1");
-            if (materials) {
-                output.push("usemtl mat1");
-            }
+         
             for (i = 0; i < trunkFaces.length; i += 3) {
             for (i = 0; i < trunkFaces.length; i += 3) {
                 output.push(
                 output.push(
-                    "f " + (trunkFaces[i + 2] + 1) + "/" + (trunkFaces[i + 2] + 1) + "/" + (trunkFaces[i + 2] + 1) +
-                    " " + (trunkFaces[i + 1] + 1) + "/" + (trunkFaces[i + 1] + 1) + "/" + (trunkFaces[i + 1] + 1) +
-                    " " + (trunkFaces[i] + 1) + "/" + (trunkFaces[i] + 1) + "/" + (trunkFaces[i] + 1)
+                    "f " + (trunkFaces[i + 2] +  v) + "/" + (trunkFaces[i + 2] + v) + "/" + (trunkFaces[i + 2] + v) +
+                    " " + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) + "/" + (trunkFaces[i + 1] + v) +
+                    " " + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v) + "/" + (trunkFaces[i] + v)
                 );
                 );
             }
             }
+         if(globalposition){
+          mesh.bakeTransformIntoVertices(lastMatrix);
+         }
+           v += curV;
+          }
             var text = output.join("\n");
             var text = output.join("\n");
             return (text);
             return (text);
         }
         }
 
 
         //Exports the material(s) of a mesh in .MTL file format (text)
         //Exports the material(s) of a mesh in .MTL file format (text)
+        //TODO: Export the materials of mesh array
         public static MTL(mesh: Mesh): string {
         public static MTL(mesh: Mesh): string {
             var output = [];
             var output = [];
             var m = <StandardMaterial>mesh.material;
             var m = <StandardMaterial>mesh.material;
@@ -100,4 +118,4 @@ module BABYLON {
             return (text);
             return (text);
         }
         }
     }
     }
-}
+}