Esempio n. 1
0
        public override RawMMDModel Read(BinaryReader reader, ModelReadConfig config)
        {
            var model   = new RawMMDModel();
            var context = new PmdReadContext();
            var meta    = ReadMeta(reader);

            if (!"Pmd".Equals(meta.Magic) || Math.Abs(meta.Version - 1.0f) > 0.0001f)
            {
                throw new MMDFileParseException("File is not a PMD 1.0 file");
            }
            ReadDescription(reader, model);
            ReadVertices(reader, model);
            ReadTriangles(reader, model);
            var toonTextureIds = new List <int>();

            ReadParts(reader, model, toonTextureIds);
            ReadBonesAndIks(reader, model, context);
            ReadFaces(reader, model);
            ReadFacdDisplayListNames(reader);
            ReadBoneNameList(reader, context);
            ReadBoneDisp(reader);
            if (MMDReaderUtil.Eof(reader))
            {
                goto PMD_READER_READ_LEGACY_30;
            }
            ReadInfoEn(reader, model, context);
            if (MMDReaderUtil.Eof(reader))
            {
                goto PMD_READER_READ_LEGACY_30;
            }
            ReadCustomTextures(reader, config, model, toonTextureIds);
            if (MMDReaderUtil.Eof(reader))
            {
                goto PMD_READER_READ_LEGACY_50;
            }
            ReadRigidBodies(reader, model, context);
            ReadConstraints(reader, model);
            goto PMD_READER_READ_SUCCEED;

PMD_READER_READ_LEGACY_30:

            for (var i = 0; i < model.Parts.Length; ++i)
            {
                var material = model.Parts[i].Material;
                material.Toon = MMDTextureUtil.GetGlobalToon(toonTextureIds[i], config.GlobalToonPath);
            }
PMD_READER_READ_LEGACY_50:
PMD_READER_READ_SUCCEED:
            model.Normalize();
            return(model);
        }
Esempio n. 2
0
        private static MMDMaterial ReadMaterial(BinaryReader reader, ModelReadConfig config, Encoding encoding,
                                                int textureIndexSize, MMDTexture[] textureList)
        {
            var material = new MMDMaterial();

            material.Name          = MMDReaderUtil.ReadSizedString(reader, encoding);
            material.NameEn        = MMDReaderUtil.ReadSizedString(reader, encoding);
            material.DiffuseColor  = MMDReaderUtil.ReadColor(reader, true);
            material.SpecularColor = MMDReaderUtil.ReadColor(reader, false);
            material.Shiness       = reader.ReadSingle();
            material.AmbientColor  = MMDReaderUtil.ReadColor(reader, false);
            var drawFlag = reader.ReadByte();

            material.DrawDoubleFace   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawDoubleFace) != 0;
            material.DrawGroundShadow = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawGroundShadow) != 0;
            material.CastSelfShadow   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialCastSelfShadow) != 0;
            material.DrawSelfShadow   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawSelfShadow) != 0;
            material.DrawEdge         = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawEdge) != 0;
            material.EdgeColor        = MMDReaderUtil.ReadColor(reader, true);
            material.EdgeSize         = reader.ReadSingle();
            var textureIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);

            if (textureIndex < textureList.Length && textureIndex >= 0)
            {
                material.Texture = textureList[textureIndex];
            }
            var subTextureIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);

            if (subTextureIndex < textureList.Length && subTextureIndex >= 0)
            {
                material.SubTexture = textureList[subTextureIndex];
            }
            material.SubTextureType = (MMDMaterial.SubTextureTypeEnum)reader.ReadByte();
            var useGlobalToon = reader.ReadByte() != 0;

            if (useGlobalToon)
            {
                int globalToonIndex = reader.ReadByte();
                material.Toon = MMDTextureUtil.GetGlobalToon(globalToonIndex, config.GlobalToonPath);
            }
            else
            {
                var toonIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);
                if (toonIndex < textureList.Length && toonIndex >= 0)
                {
                    material.Toon = textureList[toonIndex];
                }
            }
            material.MetaInfo = MMDReaderUtil.ReadSizedString(reader, encoding);
            return(material);
        }
Esempio n. 3
0
        private static void ReadCustomTextures(BinaryReader reader, ModelReadConfig config, RawMMDModel model,
                                               List <int> toonTextureIds)
        {
            var customTextures = new MMDTexture[10];

            for (var i = 0; i < 10; ++i)
            {
                customTextures[i] =
                    new MMDTexture(MMDReaderUtil.ReadStringFixedLength(reader, 100, Tools.JapaneseEncoding));
            }

            for (var i = 0; i < model.Parts.Length; ++i)
            {
                var material = model.Parts[i].Material;
                if (toonTextureIds[i] < 10)
                {
                    material.Toon = customTextures[toonTextureIds[i]];
                }
                else
                {
                    material.Toon = MMDTextureUtil.GetGlobalToon(0, config.GlobalToonPath);
                }
            }
        }