CapsuleHand.cs
8.87 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Leap.Unity.Attributes;
namespace Leap.Unity {
/** A basic Leap hand model constructed dynamically vs. using pre-existing geometry*/
public class CapsuleHand : HandModelBase {
private const int TOTAL_JOINT_COUNT = 4 * 5;
private const float CYLINDER_MESH_RESOLUTION = 0.1f; //in centimeters, meshes within this resolution will be re-used
private const int THUMB_BASE_INDEX = (int)Finger.FingerType.TYPE_THUMB * 4;
private const int PINKY_BASE_INDEX = (int)Finger.FingerType.TYPE_PINKY * 4;
private const float SPHERE_RADIUS = 0.008f;
private const float CYLINDER_RADIUS = 0.006f;
private const float PALM_RADIUS = 0.015f;
private static int _leftColorIndex = 0;
private static int _rightColorIndex = 0;
private static Color[] _leftColorList = { new Color(0.0f, 0.0f, 1.0f), new Color(0.2f, 0.0f, 0.4f), new Color(0.0f, 0.2f, 0.2f) };
private static Color[] _rightColorList = { new Color(1.0f, 0.0f, 0.0f), new Color(1.0f, 1.0f, 0.0f), new Color(1.0f, 0.5f, 0.0f) };
[SerializeField]
private Chirality handedness;
[SerializeField]
private bool _showArm = true;
[SerializeField]
private Material _material;
[SerializeField]
private Mesh _sphereMesh;
[MinValue(3)]
[SerializeField]
private int _cylinderResolution = 12;
private Material _sphereMat;
private Hand _hand;
private Vector3[] _spherePositions;
public override ModelType HandModelType {
get {
return ModelType.Graphics;
}
}
public override Chirality Handedness {
get {
return handedness;
}
set { }
}
public override bool SupportsEditorPersistence() {
return true;
}
public override Hand GetLeapHand() {
return _hand;
}
public override void SetLeapHand(Hand hand) {
_hand = hand;
}
public override void InitHand() {
if (_material != null) {
_sphereMat = new Material(_material);
_sphereMat.hideFlags = HideFlags.DontSaveInEditor;
}
}
private void OnValidate() {
_meshMap.Clear();
}
public override void BeginHand() {
base.BeginHand();
if (_hand.IsLeft) {
_sphereMat.color = _leftColorList[_leftColorIndex];
_leftColorIndex = (_leftColorIndex + 1) % _leftColorList.Length;
} else {
_sphereMat.color = _rightColorList[_rightColorIndex];
_rightColorIndex = (_rightColorIndex + 1) % _rightColorList.Length;
}
}
public override void UpdateHand() {
if (_spherePositions == null || _spherePositions.Length != TOTAL_JOINT_COUNT) {
_spherePositions = new Vector3[TOTAL_JOINT_COUNT];
}
if (_sphereMat == null) {
_sphereMat = new Material(_material);
_sphereMat.hideFlags = HideFlags.DontSaveInEditor;
}
//Update all joint spheres in the fingers
foreach (var finger in _hand.Fingers) {
for (int j = 0; j < 4; j++) {
int key = getFingerJointIndex((int)finger.Type, j);
Vector3 position = finger.Bone((Bone.BoneType)j).NextJoint.ToVector3();
_spherePositions[key] = position;
drawSphere(position);
}
}
//Now we just have a few more spheres for the hands
//PalmPos, WristPos, and mockThumbJointPos, which is derived and not taken from the frame obj
Vector3 palmPosition = _hand.PalmPosition.ToVector3();
drawSphere(palmPosition, PALM_RADIUS);
Vector3 wristPos = _hand.PalmPosition.ToVector3();
drawSphere(wristPos);
Vector3 thumbBaseToPalm = _spherePositions[THUMB_BASE_INDEX] - _hand.PalmPosition.ToVector3();
Vector3 mockThumbJointPos = _hand.PalmPosition.ToVector3() + Vector3.Reflect(thumbBaseToPalm, _hand.Basis.xBasis.ToVector3());
drawSphere(mockThumbJointPos);
//If we want to show the arm, do the calculations and display the meshes
if (_showArm) {
var arm = _hand.Arm;
Vector3 right = arm.Basis.xBasis.ToVector3() * arm.Width * 0.7f * 0.5f;
Vector3 wrist = arm.WristPosition.ToVector3();
Vector3 elbow = arm.ElbowPosition.ToVector3();
float armLength = Vector3.Distance(wrist, elbow);
wrist -= arm.Direction.ToVector3() * armLength * 0.05f;
Vector3 armFrontRight = wrist + right;
Vector3 armFrontLeft = wrist - right;
Vector3 armBackRight = elbow + right;
Vector3 armBackLeft = elbow - right;
drawSphere(armFrontRight);
drawSphere(armFrontLeft);
drawSphere(armBackLeft);
drawSphere(armBackRight);
drawCylinder(armFrontLeft, armFrontRight);
drawCylinder(armBackLeft, armBackRight);
drawCylinder(armFrontLeft, armBackLeft);
drawCylinder(armFrontRight, armBackRight);
}
//Draw cylinders between finger joints
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
int keyA = getFingerJointIndex(i, j);
int keyB = getFingerJointIndex(i, j + 1);
Vector3 posA = _spherePositions[keyA];
Vector3 posB = _spherePositions[keyB];
drawCylinder(posA, posB);
}
}
//Draw cylinders between finger knuckles
for (int i = 0; i < 4; i++) {
int keyA = getFingerJointIndex(i, 0);
int keyB = getFingerJointIndex(i + 1, 0);
Vector3 posA = _spherePositions[keyA];
Vector3 posB = _spherePositions[keyB];
drawCylinder(posA, posB);
}
//Draw the rest of the hand
drawCylinder(mockThumbJointPos, THUMB_BASE_INDEX);
drawCylinder(mockThumbJointPos, PINKY_BASE_INDEX);
}
private void drawSphere(Vector3 position, float radius = SPHERE_RADIUS) {
//multiply radius by 2 because the default unity sphere has a radius of 0.5 meters at scale 1.
Graphics.DrawMesh(_sphereMesh, Matrix4x4.TRS(position, Quaternion.identity, Vector3.one * radius * 2.0f * transform.lossyScale.x), _sphereMat, 0);
}
private void drawCylinder(Vector3 a, Vector3 b) {
float length = (a - b).magnitude;
Graphics.DrawMesh(getCylinderMesh(length),
Matrix4x4.TRS(a, Quaternion.LookRotation(b - a), new Vector3(transform.lossyScale.x, transform.lossyScale.x, 1)),
_material,
gameObject.layer);
}
private void drawCylinder(int a, int b) {
drawCylinder(_spherePositions[a], _spherePositions[b]);
}
private void drawCylinder(Vector3 a, int b) {
drawCylinder(a, _spherePositions[b]);
}
private int getFingerJointIndex(int fingerIndex, int jointIndex) {
return fingerIndex * 4 + jointIndex;
}
private Dictionary<int, Mesh> _meshMap = new Dictionary<int, Mesh>();
private Mesh getCylinderMesh(float length) {
int lengthKey = Mathf.RoundToInt(length * 100 / CYLINDER_MESH_RESOLUTION);
Mesh mesh;
if (_meshMap.TryGetValue(lengthKey, out mesh)) {
return mesh;
}
mesh = new Mesh();
mesh.name = "GeneratedCylinder";
mesh.hideFlags = HideFlags.DontSave;
List<Vector3> verts = new List<Vector3>();
List<Color> colors = new List<Color>();
List<int> tris = new List<int>();
Vector3 p0 = Vector3.zero;
Vector3 p1 = Vector3.forward * length;
for (int i = 0; i < _cylinderResolution; i++) {
float angle = (Mathf.PI * 2.0f * i) / _cylinderResolution;
float dx = CYLINDER_RADIUS * Mathf.Cos(angle);
float dy = CYLINDER_RADIUS * Mathf.Sin(angle);
Vector3 spoke = new Vector3(dx, dy, 0);
verts.Add(p0 + spoke);
verts.Add(p1 + spoke);
colors.Add(Color.white);
colors.Add(Color.white);
int triStart = verts.Count;
int triCap = _cylinderResolution * 2;
tris.Add((triStart + 0) % triCap);
tris.Add((triStart + 2) % triCap);
tris.Add((triStart + 1) % triCap);
tris.Add((triStart + 2) % triCap);
tris.Add((triStart + 3) % triCap);
tris.Add((triStart + 1) % triCap);
}
mesh.SetVertices(verts);
mesh.SetIndices(tris.ToArray(), MeshTopology.Triangles, 0);
mesh.RecalculateBounds();
mesh.RecalculateNormals();
mesh.UploadMeshData(true);
_meshMap[lengthKey] = mesh;
return mesh;
}
}
}