Skip to content

Commit

Permalink
arbitrary initial avatar rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
JLPM22 committed Jan 30, 2024
1 parent cfc2bd3 commit 1dbd0f9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace MotionMatching
public class MotionMatchingSkinnedMeshRenderer : MonoBehaviour
{
public MotionMatchingController MotionMatching;
[Tooltip("Local vector (axis) pointing in the forward direction of the character")] public Vector3 ForwardLocalVector = new Vector3(0, 0, 1);

private Animator Animator;

Expand Down Expand Up @@ -67,27 +66,58 @@ private void InitRetargeting()
SourceTPose[i] = tposeAnimation.GetWorldRotation(joint, 0);
}
}
// Target
Quaternion avatarRot = Animator.transform.rotation;
SkeletonBone[] targetSkeletonBones = Animator.avatar.humanDescription.skeleton;
Quaternion hipsRot = avatarRot;
for (int i = 0; i < BodyJoints.Length; i++)
{
Transform targetJoint = Animator.GetBoneTransform(BodyJoints[i]);

// Use Array.FindIndex to find the index of the joint in the targetSkeletonBones array
int targetJointIndex = Array.FindIndex(targetSkeletonBones, bone => bone.name == targetJoint.name);
Debug.Assert(targetJointIndex != -1, "Target joint not found: " + targetJoint.name);

// Initialize the rotation as the local rotation of the joint
Quaternion cumulativeRotation = targetSkeletonBones[targetJointIndex].rotation;

// Traverse up the hierarchy until reaching the Animator's transform
Transform currentTransform = targetJoint.parent;
while (currentTransform != null && currentTransform != Animator.transform)
{
int parentIndex = Array.FindIndex(targetSkeletonBones, bone => bone.name == currentTransform.name);
if (parentIndex != -1)
{
// Multiply with the parent's local rotation
cumulativeRotation = targetSkeletonBones[parentIndex].rotation * cumulativeRotation;
}
Debug.Assert(parentIndex != -1, "Parent joint not found: " + currentTransform.name);

// Move to the next parent in the hierarchy
currentTransform = currentTransform.parent;
}

// Store the world rotation
TargetTPose[i] = cumulativeRotation;
if (BodyJoints[i] == HumanBodyBones.Hips)
{
hipsRot = math.mul(avatarRot, cumulativeRotation);
}
}
// Correct rotations so they are facing the same direction as the target
// Correct Source
float3 currentDirection = math.mul(SourceTPose[0], mmData.HipsForwardLocalVector);
currentDirection.y = 0;
currentDirection = math.normalize(currentDirection);
float3 targetDirection = transform.TransformDirection(ForwardLocalVector);
float3 forwardLocalVector = math.mul(math.inverse(hipsRot), math.forward());
float3 targetDirection = transform.TransformDirection(forwardLocalVector);
targetDirection.y = 0;
targetDirection = math.normalize(targetDirection);
quaternion correctionRot = MathExtensions.FromToRotation(currentDirection, targetDirection, new float3(0, 1, 0));
for (int i = 0; i < BodyJoints.Length; i++)
{
SourceTPose[i] = math.mul(correctionRot, SourceTPose[i]);
}
// Target
Quaternion rot = Animator.transform.rotation;
Animator.transform.rotation = Quaternion.identity;
for (int i = 0; i < BodyJoints.Length; i++)
{
TargetTPose[i] = Animator.GetBoneTransform(BodyJoints[i]).rotation;
}
Animator.transform.rotation = rot;
// Store Transforms
Transform[] mmBones = MotionMatching.GetSkeletonTransforms();
Dictionary<string, Transform> boneDict = new Dictionary<string, Transform>();
Expand Down Expand Up @@ -195,13 +225,5 @@ private void CorrectToes(Transform toesT, Transform footT)
HumanBodyBones.RightFoot,
HumanBodyBones.RightToes
};

private void OnValidate()
{
if (math.abs(math.length(ForwardLocalVector)) < 1E-3f)
{
Debug.LogWarning("ForwardLocalVector is too close to zero. Object: " + name);
}
}
}
}
6 changes: 3 additions & 3 deletions MMVR/Assets/Scenes/Demo.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1598,15 +1598,15 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 1795449175783500850, guid: dd187f127561b6e4eaa21baf55b76917, type: 3}
propertyPath: m_LocalRotation.x
value: -0
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1795449175783500850, guid: dd187f127561b6e4eaa21baf55b76917, type: 3}
propertyPath: m_LocalRotation.y
value: -0
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1795449175783500850, guid: dd187f127561b6e4eaa21baf55b76917, type: 3}
propertyPath: m_LocalRotation.z
value: -0
value: 0
objectReference: {fileID: 0}
- target: {fileID: 1795449175783500850, guid: dd187f127561b6e4eaa21baf55b76917, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
Expand Down

0 comments on commit 1dbd0f9

Please sign in to comment.