diff --git a/KclLibrary/ObjModel.cs b/KclLibrary/ObjModel.cs index aed69a9..a38dcbb 100644 --- a/KclLibrary/ObjModel.cs +++ b/KclLibrary/ObjModel.cs @@ -132,6 +132,8 @@ public void Load(Stream stream) ObjMesh currentMesh = new ObjMesh("Mesh"); + HashSet faceHashes = new HashSet(); + Dictionary faceDupes = new Dictionary(); using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { @@ -177,13 +179,17 @@ public void Load(Stream stream) if (args.Length != 4) throw new Exception("Obj must be trianglulated!"); + int[] indices = new int[3 * 2]; //3 faces, position and normal indices + // Only support triangles for now. ObjFace face = new ObjFace() { Vertices = new ObjVertex[3] }; face.Material = currentMaterial; for (int i = 0; i < face.Vertices.Length; i++) { string[] vertexArgs = args[i + 1].Split(_vertexSeparators, StringSplitOptions.None); - face.Vertices[i].Position = Positions[Int32.Parse(vertexArgs[0]) - 1]; + int positionIndex = Int32.Parse(vertexArgs[0]) - 1; + + face.Vertices[i].Position = Positions[positionIndex]; if (float.IsNaN(face.Vertices[i].Position.X) || float.IsNaN(face.Vertices[i].Position.Y) || @@ -203,6 +209,12 @@ public void Load(Stream stream) } } + string faceStr = face.ToString(); + if (faceHashes.Contains(faceStr)) + continue; + + faceHashes.Add(faceStr); + if (face.Vertices != null) currentMesh.Faces.Add(face); continue;