Ejemplo n.º 1
0
 public PmdBone getBoneByName(String i_name)
 {
     PmdBone[] bone_array = this.m_pBoneArray;
     for (int i = 0; i < bone_array.Length; i++)
     {
         PmdBone bone = bone_array[i];
         if (bone.getName().Equals(i_name))
         {
             return(bone);
         }
     }
     return(null);
 }
Ejemplo n.º 2
0
        public MmdMotionPlayer(MmdPmdModel i_pmd_model, MmdVmdMotion i_vmd_model)
        {
            this._ref_pmd_model  = i_pmd_model;
            this._ref_vmd_motion = i_vmd_model;
            this.m_bLoop         = false;

            PmdBone[] bone_array = i_pmd_model.getBoneArray();

            //スキニング用のmatrix
            this._skinning_mat = MmdMatrix.createArray(bone_array.Length);


            //---------------------------------------------------------
            // 操作対象ボーンのポインタを設定する
            MotionData[] pMotionDataList = i_vmd_model.refMotionDataArray();
            this.m_ppBoneList = new PmdBone[pMotionDataList.Length];
            for (int i = 0; i < pMotionDataList.Length; i++)
            {
                this.m_ppBoneList[i] = i_pmd_model.getBoneByName(pMotionDataList[i].szBoneName);
            }


            //---------------------------------------------------------
            // 操作対象表情のポインタを設定する
            FaceData[] pFaceDataList = i_vmd_model.refFaceDataArray();
            this.m_ppFaceList = new PmdFace[pFaceDataList.Length];
            for (int i = 0; i < pFaceDataList.Length; i++)
            {
                this.m_ppFaceList[i] = i_pmd_model.getFaceByName(pFaceDataList[i].szFaceName);
            }


            //首^H頭のボーンを探しておく
            this.m_pNeckBone = null;
            for (int i = 0; i < bone_array.Length; i++)
            {
                if (bone_array[i].getName().Equals("頭"))
                {
                    this.m_pNeckBone = bone_array[i];
                    break;
                }
            }

            // 変数初期値設定
            this.m_fOldFrame = this.m_fFrame = 0.0f;
            return;
        }
Ejemplo n.º 3
0
        private static PmdBone[] createBoneArray(DataReader i_reader)
        {
            int      number_of_bone = i_reader.readShort();
            PMD_Bone tmp_pmd_bone   = new PMD_Bone();

            PmdBone[] result = new PmdBone[number_of_bone];
            for (int i = 0; i < number_of_bone; i++)
            {
                tmp_pmd_bone.read(i_reader);
                //ボーンの親子関係を一緒に読みだすので。
                result[i] = new PmdBone(tmp_pmd_bone, result);
            }
            for (int i = 0; i < number_of_bone; i++)
            {
                result[i].recalcOffset();
            }
            return(result);
        }
Ejemplo n.º 4
0
        private void ReadBonesAndIks(BinaryReader reader, RawMMDModel model, PmdReadContext context)
        {
            int boneNum = reader.ReadUInt16();

            context.BoneNum = boneNum;
            var rawBones = new PmdBone[boneNum];

            for (var i = 0; i < boneNum; ++i)
            {
                rawBones[i] = ReadPmdBone(reader);
            }

            var ikBoneIds = new HashSet <int>();
            int ikNum     = reader.ReadUInt16();
            var rawIkList = new List <PmdRawIk>(ikNum);

            for (var i = 0; i < ikNum; ++i)
            {
                var rawIk = new PmdRawIk();
                rawIk.Preamble = ReadPmdIkPreamable(reader);
                ikBoneIds.Add(rawIk.Preamble.IkBoneIndex);
                rawIk.Chain = new int[rawIk.Preamble.IkChainLength];
                for (var j = 0; j < rawIk.Preamble.IkChainLength; ++j)
                {
                    rawIk.Chain[j] = reader.ReadUInt16();
                }
                rawIkList.Add(rawIk);
            }

            rawIkList.Sort((ik1, ik2) =>
            {
                var a1 = 0;
                var a2 = 0;
                if (ik1.Chain.Length > 0)
                {
                    a1 = ik1.Chain[0];
                }
                if (ik2.Chain.Length > 0)
                {
                    a2 = ik2.Chain[0];
                }
                return(a1.CompareTo(a2));
            });

            var boneList = new List <Bone>();

            int?centerBoneIndex = null;

            model.Bones = new Bone[boneNum];
            for (var i = 0; i < boneNum; ++i)
            {
                var bone = new Bone();
                boneList.Add(bone);
                var rawBone = rawBones[i];
                bone.Name = rawBone.Name;
                if ("\u30BB\u30F3\u30BF\u30FC".Equals(bone.Name)) //TODO 验证是不是这个值
                {
                    centerBoneIndex = i;
                }
                bone.Position = rawBone.Position;
                // TODO - workaround here, need fix, see [1].
                if (i != rawBone.ParentId)
                {
                    bone.ParentIndex = rawBone.ParentId;
                }
                else
                {
                    bone.ParentIndex = 0; //TODO mmdlib里是nil
                }
                bone.TransformLevel          = 0;
                bone.ChildBoneVal.ChildUseId = true;
                bone.ChildBoneVal.Index      = rawBone.ChildId;
                bone.Rotatable = true;
                var type = (PmdBoneTypes)rawBone.Type;
                bone.HasIk   = type == PmdBoneTypes.PmdBoneIk || ikBoneIds.Contains(i);
                bone.Movable = type == PmdBoneTypes.PmdBoneRotateAndTranslate || bone.HasIk;
                bone.Visible = type != PmdBoneTypes.PmdBoneIkTo && type != PmdBoneTypes.PmdBoneInvisible &&
                               type != PmdBoneTypes.PmdBoneRotateRatio;
                bone.Controllable     = true;
                bone.AppendRotate     = type == PmdBoneTypes.PmdBoneRotateEffect || type == PmdBoneTypes.PmdBoneRotateRatio;
                bone.AppendTranslate  = false;
                bone.RotAxisFixed     = type == PmdBoneTypes.PmdBoneTwist;
                bone.UseLocalAxis     = false;
                bone.PostPhysics      = false;
                bone.ReceiveTransform = false;

                if (bone.AppendRotate)
                {
                    if (type == PmdBoneTypes.PmdBoneRotateEffect)
                    {
                        bone.AppendBoneVal.Index = rawBone.IkNumber;
                        bone.AppendBoneVal.Ratio = 1.0f;
                        bone.TransformLevel      = 2;
                    }
                    else
                    {
                        bone.ChildBoneVal.ChildUseId = false;
                        bone.ChildBoneVal.Offset     = Vector3.zero;
                        bone.AppendBoneVal.Index     = rawBone.ChildId;
                        bone.AppendBoneVal.Ratio     = rawBone.IkNumber * 0.01f;
                    }
                }
                if (bone.HasIk)
                {
                    bone.TransformLevel = 1;
                }

                if (bone.RotAxisFixed)
                {
                    var childId = rawBone.ChildId;
                    if (childId > boneNum)
                    {
                        childId = 0;
                    }
                    bone.RotAxis = (rawBones[childId].Position - bone.Position).normalized;
                    if (bone.ChildBoneVal.ChildUseId)
                    {
                        bone.ChildBoneVal.ChildUseId = false;
                        bone.ChildBoneVal.Offset     = Vector3.zero;
                    }
                }

                model.Bones[i] = bone;
            }

            var loLimit = Vector3.zero;
            var hiLimit = Vector3.zero;

            loLimit.x = (float)-Math.PI;
            hiLimit.x = (float)(-0.5f / 180.0f * Math.PI);

            for (var i = 0; i < boneNum; ++i)
            {
                if (!ikBoneIds.Contains(i))
                {
                    boneList[i].IkInfoVal = new Bone.IkInfo
                    {
                        IkLinks = new Bone.IkLink[0]
                    };
                    continue;
                }
                var associatedIkCount = 0;
                for (var j = 0; j < ikNum; ++j)
                {
                    var rawIk = rawIkList[j];
                    if (i != rawIk.Preamble.IkBoneIndex)
                    {
                        continue;
                    }
                    Bone bone;
                    if (associatedIkCount == 0)
                    {
                        bone = boneList[i];
                    }
                    else
                    {
                        var originalBone = boneList[i];
                        bone = Bone.CopyOf(originalBone);
                        boneList.Add(bone);
                        bone.Name                    = "[IK]" + originalBone.Name;
                        bone.NameEn                  = "[IK]" + originalBone.NameEn;
                        bone.ParentIndex             = i;
                        bone.ChildBoneVal.ChildUseId = false;
                        bone.ChildBoneVal.Offset     = Vector3.zero;
                        bone.Visible                 = false;
                        bone.IkInfoVal.IkLinks       = new Bone.IkLink[rawIk.Preamble.IkChainLength];
                        bone.HasIk                   = true;
                    }
                    bone.IkInfoVal = new Bone.IkInfo();
                    bone.IkInfoVal.IkTargetIndex   = rawIk.Preamble.IkTargetBoneIndex;
                    bone.IkInfoVal.CcdIterateLimit = rawIk.Preamble.CcdIterateLimit;
                    bone.IkInfoVal.CcdAngleLimit   = rawIk.Preamble.CcdAngleLimit;
                    bone.IkInfoVal.IkLinks         = new Bone.IkLink[rawIk.Preamble.IkChainLength];

                    for (var k = 0; k < rawIk.Preamble.IkChainLength; ++k)
                    {
                        var link = new Bone.IkLink();
                        link.LinkIndex = rawIk.Chain[k];
                        var linkName = model.Bones[link.LinkIndex].Name;
                        if ("\u5DE6\u3072\u3056".Equals(linkName) || "\u53F3\u3072\u3056".Equals(linkName))
                        {
                            link.HasLimit = true;
                            link.LoLimit  = loLimit;
                            link.HiLimit  = hiLimit;
                        }
                        else
                        {
                            link.HasLimit = false;
                        }

                        bone.IkInfoVal.IkLinks[k] = link;
                    }

                    associatedIkCount++;
                }
            }

            model.Bones = boneList.ToArray();

            // TODO - need verification

            for (var i = 0; i < boneNum; ++i)
            {
                var stable = true;
                for (var j = 0; j < boneNum; ++j)
                {
                    var bone           = model.Bones[j];
                    var transformLevel = bone.TransformLevel;
                    var parentId       = bone.ParentIndex;
                    while (parentId < boneNum)
                    {
                        var parentTransformLevel = model.Bones[parentId].TransformLevel;
                        if (transformLevel < parentTransformLevel)
                        {
                            transformLevel = parentTransformLevel;
                            stable         = false;
                        }
                        parentId = model.Bones[parentId].ParentIndex;
                    }
                    bone.TransformLevel = transformLevel;
                }
                if (stable)
                {
                    break;
                }
            }
            context.CenterBoneIndex = centerBoneIndex;
        }
Ejemplo n.º 5
0
 public PmdLoader(string Path)
 {
     using (var fs = new FileStream(Path, FileMode.Open)) {
         bs     = new BinaryStream(fs);
         Header = new PmdHeader(bs);
         Vertex = new PmdVertex[bs.ReadInt32()];
         for (int i = 0; i < Vertex.Length; i++)
         {
             Vertex[i] = new PmdVertex(bs);
         }
         Index = new PmdIndex[bs.ReadInt32() / 3];
         for (int i = 0; i < Index.Length; i++)
         {
             Index[i] = new PmdIndex(bs);
         }
         Material = new PmdMaterial[bs.ReadInt32()];
         for (int i = 0; i < Material.Length; i++)
         {
             Material[i] = new PmdMaterial(bs);
         }
         Bone = new PmdBone[bs.ReadInt16()];
         for (int i = 0; i < Bone.Length; i++)
         {
             Bone[i] = new PmdBone(bs);
         }
         Ik = new PmdIk[bs.ReadInt16()];
         for (int i = 0; i < Ik.Length; i++)
         {
             Ik[i] = new PmdIk(bs);
         }
         Skin = new PmdSkin[bs.ReadInt16()];
         for (int i = 0; i < Skin.Length; i++)
         {
             Skin[i] = new PmdSkin(bs);
         }
         bs.SkipShort(bs.ReadByte());
         byte BoneDispCount = bs.ReadByte();
         bs.Sjis(50 * BoneDispCount);
         int bondis = bs.ReadInt32();
         for (int i = 0; i < bondis; i++)
         {
             bs.ReadInt16(); bs.ReadByte();
         }
         if (bs.ReadByte() == (byte)1)
         {
             bs.Sjis(20); bs.Sjis(256);
             bs.Sjis(20 * Bone.Length);
             bs.Sjis(20 * (Skin.Length - 1));
             bs.Sjis(50 * BoneDispCount);
         }
         Toon = new PmdToon[10];
         for (int i = 0; i < 10; i++)
         {
             Toon[i] = new PmdToon(bs);
         }
         Rigid = new PmdRigid[bs.ReadInt32()];
         for (int i = 0; i < Rigid.Length; i++)
         {
             Rigid[i] = new PmdRigid(bs);
         }
         Joint = new PmdJoint[bs.ReadInt32()];
         for (int i = 0; i < Joint.Length; i++)
         {
             Joint[i] = new PmdJoint(bs);
         }
     }
 }