Ejemplo n.º 1
0
        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.
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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();
                        brokenString = currentText.Split(splitIdentifier, 50);
                        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);
                                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;
        }
Ejemplo n.º 3
0
 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;
 }
Ejemplo n.º 4
0
    private static meshStruct createMeshStructFromFile(string filename)
    {
        meshStruct mesh = new meshStruct();
        StreamReader stream = File.OpenText(filename);
        mesh.objFile = stream.ReadToEnd();
        stream.Close();

        return createMeshStruct(ref mesh);
    }
Ejemplo n.º 5
0
        private static void populatMeshStruct(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(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]), Convert.ToSingle(brokenString[3]));
                                v++;
                                break;
                            case "vt":
                                mesh.uv[vt] = new Vector2(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                                vt++;
                                break;
                            case "vt1":
                                mesh.uv[vt1] = new Vector2(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                                vt1++;
                                break;
                            case "vt2":
                                mesh.uv[vt2] = new Vector2(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]));
                                vt2++;
                                break;
                            case "vn":
                                mesh.normals[vn] = new Vector3(Convert.ToSingle(brokenString[1]), Convert.ToSingle(brokenString[2]), 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);
                                    temp.x = Convert.ToInt32(brokenBrokenString[0]);
                                    if (brokenBrokenString.Length > 1)
                                    {
                                        if (brokenBrokenString[1] != "")
                                        {
                                            temp.y = Convert.ToInt32(brokenBrokenString[1]);
                                        }
                                        temp.z = Convert.ToInt32(brokenBrokenString[2]);
                                    }
                                    j++;

                                    mesh.faceData[f2] = temp;
                                    intArray.Add(f2);
                                    f2++;
                                }
                                j = 1;
                                while (j + 2 < brokenString.Length)
                                {
                                    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.Replace("  ", " ");
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
    private static meshStruct createMeshStructFromStream(Stream stream)
    {
        meshStruct mesh = new meshStruct();
        byte[] data = new byte[stream.Length];
        stream.Read(data, 0, (int)stream.Length);
        mesh.objFile = System.Text.Encoding.UTF8.GetString(data);

        return createMeshStruct(ref mesh);
    }
Ejemplo n.º 7
0
    private static Mesh ImportFile(ref meshStruct newMesh)
    {
        populateMeshStruct(ref newMesh);

        Vector3[] newVerts = new Vector3[newMesh.faceData.Length];
        Vector2[] newUVs = new Vector2[newMesh.faceData.Length];
        Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
        int i = 0;
        /* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
         * for the appropriate Unity mesh array.
         */
        foreach (Vector3 v in newMesh.faceData)
        {
            newVerts[i] = newMesh.vertices[(int)v.x - 1];
            if (v.y >= 1)
                newUVs[i] = newMesh.uv[(int)v.y - 1];

            if (v.z >= 1)
                newNormals[i] = newMesh.normals[(int)v.z - 1];
            i++;
        }

        Mesh mesh = new Mesh();

        mesh.vertices = newVerts;
        mesh.uv = newUVs;
        mesh.normals = newNormals;
        mesh.triangles = newMesh.triangles;

        mesh.RecalculateBounds();
        mesh.Optimize();

        return mesh;
    }
Ejemplo n.º 8
0
    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);
    }
Ejemplo n.º 9
0
    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.
                    }
                }
            }
        }
    }
Ejemplo n.º 10
0
        public bool GenerateHtmlFromObj(string in_obj_file_name, string out_html_file_name)
        {
            // Load the mesh
            meshStruct mesh = LoadObjFile(in_obj_file_name);

            if (mesh.vertices.Length == 0 || mesh.faces.Length == 0)
            {
                return(true);
            }

            // Write out the header
            StreamReader stream = GetPrefabStream(@"3dscan_header.html");

            if (stream == null)
            {
                return(true);
            }
            string text = stream.ReadToEnd();

            stream.Close();
            File.WriteAllText(out_html_file_name, text);

            // Then, compose and append the data vertex...
            {
                StringBuilder sbv       = new StringBuilder();
                StringBuilder sbc       = new StringBuilder();
                string        PRECISION = "0.000000";
                sbv.Append("    var verticies = new Float64Array([");
                sbc.Append("    var colors = new Uint8Array([");
                for (Int32 v = 0; v < mesh.vertices.Length; v++)
                {
                    vertexStruct vert = mesh.vertices[v];
                    sbv.Append(vert.x.ToString(PRECISION) + ","
                               + vert.y.ToString(PRECISION) + ","
                               + vert.z.ToString(PRECISION) + ",");
                    sbc.Append((vert.r * 255).ToString("0.") + ","
                               + (vert.g * 255).ToString("0.") + ","
                               + (vert.b * 255).ToString("0.") + ",");
                }
                sbv.Append("]);\n");
                File.AppendAllText(out_html_file_name, sbv.ToString());
                sbc.Append("]);\n");
                File.AppendAllText(out_html_file_name, sbc.ToString());
            }
            // ...and face data.
            {
                StringBuilder sbf = new StringBuilder();
                sbf.Append("    var faces = new Int32Array([");
                for (Int32 f = 0; f < mesh.faces.Length; f++)
                {
                    faceStruct face = mesh.faces[f];
                    sbf.Append((face.vertex_indicies[0] - 1).ToString() + ","
                               + (face.vertex_indicies[1] - 1).ToString() + ","
                               + (face.vertex_indicies[2] - 1).ToString() + ",");
                }
                sbf.Append("]);\n");
                File.AppendAllText(out_html_file_name, sbf.ToString());
            }

            // Lastly, append the footer
            stream = GetPrefabStream(@"3dscan_footer.html");
            if (stream == null)
            {
                return(true);
            }
            text = stream.ReadToEnd();
            stream.Close();
            File.AppendAllText(out_html_file_name, text);

            return(false);
        }
Ejemplo n.º 11
0
        private meshStruct LoadObjFile(string in_obj_file_name)
        {
            meshStruct   mesh   = new meshStruct();
            StreamReader stream = File.OpenText(in_obj_file_name);
            string       text   = stream.ReadToEnd();

            stream.Close();
            const int CHUNK_SIZE = 50000;
            int       maxVerts   = CHUNK_SIZE;

            mesh.vertices = new vertexStruct[maxVerts];
            int maxFaces = CHUNK_SIZE;

            mesh.faces = new faceStruct[maxFaces];
            int vertices = 0;
            int faces    = 0;

            using (StringReader reader = new StringReader(text))
            {
                string   currentText = reader.ReadLine();
                char[]   split       = { ' ' };
                string[] elements;
                while (currentText != null)
                {
                    if (!currentText.StartsWith("f ") && !currentText.StartsWith("v "))
                    {
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");
                        }
                    }
                    else
                    {
                        currentText = currentText.Trim();
                        elements    = currentText.Split(split, 50);
                        switch (elements[0])
                        {
                        case "v":
                            mesh.vertices[vertices].x = System.Convert.ToSingle(elements[1]);
                            mesh.vertices[vertices].y = System.Convert.ToSingle(elements[2]);
                            mesh.vertices[vertices].z = System.Convert.ToSingle(elements[3]);
                            mesh.vertices[vertices].r = System.Convert.ToSingle(elements[4]);
                            mesh.vertices[vertices].g = System.Convert.ToSingle(elements[5]);
                            mesh.vertices[vertices].b = System.Convert.ToSingle(elements[6]);
                            if (++vertices == maxVerts)
                            {
                                maxVerts += CHUNK_SIZE;
                                Array.Resize(ref mesh.vertices, maxVerts);
                            }
                            break;

                        case "f":
                            char[]   index_split = { '/' };
                            string[] index_elements;
                            mesh.faces[faces].vertex_indicies = new Int32[3];
                            index_elements = elements[1].Split(index_split, 50);
                            mesh.faces[faces].vertex_indicies[0] = System.Convert.ToInt32(index_elements[0]);
                            index_elements = elements[2].Split(index_split, 50);
                            mesh.faces[faces].vertex_indicies[1] = System.Convert.ToInt32(index_elements[0]);
                            index_elements = elements[3].Split(index_split, 50);
                            mesh.faces[faces].vertex_indicies[2] = System.Convert.ToInt32(index_elements[0]);
                            if (++faces == maxFaces)
                            {
                                maxFaces += CHUNK_SIZE;
                                Array.Resize(ref mesh.faces, maxFaces);
                            }
                            break;
                        }
                        currentText = reader.ReadLine();
                        if (currentText != null)
                        {
                            currentText = currentText.Replace("  ", " ");
                        }
                    }
                }
            }
            Array.Resize(ref mesh.vertices, vertices);
            Array.Resize(ref mesh.faces, faces);
            return(mesh);
        }
Ejemplo n.º 12
0
        // Use this for initialization
        public static RawObject Create(string data)
        {
            meshStruct createMeshStruct(string rawdata)
            {
                int        triangles  = 0;
                int        vertices   = 0;
                int        vt         = 0;
                int        vn         = 0;
                int        face       = 0;
                meshStruct meshStruct = new meshStruct();

                meshStruct.fileName = rawdata;
                StreamReader stream     = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(rawdata)));
                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("  ", " ");
                            }
                        }
                    }
                }
                meshStruct.triangles = new int[triangles];
                meshStruct.vertices  = new Vector3[vertices];
                meshStruct.uv        = new Vector2[vt];
                meshStruct.normals   = new Vector3[vn];
                meshStruct.faceData  = new Vector3[face];
                return(meshStruct);
            }

            meshStruct newMesh = createMeshStruct(data);

            void populateMeshStruct()
            {
                StreamReader stream     = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(newMesh.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":
                                newMesh.vertices[v] = new Vector3(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]),
                                                                  System.Convert.ToSingle(brokenString[3]));
                                v++;
                                break;

                            case "vt":
                                newMesh.uv[vt] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                                vt++;
                                break;

                            case "vt1":
                                newMesh.uv[vt1] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                                vt1++;
                                break;

                            case "vt2":
                                newMesh.uv[vt2] = new Vector2(System.Convert.ToSingle(brokenString[1]), System.Convert.ToSingle(brokenString[2]));
                                vt2++;
                                break;

                            case "vn":
                                newMesh.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++;

                                    newMesh.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.
                                {
                                    newMesh.triangles[f] = intArray[0];
                                    f++;
                                    newMesh.triangles[f] = intArray[j];
                                    f++;
                                    newMesh.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.
                            }
                        }
                    }
                }
            }

            populateMeshStruct();

            Vector3[] newVerts   = new Vector3[newMesh.faceData.Length];
            Vector2[] newUVs     = new Vector2[newMesh.faceData.Length];
            Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
            int       i          = 0;

            /* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
             * for the appropriate Unity mesh array.
             */
            foreach (Vector3 v in newMesh.faceData)
            {
                newVerts[i] = newMesh.vertices[(int)v.x - 1];
                if (v.y >= 1)
                {
                    newUVs[i] = newMesh.uv[(int)v.y - 1];
                }

                if (v.z >= 1)
                {
                    newNormals[i] = newMesh.normals[(int)v.z - 1];
                }
                i++;
            }

            RawObject res = new RawObject();

            res.Triangles = newMesh.triangles;
            res.Normals   = newNormals;
            res.UV        = newUVs;
            res.Vertices  = newVerts;

            return(res);
        }