Example #1
0
        public VMeshData(byte[] data, ILibFile materialLibrary, string name)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (materialLibrary == null)
            {
                throw new ArgumentNullException("materialLibrary");
            }
            vmsname = name;
            ready   = false;

            using (BinaryReader reader = new BinaryReader(new MemoryStream(data)))
            {
                // Read the data header.
                MeshType             = reader.ReadUInt32();
                SurfaceType          = reader.ReadUInt32();
                MeshCount            = reader.ReadUInt16();
                IndexCount           = reader.ReadUInt16();
                FlexibleVertexFormat = (D3DFVF)reader.ReadUInt16();
                VertexCount          = reader.ReadUInt16();

                // Read the mesh headers.
                Meshes = new List <TMeshHeader>();
                int triangleStartOffset = 0;
                for (int count = 0; count < MeshCount; count++)
                {
                    TMeshHeader item = new TMeshHeader(reader, triangleStartOffset, materialLibrary);
                    triangleStartOffset += item.NumRefVertices;
                    Meshes.Add(item);
                }

                // Read the triangle data
                Indices = new ushort[IndexCount];
                for (int i = 0; i < IndexCount; i++)
                {
                    Indices[i] = reader.ReadUInt16();
                }

                // Read the vertex data.
                // The FVF defines what fields are included for each vertex.
                switch (FlexibleVertexFormat)
                {
                case D3DFVF.XYZ:     //(D3DFVF)0x0002:
                    verticesVertexPosition = new VertexPosition[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPosition[i] = new VertexPosition(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL:     //(D3DFVF)0x0012:
                    verticesVertexPositionNormal = new VertexPositionNormal[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormal[i] = new VertexPositionNormal(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.TEX1:     //(D3DFVF)0x0102:
                    verticesVertexPositionNormalTexture = new VertexPositionNormalTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalTexture[i] = new VertexPositionNormalTexture(position, Vector3.Zero, textureCoordinate);
                    }
                    CalculateNormals(verticesVertexPositionNormalTexture);
                    FlexibleVertexFormat |= D3DFVF.NORMAL;
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX1:     //(D3DFVF)0x0112:
                    verticesVertexPositionNormalTexture = new VertexPositionNormalTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector3 normal            = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalTexture[i] = new VertexPositionNormalTexture(position, normal, textureCoordinate);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.DIFFUSE | D3DFVF.TEX1:     //(D3DFVF)0x0142:
                    verticesVertexPositionNormalColorTexture = new VertexPositionNormalColorTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        int     r                 = reader.ReadByte();
                        int     g                 = reader.ReadByte();
                        int     b                 = reader.ReadByte();
                        int     a                 = reader.ReadByte();
                        Color4  diffuse           = new Color4(r / 255f, g / 255f, b / 255f, a / 255f);
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalColorTexture[i] = new VertexPositionNormalColorTexture(position, Vector3.Zero, diffuse, textureCoordinate);
                    }
                    FlexibleVertexFormat |= D3DFVF.NORMAL;
                    CalculateNormals(verticesVertexPositionNormalColorTexture);
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX1:     //(D3DFVF)0x0152:
                    verticesVertexPositionNormalColorTexture = new VertexPositionNormalColorTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        //verticesVertexPositionNormalDiffuseTexture[i] = new VertexPositionNormalDiffuseTexture(reader);
                        var     position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        var     normal            = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        int     r                 = reader.ReadByte();
                        int     g                 = reader.ReadByte();
                        int     b                 = reader.ReadByte();
                        int     a                 = reader.ReadByte();
                        Color4  diffuse           = new Color4(r / 255f, g / 255f, b / 255f, a / 255f);
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalColorTexture[i] = new VertexPositionNormalColorTexture(position, normal, diffuse, textureCoordinate);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX2:     //(D3DFVF)0x0212:
                    verticesVertexPositionNormalTextureTwo = new VertexPositionNormalTextureTwo[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormalTextureTwo[i] = new VertexPositionNormalTextureTwo(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX2:     //(D3DFVF)0x0252:
                    verticesVertexPositionNormalDiffuseTextureTwo = new VertexPositionNormalDiffuseTextureTwo[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormalDiffuseTextureTwo[i] = new VertexPositionNormalDiffuseTextureTwo(reader);
                    }
                    break;

                /*case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX4: //(D3DFVF)0x0412:
                 *  for (int i = 0; i < VertexCount; i++) vertices[i] = new VertexPositionNormalTextureTangentBinormal(reader);
                 *  break;*/
                default:
                    throw new FileContentException("UTF:VMeshData", "FVF 0x" + FlexibleVertexFormat + " not supported.");
                }
            }
        }
Example #2
0
        public VMeshData(ArraySegment <byte> data, ILibFile materialLibrary, string name)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (materialLibrary == null)
            {
                throw new ArgumentNullException("materialLibrary");
            }
            vmsname = name;
            ready   = false;

            using (BinaryReader reader = new BinaryReader(data.GetReadStream()))
            {
                // Read the data header.
                MeshType             = reader.ReadUInt32();
                SurfaceType          = reader.ReadUInt32();
                MeshCount            = reader.ReadUInt16();
                IndexCount           = reader.ReadUInt16();
                FlexibleVertexFormat = (D3DFVF)reader.ReadUInt16();
                OriginalFVF          = FlexibleVertexFormat;
                VertexCount          = reader.ReadUInt16();

                // Read the mesh headers.
                Meshes = new List <TMeshHeader>();
                int triangleStartOffset = 0;
                for (int count = 0; count < MeshCount; count++)
                {
                    TMeshHeader item = new TMeshHeader(reader, triangleStartOffset, materialLibrary);
                    if (item.NumRefVertices < 3)
                    {
                        FLLog.Warning("Vms", $"{name} mesh {count} references 0 triangles");
                    }
                    triangleStartOffset += item.NumRefVertices;
                    Meshes.Add(item);
                }

                // Read the triangle data
                Indices = new ushort[IndexCount];
                for (int i = 0; i < IndexCount; i++)
                {
                    Indices[i] = reader.ReadUInt16();
                }

                // Read the vertex data.
                // The FVF defines what fields are included for each vertex.
                switch (FlexibleVertexFormat)
                {
                case D3DFVF.XYZ:     //(D3DFVF)0x0002:
                    verticesVertexPosition = new VertexPosition[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPosition[i] = new VertexPosition(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL:     //(D3DFVF)0x0012:
                    verticesVertexPositionNormal = new VertexPositionNormal[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormal[i] = new VertexPositionNormal(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.TEX1:     //(D3DFVF)0x0102:
                    verticesVertexPositionNormalTexture = new VertexPositionNormalTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalTexture[i] = new VertexPositionNormalTexture(position, Vector3.One, textureCoordinate);
                    }
                    FlexibleVertexFormat |= D3DFVF.NORMAL;
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX1:     //(D3DFVF)0x0112:
                    verticesVertexPositionNormalTexture = new VertexPositionNormalTexture[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position          = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector3 normal            = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalTexture[i] = new VertexPositionNormalTexture(position, normal, textureCoordinate);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.DIFFUSE | D3DFVF.TEX1:     //(D3DFVF)0x0142:
                    verticesVertexPositionNormalDiffuseTexture = new VertexPositionNormalDiffuseTexture[VertexCount];
                    Diffuse = new uint[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        Vector3 position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Diffuse[i] = reader.ReadUInt32();
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalDiffuseTexture[i] = new VertexPositionNormalDiffuseTexture(position, Vector3.One, Diffuse[i], textureCoordinate);
                    }
                    FlexibleVertexFormat |= D3DFVF.NORMAL;
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX1:     //(D3DFVF)0x0152:
                    verticesVertexPositionNormalDiffuseTexture = new VertexPositionNormalDiffuseTexture[VertexCount];
                    Diffuse = new uint[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        var position = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        var normal   = new Vector3(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle());
                        Diffuse[i] = reader.ReadUInt32();
                        Vector2 textureCoordinate = new Vector2(reader.ReadSingle(), 1 - reader.ReadSingle());
                        verticesVertexPositionNormalDiffuseTexture[i] = new VertexPositionNormalDiffuseTexture(position, normal, Diffuse[i], textureCoordinate);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX2:     //(D3DFVF)0x0212:
                    verticesVertexPositionNormalTextureTwo = new VertexPositionNormalTextureTwo[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormalTextureTwo[i] = new VertexPositionNormalTextureTwo(reader);
                    }
                    break;

                case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.DIFFUSE | D3DFVF.TEX2:     //(D3DFVF)0x0252:
                    verticesVertexPositionNormalDiffuseTextureTwo = new VertexPositionNormalDiffuseTextureTwo[VertexCount];
                    for (int i = 0; i < VertexCount; i++)
                    {
                        verticesVertexPositionNormalDiffuseTextureTwo[i] = new VertexPositionNormalDiffuseTextureTwo(reader);
                    }
                    break;

                /*case D3DFVF.XYZ | D3DFVF.NORMAL | D3DFVF.TEX4: //(D3DFVF)0x0412:
                 *  for (int i = 0; i < VertexCount; i++) vertices[i] = new VertexPositionNormalTextureTangentBinormal(reader);
                 *  break;*/
                default:
                    throw new FileContentException("UTF:VMeshData", "FVF 0x" + FlexibleVertexFormat + " not supported.");
                }
            }
        }