-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoseTracking.cs
More file actions
115 lines (90 loc) · 3.84 KB
/
Copy pathPoseTracking.cs
File metadata and controls
115 lines (90 loc) · 3.84 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoseTracking : MonoBehaviour
{
// Start is called before the first frame update
public UDPReceive udpReceive;
public GameObject[] bodyPoints;
public GameObject[] leftHandPoints;
public GameObject[] rightHandPoints;
private float desiredDuration = 3f;
private float elapsedTime;
// Debug.Log("rightHandData.Count: " + rightHandData.Count);
// Debug.Log("rightHandPoints.Length: " + rightHandPoints.Length);
List<Vector3> bodyData = new List<Vector3>();
List<Vector3> leftHandData = new List<Vector3>();
List<Vector3> rightHandData = new List<Vector3>();
void Start()
{
for (int y = 0; y < 21; y++) {
Vector3 location = bodyPoints[y].transform.localPosition;
bodyData.Add(location);
}
for (int y = 0; y < 10; y++)
{
Vector3 location = leftHandPoints[y].transform.localPosition;
leftHandData.Add(location);
}
for (int y = 0; y < 10; y++)
{
Vector3 location = rightHandPoints[y].transform.localPosition;
rightHandData.Add(location);
}
}
// Update is called once per frame
void Update()
{
// try: elapsedTime += Time.deltaTime; for smoother transition
elapsedTime += 5f;
string data = udpReceive.data;
// booleans for hand detection
left_hand = true if data[0] == 'T' else false
right_hand = true if data[0] == 'T' else false
data = data[2:]
// Debug.Log("Received data: " + data);
string[] points = data.Split(',');
// Debug.Log("Number of points: " + points.Length);
// Update body points
for (int i = 0; i < bodyPoints.Length; i++)
{
// Attempt to parse the values, and handle any parsing errors
float x, y, z;
if (!float.TryParse(points[i * 3], out x) ||
!float.TryParse(points[i * 3 + 1], out y) ||
!float.TryParse(points[i * 3 + 2], out z))
{
bodyPoints[i].transform.localPosition = Vector3.Lerp(bodyData[i], endingPosition, elapsedTime/desiredDuration);
bodyData[i] = endingPosition;
}
print("finish body");
if left_hand {
// Update left hand points
int startIndex = 21;
for (int i = 0; i < leftHandPoints.Length; i++)
{
float x = float.Parse(points[startIndex + i * 3]) * -5;
float y = float.Parse(points[startIndex + i * 3 + 1]) * -5;
float z = float.Parse(points[startIndex + i * 3 + 2]) * -5;
Vector3 endingPosition = new Vector3(x, y, z);
leftHandPoints[i].transform.localPosition = Vector3.Lerp(leftHandData[i], endingPosition, elapsedTime/desiredDuration);
leftHandData[i] = endingPosition;
}
print("finish left hand");
}
if right_hand {
// Update right hand points
startIndex = 31;
for (int i = 0; i < rightHandPoints.Length; i++)
{
float x = float.Parse(points[startIndex + i * 3]) * -5;
float y = float.Parse(points[startIndex + i * 3 + 1]) * -5;
float z = float.Parse(points[startIndex + i * 3 + 2]) * -5;
Vector3 endingPosition = new Vector3(x, y, z);
rightHandPoints[i].transform.localPosition = Vector3.Lerp(rightHandData[i], endingPosition, elapsedTime / desiredDuration);
rightHandData[i] = endingPosition;
}
print("finish right hand");
}
}
}