public static string MeshToString(AssignStructs.MeshInfo m) { StringBuilder sb = new StringBuilder(); sb.Append("g ").Append(m.name).Append("\n"); foreach (Vector3 v in m.vertices) { sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, v.z)); } sb.Append("\n"); foreach (Vector3 v in m.normals) { sb.Append(string.Format("vn {0} {1} {2}\n", v.x, v.y, v.z)); } sb.Append("\n"); foreach (Vector2 v in m.uvs) { sb.Append(string.Format("vt {0} {1}\n", v.x, v.y)); } for (int i = 0; i < m.triangles.Length; i += 3) { sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", m.triangles[i] + 1, m.triangles[i + 1] + 1, m.triangles[i + 2] + 1)); } return(sb.ToString()); }
public static void MeshToFile(AssignStructs.MeshInfo m) { string filename = m.name + ".obj"; using (StreamWriter sw = new StreamWriter(filename)) { sw.Write(MeshToString(m)); } }
void createOBJ() { if (objMade == false) { objMade = true; _vertBuffer.GetData(inValues); print(inValues[0]); int numVertsTotal = ribbonWidth * 3 * 2 * (ribbonLength); AssignStructs.MeshInfo mesh = new AssignStructs.MeshInfo(); Vector3[] verts = new Vector3[ribbonLength * ribbonWidth]; Vector3[] norms = new Vector3[ribbonLength * ribbonWidth]; Vector2[] uvs = new Vector2[ribbonLength * ribbonWidth]; int[] triangles = new int[numVertsTotal]; for (int i = 0; i < ribbonWidth * ribbonLength; i++) { int baseID = i * AssignStructs.VertC4StructSize; Vector3 v = new Vector3(inValues[baseID + 0], inValues[baseID + 1], inValues[baseID + 2]); Vector3 n = new Vector3(inValues[baseID + 6], inValues[baseID + 7], inValues[baseID + 8]); Vector2 u = new Vector2(inValues[baseID + 9], inValues[baseID + 10]); verts[i] = v; norms[i] = n; uvs[i] = u; } for (int i = 0; i < numVertsTotal; i++) { triangles[i] = getID(i); } mesh.vertices = verts; mesh.normals = norms; mesh.uvs = uvs; mesh.triangles = triangles; mesh.name = "DONTUTEST"; //string objString = makeString( mesh , transform ); //print( objString ); //WriteToFile( objString , "DONUT_TEST.obj"); ObjExporter.MeshToFile(mesh); } }