From 2d7f92e5a07292d6c24050f2948806d01f14d1c4 Mon Sep 17 00:00:00 2001 From: spatialfree Date: Sun, 8 Dec 2024 01:51:09 -0500 Subject: [PATCH] initial hand tracking support --- Platforms/Android/AndroidManifest.xml | 4 +- src/Rig.cs | 66 +++++++++++++++++---------- 2 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Platforms/Android/AndroidManifest.xml b/Platforms/Android/AndroidManifest.xml index 6dc3efa..7f3ff1d 100644 --- a/Platforms/Android/AndroidManifest.xml +++ b/Platforms/Android/AndroidManifest.xml @@ -2,8 +2,8 @@ diff --git a/src/Rig.cs b/src/Rig.cs index 155174e..efac1e9 100644 --- a/src/Rig.cs +++ b/src/Rig.cs @@ -40,33 +40,51 @@ static class Rig } else { - // Hand r_hand = Input.Hand(Handed.Right); - // [!] hand input simulates controller... - Controller r_con = Input.Controller(Handed.Right); - - bool con_tracked = r_con.trackedPos > TrackState.Lost; - // bool hand_tracked = Input.HandSource(Handed.Right) > HandSource.None; - if (con_tracked) + if (Input.HandSource(Handed.Right) == HandSource.Articulated) { - btn_select.Step(r_con.x1.IsActive() || r_con.trigger > 0.5f); - btn_grip.Step(r_con.grip > 0.5f); - btn_back.Step(r_con.x2.IsActive()); + Hand r_hnd = Input.Hand(Handed.Right); + if (r_hnd.IsTracked) + { + btn_select.Step(r_hnd.IsPinched); + btn_grip.Step(r_hnd.IsGripped); + // btn_back.Step(); // [!] todo - Vec2 stick = r_con.stick; - Quat stick_rot = Quat.FromAngles(stick.y * -90, 0, stick.x * +90); - float stick_sign = r_con.IsStickClicked ? -1 : +1; - r_con_stick = r_con.aim; - // r_con_stick.position += r_con_stick.orientation * V.XYZ(0.0065f, -0.012f, -0.05f); - // r_con_stick.orientation *= Quat.FromAngles(-50, 0, 0); - fullstick = r_con_stick.orientation * stick_rot * Vec3.Up * stick_sign; + Vec3 thumb_tip = r_hnd.Get(FingerId.Thumb, JointId.Tip).position; + Vec3 index_tip = r_hnd.Get(FingerId.Index, JointId.Tip).position; + r_con_stick.position = thumb_tip; - // Vec3 fullstick = r_hand.palm.orientation * Vec3.Up; - float ax = Maths.abs(fullstick.x); - float ay = Maths.abs(fullstick.y); - float az = Maths.abs(fullstick.z); - if (ax > ay && ax > az) new_dir = new(Maths.sign(fullstick.x), 0, 0); - if (ay > ax && ay > az) new_dir = new(0, Maths.sign(fullstick.y), 0); - if (az > ax && az > ay) new_dir = new(0, 0, Maths.sign(fullstick.z)); + fullstick = Vec3.Direction(index_tip, thumb_tip); + + float ax = Maths.abs(fullstick.x); + float ay = Maths.abs(fullstick.y); + float az = Maths.abs(fullstick.z); + if (ax > ay && ax > az) new_dir = new(Maths.sign(fullstick.x), 0, 0); + if (ay > ax && ay > az) new_dir = new(0, Maths.sign(fullstick.y), 0); + if (az > ax && az > ay) new_dir = new(0, 0, Maths.sign(fullstick.z)); + } + } + else + { + Controller r_con = Input.Controller(Handed.Right); + if (r_con.IsTracked) + { + btn_select.Step(r_con.x1.IsActive() || r_con.trigger > 0.5f); + btn_grip.Step(r_con.grip > 0.5f); + btn_back.Step(r_con.x2.IsActive()); + + Vec2 stick = r_con.stick; + Quat stick_rot = Quat.FromAngles(stick.y * -90, 0, stick.x * +90); + float stick_sign = r_con.IsStickClicked ? -1 : +1; + r_con_stick = r_con.aim; + fullstick = r_con_stick.orientation * stick_rot * Vec3.Up * stick_sign; + + float ax = Maths.abs(fullstick.x); + float ay = Maths.abs(fullstick.y); + float az = Maths.abs(fullstick.z); + if (ax > ay && ax > az) new_dir = new(Maths.sign(fullstick.x), 0, 0); + if (ay > ax && ay > az) new_dir = new(0, Maths.sign(fullstick.y), 0); + if (az > ax && az > ay) new_dir = new(0, 0, Maths.sign(fullstick.z)); + } } } }