浏览代码

Mesh .OBJ format export

BABYLON.OBJExport.OBJ = Mesh in .OBJ format
BABYLON.OBJExport.MTL = Mesh material(s) in .MTL format
BABYLON.Tools.FileAsURL = download link to a (text) file as Blob
László Matuska, BitOfGold 10 年之前
父节点
当前提交
3b677ef969
共有 3 个文件被更改,包括 85 次插入0 次删除
  1. 60 0
      src/Tools/babylon.sceneSerializer.ts
  2. 16 0
      src/Tools/babylon.tools.ts
  3. 9 0
      src/babylon.mixins.ts

+ 60 - 0
src/Tools/babylon.sceneSerializer.ts

@@ -951,6 +951,66 @@
             return serializationObject;
         }
     }
+
+    export class OBJExport {
+
+        //Exports the geometry of a Mesh in .OBJ file format (text)
+        public static OBJ(mesh:Mesh, materials?:boolean):string {
+            var output = [];
+            var g = mesh.geometry;
+            var trunkVerts = g.getVerticesData('position');
+            var trunkNormals = g.getVerticesData('normal');
+            var trunkUV = g.getVerticesData('uv');
+            var trunkFaces = g.getIndices();
+            if (materials) {
+                output.push("mtllib mat.mtl");
+            }
+            for (var i = 0; i < trunkVerts.length; i += 3) {
+                output.push("v " + trunkVerts[i] + " " + trunkVerts[i + 1] + " " + trunkVerts[i + 2]);
+            }
+            for (i = 0; i < trunkNormals.length; i += 3) {
+                output.push("vn " + trunkNormals[i] + " " + trunkNormals[i + 1] + " " + trunkNormals[i + 2]);
+            }
+            for (i = 0; i < trunkUV.length; i += 2) {
+                output.push("vt " + trunkUV[i] + " " + trunkUV[i + 1]);
+            }
+            output.push("g gr1");
+            if (materials) {
+                output.push("usemtl mat1");
+            }
+            for (i = 0; i < trunkFaces.length; i += 3) {
+                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)
+                );
+            }
+            var text = output.join("\n");
+            return (text);
+        }
+
+        //Exports the material(s) of a mesh in .MTL file format (text)
+        public static MTL(mesh:Mesh):string {
+            var output = [];
+            output.push("newmtl mat1");
+            output.push("  Ns 10.0000");
+            output.push("  Ni 1.5000");
+            output.push("  d 1.0000");
+            output.push("  Tr 0.0000");
+            output.push("  Tf 1.0000 1.0000 1.0000");
+            output.push("  illum 2");
+            output.push("  Ka 0.0000 0.0000 0.0000");
+            output.push("  Kd 1.0000 1.0000 1.0000");
+            output.push("  Ks 1.0000 1.0000 1.0000");
+            output.push("  Ke 0.0000 0.0000 0.0000");
+            //output.push("  map_Ka cube.png");
+            //output.push("  map_Kd cube.png");
+            var text = output.join("\n");
+            return(text);
+        }
+
+    }
+
 }
 
 

+ 16 - 0
src/Tools/babylon.tools.ts

@@ -341,6 +341,22 @@
             }
         }
 
+        //returns a downloadable url to a file content.
+        public static FileAsURL(content:string): string {
+            var fileBlob;
+            try {
+                fileBlob = new Blob([content]);
+            } catch(e) {
+                var blobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder;
+                var bb = new blobBuilder();
+                bb.append([content]);
+                fileBlob = bb.getBlob();
+            }
+            var url = window.URL || window.webkitURL;
+            var link:string = url.createObjectURL(fileBlob);
+            return link;
+        }
+
         // Misc.   
         public static Clamp(value: number, min = 0, max = 1): number {
             return Math.min(max, Math.max(min, value));

+ 9 - 0
src/babylon.mixins.ts

@@ -18,12 +18,21 @@ interface Window {
     SIMD: any;
     AudioContext: AudioContext;
     webkitAudioContext: AudioContext;
+    BlobBuilder: HTMLBLOBBUILDER;
+    MozBlobBuilder: HTMLBLOBBUILDER;
+    WebKitBlobBuilder: HTMLBLOBBUILDER;
 }
 
 interface HTMLURL {
     createObjectURL(param1: any, param2?: any);
 }
 
+interface HTMLBLOBBUILDER {
+    new(): HTMLBLOBBUILDER;
+    append(param1: any);
+    getBlob();
+}
+
 interface Document {
     exitFullscreen(): void;
     webkitCancelFullScreen(): void;