internal IKArmature GetIkArmature() { var restArmature = GetRestArmature(); var ikArmature = new IKArmature(restArmature); var getCorrespondingRestJoint = new Func <int, Joint>((jointIndex) => { var bone = bones[jointIndex]; var restJoint = restArmature.GetJoint(bone.Name); return(restJoint); }); var ikJointIndexSet = new HashSet <int>(); foreach (var ikChain in ikChains) { var endEffectorJoint = getCorrespondingRestJoint(ikChain.EndEffectorIndex); var endEffector = ikArmature.MakeEndEffector(endEffectorJoint.Index); foreach (var ikBoneIndex in ikChain.AffectedBoneIndices) { var ikRestJoint = getCorrespondingRestJoint(ikBoneIndex); var ikJointIndex = ikRestJoint.Index; endEffector.AppendIkJointIndex(ikRestJoint.Index); var joint = restArmature.GetJoint(ikRestJoint.Index); var jointName = joint.Name; ikJointIndexSet.Add(ikJointIndex); } } foreach (var ikJointIndex in ikJointIndexSet) { ikArmature.MakeIkJoint(ikJointIndex); } return(ikArmature); }
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)); }