public void ExportGameObjectToOBJ() { //The following exports the GameObject Onion to the persistent data path string exportPath = Application.persistentDataPath; GameObject exportObject = GameObject.Find("onion"); if (exportObject) { exportObject = exportObject.transform.GetChild(0).GetChild(0).gameObject; } else { exportObject = GameObject.Find("Meat"); if (!exportObject) { return; } else { exportObject = exportObject.transform.GetChild(0).GetChild(0).gameObject; } } PolyfewRuntime.OBJExportOptions exportOptions = new PolyfewRuntime.OBJExportOptions(true, true, true, true, true); PolyfewRuntime.ExportGameObjectToOBJ(exportObject, exportPath, () => { Debug.Log("Successfully exported GameObject: " + exportObject.name); string message = "Successfully exported the file to: \n" + Application.persistentDataPath; StartCoroutine(ShowMessage(message)); }, (Exception ex) => { Debug.LogError("Failed to export OBJ. " + ex.ToString()); }, exportOptions); }
public void ExportGameObjectToOBJ(GameObject toExport, string exportPath, PolyfewRuntime.OBJExportOptions exportOptions = null, Action OnSuccess = null) { if (Application.platform == RuntimePlatform.WebGLPlayer) { Debug.LogWarning("The function cannot run on WebGL player. As web apps cannot read from or write to local file system."); return; } //init stuff Dictionary <string, bool> materialCache = new Dictionary <string, bool>(); //Debug.Log("Exporting OBJ. Please wait.. Starting to export."); InitializeExporter(toExport, exportPath, exportOptions); //get list of required export things string objectName = toExport.gameObject.name; //work on export StringBuilder sb = new StringBuilder(); StringBuilder sbMaterials = new StringBuilder(); if (generateMaterials) { sb.AppendLine("mtllib " + objectName + ".mtl"); } int lastIndex = 0; if (meshRenderer != null && generateMaterials) { Material[] mats = meshRenderer.sharedMaterials; for (int j = 0; j < mats.Length; j++) { Material m = mats[j]; if (!materialCache.ContainsKey(m.name)) { materialCache[m.name] = true; sbMaterials.Append(MaterialToString(m)); sbMaterials.AppendLine(); } } } //export the meshhh :3 int faceOrder = (int)Mathf.Clamp((toExport.gameObject.transform.lossyScale.x * toExport.gameObject.transform.lossyScale.z), -1, 1); //export vector data (FUN :D)! foreach (Vector3 vx in meshToExport.vertices) { Vector3 v = vx; if (applyScale) { v = MultiplyVec3s(v, toExport.gameObject.transform.lossyScale); } if (applyRotation) { v = RotateAroundPoint(v, Vector3.zero, toExport.gameObject.transform.rotation); } if (applyPosition) { v += toExport.gameObject.transform.position; } v.x *= -1; sb.AppendLine("v " + v.x + " " + v.y + " " + v.z); } foreach (Vector3 vx in meshToExport.normals) { Vector3 v = vx; if (applyScale) { v = MultiplyVec3s(v, toExport.gameObject.transform.lossyScale.normalized); } if (applyRotation) { v = RotateAroundPoint(v, Vector3.zero, toExport.gameObject.transform.rotation); } v.x *= -1; sb.AppendLine("vn " + v.x + " " + v.y + " " + v.z); } foreach (Vector2 v in meshToExport.uv) { sb.AppendLine("vt " + v.x + " " + v.y); } for (int j = 0; j < meshToExport.subMeshCount; j++) { if (meshRenderer != null && j < meshRenderer.sharedMaterials.Length) { string matName = meshRenderer.sharedMaterials[j].name; sb.AppendLine("usemtl " + matName); } else { sb.AppendLine("usemtl " + objectName + "_sm" + j); } int[] tris = meshToExport.GetTriangles(j); for (int t = 0; t < tris.Length; t += 3) { int idx2 = tris[t] + 1 + lastIndex; int idx1 = tris[t + 1] + 1 + lastIndex; int idx0 = tris[t + 2] + 1 + lastIndex; if (faceOrder < 0) { sb.AppendLine("f " + ConstructOBJString(idx2) + " " + ConstructOBJString(idx1) + " " + ConstructOBJString(idx0)); } else { sb.AppendLine("f " + ConstructOBJString(idx0) + " " + ConstructOBJString(idx1) + " " + ConstructOBJString(idx2)); } } } lastIndex += meshToExport.vertices.Length; //write to disk string writePath = System.IO.Path.Combine(exportPath, objectName + ".obj"); System.IO.File.WriteAllText(writePath, sb.ToString()); if (generateMaterials) { writePath = System.IO.Path.Combine(exportPath, objectName + ".mtl"); System.IO.File.WriteAllText(writePath, sbMaterials.ToString()); } //export complete, close progress dialog OnSuccess?.Invoke(); }
private void InitializeExporter(GameObject toExport, string exportPath, PolyfewRuntime.OBJExportOptions exportOptions) { this.exportPath = exportPath; if (string.IsNullOrWhiteSpace(exportPath)) { throw new DirectoryNotFoundException("The path provided is non-existant."); } else { exportPath = Path.GetFullPath(exportPath); if (exportPath[exportPath.Length - 1] == '\\') { exportPath = exportPath.Remove(exportPath.Length - 1); } else if (exportPath[exportPath.Length - 1] == '/') { exportPath = exportPath.Remove(exportPath.Length - 1); } } if (!System.IO.Directory.Exists(exportPath)) { throw new DirectoryNotFoundException("The path provided is non-existant."); } if (toExport == null) { throw new ArgumentNullException("toExport", "Please provide a GameObject to export as OBJ file."); } meshRenderer = toExport.GetComponent <MeshRenderer>(); meshFilter = toExport.GetComponent <MeshFilter>(); if (meshRenderer == null) { } else { if (meshRenderer.isPartOfStaticBatch) { throw new InvalidOperationException("The provided object is static batched. Static batched object cannot be exported. Please disable it before trying to export the object."); } } if (meshFilter == null) { throw new InvalidOperationException("There is no MeshFilter attached to the provided GameObject."); } else { meshToExport = meshFilter.sharedMesh; if (meshToExport == null || meshToExport.triangles == null || meshToExport.triangles.Length == 0) { throw new InvalidOperationException("The MeshFilter on the provided GameObject has invalid or no mesh at all."); } } if (exportOptions != null) { applyPosition = exportOptions.applyPosition; applyRotation = exportOptions.applyRotation; applyScale = exportOptions.applyScale; generateMaterials = exportOptions.generateMaterials; exportTextures = exportOptions.exportTextures; } }