//------------------------------------------------------------------------------------------------------------ public string ToString(int lIndex) { OBJFaceVertex lFaceVertex = m_Vertices[lIndex]; string lOutput = (lFaceVertex.m_VertexIndex + 1).ToString(); if (lFaceVertex.m_UVIndex > -1) { lOutput += string.Format("/{0}", (lFaceVertex.m_UVIndex + 1).ToString()); } if (lFaceVertex.m_NormalIndex > -1) { lOutput += string.Format("/{0}", (lFaceVertex.m_NormalIndex + 1).ToString()); } if (lFaceVertex.m_UV2Index > -1) { lOutput += string.Format("/{0}", (lFaceVertex.m_UV2Index + 1).ToString()); } if (lFaceVertex.m_ColorIndex > -1) { lOutput += string.Format("/{0}", (lFaceVertex.m_ColorIndex + 1).ToString()); } return(lOutput); }
//------------------------------------------------------------------------------------------------------------ public static OBJData EncodeOBJ(this Mesh lMesh) { OBJData lData = 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) }; int[] lIndices = null; OBJGroup lGroup = null; OBJFace lFace = null; OBJFaceVertex lFaceVertex = null; for (int lMCount = 0; lMCount < lMesh.subMeshCount; ++lMCount) { lIndices = lMesh.GetTriangles(lMCount); lGroup = new OBJGroup(lMesh.name + "_" + lMCount.ToString()); for (int lCount = 0; lCount < lIndices.Length; lCount += 3) { lFace = new OBJFace(); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount] : -1; lFace.AddVertex(lFaceVertex); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 1] : -1; lFace.AddVertex(lFaceVertex); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 2] : -1; lFace.AddVertex(lFaceVertex); lGroup.AddFace(lFace); } lData.m_Groups.Add(lGroup); } return(lData); }
public override bool Equals(object obj) { OBJFaceVertex faceVertex = (OBJFaceVertex)obj; return(m_VertexIndex == faceVertex.m_VertexIndex && m_UVIndex == faceVertex.m_UVIndex && m_UV2Index == faceVertex.m_UV2Index && m_NormalIndex == faceVertex.m_NormalIndex); }
//------------------------------------------------------------------------------------------------------------ public void ParseVertex(string lVertexString) { var fields = lVertexString.Split(new[] { '/' }, StringSplitOptions.None); var lIndex = fields[0].ParseInvariantInt(); var faceVertex = new OBJFaceVertex { m_VertexIndex = lIndex - 1 }; if (fields.Length > 1) { lIndex = fields[1].Length == 0 ? 0 : fields[1].ParseInvariantInt(); faceVertex.m_UVIndex = lIndex - 1; } if (fields.Length > 2) { lIndex = fields[2].Length == 0 ? 0 : fields[2].ParseInvariantInt(); faceVertex.m_NormalIndex = lIndex - 1; } if (fields.Length > 3) { lIndex = fields[3].Length == 0 ? 0 : fields[3].ParseInvariantInt(); faceVertex.m_UV2Index = lIndex - 1; } if (fields.Length > 4) { lIndex = fields[4].Length == 0 ? 0 : fields[4].ParseInvariantInt(); faceVertex.m_ColorIndex = lIndex - 1; } AddVertex(faceVertex); }
//------------------------------------------------------------------------------------------------------------ public void AddVertex(OBJFaceVertex lVertex) { m_Vertices.Add(lVertex); }
//------------------------------------------------------------------------------------------------------------ public static void LoadOBJ(this Mesh lMesh, OBJData lData) { List <Vector3> lVertices = new List <Vector3>(); List <Vector3> lNormals = new List <Vector3>(); List <Vector2> lUVs = new List <Vector2>(); List <int>[] lIndices = new List <int> [lData.m_Groups.Count]; Dictionary <OBJFaceVertex, int> lVertexIndexRemap = new Dictionary <OBJFaceVertex, int>(); bool lHasNormals = lData.m_Normals.Count > 0; bool lHasUVs = lData.m_UVs.Count > 0; lMesh.subMeshCount = lData.m_Groups.Count; for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount) { OBJGroup lGroup = lData.m_Groups[lGCount]; lIndices[lGCount] = new List <int>(); for (int lFCount = 0; lFCount < lGroup.Faces.Count; ++lFCount) { OBJFace lFace = lGroup.Faces[lFCount]; // Unity3d doesn't support non-triangle faces // so we do simple fan triangulation for (int lVCount = 1; lVCount < lFace.Count - 1; ++lVCount) { foreach (int i in new int[] { 0, lVCount, lVCount + 1 }) { OBJFaceVertex lFaceVertex = lFace[i]; int lVertexIndex = -1; if (!lVertexIndexRemap.TryGetValue(lFaceVertex, out lVertexIndex)) { lVertexIndexRemap[lFaceVertex] = lVertices.Count; lVertexIndex = lVertices.Count; lVertices.Add(lData.m_Vertices[lFaceVertex.m_VertexIndex]); if (lHasUVs) { lUVs.Add(lData.m_UVs[lFaceVertex.m_UVIndex]); } if (lHasNormals) { lNormals.Add(lData.m_Normals[lFaceVertex.m_NormalIndex]); } } lIndices[lGCount].Add(lVertexIndex); } } } } lMesh.triangles = new int[] { }; lMesh.vertices = lVertices.ToArray(); lMesh.uv = lUVs.ToArray(); lMesh.normals = lNormals.ToArray(); if (!lHasNormals) { lMesh.RecalculateNormals(); } lMesh.RecalculateTangents(); for (int lGCount = 0; lGCount < lData.m_Groups.Count; ++lGCount) { lMesh.SetTriangles(lIndices[lGCount].ToArray(), lGCount); } }
//------------------------------------------------------------------------------------------------------------ public static OBJData EncodeOBJ(this Mesh lMesh) { OBJData lData = 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.uv1), m_Colors = new List<Color>(lMesh.colors) }; int[] lIndices = null; OBJGroup lGroup = null; OBJFace lFace = null; OBJFaceVertex lFaceVertex = null; for (int lMCount = 0; lMCount < lMesh.subMeshCount; ++lMCount) { lIndices = lMesh.GetTriangles(lMCount); lGroup = new OBJGroup(lMesh.name + "_" + lMCount.ToString()); for (int lCount = 0; lCount < lIndices.Length; lCount += 3) { lFace = new OBJFace(); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount] : -1; lFace.AddVertex(lFaceVertex); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 1] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 1] : -1; lFace.AddVertex(lFaceVertex); lFaceVertex = new OBJFaceVertex(); lFaceVertex.m_VertexIndex = lData.m_Vertices.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_UVIndex = lData.m_UVs.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_NormalIndex = lData.m_Normals.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_UV2Index = lData.m_UV2s.Count > 0 ? lIndices[lCount + 2] : -1; lFaceVertex.m_ColorIndex = lData.m_Colors.Count > 0 ? lIndices[lCount + 2] : -1; lFace.AddVertex(lFaceVertex); lGroup.AddFace(lFace); } lData.m_Groups.Add(lGroup); } return lData; }
public override bool Equals(object obj) { OBJFaceVertex oBJFaceVertex = (OBJFaceVertex)obj; return(this.m_VertexIndex == oBJFaceVertex.m_VertexIndex && this.m_UVIndex == oBJFaceVertex.m_UVIndex && this.m_UV2Index == oBJFaceVertex.m_UV2Index && this.m_NormalIndex == oBJFaceVertex.m_NormalIndex && this.m_ColorIndex == oBJFaceVertex.m_ColorIndex); }