public Geometry(SectionHeader header, Stream stream)
            : base(header, stream)
        {
            var dataHeader = SectionHeader.Read(stream);
            var reader = new BinaryReader(stream);

            Flags = (GeometryFlag) reader.ReadUInt16();
            var uvCount = reader.ReadByte(); // uv count
            reader.ReadByte(); // native flags
            FaceCount = reader.ReadUInt32();
            VertexCount = reader.ReadUInt32();
            FrameCount = reader.ReadUInt32();

            if (dataHeader.Version == 4099) {
                Ambient = reader.ReadSingle();
                Diffuse = reader.ReadSingle();
                Specular = reader.ReadSingle();
            }

            if ((Flags & GeometryFlag.Colors) != 0) {
                Colours = new Color4[VertexCount];
                for (var i = 0; i < VertexCount; ++i) {
                    Colours[i] = new Color4(reader);
                }
            }

            if ((Flags & (GeometryFlag.TexCoords | GeometryFlag.TexCoords2)) != 0) {
                TexCoords = new Vector2[uvCount][];
                for (var j = 0; j < uvCount; ++j) {
                    var uvs =  TexCoords[j] = new Vector2[VertexCount];
                    for (var i = 0; i < VertexCount; ++i) {
                        uvs[i] = new Vector2(reader);
                    }
                }
            }

            Faces = new FaceInfo[FaceCount];
            for (var i = 0; i < FaceCount; ++i) {
                Faces[i] = new FaceInfo(reader);
            }

            BoundingSphere = new BoundingSphere(reader);

            HasPosition = reader.ReadUInt32();
            HasNormals = reader.ReadUInt32();

            if (HasPosition > 1 || HasNormals > 1) {
                throw new Exception("Well there you go");
            }

            if ((Flags & GeometryFlag.VertexTranslation) != 0)
            {
                Vertices = new Vector3[VertexCount];
                for (var i = 0; i < VertexCount; ++i)
                {
                    Vertices[i] = new Vector3(reader);
                }
            }

            if ((Flags & GeometryFlag.Normals) != 0)
            {
                Normals = new Vector3[VertexCount];
                for (var i = 0; i < VertexCount; ++i)
                {
                    Normals[i] = new Vector3(reader);
                }
            }

            Materials = ReadSection<MaterialList>().Materials;

            var extensions = ReadSection<Extension>();

            MaterialSplits = extensions.FirstOrDefault<MaterialSplitList>().MaterialSplits;
            Skinning = extensions.FirstOrDefault<Skin>();
        }
Beispiel #2
0
        public Geometry(SectionHeader header, Stream stream)
            : base(header, stream)
        {
            var dataHeader = SectionHeader.Read(stream);
            var reader     = new BinaryReader(stream);

            Flags = (GeometryFlag)reader.ReadUInt16();
            var uvCount = reader.ReadByte(); // uv count

            reader.ReadByte();               // native flags
            FaceCount   = reader.ReadUInt32();
            VertexCount = reader.ReadUInt32();
            FrameCount  = reader.ReadUInt32();

            if (dataHeader.Version == 4099)
            {
                Ambient  = reader.ReadSingle();
                Diffuse  = reader.ReadSingle();
                Specular = reader.ReadSingle();
            }

            if ((Flags & GeometryFlag.Colors) != 0)
            {
                Colours = new UnityEngine.Color32[VertexCount];
                for (var i = 0; i < VertexCount; ++i)
                {
                    Colours[i] = Conversion.Types.Convert(new Color4(reader));
                }
            }

            if ((Flags & (GeometryFlag.TexCoords | GeometryFlag.TexCoords2)) != 0)
            {
                TexCoords = new UnityEngine.Vector2[uvCount][];
                for (var j = 0; j < uvCount; ++j)
                {
                    var uvs = TexCoords[j] = new UnityEngine.Vector2[VertexCount];
                    for (var i = 0; i < VertexCount; ++i)
                    {
                        uvs[i] = Conversion.Types.Convert(new Vector2(reader));
                    }
                }
            }

            Faces = new FaceInfo[FaceCount];
            for (var i = 0; i < FaceCount; ++i)
            {
                Faces[i] = new FaceInfo(reader);
            }

            BoundingSphere = new BoundingSphere(reader);

            HasPosition = reader.ReadUInt32();
            HasNormals  = reader.ReadUInt32();

            if (HasPosition > 1 || HasNormals > 1)
            {
                throw new Exception("Well there you go");
            }

            if ((Flags & GeometryFlag.VertexTranslation) != 0)
            {
                Vertices = new UnityEngine.Vector3[VertexCount];
                for (var i = 0; i < VertexCount; ++i)
                {
                    Vertices[i] = Conversion.Types.Convert(new Vector3(reader));
                }
            }

            if ((Flags & GeometryFlag.Normals) != 0)
            {
                Normals = new UnityEngine.Vector3[VertexCount];
                for (var i = 0; i < VertexCount; ++i)
                {
                    Normals[i] = Conversion.Types.Convert(new Vector3(reader));
                }
            }

            Materials = ReadSection <MaterialList>().Materials;

            var extensions = ReadSection <Extension>();

            MaterialSplits = extensions.FirstOrDefault <MaterialSplitList>().MaterialSplits;
            Skinning       = extensions.FirstOrDefault <Skin>();
        }