-
Notifications
You must be signed in to change notification settings - Fork 171
Experimental: Meta Compatibility #1680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3befe3
WIP
rblenkinsopp 823f907
Initial implementation of a MetaCompatibilityProvider
rblenkinsopp b50102e
Move Meta compatibility to the OpenXR provider
rblenkinsopp ab5580e
Update changelog
rblenkinsopp 084a269
Update MetaCompatibility.cs
MaxPalmer-UH 67de727
Moved location of header attribute to avoid compile error
MaxPalmer-UH 047396f
Merge branch 'develop' into rcb/meta-compatibility
MaxPalmer-UH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Packages/Tracking/OpenXR/Runtime/Scripts/MetaCompatibility.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using Leap; | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace Leap.Tracking.OpenXR | ||
{ | ||
public static class MetaCompatibility | ||
{ | ||
private enum ConversionType { MetaToLeap, LeapToMeta }; | ||
|
||
// These constants encode the Leap -> Meta scaling / offsets. | ||
// They have been dervied from reviewing the differences in the joint positions and rotations between LeapC and Meta OpenXR data when viewing hands in the same pose | ||
private static readonly float[] MetacarpalOutRotations = { 0.0f, 0.0f, 0.0f, 6.0f, 4.0f }; | ||
private static readonly float[] MetacarpalDownRotations = { 0.0f, 12.0f, 14.0f, 12.0f, 10.0f }; | ||
|
||
public static Hand ToMetaLayout(this Hand hand) => hand.ConvertHand(ConversionType.LeapToMeta); | ||
|
||
public static Hand FromMetaLayout(this Hand hand) => hand.ConvertHand(ConversionType.MetaToLeap); | ||
|
||
private static Hand ConvertHand(this Hand hand, ConversionType type) | ||
{ | ||
// Scale and re-orientate all the metacarpal joints from Meta's layout. | ||
foreach (var finger in hand.fingers) | ||
{ | ||
var proximal = finger.Proximal; | ||
var metacarpal = finger.Metacarpal; | ||
|
||
// Thumb is adjusted differently. | ||
if (finger.Type == Finger.FingerType.THUMB) | ||
{ | ||
var thumbUp = metacarpal.Rotation * Vector3.up; | ||
var thumbAdjustment = (type == ConversionType.LeapToMeta ? 1.0f : -1.0f) * | ||
new Vector3(hand.IsLeft ? 0.005f : -0.005f, 0f, 0.012f); | ||
|
||
// Adjust Metacarpal | ||
metacarpal.PrevJoint += thumbAdjustment; | ||
metacarpal.NextJoint += thumbAdjustment; | ||
metacarpal.Center += thumbAdjustment; | ||
|
||
// Adjust proximal (requires recalculating direction properties). | ||
proximal.PrevJoint += thumbAdjustment; | ||
proximal.Center = Vector3.Lerp(proximal.PrevJoint, proximal.NextJoint, 0.5f); | ||
proximal.Direction = (proximal.NextJoint - proximal.PrevJoint).normalized; | ||
proximal.Rotation = Quaternion.LookRotation(proximal.Direction, thumbUp); | ||
} | ||
else | ||
{ | ||
// Recalculate the required rotation and adjust the root bone position. | ||
metacarpal.Rotation *= Quaternion.Euler( | ||
(type == ConversionType.LeapToMeta ? -1.0f : 1.0f) * MetacarpalDownRotations[(int)finger.Type], | ||
(type == ConversionType.LeapToMeta ? 1.0f : -1.0f) * (hand.IsLeft | ||
? MetacarpalOutRotations[(int)finger.Type] | ||
: -MetacarpalOutRotations[(int)finger.Type]), | ||
0); | ||
var adjustedReverseDirection = metacarpal.Rotation * Vector3.back; | ||
metacarpal.PrevJoint = metacarpal.NextJoint + (adjustedReverseDirection * metacarpal.Length); | ||
|
||
// Recalculate the other properties to be consistent. | ||
metacarpal.Direction = metacarpal.Rotation * Vector3.forward; | ||
metacarpal.Center = Vector3.Lerp(metacarpal.PrevJoint, metacarpal.NextJoint, 0.5f); | ||
} | ||
} | ||
|
||
// Adjust the wrist and the arm joints. | ||
hand.WristPosition += hand.Direction * (type == ConversionType.LeapToMeta ? -0.005f : 0.005f); | ||
var arm = hand.Arm; | ||
var armUp = arm.Rotation * Vector3.up; | ||
arm.NextJoint = hand.WristPosition; | ||
arm.Center = Vector3.Lerp(arm.PrevJoint, arm.NextJoint, 0.5f); | ||
arm.Direction = (arm.NextJoint - arm.PrevJoint).normalized; | ||
arm.Rotation = Quaternion.LookRotation(arm.Direction, armUp); | ||
|
||
return hand; | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
Packages/Tracking/OpenXR/Runtime/Scripts/MetaCompatibility.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.