Pathfinding.ObjImporter.populateMeshStruct C# (CSharp) Method

populateMeshStruct() private static method

private static populateMeshStruct ( meshStruct &mesh ) : void
mesh meshStruct
return void
	    private static void populateMeshStruct(ref meshStruct mesh)
	    {
	        StreamReader stream = File.OpenText(mesh.fileName);
	        string entireText = stream.ReadToEnd();
	        stream.Close();
	        using (StringReader reader = new StringReader(entireText))
	        {
	            string currentText = reader.ReadLine();
	            
	            char[] splitIdentifier = { ' ' };
	            char[] splitIdentifier2 = { '/' };
	            string[] brokenString;
	            string[] brokenBrokenString;
	            int f = 0;
	            int f2 = 0;
	            int v = 0;
	            int vn = 0;
	            int vt = 0;
	            int vt1 = 0;
	            int vt2 = 0;
	            while (currentText != null)
	            {
	                if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ") &&
	                    !currentText.StartsWith("vn ") && !currentText.StartsWith("g ") && !currentText.StartsWith("usemtl ") &&
	                    !currentText.StartsWith("mtllib ") && !currentText.StartsWith("vt1 ") && !currentText.StartsWith("vt2 ") &&
	                    !currentText.StartsWith("vc ") && !currentText.StartsWith("usemap "))
	                {
	                    currentText = reader.ReadLine();
	                    if (currentText != null)
	                    {
	                        currentText = currentText.Replace("  ", " ");
	                    }
	                }
	                else
	                {
	                    currentText = currentText.Trim();
	                    brokenString = currentText.Split(splitIdentifier, 50);
	                    switch (brokenString[0])
	                    {
	                        case "g":
	                            break;
	                        case "usemtl":
	                            break;
	                        case "usemap":
	                            break;
	                        case "mtllib":
	                            break;
	                        case "v":
	                            mesh.vertices[v] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
	                                                     System.Convert.ToSingle(brokenString[3]));
	                            v++;
	                            break;
	                        case "vt":
	                            mesh.uv[vt] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
	                            vt++;
	                            break;
	                        case "vt1":
	                            mesh.uv[vt1] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
	                            vt1++;
	                            break;
	                        case "vt2":
	                            mesh.uv[vt2] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
	                            vt2++;
	                            break;
	                        case "vn":
	                            mesh.normals[vn] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
	                                                    System.Convert.ToSingle(brokenString[3]));
	                            vn++;
	                            break;
	                        case "vc":
	                            break;
	                        case "f":
	                        
	                            int j = 1;
	                            List<int> intArray = new List<int>();
	                            while (j < brokenString.Length && ("" + brokenString[j]).Length > 0)
	                            {
	                                Vector3 temp = new Vector3();
	                                brokenBrokenString = brokenString[j].Split(splitIdentifier2, 3);    //Separate the face into individual components (vert, uv, normal)
	                                temp.x = System.Convert.ToInt32(brokenBrokenString[0]);
	                                if (brokenBrokenString.Length > 1)                                  //Some .obj files skip UV and normal
	                                {
	                                    if (brokenBrokenString[1] != "")                                    //Some .obj files skip the uv and not the normal
	                                    {
	                                        temp.y = System.Convert.ToInt32(brokenBrokenString[1]);
	                                    }
	                                    temp.z = System.Convert.ToInt32(brokenBrokenString[2]);
	                                }
	                                j++;
	                                
	                                mesh.faceData[f2] = temp;
	                                intArray.Add(f2);
	                                f2++;
	                            }
	                            j = 1;
	                            while (j + 2 < brokenString.Length)     //Create triangles out of the face data.  There will generally be more than 1 triangle per face.
	                            {
	                                mesh.triangles[f] = intArray[0];
	                                f++;
	                                mesh.triangles[f] = intArray[j];
	                                f++;
	                                mesh.triangles[f] = intArray[j+1];
	                                f++;
	                                
	                                j++;
	                            }
	                            break;
	                    }
	                    currentText = reader.ReadLine();
	                    if (currentText != null)
	                    {
	                        currentText = currentText.Replace("  ", " ");       //Some .obj files insert double spaces, this removes them.
	                    }
	                }
	            }
	        }
	    }
	}

Usage Example

        public static Mesh ImportFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                Debug.LogError("No file was found at '" + filePath + "'");
                return(null);
            }
            ObjImporter.meshStruct meshStruct = ObjImporter.createMeshStruct(filePath);
            ObjImporter.populateMeshStruct(ref meshStruct);
            Vector3[] array  = new Vector3[meshStruct.faceData.Length];
            Vector2[] array2 = new Vector2[meshStruct.faceData.Length];
            Vector3[] array3 = new Vector3[meshStruct.faceData.Length];
            int       num    = 0;

            Vector3[] faceData = meshStruct.faceData;
            for (int i = 0; i < faceData.Length; i++)
            {
                Vector3 vector = faceData[i];
                array[num] = meshStruct.vertices[(int)vector.x - 1];
                if (vector.y >= 1f)
                {
                    array2[num] = meshStruct.uv[(int)vector.y - 1];
                }
                if (vector.z >= 1f)
                {
                    array3[num] = meshStruct.normals[(int)vector.z - 1];
                }
                num++;
            }
            Mesh mesh = new Mesh();

            mesh.vertices  = array;
            mesh.uv        = array2;
            mesh.normals   = array3;
            mesh.triangles = meshStruct.triangles;
            mesh.RecalculateBounds();
            return(mesh);
        }