Pathfinding.ObjImporter.createMeshStruct C# (CSharp) Method

createMeshStruct() private static method

private static createMeshStruct ( string filename ) : meshStruct
filename string
return meshStruct
	    private static meshStruct createMeshStruct(string filename)
	    {
	        int triangles = 0;
	        int vertices = 0;
	        int vt = 0;
	        int vn = 0;
	        int face = 0;
	        meshStruct mesh = new meshStruct();
	        mesh.fileName = filename;
	        StreamReader stream = File.OpenText(filename);
	        string entireText = stream.ReadToEnd();
	        stream.Close();
	        using (StringReader reader = new StringReader(entireText))
	        {
	            string currentText = reader.ReadLine();
	            char[] splitIdentifier = { ' ' };
	            string[] brokenString;
	            while (currentText != null)
	            {
	                if (!currentText.StartsWith("f ") && !currentText.StartsWith("v ") && !currentText.StartsWith("vt ")
	                    && !currentText.StartsWith("vn "))
	                {
	                    currentText = reader.ReadLine();
	                    if (currentText != null)
	                    {
	                        currentText = currentText.Replace("  ", " ");
	                    }
	                }
	                else
	                {
	                    currentText = currentText.Trim();                           //Trim the current line
	                    brokenString = currentText.Split(splitIdentifier, 50);      //Split the line into an array, separating the original line by blank spaces
	                    switch (brokenString[0])
	                    {
	                        case "v":
	                            vertices++;
	                            break;
	                        case "vt":
	                            vt++;
	                            break;
	                        case "vn":
	                            vn++;
	                            break;
	                        case "f":
	                            face = face + brokenString.Length - 1;
	                            triangles = triangles + 3 * (brokenString.Length - 2); /*brokenString.Length is 3 or greater since a face must have at least
	                                                                                     3 vertices.  For each additional vertice, there is an additional
	                                                                                     triangle in the mesh (hence this formula).*/
	                            break;
	                    }
	                    currentText = reader.ReadLine();
	                    if (currentText != null)
	                    {
	                        currentText = currentText.Replace("  ", " ");
	                    }
	                }
	            }
	        }
	        mesh.triangles = new int[triangles];
	        mesh.vertices = new Vector3[vertices];
	        mesh.uv = new Vector2[vt];
	        mesh.normals = new Vector3[vn];
	        mesh.faceData = new Vector3[face];
	        return mesh;
	    }
	    

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);
        }