Beispiel #1
0
        public Mesh(string Filepath, bool BodyMesh)
        {
            BinaryReader Reader = new BinaryReader(File.Open(Filepath, FileMode.Open));

            IsBodyMesh = BodyMesh;

            m_Version = Endian.SwapInt32(Reader.ReadInt32());

            m_BoneCount = Endian.SwapInt32(Reader.ReadInt32());

            for (int i = 0; i < m_BoneCount; i++)
            {
                byte StrLen = Reader.ReadByte();
                string BoneName = Encoding.ASCII.GetString(Reader.ReadBytes(StrLen));
                m_BoneNames.Add(BoneName);
            }

            m_FaceCount = Endian.SwapInt32(Reader.ReadInt32());
            m_Faces = new Face[m_FaceCount];

            for (int i = 0; i < m_FaceCount; i++)
            {
                m_Faces[i].AVertexIndex = Endian.SwapInt32(Reader.ReadInt32());
                m_Faces[i].BVertexIndex = Endian.SwapInt32(Reader.ReadInt32());
                m_Faces[i].CVertexIndex = Endian.SwapInt32(Reader.ReadInt32());
            }

            m_BndCount = Endian.SwapInt32(Reader.ReadInt32());
            /*m_BoneBindings = new int[m_BndCount, 5];

            for (int i = 0; i < m_BndCount; i++)
                for (int j = 0; j < 5; j++)
                    m_BoneBindings[i, j] = Endian.SwapInt32(Reader.ReadInt32());*/
            for (int i = 0; i < m_BndCount; i++)
            {
                BoneBinding Binding = new BoneBinding();
                Binding.BoneIndex = Endian.SwapInt32(Reader.ReadInt32());
                Binding.FirstVertex = Endian.SwapInt32(Reader.ReadInt32());
                Binding.VertexCount = Endian.SwapInt32(Reader.ReadInt32());
                Binding.FirstBlendedVert = Endian.SwapInt32(Reader.ReadInt32());
                Binding.BlendedVertexCount = Endian.SwapInt32(Reader.ReadInt32());

                m_BoneBindings.Add(Binding);
            }

            m_RealVertexCount = Endian.SwapInt32(Reader.ReadInt32());
            m_TexVerticies = new Single[m_RealVertexCount, 3];

            for (int i = 0; i < m_RealVertexCount; i++)
            {
                m_TexVerticies[i, 0] = i;
                m_TexVerticies[i, 1] = Reader.ReadSingle();
                m_TexVerticies[i, 2] = Reader.ReadSingle();
            }

            m_BlendCount = Endian.SwapInt32(Reader.ReadInt32());

            for (int i = 0; i < m_BlendCount; i++)
            {
                BlendData Blend = new BlendData();
                Blend.WeightFixed = (float)(Endian.SwapInt32(Reader.ReadInt32())) / 0x8000;
                Blend.OtherVertexIndex = Endian.SwapInt32(Reader.ReadInt32());
                m_BlendData.Add(Blend);
            }

            m_TotalVertexCount = Endian.SwapInt32(Reader.ReadInt32());

            m_VertexData = new Single[m_TotalVertexCount, 6];
            m_TransformedVertices = new Vertex[m_TotalVertexCount];

            for (int i = 0; i < m_TotalVertexCount; i++)
            {
                m_VertexData[i, 0] = Reader.ReadSingle();
                m_VertexData[i, 1] = Reader.ReadSingle();
                m_VertexData[i, 2] = Reader.ReadSingle();
                //Normals
                m_VertexData[i, 3] = Reader.ReadSingle();
                m_VertexData[i, 4] = Reader.ReadSingle();
                m_VertexData[i, 5] = Reader.ReadSingle();

                if (i < m_RealVertexCount)
                {
                    if (m_TransformedVertices[i] == null)
                        m_TransformedVertices[i] = new Vertex();

                    //Fixed vertex
                    m_TransformedVertices[i].TextureCoord.X = m_TexVerticies[i, 1];
                    m_TransformedVertices[i].TextureCoord.Y = m_TexVerticies[i, 2];
                }
                else
                {
                    if (m_TransformedVertices[i] == null)
                        m_TransformedVertices[i] = new Vertex();

                    //Blended vertex
                    m_TransformedVertices[i].Blend.WeightFixed = m_BlendData[i - m_RealVertexCount].WeightFixed;
                    m_TransformedVertices[i].Blend.OtherVertexIndex = m_BlendData[i - m_RealVertexCount].OtherVertexIndex;
                }
            }
        }
Beispiel #2
0
        public unsafe void Read(Stream stream)
        {
            using (var io = IoBuffer.FromStream(stream))
            {
                var version = io.ReadInt32();
                var boneCount = io.ReadInt32();
                var boneNames = new string[boneCount];
                for (var i = 0; i < boneCount; i++)
                {
                    boneNames[i] = io.ReadPascalString();
                }

                var faceCount = io.ReadInt32();
                NumPrimitives = faceCount;

                IndexBuffer = new short[faceCount * 3];
                int offset = 0;
                for (var i = 0; i < faceCount; i++)
                {
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                }

                /** Bone bindings **/
                var bindingCount = io.ReadInt32();
                BoneBindings = new BoneBinding[bindingCount];
                for (var i = 0; i < bindingCount; i++)
                {
                    BoneBindings[i] = new BoneBinding
                    {
                        BoneIndex = io.ReadInt32(),
                        FirstRealVertex = io.ReadInt32(),
                        RealVertexCount = io.ReadInt32(),
                        FirstBlendVertex = io.ReadInt32(),
                        BlendVertexCount = io.ReadInt32()
                    };

                    BoneBindings[i].BoneName = boneNames[BoneBindings[i].BoneIndex];
                }

                var realVertexCount = io.ReadInt32();
                RealVertexBuffer = new MeshVertex[realVertexCount];

                for (var i = 0; i < realVertexCount; i++)
                {
                    RealVertexBuffer[i].UV.X = io.ReadFloat();
                    RealVertexBuffer[i].UV.Y = io.ReadFloat();
                }

                /** Blend data **/
                var blendVertexCount = io.ReadInt32();
                BlendData = new BlendData[blendVertexCount];
                for (var i = 0; i < blendVertexCount; i++)
                {
                    BlendData[i] = new BlendData
                    {
                        Weight = (float)io.ReadInt32() / 0x8000,
                        OtherVertex = io.ReadInt32()
                    };
                }

                var realVertexCount2 = io.ReadInt32();
                BlendVertexBuffer = new MeshVertex[realVertexCount];

                for (int i = 0; i < realVertexCount; i++)
                {
                    RealVertexBuffer[i].Position = new Microsoft.Xna.Framework.Vector3(
                        -io.ReadFloat(),
                        io.ReadFloat(),
                        io.ReadFloat()
                    );

                    BlendVertexBuffer[i].Position = RealVertexBuffer[i].Position;
                    BlendVertexBuffer[i].Normal = new Microsoft.Xna.Framework.Vector3(
                        -io.ReadFloat(),
                        io.ReadFloat(),
                        io.ReadFloat()
                    );
                    BlendVertexBuffer[i].UV = RealVertexBuffer[i].UV;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Reads this mesh.
        /// </summary>
        /// <param name="data">The data for the mesh file to read from.</param>
        public void Read(byte[] data)
        {
            using (var reader = new VBReader(new MemoryStream(data)))
            {
                System.Diagnostics.Debug.WriteLine("========== Mesh ==========");

                Version = reader.ReadInt32();
                BoneCount = reader.ReadInt32();

                System.Diagnostics.Debug.WriteLine("Version: " + Version);
                System.Diagnostics.Debug.WriteLine("BoneCount: " + BoneCount);

                /** Read bone names |str_len|str_body| **/
                BoneNames = new string[BoneCount];
                for (var i = 0; i < BoneCount; i++){
                    BoneNames[i] = reader.ReadPascalString();

                    System.Diagnostics.Debug.WriteLine("| Bone " + (i + 1) + ": " + BoneNames[i]);
                }

                /** Faces **/
                FaceCount = reader.ReadInt32();
                System.Diagnostics.Debug.WriteLine("FaceCount: " + FaceCount);

                FaceData = new Face[FaceCount];
                for (var i = 0; i < FaceCount; i++){
                    FaceData[i] = new Face {
                        VertexA = reader.ReadInt32(),
                        VertexB = reader.ReadInt32(),
                        VertexC = reader.ReadInt32()
                    };
                }

                /** Bone bindings **/
                BindingCount = reader.ReadInt32();
                BoneBindings = new BoneBinding[BindingCount];
                for (var i = 0; i < BindingCount; i++){
                    BoneBindings[i] = new BoneBinding {
                        BoneIndex = reader.ReadInt32(),
                        FirstRealVertex = reader.ReadInt32(),
                        RealVertexCount = reader.ReadInt32(),
                        FirstBlendVertex = reader.ReadInt32(),
                        BlendVertexCount = reader.ReadInt32()
                    };
                }

                /** Texture vertex data **/
                RealVertexCount = reader.ReadInt32();

                var textureData = new Vector2[RealVertexCount];
                for (var i = 0; i < RealVertexCount; i++){
                    textureData[i] = new Vector2(
                        reader.ReadFloat(),
                        reader.ReadFloat()
                    );
                }

                /** Blend data **/
                BlendVertexCount = reader.ReadInt32();
                var blend = new BlendData[BlendVertexCount];
                for (var i = 0; i < BlendVertexCount; i++)
                {
                    blend[i] = new BlendData {
                        Weight = (float)reader.ReadInt32()/0x8000,
                        OtherVertex = reader.ReadInt32()
                    };
                }

                TotalVertexCount = reader.ReadInt32();

                Vertex = new MeshVertexData[TotalVertexCount];
                TransformedVertices = new MeshVertexData[TotalVertexCount];

                for (var i = 0; i < TotalVertexCount; i++)
                {
                    var vertexData = new MeshVertex
                    {
                        Coord = new Vector3
                        (
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                        )
                    };
                    var tVertexData = new MeshVertex
                    {
                        Coord = vertexData.Coord,
                        NormalCoord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                        )
                    };

                    var vertex = new MeshVertexData {
                        Vertex = vertexData
                    };

                    var tVertex = new MeshVertexData{
                        Vertex = tVertexData
                    };

                    if (i < RealVertexCount)
                    {
                        tVertex.Vertex.TextureCoord = textureData[i];
                    }
                    else
                    {
                        tVertex.BlendData = blend[i - RealVertexCount];
                    }

                    Vertex[i] = vertex;
                    TransformedVertices[i] = tVertex;
                }
            }
        }
Beispiel #4
0
        public unsafe void Read(Stream stream)
        {
            using (var io = IoBuffer.FromStream(stream))
            {
                var version   = io.ReadInt32();
                var boneCount = io.ReadInt32();
                var boneNames = new string[boneCount];
                for (var i = 0; i < boneCount; i++)
                {
                    boneNames[i] = io.ReadPascalString();
                }

                var faceCount = io.ReadInt32();
                NumPrimitives = faceCount;

                IndexBuffer = new short[faceCount * 3];
                int offset = 0;
                for (var i = 0; i < faceCount; i++)
                {
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                    IndexBuffer[offset++] = (short)io.ReadInt32();
                }

                /** Bone bindings **/
                var bindingCount = io.ReadInt32();
                BoneBindings = new BoneBinding[bindingCount];
                for (var i = 0; i < bindingCount; i++)
                {
                    BoneBindings[i] = new BoneBinding
                    {
                        BoneIndex        = io.ReadInt32(),
                        FirstRealVertex  = io.ReadInt32(),
                        RealVertexCount  = io.ReadInt32(),
                        FirstBlendVertex = io.ReadInt32(),
                        BlendVertexCount = io.ReadInt32()
                    };

                    BoneBindings[i].BoneName = boneNames[BoneBindings[i].BoneIndex];
                }

                var realVertexCount = io.ReadInt32();
                RealVertexBuffer = new MeshVertex[realVertexCount];

                for (var i = 0; i < realVertexCount; i++)
                {
                    RealVertexBuffer[i].UV.X = io.ReadFloat();
                    RealVertexBuffer[i].UV.Y = io.ReadFloat();
                }

                /** Blend data **/
                var blendVertexCount = io.ReadInt32();
                BlendData = new BlendData[blendVertexCount];
                for (var i = 0; i < blendVertexCount; i++)
                {
                    BlendData[i] = new BlendData
                    {
                        Weight      = (float)io.ReadInt32() / 0x8000,
                        OtherVertex = io.ReadInt32()
                    };
                }

                var realVertexCount2 = io.ReadInt32();
                BlendVertexBuffer = new MeshVertex[realVertexCount];

                for (int i = 0; i < realVertexCount; i++)
                {
                    RealVertexBuffer[i].Position = new Microsoft.Xna.Framework.Vector3(
                        -io.ReadFloat(),
                        io.ReadFloat(),
                        io.ReadFloat()
                        );

                    BlendVertexBuffer[i].Position = RealVertexBuffer[i].Position;
                    BlendVertexBuffer[i].Normal   = new Microsoft.Xna.Framework.Vector3(
                        -io.ReadFloat(),
                        io.ReadFloat(),
                        io.ReadFloat()
                        );
                    BlendVertexBuffer[i].UV = RealVertexBuffer[i].UV;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Reads this mesh.
        /// </summary>
        /// <param name="data">The data for the mesh file to read from.</param>
        public void Read(byte[] data)
        {
            using (var reader = new VBReader(new MemoryStream(data)))
            {
                System.Diagnostics.Debug.WriteLine("========== Mesh ==========");

                Version   = reader.ReadInt32();
                BoneCount = reader.ReadInt32();

                System.Diagnostics.Debug.WriteLine("Version: " + Version);
                System.Diagnostics.Debug.WriteLine("BoneCount: " + BoneCount);

                /** Read bone names |str_len|str_body| **/
                BoneNames = new string[BoneCount];
                for (var i = 0; i < BoneCount; i++)
                {
                    BoneNames[i] = reader.ReadPascalString();

                    System.Diagnostics.Debug.WriteLine("| Bone " + (i + 1) + ": " + BoneNames[i]);
                }

                /** Faces **/
                FaceCount = reader.ReadInt32();
                System.Diagnostics.Debug.WriteLine("FaceCount: " + FaceCount);

                FaceData = new Face[FaceCount];
                for (var i = 0; i < FaceCount; i++)
                {
                    FaceData[i] = new Face {
                        VertexA = reader.ReadInt32(),
                        VertexB = reader.ReadInt32(),
                        VertexC = reader.ReadInt32()
                    };
                }

                /** Bone bindings **/
                BindingCount = reader.ReadInt32();
                BoneBindings = new BoneBinding[BindingCount];
                for (var i = 0; i < BindingCount; i++)
                {
                    BoneBindings[i] = new BoneBinding {
                        BoneIndex        = reader.ReadInt32(),
                        FirstRealVertex  = reader.ReadInt32(),
                        RealVertexCount  = reader.ReadInt32(),
                        FirstBlendVertex = reader.ReadInt32(),
                        BlendVertexCount = reader.ReadInt32()
                    };
                }

                /** Texture vertex data **/
                RealVertexCount = reader.ReadInt32();

                var textureData = new Vector2[RealVertexCount];
                for (var i = 0; i < RealVertexCount; i++)
                {
                    textureData[i] = new Vector2(
                        reader.ReadFloat(),
                        reader.ReadFloat()
                        );
                }

                /** Blend data **/
                BlendVertexCount = reader.ReadInt32();
                var blend = new BlendData[BlendVertexCount];
                for (var i = 0; i < BlendVertexCount; i++)
                {
                    blend[i] = new BlendData {
                        Weight      = (float)reader.ReadInt32() / 0x8000,
                        OtherVertex = reader.ReadInt32()
                    };
                }

                TotalVertexCount = reader.ReadInt32();

                Vertex = new MeshVertexData[TotalVertexCount];
                TransformedVertices = new MeshVertexData[TotalVertexCount];

                for (var i = 0; i < TotalVertexCount; i++)
                {
                    var vertexData = new MeshVertex
                    {
                        Coord = new Vector3
                                (
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                                )
                    };
                    var tVertexData = new MeshVertex
                    {
                        Coord       = vertexData.Coord,
                        NormalCoord = new Vector3(
                            -reader.ReadFloat(),
                            reader.ReadFloat(),
                            reader.ReadFloat()
                            )
                    };

                    var vertex = new MeshVertexData {
                        Vertex = vertexData
                    };

                    var tVertex = new MeshVertexData {
                        Vertex = tVertexData
                    };

                    if (i < RealVertexCount)
                    {
                        tVertex.Vertex.TextureCoord = textureData[i];
                    }
                    else
                    {
                        tVertex.BlendData = blend[i - RealVertexCount];
                    }

                    Vertex[i] = vertex;
                    TransformedVertices[i] = tVertex;
                }
            }
        }