Skip to content

Commit 8bc9397

Browse files
Andrew Kimfacebook-github-bot
authored andcommitted
fix(Editor): Update spine adjustments calculation to affect only one axis
Summary: Depending on how a model is rigged, it was possible for the automatically calculated spine adjustments to affect more than one axis. However, only one axis should be affected for adjustments to look correct visually - this fixes that. Reviewed By: sohailshafiiWk Differential Revision: D56252868 fbshipit-source-id: 601ef90a302a31c475b3f513d8a26005fbb760b1
1 parent 7d530fa commit 8bc9397

File tree

2 files changed

+51
-16
lines changed

2 files changed

+51
-16
lines changed

Runtime/Scripts/AnimationRigging/DeformationUtilities.cs

Lines changed: 50 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
using System;
44
using System.Collections;
55
using System.Collections.Generic;
6+
using Oculus.Interaction.Input;
67
using Oculus.Movement.Utils;
78
using UnityEngine;
9+
using UnityEngine.Animations;
810
using UnityEngine.Animations.Rigging;
911
using static OVRUnityHumanoidSkeletonRetargeter;
1012

@@ -393,21 +395,63 @@ public static BoneAdjustmentData[] GetDeformationBoneAdjustments(Animator animat
393395
// Calculate an adjustment alignment if needed, using the desired right and forward from the
394396
// rest pose humanoid, which is Vector3.right and Vector3.forward.
395397
var adjustmentAlignment =
396-
GetHipsRightForwardAlignmentForAdjustments(animator, Vector3.right, Vector3.forward);
398+
GetHipsAlignmentForAdjustments(animator, Vector3.right, Vector3.forward);
399+
400+
// Flatten out the spine adjustments, as the adjustment should only be along one axis by
401+
// previewing the hips adjustment, and taking the largest value.
402+
var adjustmentAxis = GetHipsAdjustmentAxis(adjustmentAlignment,
403+
boneAdjustments[0].Adjustment.eulerAngles);
404+
397405
for (int i = 0; i < boneAdjustments.Count; i++)
398406
{
399407
var adjustment = boneAdjustments[i];
400408
// We use euler angles here as we want to rotate the adjustment point with the alignment rotation,
401409
// rather than combine the rotations.
402410
var adjustmentPoint = adjustment.Adjustment.eulerAngles;
403-
adjustment.Adjustment =
404-
Quaternion.Euler(adjustmentAlignment * adjustmentPoint);
411+
var adjustmentResult = adjustmentAlignment * adjustmentPoint;
412+
switch (adjustmentAxis)
413+
{
414+
case Axis.X:
415+
adjustmentResult = Vector3.right * adjustmentResult.x;
416+
break;
417+
case Axis.Y:
418+
adjustmentResult = Vector3.up * adjustmentResult.y;
419+
break;
420+
case Axis.Z:
421+
adjustmentResult = Vector3.forward * adjustmentResult.z;
422+
break;
423+
}
424+
adjustment.Adjustment = Quaternion.Euler(adjustmentResult);
405425
boneAdjustments[i] = adjustment;
406426
}
427+
407428
boneAdjustments.AddRange(shoulderBoneAdjustments);
408429
return boneAdjustments.ToArray();
409430
}
410431

432+
private static Axis GetHipsAdjustmentAxis(Quaternion adjustmentAlignment, Vector3 hipsAdjustmentEulerAngles)
433+
{
434+
var hipsPreviewAdjustmentEuler =
435+
Quaternion.Euler(adjustmentAlignment * hipsAdjustmentEulerAngles).eulerAngles;
436+
var worldAdjustmentX = Mathf.Abs(hipsPreviewAdjustmentEuler.x > 180 ?
437+
hipsPreviewAdjustmentEuler.x - 360 : hipsPreviewAdjustmentEuler.x);
438+
var worldAdjustmentY = Mathf.Abs(hipsPreviewAdjustmentEuler.y > 180 ?
439+
hipsPreviewAdjustmentEuler.y - 360 : hipsPreviewAdjustmentEuler.y);
440+
var worldAdjustmentZ = Mathf.Abs(hipsPreviewAdjustmentEuler.z > 180 ?
441+
hipsPreviewAdjustmentEuler.z - 360 : hipsPreviewAdjustmentEuler.z);
442+
var largestWorldAdjustment =
443+
Mathf.Max(worldAdjustmentX, Mathf.Max(worldAdjustmentY, worldAdjustmentZ));
444+
if (Mathf.Approximately(largestWorldAdjustment, worldAdjustmentZ))
445+
{
446+
return Axis.Z;
447+
}
448+
if (Mathf.Approximately(largestWorldAdjustment, worldAdjustmentY))
449+
{
450+
return Axis.Y;
451+
}
452+
return Axis.X;
453+
}
454+
411455
/// <summary>
412456
/// Returns joint adjustments required for retargeting.
413457
/// </summary>
@@ -419,15 +463,7 @@ public static JointAdjustment[] GetJointAdjustments(Animator animator, RestPoseO
419463
FullBodyDeformationConstraint constraint = null)
420464
{
421465
var adjustments = new List<JointAdjustment>();
422-
BoneAdjustmentData[] boneAdjustmentData;
423-
if (constraint != null)
424-
{
425-
boneAdjustmentData = (constraint.data as IFullBodyDeformationData).BoneAdjustments;
426-
}
427-
else
428-
{
429-
boneAdjustmentData = GetDeformationBoneAdjustments(animator, restPoseObject);
430-
}
466+
var boneAdjustmentData = GetDeformationBoneAdjustments(animator, restPoseObject);
431467
foreach (var boneAdjustment in boneAdjustmentData)
432468
{
433469
var rotationTweak = boneAdjustment.Adjustment;
@@ -484,7 +520,7 @@ private static JointAdjustment[] GetFeetAdjustments(Animator animator, RestPoseO
484520
var legDotProduct = Vector3.Dot(leftLeg.forward, rightLeg.forward);
485521
var shouldMirrorLegs = legDotProduct < 0.0f;
486522
var adjustmentAlignment =
487-
GetHipsRightForwardAlignmentForAdjustments(animator, Vector3.right, Vector3.forward);
523+
GetHipsAlignmentForAdjustments(animator, Vector3.right, Vector3.forward);
488524

489525
var leftFootAdjustment =
490526
GetFootJointAdjustment(animator, restPoseObject, HumanBodyBones.LeftFoot, HumanBodyBones.LeftToes,
@@ -556,7 +592,7 @@ private static HumanBodyBones GetValidShoulderParentBone(Animator animator)
556592
/// <param name="alignmentRightDirection">The alignment right direction.</param>
557593
/// <param name="alignmentForwardDirection">The alignment forward direction.</param>
558594
/// <returns>Rotation to align the hips right with the alignment right direction.</returns>
559-
private static Quaternion GetHipsRightForwardAlignmentForAdjustments(Animator animator,
595+
private static Quaternion GetHipsAlignmentForAdjustments(Animator animator,
560596
Vector3 alignmentRightDirection,
561597
Vector3 alignmentForwardDirection)
562598
{

Samples/RetargetingProcessors/ArmatureCorrectBones.asset

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ MonoBehaviour:
1515
Weight: 1
1616
_correctPositionsLateUpdate: 1
1717
_shoulderCorrectionWeightLateUpdate: 0
18-
_leftHandCorrectionWeightLateUpdate: 1
19-
_rightHandCorrectionWeightLateUpdate: 1
18+
_fingerPositionCorrectionWeight: 0

0 commit comments

Comments
 (0)