コード例 #1
0
        public override RawMMDModel Read(BinaryReader reader, ModelReadConfig config)
        {
            PmxMeta pmxHeader;

            try
            {
                pmxHeader = ReadMeta(reader);
            }
            catch
            {
                throw new MMDFileParseException("Could not read pmx meta data.");
            }

            if (!"PMX ".Equals(pmxHeader.Magic) || Math.Abs(pmxHeader.Version - 2.0f) > 0.0001f || pmxHeader.FileFlagSize != 8)
            {
                throw new MMDFileParseException("File is not a PMX 2.0 file");
            }

            var model     = new RawMMDModel();
            var pmxConfig = ReadPmxConfig(reader, model);

            ReadModelNameAndDescription(reader, model, pmxConfig);
            ReadVertices(reader, model, pmxConfig);
            ReadTriangles(reader, model, pmxConfig);
            var textureList = ReadTextureList(reader, pmxConfig);

            ReadParts(reader, config, model, pmxConfig, textureList);
            ReadBones(reader, model, pmxConfig);
            ReadMorphs(reader, model, pmxConfig);
            ReadEntries(reader, pmxConfig);
            ReadRigidBodies(reader, model, pmxConfig);
            ReadJoints(reader, model, pmxConfig);
            model.Normalize();
            return(model);
        }
コード例 #2
0
        /// <summary>
        /// 读取mmd
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public override MmdModel Read(BinaryReader reader, ModelReadConfig config)
        {
            //config虽然现在是空的,但是等下会被赋值,作者只是想顺便传出去一下
            var pmxHeader = ReadMeta(reader);                                                                                   //OYM:读取

            if (!"PMX ".Equals(pmxHeader.Magic) || Math.Abs(pmxHeader.Version - 2.0f) > 0.0001f || pmxHeader.FileFlagSize != 8) //OYM:验证文件
            {
                throw new MmdFileParseException("File is not a PMX 2.0 file");
            }

            var model     = new MmdModel();
            var pmxConfig = ReadPmxConfig(reader, model);             //OYM:底层读取,另外config在这里获取

            ReadModelNameAndDescription(reader, model, pmxConfig);    //OYM:读取文件信息
            ReadVertices(reader, model, pmxConfig);                   //OYM:读取顶点
            ReadTriangles(reader, model, pmxConfig);                  //OYM:读取三角形
            var textureList = ReadTextureList(reader, pmxConfig);     //OYM:获取所有材质的路径的一个类

            ReadParts(reader, config, model, pmxConfig, textureList); //OYM:获取材质shader的类
            ReadBones(reader, model, pmxConfig);                      //OYM:获取骨骼
            ReadMorphs(reader, model, pmxConfig);                     //OYM:表情包~
            ReadEntries(reader, pmxConfig);                           //OYM:没用,但是还是要把字节取出来丢掉
            ReadRigidBodies(reader, model, pmxConfig);                //OYM:获取碰撞
            ReadConstraints(reader, model, pmxConfig);                //OYM:获取约束
            model.Normalize();                                        //OYM:检查顶点
            return(model);
        }
コード例 #3
0
        /// <summary>
        /// 这里是一个父类的方法,但是他是在子类里被调用的,看起来好像很牛逼的样子(减少了引用)实际上我觉得还是挺奇怪的
        /// </summary>
        /// <param name="path"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public MmdModel Read(string path, ModelReadConfig config)
        {
            var fileStream     = new FileStream(path, FileMode.Open); //OYM:打开文件
            var bufferedStream = new BufferedStream(fileStream);      //OYM:缓存到这个类可以减少在不用时候产生性能的开销,等等那这里有个毛用啊
            var binaryReader   = new BinaryReader(bufferedStream);    //OYM:读取成二进制

            return(Read(binaryReader, config));                       //OYM:返回一个巨复杂的mmd类,包含mmd所有信息(吧)
        }
コード例 #4
0
 public RawMMDModel Read(string path, ModelReadConfig config)
 {
     using (var fileStream = new FileStream(path, FileMode.Open))
     {
         using (var bufferedStream = new BufferedStream(fileStream)) {
             using (var binaryReader = new BinaryReader(bufferedStream))
             {
                 return(Read(binaryReader, config));
             }
         }
     }
 }
コード例 #5
0
        /// <summary>
        /// 获取这个material各种杂七杂八的属性
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="config"></param>
        /// <param name="encoding"></param>
        /// <param name="textureIndexSize"></param>
        /// <param name="textureList"></param>
        /// <returns></returns>
        private static MmdMaterial ReadMaterial(BinaryReader reader, ModelReadConfig config, Encoding encoding,
                                                int textureIndexSize, MmdTexture[] textureList)
        {
            //OYM:别想着优化这一坨代码,人家按顺序来的
            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();

            //OYM:下面一排位运算,谁有兴趣去看一眼?
            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);//OYM:后面是选择相应的着色器

            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);
        }
コード例 #6
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);
        }
コード例 #7
0
        public static RawMMDModel LoadMmdModel(string path, ModelReadConfig config)
        {
            var fileExt = new FileInfo(path).Extension.ToLower();

            if (".pmd".Equals(fileExt))
            {
                return(new PMDReader().Read(path, config));
            }
            if (".pmx".Equals(fileExt))
            {
                return(new PMXReader().Read(path, config));
            }
            throw new MMDFileParseException("File " + path +
                                            " is not a MMD model file. File name should ends with \"pmd\" or \"pmx\".");
        }
コード例 #8
0
        public static MmdModel LoadMmdModel(string path, ModelReadConfig config)
        {
            var fileExt = new FileInfo(path).Extension.ToLower(); //OYM:extension是扩展名,tolower是换成小写

            if (".pmd".Equals(fileExt))                           //OYM:是pmd
            {
                return(new PmdReader().Read(path, config));
            }
            if (".pmx".Equals(fileExt))                     //OYM:是pmx
            {
                return(new PmxReader().Read(path, config)); //OYM:主要看这个吧,pmd的出bug就不管了
            }
            throw new MmdFileParseException("File " + path +
                                            " is not a MMD model file. File name should ends with \"pmd\" or \"pmx\".");//OYM:抛出错误
        }
コード例 #9
0
        private static void ReadParts(BinaryReader reader, ModelReadConfig config, RawMMDModel model, PmxConfig pmxConfig, MMDTexture[] textureList)
        {
            var partNum       = reader.ReadInt32();
            var partBaseShift = 0;

            model.Parts = new Part[partNum];
            for (var i = 0; i < partNum; i++)
            {
                var part     = new Part();
                var material = ReadMaterial(reader, config, pmxConfig.Encoding, pmxConfig.TextureIndexSize, textureList);
                part.Material = material;
                var partTriangleIndexNum = reader.ReadInt32();
                if (partTriangleIndexNum % 3 != 0)
                {
                    throw new MMDFileParseException("part" + i + " triangle index count " + partTriangleIndexNum +
                                                    " is not multiple of 3");
                }
                part.BaseShift        = partBaseShift;
                part.TriangleIndexNum = partTriangleIndexNum;
                partBaseShift        += partTriangleIndexNum;
                model.Parts[i]        = part;
            }
        }
コード例 #10
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);
                }
            }
        }
コード例 #11
0
 public abstract RawMMDModel Read(BinaryReader reader, ModelReadConfig config);
コード例 #12
0
 public abstract MmdModel Read(BinaryReader reader, ModelReadConfig config);