Skip to content

Commit e080691

Browse files
author
Chris Handzlik
committed
initial commit
0 parents  commit e080691

21 files changed

+1810
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.meta
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using System;
2+
using Assets.RemoteHandsTracking.Data;
3+
using Assets.RemoteHandsTracking.Extensions;
4+
using OculusSampleFramework;
5+
using UnityEngine;
6+
7+
namespace Assets.RemoteHandsTracking.Customisations
8+
{
9+
public class OculusIntegrationExampleHandsDataFeeder: HandsDataFeederBase
10+
{
11+
public Hands Hands;
12+
13+
private HandReflection _leftHandReflection;
14+
private HandReflection _rightHandReflection;
15+
16+
private Action _setScaledAlphaInLateUpdate;
17+
18+
public void Start()
19+
{
20+
_leftHandReflection = new HandReflection(Hands.LeftHand);
21+
_rightHandReflection = new HandReflection(Hands.RightHand);
22+
}
23+
24+
public override void ProcessData(HandData handData)
25+
{
26+
var handReflection = GetHandReflection(handData);
27+
ExecuteUpdatePose(handData, handReflection);
28+
}
29+
30+
public void LateUpdate()
31+
{
32+
_setScaledAlphaInLateUpdate?.Invoke();
33+
}
34+
35+
private void ExecuteUpdatePose(HandData handData, HandReflection handReflection)
36+
{
37+
var currentState = handData.HandState;
38+
39+
var isTracked = (currentState.Status & OVRPlugin.HandStatus.HandTracked) == OVRPlugin.HandStatus.HandTracked;
40+
handReflection.SetIsTracked(isTracked);
41+
42+
if (isTracked)
43+
{
44+
handReflection.SetHandConfidence(Hand.OVRPluginConfidenceToHand(currentState.HandConfidence));
45+
}
46+
else
47+
{
48+
handReflection.SetHandConfidence(Hand.HandTrackingConfidence.None);
49+
}
50+
51+
//WARN: there is some issue with hand flickering most likely due to some timing issue when setting AlphaValue [being overriden by default process]
52+
//HACK: for now it may be easiest to always return 1f from 'ScaledAlpha' getter in 'Hand' class
53+
// Fade hand according to confidence.
54+
var scaledAlphaToApply = handReflection.HandConfidenceFader.NextAlphaValue(handReflection.Hand.HandConfidence);
55+
_setScaledAlphaInLateUpdate = () => //That update needs to happen in LateUpdate, otherwise it could be overriden by default process
56+
{
57+
handReflection.Hand.ScaledAlpha = scaledAlphaToApply;
58+
if (handReflection.Hand.HandMesh)
59+
{
60+
handReflection.Hand.HandMesh.UpdatePose();
61+
}
62+
};
63+
// Update Pointer
64+
var pointer = handReflection.GetPointer();
65+
pointer.PointerPosition = currentState.PointerPose.Position.FromFlippedZVector3f();
66+
pointer.PointerOrientation = currentState.PointerPose.Orientation.FromFlippedZQuatf();
67+
pointer.PointerStatusValid = (currentState.Status & OVRPlugin.HandStatus.InputStateValid) ==
68+
OVRPlugin.HandStatus.InputStateValid;
69+
70+
if (handReflection.Hand.Skeleton)
71+
{
72+
handReflection.Hand.Skeleton.UpdatePose(currentState);
73+
}
74+
75+
if (handData.Step == OVRPlugin.Step.Physics && handReflection.Hand.Physics)
76+
{
77+
handReflection.Hand.Physics.UpdatePose();
78+
}
79+
}
80+
81+
private HandReflection GetHandReflection(HandData handData)
82+
{
83+
HandReflection handReflection;
84+
switch (handData.Hand)
85+
{
86+
case OVRPlugin.Hand.HandLeft:
87+
handReflection = _leftHandReflection;
88+
break;
89+
case OVRPlugin.Hand.HandRight:
90+
handReflection = _rightHandReflection;
91+
break;
92+
case OVRPlugin.Hand.None:
93+
default:
94+
throw new ArgumentOutOfRangeException();
95+
}
96+
97+
return handReflection;
98+
}
99+
100+
private class HandReflection
101+
{
102+
//private object _confidenceFader;
103+
private Action<Hand, bool> IsTrackedSetter { get; }
104+
private Action<Hand, Hand.HandTrackingConfidence> HandConfidenceSetter { get; }
105+
//private Func<Hand, object> ConfidenceFaderGetter { get; }
106+
//private Func<Hand.HandTrackingConfidence, float> ConfidenceFaderExecuteNextAlphaValueMethodFunc { get; }
107+
private Func<Hand, Hand.PointerState> PointerGetter { get; }
108+
public HandConfidenceFader HandConfidenceFader { get; }
109+
110+
public Hand Hand { get; }
111+
112+
113+
public HandReflection(Hand hand)
114+
{
115+
Hand = hand;
116+
var handType = typeof(Hand);
117+
IsTrackedSetter = handType.CreateSetFieldDelegate<Hand, bool>("_isTracked");
118+
HandConfidenceSetter = handType.CreateSetFieldDelegate<Hand, Hand.HandTrackingConfidence>("_handConfidence");
119+
120+
HandConfidenceFader = new HandConfidenceFader(40);
121+
//ConfidenceFaderGetter = handType.CreateGetFieldDelegate<Hand, object>("_confidenceFader");
122+
//_confidenceFader = ConfidenceFaderGetter(Hand);
123+
//var confidenceFaderNextAlphaValueMethod = _confidenceFader.GetType()
124+
// .GetMethod("NextAlphaValue", new[] { typeof(Hand.HandTrackingConfidence) });
125+
//ConfidenceFaderExecuteNextAlphaValueMethodFunc = (Func<Hand.HandTrackingConfidence, float>)
126+
// Delegate.CreateDelegate(typeof(Func<Hand.HandTrackingConfidence, float>), _confidenceFader, confidenceFaderNextAlphaValueMethod);
127+
128+
PointerGetter = handType.CreateGetFieldDelegate<Hand, Hand.PointerState>("_pointer");
129+
}
130+
131+
public void SetIsTracked(bool isTracked) => IsTrackedSetter(Hand, isTracked);
132+
public void SetHandConfidence(Hand.HandTrackingConfidence handConfidence) => HandConfidenceSetter(Hand, handConfidence);
133+
//public float ConfidenceFaderExecuteNextAlphaValueMethod(Hand.HandTrackingConfidence handTrackingConfidence)
134+
// => ConfidenceFaderExecuteNextAlphaValueMethodFunc(handTrackingConfidence);
135+
136+
public Hand.PointerState GetPointer() => PointerGetter(Hand);
137+
138+
}
139+
140+
private class HandConfidenceFader
141+
{
142+
private static float MAX_ALPHA = 1.0F;
143+
private int _numberOfFrames;
144+
private float _minTrackedAlpha = 0.0f;
145+
private float _maxTrackedAlpha = MAX_ALPHA;
146+
private int _currentCount = 0;
147+
148+
public HandConfidenceFader(int numberOfFramse)
149+
{
150+
_numberOfFrames = numberOfFramse;
151+
}
152+
153+
public float NextAlphaValue(Hand.HandTrackingConfidence confidence)
154+
{
155+
var calculatedAlpha = 0.0f;
156+
switch (confidence)
157+
{
158+
case Hand.HandTrackingConfidence.High:
159+
_currentCount = Mathf.Min((_currentCount + 1), _numberOfFrames);
160+
calculatedAlpha = Mathf.Clamp(_currentCount / (float)_numberOfFrames, _minTrackedAlpha, _maxTrackedAlpha);
161+
break;
162+
case Hand.HandTrackingConfidence.Low:
163+
_currentCount = Mathf.Max((_currentCount - 1), 0);
164+
calculatedAlpha = Mathf.Clamp(_currentCount / (float)_numberOfFrames, _minTrackedAlpha, _maxTrackedAlpha);
165+
break;
166+
default:
167+
_currentCount = 0;
168+
calculatedAlpha = 0.0f;
169+
break;
170+
}
171+
return calculatedAlpha;
172+
}
173+
}
174+
}
175+
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections;
3+
using System.Reflection;
4+
using Assets.RemoteHandsTracking.Data;
5+
using OculusSampleFramework;
6+
using UnityEngine;
7+
8+
namespace Assets.RemoteHandsTracking.Customisations
9+
{
10+
public class OculusIntegrationExampleMeshDataFeeder: MonoBehaviour
11+
{
12+
public Hands Hands;
13+
14+
private HandMeshReflection _leftHandMeshReflection;
15+
private HandMeshReflection _rightHandMeshReflection;
16+
17+
public void Start()
18+
{
19+
_leftHandMeshReflection = new HandMeshReflection(Hands.LeftHand.HandMesh);
20+
_rightHandMeshReflection = new HandMeshReflection(Hands.RightHand.HandMesh);
21+
}
22+
23+
public void ProcessData(MeshData meshData)
24+
{
25+
StartCoroutine(InitializeHandMesh(meshData));
26+
}
27+
28+
private IEnumerator InitializeHandMesh(MeshData meshData)
29+
{
30+
var handMeshReflection = GetHandMeshReflection(meshData);
31+
bool success = false;
32+
while (!success)
33+
{
34+
var mesh = meshData.Mesh;
35+
success = handMeshReflection.InitializeMesh(mesh);
36+
yield return null;
37+
}
38+
39+
Debug.Log($"HandMesh({meshData.MeshType}) - initialized");
40+
}
41+
42+
43+
private HandMeshReflection GetHandMeshReflection(MeshData meshData)
44+
{
45+
HandMeshReflection reflection;
46+
switch (meshData.MeshType)
47+
{
48+
case OVRPlugin.MeshType.HandLeft:
49+
reflection = _leftHandMeshReflection;
50+
break;
51+
case OVRPlugin.MeshType.HandRight:
52+
reflection = _rightHandMeshReflection;
53+
break;
54+
55+
default:
56+
throw new ArgumentOutOfRangeException();
57+
}
58+
59+
return reflection;
60+
}
61+
62+
private class HandMeshReflection
63+
{
64+
public HandMesh HandMesh { get; }
65+
private MethodInfo InitializeMeshMethod { get; }
66+
67+
public HandMeshReflection(HandMesh handMesh)
68+
{
69+
HandMesh = handMesh;
70+
71+
InitializeMeshMethod = typeof(HandMesh).GetMethod("InitializeMesh", BindingFlags.Instance | BindingFlags.NonPublic);
72+
73+
}
74+
75+
public bool InitializeMesh(OVRPlugin.Mesh mesh)
76+
{
77+
return (bool) InitializeMeshMethod.Invoke(HandMesh, new object[] { mesh });
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)