Example #1
0
        public static unsafe PmxMaterial ReadData(byte *pData, PmxHeader header, out byte *offset)
        {
            offset = pData;
            ToonType type;
            var      mat = new PmxMaterial();

            mat.Name             = PmxUtils.ReadString(header.TextEncoding, offset, out offset);
            mat.UniversalName    = PmxUtils.ReadString(header.TextEncoding, offset, out offset);
            mat.DiffuseColor     = *((Vec4 *)PmxUtils.ReadAndIncrement(ref offset, sizeof(Vec4)));
            mat.SpecularColor    = *((Vec3 *)PmxUtils.ReadAndIncrement(ref offset, sizeof(Vec3)));
            mat.SpecularStrength = *((float *)PmxUtils.ReadAndIncrement(ref offset, sizeof(float)));
            mat.AmbientColor     = *((Vec3 *)PmxUtils.ReadAndIncrement(ref offset, sizeof(Vec3)));
            mat.DrawingModeFlags =
                *((DrawingModeFlags *)PmxUtils.ReadAndIncrement(ref offset, sizeof(DrawingModeFlags)));
            mat.EdgeColor = *((Vec3 *)PmxUtils.ReadAndIncrement(ref offset, sizeof(Vec3)));
            mat.EdgeSize  = *((float *)PmxUtils.ReadAndIncrement(ref offset, sizeof(float)));
            var garbage = *((float *)PmxUtils.ReadAndIncrement(ref offset, sizeof(float)));

            mat.TextureIndex         = PmxUtils.ReadVarInt(ref offset, header.TextureIndexSize);
            mat.EnvironmentIndex     = PmxUtils.ReadVarInt(ref offset, header.TextureIndexSize);
            mat.EnvironmentBlendMode =
                *((EnvironmentBlendMode *)PmxUtils.ReadAndIncrement(ref offset, sizeof(EnvironmentBlendMode)));
            mat.ToonType  = (type = *((ToonType *)PmxUtils.ReadAndIncrement(ref offset, sizeof(ToonType))));
            mat.ToonIndex = type == ToonType.Internal
                ? *PmxUtils.ReadAndIncrement(ref offset, sizeof(byte))
                : *((int *)PmxUtils.ReadAndIncrement(ref offset, header.TextureIndexSize));
            mat.MetaData     = PmxUtils.ReadString(header.TextEncoding, offset, out offset);
            mat.SurfaceCount = *((int *)PmxUtils.ReadAndIncrement(ref offset, sizeof(int)));
            return(mat);
        }
Example #2
0
        public static unsafe VertexData ReadData(byte *pData, PmxHeader pmxHeader, out byte *offset)
        {
            var vertexData = new VertexData();

            var vertexCount = *((int *)PmxUtils.ReadAndIncrement(ref pData, sizeof(int)));

            vertexData.Vertices = new PmxVertex[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                var vertex = new PmxVertex();
                vertex.Position   = *((Vec3 *)PmxUtils.ReadAndIncrement(ref pData, sizeof(Vec3)));
                vertex.Normal     = *((Vec3 *)PmxUtils.ReadAndIncrement(ref pData, sizeof(Vec3)));
                vertex.TextureUV  = *((Vec2 *)PmxUtils.ReadAndIncrement(ref pData, sizeof(Vec2)));
                vertex.AppendixUV = new Vec4[pmxHeader.AppendixUV];

                for (int u = 0; u < pmxHeader.AppendixUV; u++)
                {
                    vertex.AppendixUV[u] = *((Vec4 *)PmxUtils.ReadAndIncrement(ref pData, sizeof(Vec4)));
                }

                vertex.WeightType = *((PmxBoneWeightType *)PmxUtils.ReadAndIncrement(ref pData, sizeof(PmxBoneWeightType)));

                switch (vertex.WeightType)
                {
                case PmxBoneWeightType.BDEF1:
                    pData += pmxHeader.BoneIndexSize;
                    break;

                case PmxBoneWeightType.BDEF2:
                    pData += pmxHeader.BoneIndexSize * 2 + sizeof(float);
                    break;

                case PmxBoneWeightType.BDEF4:
                    pData += pmxHeader.BoneIndexSize * 4 + sizeof(float) * 4;
                    break;

                case PmxBoneWeightType.SDEF:
                    pData += pmxHeader.BoneIndexSize * 2 + sizeof(float) + sizeof(Vec3) * 3;
                    break;

                case PmxBoneWeightType.QDEF:
                    pData += pmxHeader.BoneIndexSize * 4 + sizeof(float) * 4;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                vertex.EdgeScale = *((float *)PmxUtils.ReadAndIncrement(ref pData, sizeof(float)));

                vertexData.Vertices[i] = vertex;
            }

            offset = pData;
            return(vertexData);
        }
Example #3
0
 public static unsafe ModelInfo ReadData(byte *pData, PmxHeader header, out byte *offset)
 {
     return(new ModelInfo
     {
         CharacterName = PmxUtils.ReadString(header.TextEncoding, pData, out pData),
         UniversalCharacterName = PmxUtils.ReadString(header.TextEncoding, pData, out pData),
         Comment = PmxUtils.ReadString(header.TextEncoding, pData, out pData),
         UniversalComment = PmxUtils.ReadString(header.TextEncoding, pData, out offset)
     });
 }
Example #4
0
        public static unsafe PmxTextureData ReadData(byte *pData, PmxHeader header, out byte *offset)
        {
            var textureData = new PmxTextureData();
            var texCount    = *((int *)PmxUtils.ReadAndIncrement(ref pData, sizeof(int)));

            textureData.Textures = new string[texCount];
            offset = pData;

            for (int i = 0; i < texCount; i++)
            {
                textureData.Textures[i] = PmxUtils.ReadString(header.TextEncoding, offset, out offset);
            }

            return(textureData);
        }
Example #5
0
        public static unsafe PmxMaterialData ReadData(byte *pData, PmxHeader header, out byte *offset)
        {
            var materialData = new PmxMaterialData();
            var count        = *((int *)PmxUtils.ReadAndIncrement(ref pData, sizeof(int)));

            materialData.Materials = new PmxMaterial[count];
            offset = pData;

            for (int i = 0; i < count; i++)
            {
                materialData.Materials[i] = PmxMaterial.ReadData(offset, header, out offset);
            }

            offset = pData;
            return(materialData);
        }
Example #6
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);
        }