コード例 #1
0
ファイル: PMXBone.cs プロジェクト: haolink/PMXStructure
        /// <summary>
        /// Create a local coordinate axis for the current bone.
        /// </summary>
        public void CreateLocalCoodinateAxis()
        {
            PMXVector3 targetLocation;

            if (this.HasChildBone)
            {
                if (this.ChildBone == this)
                {
                    return;
                }
                targetLocation = this.ChildBone.Position;
            }
            else
            {
                targetLocation = this.Position + this.ChildVector;
            }

            PMXVector3 direction = targetLocation - this.Position;

            PMXVector3 zAxis = (new PMXVector3(0, 1, 0)).CrossProduct(direction);

            if (zAxis.Z > 0)
            {
                zAxis *= -1.0f;
            }

            /*float dotProduct = direction.DotProduct(zAxis);
             * Console.WriteLine(dotProduct.ToString());*/

            this.LocalCoordinates  = true;
            this.LocalCoordinatesX = direction.Normalize();
            this.LocalCoordinatesZ = zAxis.Normalize();
        }
コード例 #2
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int vtxIndex;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX
                vtxIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.VertexIndexLength);
            }
            else
            { //PMD
                vtxIndex = br.ReadInt32();

                if (importSettings.BaseMorph != null)
                {
                    if (vtxIndex < importSettings.BaseMorph.Offsets.Count)
                    {
                        PMXMorphOffsetVertex mov = (PMXMorphOffsetVertex)importSettings.BaseMorph.Offsets[vtxIndex];
                        vtxIndex = this.Model.Vertices.IndexOf(mov.Vertex);
                    }
                }
            }

            this.Vertex      = this.Model.Vertices[vtxIndex];
            this.Translation = PMXVector3.LoadFromStreamStatic(br);
        }
コード例 #3
0
ファイル: PMXBone.cs プロジェクト: haolink/PMXStructure
        /// <summary>
        /// Parses PMD Twist data if available.
        /// </summary>
        public void ParsePMDTwist()
        {
            if (!this._isPMDTwist || this.Parent == null)
            {
                return;
            }
            this._isPMDTwist = false;
            PMXBone p = this.Parent;

            PMXVector3 targetLocation;

            if (p.HasChildBone)
            {
                if (p.ChildBone == this)
                {
                    return;
                }
                targetLocation = p.ChildBone.Position;
            }
            else
            {
                targetLocation = p.Position + p.ChildVector;
            }

            PMXVector3 direction = targetLocation - this.Position;

            direction = direction.Normalize();

            this.FixedAxis = true;
            this.AxisLimit = direction;
        }
コード例 #4
0
ファイル: PMXRigidBody.cs プロジェクト: haolink/PMXStructure
 public PMXRigidBody(PMXModel model) : base(model)
 {
     this._shapeSize         = new PMXVector3();
     this.Position           = new PMXVector3();
     this.Rotation           = new PMXVector3();
     this.NoCollissionGroups = new PMXNoCollissionGroup(this.Model, this);
 }
コード例 #5
0
        public static PMXVector3 LoadFromStreamStatic(BinaryReader br)
        {
            PMXVector3 res = new PMXVector3();

            res.LoadFromStream(br);
            return(res);
        }
コード例 #6
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

            this.Bone        = this.Model.Bones[boneIndex];
            this.Translation = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation    = PMXQuaternion.LoadFromStreamStatic(br);
        }
コード例 #7
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            base.LoadFromStream(br, importSettings);

            this.C  = PMXVector3.LoadFromStreamStatic(br);
            this.R0 = PMXVector3.LoadFromStreamStatic(br);
            this.R1 = PMXVector3.LoadFromStreamStatic(br);
        }
コード例 #8
0
 public PMXVector3 CrossProduct(PMXVector3 b)
 {
     return(new PMXVector3(
                this.Y * b.Z - this.Z * b.Y,
                this.Z * b.X - this.X * b.Z,
                this.X * b.Y - this.Y * b.X
                ));
 }
コード例 #9
0
 public PMXJoint(PMXModel model) : base(model)
 {
     this.Position                  = new PMXVector3();
     this.Rotation                  = new PMXVector3();
     this.TranslationLimitMin       = new PMXVector3();
     this.TranslationLimitMax       = new PMXVector3();
     this.RotationLimitMin          = new PMXVector3();
     this.RotationLimitMax          = new PMXVector3();
     this.SpringConstantTranslation = new PMXVector3();
     this.SpringConstantRotation    = new PMXVector3();
 }
コード例 #10
0
        public PMXVector3 Normalize()
        {
            PMXVector3 n = new PMXVector3(this.X, this.Y, this.Z);
            float      v = this.Value;

            if (v != 0.0f)
            {
                n.X /= v;
                n.Y /= v;
                n.Z /= v;
            }
            return(n);
        }
コード例 #11
0
ファイル: PMXIKLink.cs プロジェクト: haolink/PMXStructure
 /// <summary>
 /// Updates IKs of PMD files for legs.
 /// </summary>
 public void UpdatePMDIKs()
 {
     if (this.Bone != null)
     {
         string bnName = this.Bone.NameJP;
         if (Array.IndexOf <string>(PMXIKLink.ThighBones, bnName) >= 0)
         {
             this.HasLimits = true;
             this.Minimum   = new PMXVector3((float)((-1) * Math.PI), 0.0f, 0.0f);
             this.Maximum   = new PMXVector3((float)((-1.0f / 360.0f) * Math.PI), 0.0f, 0.0f);
         }
     }
 }
コード例 #12
0
ファイル: PMXVertex.cs プロジェクト: haolink/PMXStructure
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            this.Position = PMXVector3.LoadFromStreamStatic(br);
            this.Normals  = PMXVector3.LoadFromStreamStatic(br);
            this.UV       = PMXVector2.LoadFromStreamStatic(br);


            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                for (int i = 0; i < importSettings.ExtendedUV; i++)
                {
                    this.AddedUVs.Add(PMXQuaternion.LoadFromStreamStatic(br));
                }

                byte deformType = br.ReadByte();

                switch (deformType)
                {
                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF1:
                    this.Deform = new PMXVertexDeformBDEF1(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF2:
                    this.Deform = new PMXVertexDeformBDEF2(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_BDEF4:
                    this.Deform = new PMXVertexDeformBDEF4(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_SDEF:
                    this.Deform = new PMXVertexDeformSDEF(this.Model, this);
                    break;

                case PMXBaseDeform.DEFORM_IDENTIFY_QDEF:
                default:
                    this.Deform = new PMXVertexDeformQDEF(this.Model, this);
                    break;
                }
                this.Deform.LoadFromStream(br, importSettings);

                this.OutlineMagnification = br.ReadSingle();
            }
            else
            { //PMD format
                this.Deform = PMXVertexDeformBDEF2.DeformFromPMDFile(this.Model, this, br);

                this.OutlineMagnification = ((br.ReadByte() == 0) ? 1.0f : 0.0f);
            }
        }
コード例 #13
0
ファイル: PMXRigidBody.cs プロジェクト: haolink/PMXStructure
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int boneIndex;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            {
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
            }
            else
            {
                this.NameJP = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.NameEN = this.NameJP;

                boneIndex = br.ReadInt16();
            }

            if (boneIndex < 0)
            {
                this.Bone = null;
            }
            else
            {
                this.Bone = this.Model.Bones[boneIndex];
            }

            this.CollissionGroup = br.ReadByte();
            this.NoCollissionGroups.LoadFromStream(br, importSettings);

            this.Shape      = (BodyShape)(int)br.ReadByte();
            this._shapeSize = PMXVector3.LoadFromStreamStatic(br);

            this.Position = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation = PMXVector3.LoadFromStreamStatic(br);

            this.Mass           = br.ReadSingle();
            this.LinearDamping  = br.ReadSingle();
            this.AngularDamping = br.ReadSingle();
            this.Repulsion      = br.ReadSingle();
            this.Friction       = br.ReadSingle();

            this.Type = (BodyType)(int)br.ReadByte();

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMD && this.Bone != null)
            { //PMD location fix
                this.Position += this.Bone.Position;
            }
        }
コード例 #14
0
ファイル: PMXIKLink.cs プロジェクト: haolink/PMXStructure
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX format
                this.boneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

                this.HasLimits = (br.ReadByte() == 1);
                if (this.HasLimits)
                {
                    this.Minimum = PMXVector3.LoadFromStreamStatic(br);
                    this.Maximum = PMXVector3.LoadFromStreamStatic(br);
                }
            }
            else
            {
                this.boneIndex = br.ReadUInt16();
            }
        }
コード例 #15
0
ファイル: PMXRigidBody.cs プロジェクト: haolink/PMXStructure
        public override void WriteToStream(BinaryWriter bw, MMDExportSettings exportSettings)
        {
            if (exportSettings.Format == MMDExportSettings.ModelFormat.PMX)
            {
                PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameJP);
                PMXParser.WriteString(bw, exportSettings.TextEncoding, this.NameEN);

                PMXParser.WriteIndex(bw, exportSettings.BitSettings.BoneIndexLength, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true));
            }
            else
            {
                PMDParser.WriteString(bw, 20, exportSettings.TextEncoding, this.NameEN);

                PMXParser.WriteIndex(bw, 2, PMXBone.CheckIndexInModel(this.Bone, exportSettings, true));
            }

            bw.Write((byte)this.CollissionGroup);
            this.NoCollissionGroups.WriteToStream(bw, exportSettings);

            bw.Write((byte)(int)this.Shape);
            this._shapeSize.WriteToStream(bw);

            if (exportSettings.Format == MMDExportSettings.ModelFormat.PMD && this.Bone != null)
            { //PMD location fix
                PMXVector3 pos = new PMXVector3(this.Position.X, this.Position.Y, this.Position.Z);
                pos -= this.Bone.Position;
                pos.WriteToStream(bw);
            }
            else
            {
                this.Position.WriteToStream(bw);
            }

            this.Rotation.WriteToStream(bw);

            bw.Write(this.Mass);
            bw.Write(this.LinearDamping);
            bw.Write(this.AngularDamping);
            bw.Write(this.Repulsion);
            bw.Write(this.Friction);

            bw.Write((byte)(int)this.Type);
        }
コード例 #16
0
        public override void LoadFromStream(BinaryReader br, MMDImportSettings importSettings)
        {
            int rigidBodyIndexA, rigidBodyIndexB;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            {
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Type = (JointType)(int)br.ReadByte();

                rigidBodyIndexA = PMXParser.ReadIndex(br, importSettings.BitSettings.RigidBodyIndexLength);
                rigidBodyIndexB = PMXParser.ReadIndex(br, importSettings.BitSettings.RigidBodyIndexLength);
            }
            else
            {
                this.NameJP = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.NameEN = this.NameJP;

                this.Type = JointType.SpringSixDOF;

                rigidBodyIndexA = br.ReadInt32();
                rigidBodyIndexB = br.ReadInt32();
            }


            this.RigidBodyA = this.Model.RigidBodies[rigidBodyIndexA];
            this.RigidBodyB = this.Model.RigidBodies[rigidBodyIndexB];

            this.Position                  = PMXVector3.LoadFromStreamStatic(br);
            this.Rotation                  = PMXVector3.LoadFromStreamStatic(br);
            this.TranslationLimitMin       = PMXVector3.LoadFromStreamStatic(br);
            this.TranslationLimitMax       = PMXVector3.LoadFromStreamStatic(br);
            this.RotationLimitMin          = PMXVector3.LoadFromStreamStatic(br);
            this.RotationLimitMax          = PMXVector3.LoadFromStreamStatic(br);
            this.SpringConstantTranslation = PMXVector3.LoadFromStreamStatic(br);
            this.SpringConstantRotation    = PMXVector3.LoadFromStreamStatic(br);
        }
コード例 #17
0
ファイル: PMXIKLink.cs プロジェクト: haolink/PMXStructure
 public PMXIKLink(PMXModel model, PMXIK ik) : base(model)
 {
     this.IK      = ik;
     this.Minimum = new PMXVector3();
     this.Maximum = new PMXVector3();
 }
コード例 #18
0
        public PMXMorphOffsetVertex(PMXModel model, PMXMorph morph) : base(model, morph)
        {
            this.MorphTargetType = PMXMorph.MORPH_IDENTIFY_VERTEX;

            this.Translation = new PMXVector3();
        }
コード例 #19
0
 public float DotProduct(PMXVector3 b)
 {
     return(this.X * b.X + this.Y * b.Y + this.Z * b.Z);
 }
コード例 #20
0
ファイル: PMXBone.cs プロジェクト: haolink/PMXStructure
        public void LoadFromStream(BinaryReader br, MMDImportSettings importSettings, out int pmdIKIndex)
        {
            pmdIKIndex = -1;

            if (importSettings.Format == MMDImportSettings.ModelFormat.PMX)
            { //PMX
                this.NameJP = PMXParser.ReadString(br, importSettings.TextEncoding);
                this.NameEN = PMXParser.ReadString(br, importSettings.TextEncoding);

                this.Position = PMXVector3.LoadFromStreamStatic(br);

                this.parentIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);

                this.Layer = br.ReadInt32();

                short flags = br.ReadInt16();

                this.HasChildBone = ((flags & PMXBone.BONE_TAILPOS_IS_BONE) != 0);
                if (this.HasChildBone)
                {
                    this.childBoneIndex = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                }
                else
                {
                    this.ChildVector = PMXVector3.LoadFromStreamStatic(br);
                }

                this.Rotatable    = ((flags & PMXBone.BONE_CAN_ROTATE) != 0);
                this.Translatable = ((flags & PMXBone.BONE_CAN_TRANSLATE) != 0);
                this.Visible      = ((flags & PMXBone.BONE_IS_VISIBLE) != 0);
                this.Operating    = ((flags & PMXBone.BONE_CAN_MANIPULATE) != 0);

                bool extRotation    = ((flags & PMXBone.BONE_IS_EXTERNAL_ROTATION) != 0);
                bool extTranslation = ((flags & PMXBone.BONE_IS_EXTERNAL_TRANSLATION) != 0);

                int rotFlag = 0;
                if (extRotation)
                {
                    rotFlag |= 1;
                }
                if (extTranslation)
                {
                    rotFlag |= 2;
                }
                this.ExternalModificationType = (BoneExternalModificationType)rotFlag;
                if (this.ExternalModificationType != BoneExternalModificationType.None)
                {
                    this.externalBoneIndex  = PMXParser.ReadIndex(br, importSettings.BitSettings.BoneIndexLength);
                    this.ExternalBoneEffect = br.ReadSingle();
                }

                this.FixedAxis = ((flags & PMXBone.BONE_HAS_FIXED_AXIS) != 0);
                if (this.FixedAxis)
                {
                    this.AxisLimit = PMXVector3.LoadFromStreamStatic(br);
                }

                this.LocalCoordinates = ((flags & PMXBone.BONE_HAS_LOCAL_COORDINATE) != 0);
                if (this.LocalCoordinates)
                {
                    this.LocalCoordinatesX = PMXVector3.LoadFromStreamStatic(br);
                    this.LocalCoordinatesZ = PMXVector3.LoadFromStreamStatic(br);
                }

                this.HasExternalParent = ((flags & PMXBone.BONE_IS_EXTERNAL_PARENT_DEFORM) != 0);
                if (this.HasExternalParent)
                {
                    this.ExternalParentKey = br.ReadInt32();
                }

                this.TransformPhysicsFirst = ((flags & PMXBone.BONE_IS_AFTER_PHYSICS_DEFORM) != 0);

                bool isIKBone = ((flags & PMXBone.BONE_IS_IK) != 0);
                if (isIKBone)
                {
                    PMXIK ikData = new PMXIK(this.Model, this);
                    ikData.LoadFromStream(br, importSettings);
                    this.IK = ikData;
                }
                else
                {
                    this.IK = null;
                }
            }

            else
            { //PMD
                this.NameJP      = PMDParser.ReadString(br, 20, importSettings.TextEncoding);
                this.parentIndex = br.ReadInt16();

                this.HasChildBone   = true;
                this.childBoneIndex = br.ReadUInt16();

                byte type = br.ReadByte();

                ushort ikIndex = br.ReadUInt16();

                this.Position = PMXVector3.LoadFromStreamStatic(br);

                switch (type)
                {
                case PMD_BONE_TYPE_ROTATE:
                    //Default
                    break;

                case PMD_BONE_TYPE_ROTATE_MOVE:
                    this.Translatable = true;
                    break;

                case PMD_BONE_TYPE_IK:
                    //IK parameters will be initialised later
                    this.Translatable = true;
                    break;

                case PMD_BONE_TYPE_IK_CHILD:
                    //PMX doesn't even bother about these
                    break;

                case PMD_BONE_TYPE_EXTERNAL_ROTATOR:
                    this.ExternalModificationType = BoneExternalModificationType.Rotation;
                    this.externalBoneIndex        = (int)ikIndex;
                    break;

                case PMD_BONE_TYPE_IK_TARGET:
                    //PMX doesn't bother either
                    this.Visible = false;
                    break;

                case PMD_BONE_TYPE_INVISIBLE:
                    this.Visible = false;
                    break;

                case PMD_BONE_TYPE_TWIST:
                    //PMX handles these differently
                    this._isPMDTwist = true;
                    break;

                case PMD_BONE_TYPE_TWIST_INVISIBLE:
                    //PMX handles these differently
                    this._isPMDTwist = true;
                    this.Visible     = false;
                    break;
                }
            }
        }