forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatedHand.cs
More file actions
129 lines (107 loc) · 4.56 KB
/
Copy pathSimulatedHand.cs
File metadata and controls
129 lines (107 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Toolkit.Utilities;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Microsoft.MixedReality.Toolkit.Input
{
/// <summary>
/// Snapshot of simulated hand data.
/// </summary>
[Serializable]
public class SimulatedHandData
{
[SerializeField]
private bool isTracked = false;
/// <summary>
/// Whether the hand is currently being tracked
/// </summary>
public bool IsTracked => isTracked;
[SerializeField]
private MixedRealityPose[] joints = new MixedRealityPose[ArticulatedHandPose.JointCount];
/// <summary>
/// Array storing the joints of the hand
/// </summary>
public MixedRealityPose[] Joints => joints;
[SerializeField]
private bool isPinching = false;
/// <summary>
/// Whether the hand is pinching
/// </summary>
public bool IsPinching => isPinching;
/// <summary>
/// Generator function producing joint positions and rotations
/// </summary>
public delegate void HandJointDataGenerator(MixedRealityPose[] jointPoses);
public void Copy(SimulatedHandData other)
{
isTracked = other.isTracked;
isPinching = other.isPinching;
for (int i = 0; i < ArticulatedHandPose.JointCount; ++i)
{
joints[i] = other.joints[i];
}
}
/// <summary>
/// Replace the hand data with the given values.
/// </summary>
/// <returns>True if the hand data has been changed.</returns>
/// <param name="isTrackedNew">True if the hand is currently tracked.</param>
/// <param name="isPinchingNew">True if the hand is in a pinching pose that causes a "Select" action.</param>
/// <param name="generator">Generator function that produces joint positions and rotations. The joint data generator is only used when the hand is tracked.</param>
/// <remarks>The timestamp of the hand data will be the current time, see [DateTime.UtcNow](https://docs.microsoft.com/dotnet/api/system.datetime.utcnow?view=netframework-4.8).</remarks>
public bool Update(bool isTrackedNew, bool isPinchingNew, HandJointDataGenerator generator)
{
bool handDataChanged = false;
if (isTracked != isTrackedNew || isPinching != isPinchingNew)
{
isTracked = isTrackedNew;
isPinching = isPinchingNew;
handDataChanged = true;
}
if (isTracked)
{
generator?.Invoke(Joints);
handDataChanged = true;
}
return handDataChanged;
}
}
public abstract class SimulatedHand : BaseHand
{
public abstract ControllerSimulationMode SimulationMode { get; }
protected readonly Dictionary<TrackedHandJoint, MixedRealityPose> jointPoses = new Dictionary<TrackedHandJoint, MixedRealityPose>();
/// <summary>
/// Constructor.
/// </summary>
protected SimulatedHand(
TrackingState trackingState,
Handedness controllerHandedness,
IMixedRealityInputSource inputSource = null,
MixedRealityInteractionMapping[] interactions = null,
IMixedRealityInputSourceDefinition definition = null)
: base(trackingState, controllerHandedness, inputSource, interactions, definition)
{ }
/// <inheritdoc/>
public override bool IsJointDataAvailable => jointPoses.Count > 0;
/// <inheritdoc />
public override bool TryGetJoint(TrackedHandJoint joint, out MixedRealityPose pose) => jointPoses.TryGetValue(joint, out pose);
public void UpdateState(SimulatedHandData handData)
{
UpdateHandJoints(handData);
UpdateVelocity();
UpdateInteractions(handData);
}
/// <summary>
/// Updates the positions and orientations of the hand joints of the simulated hand
/// </summary>
/// <param name="handData">hand data provided by the simulation</param>
protected abstract void UpdateHandJoints(SimulatedHandData handData);
/// <summary>
/// Updates the interactions raised by the simulated hand
/// </summary>
/// <param name="handData">hand data provided by the simulation</param>
protected abstract void UpdateInteractions(SimulatedHandData handData);
}
}