public static OBJData LoadOBJ(Stream lStream) { OBJLoader.m_OBJData = new OBJData(); OBJLoader.m_CurrentMaterial = null; OBJLoader.m_CurrentGroup = null; StreamReader streamReader = new StreamReader(lStream); Action<string> action = null; while (!streamReader.EndOfStream) { string text = streamReader.ReadLine(); if (!StringExt.IsNullOrWhiteSpace(text) && text[0] != '#') { string[] array = text.Trim().Split(null, 2); if (array.Length >= 2) { string text2 = array[0].Trim(); string obj = array[1].Trim(); action = null; OBJLoader.m_ParseOBJActionDictionary.TryGetValue(text2.ToLowerInvariant(), out action); if (action != null) { action(obj); } } } } OBJData oBJData = OBJLoader.m_OBJData; OBJLoader.m_OBJData = null; return oBJData; }
public static OBJData LoadOBJ(Stream lStream) { OBJLoader.m_OBJData = new OBJData(); OBJLoader.m_CurrentMaterial = null; OBJLoader.m_CurrentGroup = null; StreamReader streamReader = new StreamReader(lStream); Action <string> action = null; while (!streamReader.EndOfStream) { string text = streamReader.ReadLine(); if (!StringExt.IsNullOrWhiteSpace(text) && text[0] != '#') { string[] array = text.Trim().Split(null, 2); if (array.Length >= 2) { string text2 = array[0].Trim(); string obj = array[1].Trim(); action = null; OBJLoader.m_ParseOBJActionDictionary.TryGetValue(text2.ToLowerInvariant(), out action); if (action != null) { action(obj); } } } } OBJData oBJData = OBJLoader.m_OBJData; OBJLoader.m_OBJData = null; return(oBJData); }
public static OBJData EncodeOBJ(this Mesh lMesh) { OBJData oBJData = new OBJData { m_Vertices = new List <Vector3>(lMesh.vertices), m_UVs = new List <Vector2>(lMesh.uv), m_Normals = new List <Vector3>(lMesh.normals), m_UV2s = new List <Vector2>(lMesh.uv2), m_Colors = new List <Color>(lMesh.colors) }; for (int i = 0; i < lMesh.subMeshCount; i++) { int[] triangles = lMesh.GetTriangles(i); OBJGroup oBJGroup = new OBJGroup(lMesh.name + "_" + i.ToString()); for (int j = 0; j < triangles.Length; j += 3) { OBJFace oBJFace = new OBJFace(); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j] : -1 }); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 1] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 1] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 1] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 1] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 1] : -1 }); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 2] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 2] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 2] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 2] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 2] : -1 }); oBJGroup.AddFace(oBJFace); } oBJData.m_Groups.Add(oBJGroup); } return(oBJData); }
public static void LoadOBJ(this Mesh lMesh, OBJData lData) { List <Vector3> list = new List <Vector3>(); List <Vector3> list2 = new List <Vector3>(); List <Vector2> list3 = new List <Vector2>(); List <int>[] array = new List <int> [lData.m_Groups.Count]; Dictionary <OBJFaceVertex, int> dictionary = new Dictionary <OBJFaceVertex, int>(); bool flag = lData.m_Normals.Count > 0; bool flag2 = lData.m_UVs.Count > 0; lMesh.subMeshCount = lData.m_Groups.Count; for (int i = 0; i < lData.m_Groups.Count; i++) { OBJGroup oBJGroup = lData.m_Groups[i]; array[i] = new List <int>(); for (int j = 0; j < oBJGroup.Faces.Count; j++) { OBJFace oBJFace = oBJGroup.Faces[j]; for (int k = 1; k < oBJFace.Count - 1; k++) { int[] array2 = new int[] { 0, k, k + 1 }; for (int l = 0; l < array2.Length; l++) { int i2 = array2[l]; OBJFaceVertex oBJFaceVertex = oBJFace[i2]; int item = -1; if (!dictionary.TryGetValue(oBJFaceVertex, out item)) { dictionary[oBJFaceVertex] = list.Count; item = list.Count; list.Add(lData.m_Vertices[oBJFaceVertex.m_VertexIndex]); if (flag2) { list3.Add(lData.m_UVs[oBJFaceVertex.m_UVIndex]); } if (flag) { list2.Add(lData.m_Normals[oBJFaceVertex.m_NormalIndex]); } } array[i].Add(item); } } } } lMesh.triangles = new int[0]; lMesh.vertices = list.ToArray(); lMesh.uv = list3.ToArray(); lMesh.normals = list2.ToArray(); if (!flag) { lMesh.RecalculateNormals(); } lMesh.RecalculateTangents(); for (int m = 0; m < lData.m_Groups.Count; m++) { lMesh.SetTriangles(array[m].ToArray(), m); } }
public static void LoadOBJ(this Mesh lMesh, OBJData lData, string subOject) { List <Vector3> list = new List <Vector3>(); List <Vector3> list2 = new List <Vector3>(); List <Vector2> list3 = new List <Vector2>(); List <int> list4 = new List <int>(); Dictionary <OBJFaceVertex, int> dictionary = new Dictionary <OBJFaceVertex, int>(); bool flag = lData.m_Normals.Count > 0; bool flag2 = lData.m_UVs.Count > 0; for (int i = 0; i < lData.m_Groups.Count; i++) { OBJGroup oBJGroup = lData.m_Groups[i]; if (!(oBJGroup.m_Name != subOject)) { for (int j = 0; j < oBJGroup.Faces.Count; j++) { OBJFace oBJFace = oBJGroup.Faces[j]; for (int k = 1; k < oBJFace.Count - 1; k++) { int[] array = new int[] { 0, k, k + 1 }; for (int l = 0; l < array.Length; l++) { int i2 = array[l]; OBJFaceVertex oBJFaceVertex = oBJFace[i2]; int item = -1; if (!dictionary.TryGetValue(oBJFaceVertex, out item)) { dictionary[oBJFaceVertex] = list.Count; item = list.Count; list.Add(lData.m_Vertices[oBJFaceVertex.m_VertexIndex]); if (flag2) { list3.Add(lData.m_UVs[oBJFaceVertex.m_UVIndex]); } if (flag) { list2.Add(lData.m_Normals[oBJFaceVertex.m_NormalIndex]); } } list4.Add(item); } } } } } if (list4.Count == 0) { return; } lMesh.vertices = list.ToArray(); lMesh.triangles = list4.ToArray(); lMesh.uv = list3.ToArray(); lMesh.normals = list2.ToArray(); if (!flag) { lMesh.RecalculateNormals(); } lMesh.RecalculateTangents(); }
public static void LoadOBJ(this Mesh lMesh, OBJData lData) { List<Vector3> list = new List<Vector3>(); List<Vector3> list2 = new List<Vector3>(); List<Vector2> list3 = new List<Vector2>(); List<int>[] array = new List<int>[lData.m_Groups.Count]; Dictionary<OBJFaceVertex, int> dictionary = new Dictionary<OBJFaceVertex, int>(); bool flag = lData.m_Normals.Count > 0; bool flag2 = lData.m_UVs.Count > 0; lMesh.subMeshCount = lData.m_Groups.Count; for (int i = 0; i < lData.m_Groups.Count; i++) { OBJGroup oBJGroup = lData.m_Groups[i]; array[i] = new List<int>(); for (int j = 0; j < oBJGroup.Faces.Count; j++) { OBJFace oBJFace = oBJGroup.Faces[j]; for (int k = 1; k < oBJFace.Count - 1; k++) { int[] array2 = new int[] { 0, k, k + 1 }; for (int l = 0; l < array2.Length; l++) { int i2 = array2[l]; OBJFaceVertex oBJFaceVertex = oBJFace[i2]; int item = -1; if (!dictionary.TryGetValue(oBJFaceVertex, out item)) { dictionary[oBJFaceVertex] = list.Count; item = list.Count; list.Add(lData.m_Vertices[oBJFaceVertex.m_VertexIndex]); if (flag2) { list3.Add(lData.m_UVs[oBJFaceVertex.m_UVIndex]); } if (flag) { list2.Add(lData.m_Normals[oBJFaceVertex.m_NormalIndex]); } } array[i].Add(item); } } } } lMesh.triangles = new int[0]; lMesh.vertices = list.ToArray(); lMesh.uv = list3.ToArray(); lMesh.normals = list2.ToArray(); if (!flag) { lMesh.RecalculateNormals(); } lMesh.RecalculateTangents(); for (int m = 0; m < lData.m_Groups.Count; m++) { lMesh.SetTriangles(array[m].ToArray(), m); } }
public static OBJData EncodeOBJ(this Mesh lMesh) { OBJData oBJData = new OBJData { m_Vertices = new List<Vector3>(lMesh.vertices), m_UVs = new List<Vector2>(lMesh.uv), m_Normals = new List<Vector3>(lMesh.normals), m_UV2s = new List<Vector2>(lMesh.uv2), m_Colors = new List<Color>(lMesh.colors) }; for (int i = 0; i < lMesh.subMeshCount; i++) { int[] triangles = lMesh.GetTriangles(i); OBJGroup oBJGroup = new OBJGroup(lMesh.name + "_" + i.ToString()); for (int j = 0; j < triangles.Length; j += 3) { OBJFace oBJFace = new OBJFace(); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j] : -1 }); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 1] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 1] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 1] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 1] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 1] : -1 }); oBJFace.AddVertex(new OBJFaceVertex { m_VertexIndex = (oBJData.m_Vertices.Count > 0) ? triangles[j + 2] : -1, m_UVIndex = (oBJData.m_UVs.Count > 0) ? triangles[j + 2] : -1, m_NormalIndex = (oBJData.m_Normals.Count > 0) ? triangles[j + 2] : -1, m_UV2Index = (oBJData.m_UV2s.Count > 0) ? triangles[j + 2] : -1, m_ColorIndex = (oBJData.m_Colors.Count > 0) ? triangles[j + 2] : -1 }); oBJGroup.AddFace(oBJFace); } oBJData.m_Groups.Add(oBJGroup); } return oBJData; }
public static void LoadOBJ(this Mesh lMesh, OBJData lData, string subOject) { List<Vector3> list = new List<Vector3>(); List<Vector3> list2 = new List<Vector3>(); List<Vector2> list3 = new List<Vector2>(); List<int> list4 = new List<int>(); Dictionary<OBJFaceVertex, int> dictionary = new Dictionary<OBJFaceVertex, int>(); bool flag = lData.m_Normals.Count > 0; bool flag2 = lData.m_UVs.Count > 0; for (int i = 0; i < lData.m_Groups.Count; i++) { OBJGroup oBJGroup = lData.m_Groups[i]; if (!(oBJGroup.m_Name != subOject)) { for (int j = 0; j < oBJGroup.Faces.Count; j++) { OBJFace oBJFace = oBJGroup.Faces[j]; for (int k = 1; k < oBJFace.Count - 1; k++) { int[] array = new int[] { 0, k, k + 1 }; for (int l = 0; l < array.Length; l++) { int i2 = array[l]; OBJFaceVertex oBJFaceVertex = oBJFace[i2]; int item = -1; if (!dictionary.TryGetValue(oBJFaceVertex, out item)) { dictionary[oBJFaceVertex] = list.Count; item = list.Count; list.Add(lData.m_Vertices[oBJFaceVertex.m_VertexIndex]); if (flag2) { list3.Add(lData.m_UVs[oBJFaceVertex.m_UVIndex]); } if (flag) { list2.Add(lData.m_Normals[oBJFaceVertex.m_NormalIndex]); } } list4.Add(item); } } } } } if (list4.Count == 0) { return; } lMesh.vertices = list.ToArray(); lMesh.triangles = list4.ToArray(); lMesh.uv = list3.ToArray(); lMesh.normals = list2.ToArray(); if (!flag) { lMesh.RecalculateNormals(); } lMesh.RecalculateTangents(); }
public static void ExportOBJ(OBJData lData, Stream lStream) { StreamWriter streamWriter = new StreamWriter(lStream); streamWriter.WriteLine(string.Format("# File exported by Unity3D version {0}", Application.unityVersion)); for (int i = 0; i < lData.m_Vertices.Count; i++) { TextWriter arg_88_0 = streamWriter; string arg_83_0 = "v {0} {1} {2}"; float x = lData.m_Vertices[i].x; object arg_83_1 = x.ToString("n8"); float y = lData.m_Vertices[i].y; object arg_83_2 = y.ToString("n8"); float z = lData.m_Vertices[i].z; arg_88_0.WriteLine(string.Format(arg_83_0, arg_83_1, arg_83_2, z.ToString("n8"))); } for (int j = 0; j < lData.m_UVs.Count; j++) { TextWriter arg_EC_0 = streamWriter; string arg_E7_0 = "vt {0} {1}"; float x2 = lData.m_UVs[j].x; object arg_E7_1 = x2.ToString("n5"); float y2 = lData.m_UVs[j].y; arg_EC_0.WriteLine(string.Format(arg_E7_0, arg_E7_1, y2.ToString("n5"))); } for (int k = 0; k < lData.m_UV2s.Count; k++) { TextWriter arg_150_0 = streamWriter; string arg_14B_0 = "vt2 {0} {1}"; float x3 = lData.m_UVs[k].x; object arg_14B_1 = x3.ToString("n5"); float y3 = lData.m_UVs[k].y; arg_150_0.WriteLine(string.Format(arg_14B_0, arg_14B_1, y3.ToString("n5"))); } for (int l = 0; l < lData.m_Normals.Count; l++) { TextWriter arg_1D7_0 = streamWriter; string arg_1D2_0 = "vn {0} {1} {2}"; float x4 = lData.m_Normals[l].x; object arg_1D2_1 = x4.ToString("n8"); float y4 = lData.m_Normals[l].y; object arg_1D2_2 = y4.ToString("n8"); float z2 = lData.m_Normals[l].z; arg_1D7_0.WriteLine(string.Format(arg_1D2_0, arg_1D2_1, arg_1D2_2, z2.ToString("n8"))); } for (int m = 0; m < lData.m_Colors.Count; m++) { TextWriter arg_2A1_0 = streamWriter; string arg_29C_0 = "vc {0} {1} {2} {3}"; object[] array = new object[4]; object[] arg_22D_0 = array; int arg_22D_1 = 0; float r = lData.m_Colors[m].r; arg_22D_0[arg_22D_1] = r.ToString("n8"); object[] arg_251_0 = array; int arg_251_1 = 1; float g = lData.m_Colors[m].g; arg_251_0[arg_251_1] = g.ToString("n8"); object[] arg_275_0 = array; int arg_275_1 = 2; float b = lData.m_Colors[m].b; arg_275_0[arg_275_1] = b.ToString("n8"); object[] arg_299_0 = array; int arg_299_1 = 3; float a = lData.m_Colors[m].a; arg_299_0[arg_299_1] = a.ToString("n8"); arg_2A1_0.WriteLine(string.Format(arg_29C_0, array)); } for (int n = 0; n < lData.m_Groups.Count; n++) { streamWriter.WriteLine(string.Format("g {0}", lData.m_Groups[n].m_Name)); for (int num = 0; num < lData.m_Groups[n].Faces.Count; num++) { streamWriter.WriteLine(string.Format("f {0} {1} {2}", lData.m_Groups[n].Faces[num].ToString(0), lData.m_Groups[n].Faces[num].ToString(1), lData.m_Groups[n].Faces[num].ToString(2))); } } streamWriter.Flush(); }