-
Notifications
You must be signed in to change notification settings - Fork 19
/
HandsDataPlayer.cs
134 lines (111 loc) · 4.61 KB
/
HandsDataPlayer.cs
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
130
131
132
133
134
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.RemoteHandsTracking.Data;
using Assets.RemoteHandsTracking.Utilities;
using UnityEngine;
using UnityEngine.Events;
namespace Assets.RemoteHandsTracking
{
public class HandsDataPlayer : MonoBehaviour
{
private const int ManuallyMoveToFrameDisabledValue = -1;
[Serializable] public class HandDataProcessingUnityEvent : UnityEvent<HandData> { }
[Serializable] public class SkeletonDataProcessingUnityEvent : UnityEvent<SkeletonData> { }
[Serializable] public class MeshDataProcessingUnityEvent : UnityEvent<MeshData> { }
public HandDataProcessingUnityEvent HandDataProcessing = new HandDataProcessingUnityEvent();
public SkeletonDataProcessingUnityEvent SkeletonDataProcessing = new SkeletonDataProcessingUnityEvent();
public MeshDataProcessingUnityEvent MeshDataProcessing = new MeshDataProcessingUnityEvent();
[ShowOnly]
public int CurrentlyPlayedFrame;
[Tooltip("Set to -1 to disable")]
public int ManuallyMoveToFrame = ManuallyMoveToFrameDisabledValue;
public HandsDataRecording CurrentPlayedRecording;
private Queue<HandsDataRecordedFrame> _framesToPlay;
private HandsDataRecordedFrame _currentlyPlayedFrame;
private bool _isSkeletonDataProcessed;
private bool _isMeshDataProcessed;
public void Play(HandsDataRecording recording)
{
ManuallyMoveToFrame = ManuallyMoveToFrameDisabledValue;
CurrentPlayedRecording = recording;
_framesToPlay = new Queue<HandsDataRecordedFrame>(recording.HandsDataRecordedFrames);
CurrentlyPlayedFrame = 0;
}
[EditorButton]
public void Replay()
{
if (CurrentPlayedRecording != null)
{
Play(CurrentPlayedRecording);
}
}
[EditorButton]
public void StopPlaying()
{
_framesToPlay = null;
_currentlyPlayedFrame = null;
}
private void Start()
{
ManuallyMoveToFrame = ManuallyMoveToFrameDisabledValue;
}
private void FixedUpdate()
{
SetCurrentlyPlayedFrame();
if (_currentlyPlayedFrame == null) return;
if (!_isSkeletonDataProcessed)
{
ProcessSkeletonData(CurrentPlayedRecording.InitHandData.LeftHandSkeletonData);
ProcessSkeletonData(CurrentPlayedRecording.InitHandData.RightHandSkeletonData);
_isSkeletonDataProcessed = true;
}
ProcessHandData(_currentlyPlayedFrame.LeftHandPhysicsUpdate);
ProcessHandData(_currentlyPlayedFrame.RightHandPhysicsUpdate);
}
private void Update()
{
if (_currentlyPlayedFrame == null) return;
if (!_isMeshDataProcessed)
{
ProcessMeshData(CurrentPlayedRecording.InitHandData.LeftHandMeshData);
ProcessMeshData(CurrentPlayedRecording.InitHandData.RightHandMeshData);
_isMeshDataProcessed = true;
}
ProcessHandData(_currentlyPlayedFrame.LeftHandRenderUpdate);
ProcessHandData(_currentlyPlayedFrame.RightHandRenderUpdate);
}
private void SetCurrentlyPlayedFrame()
{
if (CurrentPlayedRecording != null && ManuallyMoveToFrame != ManuallyMoveToFrameDisabledValue)
{
ManuallyMoveToFrame = Mathf.Clamp(ManuallyMoveToFrame, ManuallyMoveToFrameDisabledValue, CurrentPlayedRecording.HandsDataRecordedFrames.Count);
_currentlyPlayedFrame = CurrentPlayedRecording.HandsDataRecordedFrames[ManuallyMoveToFrame < 0 ? 0 : ManuallyMoveToFrame];
}
else
{
if (_framesToPlay != null && _framesToPlay.Any())
{
_currentlyPlayedFrame = _framesToPlay.Dequeue();
CurrentlyPlayedFrame++;
}
else
{
_currentlyPlayedFrame = null;
}
}
}
private void ProcessSkeletonData(SkeletonData skeletonData)
{
if (skeletonData != null) SkeletonDataProcessing?.Invoke(skeletonData);
}
private void ProcessMeshData(MeshData meshData)
{
if (meshData != null) MeshDataProcessing?.Invoke(meshData);
}
private void ProcessHandData(HandData handData)
{
if (handData != null) HandDataProcessing?.Invoke(handData);
}
}
}