Esempio n. 1
0
        public Armature GetRestArmature()
        {
            var armature  = new Armature();
            var necessary = Enumerable.Repeat(false, bones.Count).ToArray();

            foreach (var ikChain in ikChains)
            {
                foreach (var index in ikChain.AffectedBoneIndices)
                {
                    necessary[index] = true;
                }
                necessary[ikChain.EndEffectorIndex] = true;
            }

            foreach (var vertex in vertices)
            {
                necessary[vertex.Bone0Number] = true;
                necessary[vertex.Bone1Number] = true;
            }

            for (int boneIndex = 0; boneIndex < bones.Count; boneIndex++)
            {
                var bone = bones[boneIndex];

                if (necessary[boneIndex] ||
                    (new int[] {
                    Bone.BONE_ROTATE,
                    Bone.BONE_ROTATE_TRANSLATE,
                    Bone.BONE_IK_ROTATION_INFLUENCED,
                    Bone.BONE_IK_TARGET,
                    Bone.BONE_ROTATION_INFLUENCED
                }.Contains(bone.BoneType)))
                {
                    var current = boneIndex;
                    while (current >= 0)
                    {
                        necessary[current] = true;
                        current            = bones[current].ParentIndex;
                    }
                }
            }

            for (int boneIndex = 0; boneIndex < bones.Count; boneIndex++)
            {
                var bone = bones[boneIndex];
                if (necessary[boneIndex])
                {
                    Vector3D position;
                    if (bone.ParentIndex >= 0)
                    {
                        var parent = bones[bone.ParentIndex];
                        position = bone.Position - parent.Position;
                        position = new Vector3D(position.X, position.Y, -position.Z);
                    }
                    else
                    {
                        position = new Vector3D(bone.Position.X, bone.Position.Y, -bone.Position.Z);
                    }
                    var joint = new Joint(bone.Name, position, Quaternion.Identity);
                    armature.AppendJoint(joint);
                }
            }

            for (int boneIndex = 0; boneIndex < bones.Count; boneIndex++)
            {
                var   bone = bones[boneIndex];
                Joint joint;
                try
                {
                    joint = armature.GetJoint(bone.Name);
                }
                catch (ValueException)
                {
                    continue;
                }
                if (bone.ParentIndex >= 0)
                {
                    armature.SetParent(joint.Name, bones[bone.ParentIndex].Name);
                }
            }

            return(armature);
        }
Esempio n. 2
0
        private Tuple <IKArmature, string, string> GetIkChainIkArmature(int ik_chain_index)
        {
            var ik_chain      = ikChains[ik_chain_index];
            var rest_armature = new Armature();

            var joint_indices = ik_chain.AffectedBoneIndices.ToList();

            joint_indices.Add(ik_chain.EndEffectorIndex);

            foreach (var joint_index in joint_indices)
            {
                rest_armature.AppendJoint(new Joint(bones[joint_index].Name));
            }
            string root_name = null;

            foreach (var joint_index in joint_indices)
            {
                var bone = bones[joint_index];
                if (joint_indices.Contains(bone.ParentIndex))
                {
                    var child_name   = bones[joint_index].Name;
                    var parent_index = bones[joint_index].ParentIndex;
                    var parent_name  = bones[parent_index].Name;
                    rest_armature.SetParent(child_name, parent_name);
                    var joint = rest_armature.GetJoint(child_name);
                    var m     = Matrix3D.Identity;
                    m.Scale(new Vector3D(1, 1, -1));
                    var position = m.Transform(bones[joint_index].Position - bones[parent_index].Position);
                    joint.Position = position;
                }
                else
                {
                    root_name = bone.Name;
                }
            }

            var ik_bone_name      = bones[ik_chain.IkBoneIndex].Name;
            var ik_armature       = new IKArmature(rest_armature);
            var end_effector_name = bones[ik_chain.EndEffectorIndex].Name;

            ik_armature.MakeEndEffector(end_effector_name);
            foreach (var joint_index in ik_chain.AffectedBoneIndices)
            {
                var bone     = bones[joint_index];
                var ik_joint = ik_armature.MakeIkJoint(bone.Name);
                ik_armature.AddIkJointToEndEffector(bone.Name, end_effector_name);
                ik_joint.DisableParameter(IKJointParameters.XTranslate);
                ik_joint.DisableParameter(IKJointParameters.YTranslate);
                ik_joint.DisableParameter(IKJointParameters.ZTranslate);
            }

            if (new[] { "leg_ik.R", "leg_ik.L" }.Contains(ik_bone_name))
            {
                IKJoint knee_joint;
                IKJoint leg_joint;

                if (ik_bone_name == "leg_ik.R")
                {
                    knee_joint = ik_armature.GetIkJoint("knee.R");
                    leg_joint  = ik_armature.GetIkJoint("leg.R");
                }
                else
                {
                    leg_joint  = ik_armature.GetIkJoint("leg.L");
                    knee_joint = ik_armature.GetIkJoint("knee.L");
                }

                knee_joint.DisableParameter(IKJointParameters.YRotate);
                knee_joint.DisableParameter(IKJointParameters.ZRotate);
                knee_joint.SetLimit(IKJointParameters.XRotate, 0, 180);

                leg_joint.DisableParameter(IKJointParameters.YRotate);
                leg_joint.DisableParameter(IKJointParameters.ZRotate);
            }

            return(Tuple.Create(ik_armature, root_name, end_effector_name));
        }