示例#1
0
    public static void readMesh(string path, string filename)
    {
        if (File.Exists(path + "/" + filename))
        {
            string     nm         = Path.GetFileNameWithoutExtension(filename);
            GameObject ObjectRoot = new GameObject(nm);
            MD2Model   model      = (MD2Model)ObjectRoot.AddComponent(typeof(MD2Model));
            model.setup();



            int            vertex_Count;
            int            uv_count;
            int            face_Count;
            int            frames_Count;
            List <Face>    faces    = new List <Face> ();
            List <Face>    tfaces   = new List <Face> ();
            List <Vector2> uvCoords = new List <Vector2> ();


            using (FileStream fs = File.OpenRead(path + "/" + filename)) {
                BinaryReader file = new BinaryReader(fs);

                int Magic            = file.ReadInt32();
                int Version          = file.ReadInt32();
                int SkinWidth        = file.ReadInt32();
                int SkinHeight       = file.ReadInt32();
                int FrameSize        = file.ReadInt32();
                int NumSkins         = file.ReadInt32();
                int NumVertices      = file.ReadInt32();
                int NumTexCoords     = file.ReadInt32();
                int NumTriangles     = file.ReadInt32();
                int NumGlCommands    = file.ReadInt32();
                int NumFrames        = file.ReadInt32();
                int OffsetSkins      = file.ReadInt32();
                int OffsetTexCoords  = file.ReadInt32();
                int OffsetTriangles  = file.ReadInt32();
                int OffsetFrames     = file.ReadInt32();
                int OffsetGlCommands = file.ReadInt32();
                int OffsetEnd        = file.ReadInt32();

                frames_Count = NumFrames;
                vertex_Count = NumVertices;
                face_Count   = NumTriangles;
                uv_count     = NumTexCoords;


                file.BaseStream.Seek(OffsetTriangles, SeekOrigin.Begin);
                Debug.Log("Num triangles" + NumTriangles);
                for (int i = 0; i < NumTriangles; i++)
                {
                    int v0, v1, v2;

                    //vertex face
                    v0 = (int)file.ReadUInt16();
                    v1 = (int)file.ReadUInt16();
                    v2 = (int)file.ReadUInt16();

                    faces.Add(new Face(v0, v1, v2));

                    //texture faces
                    v0 = (int)file.ReadUInt16();
                    v1 = (int)file.ReadUInt16();
                    v2 = (int)file.ReadUInt16();
                    tfaces.Add(new Face(v0, v1, v2));
                }

                file.BaseStream.Seek(OffsetTexCoords, SeekOrigin.Begin);
                Debug.Log("Num TextureCoord" + NumTexCoords);
                for (int i = 0; i < NumTexCoords; i++)
                {
                    float u = (float)file.ReadInt16() / SkinWidth;
                    float v = (float)1 * -file.ReadInt16() / SkinWidth;
                    uvCoords.Add(new Vector2(u, v));
                }

                //**************************************************
                file.BaseStream.Seek(OffsetFrames, SeekOrigin.Begin);
                Debug.Log("Num Frames" + OffsetFrames);
                for (int i = 0; i < NumFrames; i++)
                {
                    Vector3 Scale     = Vector3.zero;
                    Vector3 Translate = Vector3.zero;

                    Scale.x = file.ReadSingle();
                    Scale.y = file.ReadSingle();
                    Scale.z = file.ReadSingle();

                    Translate.x = file.ReadSingle();
                    Translate.y = file.ReadSingle();
                    Translate.z = file.ReadSingle();

                    char[] name = file.ReadChars(16);


                    //Debug.LogWarning(new string(name));

                    for (int j = 0; j < NumVertices; j++)
                    {
                        byte x = file.ReadByte();
                        byte y = file.ReadByte();
                        byte z = file.ReadByte();
                        byte w = file.ReadByte();
                        //	System.BitConverter.ToSingle(buffer, 0);


                        float sx = Scale.x * x + Translate.x;
                        float sy = Scale.z * z + Translate.z;
                        float sz = Scale.y * y + Translate.y;

                        model.vertex.Add(new Vector3(sx, sy, sz));
                    }
                }

                //meshFilter.sharedMesh =Surface.createCube ("cube");
                for (int f = 0; f < NumFrames; f++)
                {
                    Surface surface = new Surface("frame");
                    for (int i = 0; i < faces.Count; i++)
                    {
                        Vector3 v1  = model.vertex[f * vertex_Count + faces[i].v0];
                        Vector3 v2  = model.vertex[f * vertex_Count + faces[i].v1];
                        Vector3 v3  = model.vertex[f * vertex_Count + faces[i].v2];
                        Vector2 uv1 = uvCoords[0 * uv_count + tfaces[i].v0];
                        Vector2 uv2 = uvCoords[0 * uv_count + tfaces[i].v1];
                        Vector2 uv3 = uvCoords[0 * uv_count + tfaces[i].v2];
                        surface.addFace(v1, v2, v3, uv1, uv2, uv3);
                    }
                    surface.build();
                    surface.RecalculateNormals();
                    surface.Optimize();
                    model.Frames.Add(surface.getMesh());
                }

                model.meshFilter.sharedMesh = model.Frames[0];
                model.ready = true;

                Debug.LogWarning("read ok");
            }
        }
        else
        {
            Debug.LogError(filename + " dont exits");
        }
    }