Example #1
0
        public unsafe PmxModel(Stream model)
        {
            var buffer = new byte[model.Length];

            model.Read(buffer, 0, (int)model.Length);

            fixed(byte *p = buffer)
            {
                var mp = p;

                this.Header = *((PmxHeader *)mp);
                mp         += sizeof(PmxHeader);

                this.ModelInfo    = ModelInfo.ReadData(mp, this.Header, out mp);
                this.VertexData   = VertexData.ReadData(mp, this.Header, out mp);
                this.FaceData     = FaceData.ReadData(mp, this.Header, out mp);
                this.TextureData  = PmxTextureData.ReadData(mp, this.Header, out mp);
                this.MaterialData = PmxMaterialData.ReadData(mp, this.Header, out mp);
            }
        }
Example #2
0
        public static unsafe FaceData ReadData(byte *pData, PmxHeader header, out byte *offset)
        {
            var indexCount = *((int *)PmxUtils.ReadAndIncrement(ref pData, sizeof(int)));
            var faceData   = new FaceData();

            faceData.Indices = new uint[indexCount];

            switch (header.VertexIndexSize)
            {
            case sizeof(byte):
                for (int i = 0; i < indexCount; i++)
                {
                    faceData.Indices[i] = *pData++;
                }
                offset = (byte *)pData;
                break;

            case sizeof(ushort):
                var ushortPtr = (ushort *)pData;
                for (int i = 0; i < indexCount; i++)
                {
                    faceData.Indices[i] = *ushortPtr++;
                }
                offset = (byte *)ushortPtr;
                break;

            case sizeof(uint):
                var uintPtr = (uint *)pData;
                for (int i = 0; i < indexCount; i++)
                {
                    faceData.Indices[i] = *uintPtr++;
                }
                offset = (byte *)uintPtr;
                break;

            default:
                throw new NotSupportedException($"Unsupported index size of {header.VertexIndexSize}");
            }
            return(faceData);
        }