示例#1
0
        public void Write(BinaryWriter bw, SimpleSkinVertexType vertexType)
        {
            this.Position.Write(bw);

            for (int i = 0; i < 4; i++)
            {
                bw.Write(this.BoneIndices[i]);
            }

            for (int i = 0; i < 4; i++)
            {
                bw.Write(this.Weights[i]);
            }

            this.Normal.Write(bw);
            this.UV.Write(bw);

            if (vertexType == SimpleSkinVertexType.Color)
            {
                if (this.Color.HasValue)
                {
                    bw.WriteColor(this.Color.Value, ColorFormat.RgbaU8);
                }
                else
                {
                    bw.WriteColor(new Color(0, 0, 0, 255), ColorFormat.RgbaU8);
                }
            }
        }
示例#2
0
        public SimpleSkin(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                uint magic = br.ReadUInt32();
                if (magic != 0x00112233)
                {
                    throw new Exception("Not a valid SKN file");
                }

                ushort major = br.ReadUInt16();
                ushort minor = br.ReadUInt16();
                if (major != 2 && major != 4 && minor != 1)
                {
                    throw new Exception("This SKN version is not supported");
                }

                uint submeshCount = br.ReadUInt32();

                for (int i = 0; i < submeshCount; i++)
                {
                    this.Submeshes.Add(new SimpleSkinSubmesh(br));
                }
                if (major == 4)
                {
                    uint unknown = br.ReadUInt32();
                }

                uint indexCount  = br.ReadUInt32();
                uint vertexCount = br.ReadUInt32();

                uint vertexSize = major == 4 ? br.ReadUInt32() : 52;
                SimpleSkinVertexType vertexType = major == 4 ? (SimpleSkinVertexType)br.ReadUInt32() : SimpleSkinVertexType.Basic;
                R3DBox    boundingBox           = major == 4 ? new R3DBox(br) : R3DBox.Infinite;
                R3DSphere boundingSphere        = major == 4 ? new R3DSphere(br) : R3DSphere.Infinite;

                List <ushort>           indices  = new List <ushort>();
                List <SimpleSkinVertex> vertices = new List <SimpleSkinVertex>();
                for (int i = 0; i < indexCount; i++)
                {
                    indices.Add(br.ReadUInt16());
                }
                for (int i = 0; i < vertexCount; i++)
                {
                    vertices.Add(new SimpleSkinVertex(br, vertexType));
                }

                foreach (SimpleSkinSubmesh submesh in this.Submeshes)
                {
                    List <ushort> submeshIndices = indices.GetRange((int)submesh._startIndex, (int)submesh._indexCount);
                    ushort        minIndex       = submeshIndices.Min();

                    submesh.Indices  = submeshIndices.Select(x => x -= minIndex).ToList();
                    submesh.Vertices = vertices.GetRange((int)submesh._startVertex, (int)submesh._vertexCount);
                }
            }
        }
示例#3
0
        public SimpleSkinVertex(BinaryReader br, SimpleSkinVertexType vertexType)
        {
            this.Position    = new Vector3(br);
            this.BoneIndices = new byte[] { br.ReadByte(), br.ReadByte(), br.ReadByte(), br.ReadByte() };
            this.Weights     = new float[] { br.ReadSingle(), br.ReadSingle(), br.ReadSingle(), br.ReadSingle() };
            this.Normal      = new Vector3(br);
            this.UV          = new Vector2(br);

            if (vertexType == SimpleSkinVertexType.Color)
            {
                this.Color = br.ReadColor(ColorFormat.RgbaU8);
            }
        }
示例#4
0
        public SimpleSkin(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                uint magic = br.ReadUInt32();
                if (magic != 0x00112233)
                {
                    throw new InvalidFileSignatureException();
                }

                ushort major = br.ReadUInt16();
                ushort minor = br.ReadUInt16();
                if (major != 0 && major != 2 && major != 4 && minor != 1)
                {
                    throw new UnsupportedFileVersionException();
                }

                uint indexCount  = 0;
                uint vertexCount = 0;
                SimpleSkinVertexType vertexType = SimpleSkinVertexType.Basic;
                if (major == 0)
                {
                    indexCount  = br.ReadUInt32();
                    vertexCount = br.ReadUInt32();
                }
                else
                {
                    uint submeshCount = br.ReadUInt32();

                    for (int i = 0; i < submeshCount; i++)
                    {
                        this.Submeshes.Add(new SimpleSkinSubmesh(br));
                    }
                    if (major == 4)
                    {
                        uint flags = br.ReadUInt32();
                    }

                    indexCount  = br.ReadUInt32();
                    vertexCount = br.ReadUInt32();

                    uint vertexSize = major == 4 ? br.ReadUInt32() : 52;
                    vertexType = major == 4 ? (SimpleSkinVertexType)br.ReadUInt32() : SimpleSkinVertexType.Basic;
                    R3DBox    boundingBox    = major == 4 ? new R3DBox(br) : new R3DBox(Vector3.Zero, Vector3.Zero);
                    R3DSphere boundingSphere = major == 4 ? new R3DSphere(br) : R3DSphere.Infinite;
                }

                List <ushort>           indices  = new List <ushort>();
                List <SimpleSkinVertex> vertices = new List <SimpleSkinVertex>();
                for (int i = 0; i < indexCount; i++)
                {
                    indices.Add(br.ReadUInt16());
                }
                for (int i = 0; i < vertexCount; i++)
                {
                    vertices.Add(new SimpleSkinVertex(br, vertexType));
                }

                if (major == 0)
                {
                    this.Submeshes.Add(new SimpleSkinSubmesh("Base", indices, vertices));
                }
                else
                {
                    foreach (SimpleSkinSubmesh submesh in this.Submeshes)
                    {
                        List <ushort> submeshIndices = indices.GetRange((int)submesh._startIndex, (int)submesh._indexCount);
                        ushort        minIndex       = submeshIndices.Min();

                        submesh.Indices  = submeshIndices.Select(x => x -= minIndex).ToList();
                        submesh.Vertices = vertices.GetRange((int)submesh._startVertex, (int)submesh._vertexCount);
                    }
                }
            }
        }