static int GetJointIndexFromHumanBone(AnimationRig rig, string humanBoneName) { var avatar = rig.Avatar; for (int i = 0; i < avatar.humanDescription.human.Length; ++i) { ref HumanBone humanBone = ref avatar.humanDescription.human[i]; if (humanBone.humanName == humanBoneName) { int jointIndex = rig.GetJointIndexFromName(humanBone.boneName); if (jointIndex < 0) { throw new Exception($"Avatar {avatar.name} joint {humanBone.boneName} not found"); } return(jointIndex); } }
/// <summary> /// /// </summary> /// <param name="scene"></param> public Door(Scene scene) { rig = new AnimationRig(); Children.Add(rig); OBJ_File obj = new OBJ_File(); Name = "Door"; obj.Load("C:\\Users\\Michael\\Documents\\XNA\\Hunt-Or-Gatherers\\Models\\Objects\\door.obj", Engine.g, new Vector3(0, 0, 0), Vector3.Zero, 1f, false, false, rig); rig.Children[0].Center = -rig.Children[0].Position; rig.Children.Add(new Transform() { Position = rig.Children[0].Position }); rig.Children.Add(new Transform() { Position = rig.Children[0].Position, YawPitchRoll = new Vector3(MathHelper.ToRadians(90), 0, 0) } ); rig.Update_Rig = false; }
public HumanoidAnimationRetargeter(AnimationRig sourceRig, AnimationRig targetRig) : base(sourceRig) { #if USE_HUMAN_POSE_HANDLER_RETARGETING var sourceAvatar = sourceRig.Avatar; var targetAvatar = targetRig.Avatar; sourceHumanPoseHandler = new HumanPoseHandler(sourceRig.Avatar, sourceRig.JointPaths); targetHumanPoseHandler = new HumanPoseHandler(targetRig.Avatar, targetRig.JointPaths); this.targetRig = targetRig; sourceToTargetScale = GetAvatarHumanScale(targetAvatar) / GetAvatarHumanScale(sourceAvatar); pelvisOffset = GetAvatarHumanScale(targetAvatar) * 0.05f; // lower pelvis of 5 percent to avoid overextension sourceAvatarPose = new NativeArray <AffineTransform>(sourceRig.NumJoints, Allocator.Persistent); targetAvatarPose = new NativeArray <AffineTransform>(targetRig.NumJoints, Allocator.Persistent); humanPose = new HumanPose(); try { // Compute "half stretched" pose where all humanoid joints are folded at 90 degrees angle (corresponding to the muscle value being 0) // and store it into targetAvatarPose. Those joints at 90 degrees will serve at starting mid joint rotations for IK in order to ensure stability int affineTransformSize = UnsafeUtility.SizeOf <AffineTransform>(); targetHumanPoseHandler.GetInternalHumanPose(ref humanPose); for (int i = 0; i < humanPose.muscles.Length; ++i) { humanPose.muscles[i] = 0.0f; } targetHumanPoseHandler.SetInternalHumanPose(ref humanPose); targetHumanPoseHandler.GetInternalAvatarPose(targetAvatarPose.Reinterpret <float>(affineTransformSize)); CreateLimbPairIK("UpperLeg", "LowerLeg", "Foot", out feetIK); CreateLimbPairIK("UpperArm", "LowerArm", "Hand", out handsIK); enableIK = true; } catch (Exception e) { Debug.LogError(e); enableIK = false; } sourceToTargetJointIndices = new NativeArray <int>(sourceRig.NumJoints, Allocator.Persistent); for (int i = 0; i < sourceRig.NumJoints; ++i) { sourceToTargetJointIndices[i] = -1; } for (int i = 0; i < sourceAvatar.humanDescription.human.Length; ++i) { int sourceJointIndex = sourceRig.GetJointIndexFromName(sourceAvatar.humanDescription.human[i].boneName); if (sourceJointIndex >= 0) { int targetJointIndex = -1; try { targetJointIndex = GetJointIndexFromHumanBone(targetRig, sourceAvatar.humanDescription.human[i].humanName); } catch (Exception) {} sourceToTargetJointIndices[sourceJointIndex] = targetJointIndex; } } #endif }