-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomHand.cs
More file actions
462 lines (365 loc) · 18.1 KB
/
CustomHand.cs
File metadata and controls
462 lines (365 loc) · 18.1 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Leap;
using System;
using System.Linq;
using UnityEngine.Networking;
namespace Leap.Unity {
/** A basic Leap hand model constructed dynamically vs. using pre-existing geometry*/
public class CustomHand : IHandModel {
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;
[SerializeField]
private int _cylinderResolution = 12;
private bool _hasGeneratedMeshes = false;
private Material jointMat;
[SerializeField, HideInInspector]
private List<Transform> _serializedTransforms;
private Transform[] _jointSpheres;
private Transform mockThumbJointSphere;
private Transform palmPositionSphere;
private Transform wristPositionSphere;
private List<Renderer> _armRenderers;
private List<Transform> _cylinderTransforms;
private List<Transform> _sphereATransforms;
private List<Transform> _sphereBTransforms;
private Transform armFrontLeft, armFrontRight, armBackLeft, armBackRight;
private Hand hand_;
public override ModelType HandModelType {
get {
return ModelType.Graphics;
}
}
public override Chirality Handedness {
get {
return handedness;
}
set { }
}
public Color PlayerColor {
get; set;
}
public override bool SupportsEditorPersistence() {
return true;
}
public override Hand GetLeapHand() {
return hand_;
}
public override void SetLeapHand(Hand hand) {
hand_ = hand;
}
void OnValidate() {
//Resolution must be at least 3!
_cylinderResolution = Mathf.Max(3, _cylinderResolution);
//Update visibility on validate so that the user can toggle in real-time
if (_armRenderers != null) {
updateArmVisibility();
}
}
public override void InitHand() {
if (_material != null) {
jointMat = new Material(_material);
jointMat.hideFlags = HideFlags.DontSaveInEditor;
}
if (_serializedTransforms != null) {
//for (int i = 0; i < _serializedTransforms.Count; i++) {
// var obj = _serializedTransforms[i];
// if (obj != null) {
// DestroyImmediate(obj.gameObject);
// }
//}
//_serializedTransforms.Clear();
} else {
_serializedTransforms = new List<Transform>();
}
_jointSpheres = new Transform[4 * 5];
_armRenderers = new List<Renderer>();
_cylinderTransforms = new List<Transform>();
_sphereATransforms = new List<Transform>();
_sphereBTransforms = new List<Transform>();
//createSpheres();
//createCylinders();
BuildSpheres();
BuildCylinders();
updateArmVisibility();
_hasGeneratedMeshes = false;
}
public override void BeginHand() {
base.BeginHand();
if (hand_.IsLeft) {
jointMat.color = _leftColorList[_leftColorIndex];
_leftColorIndex = (_leftColorIndex + 1) % _leftColorList.Length;
} else {
jointMat.color = _rightColorList[_rightColorIndex];
_rightColorIndex = (_rightColorIndex + 1) % _rightColorList.Length;
}
}
public override void UpdateHand() {
//Update the spheres first
updateSpheres();
//Update Arm only if we need to
if (_showArm) {
updateArm();
}
//The cylinder transforms are deterimined by the spheres they are connected to
updateCylinders();
}
//Transform updating methods
private void updateSpheres() {
//Update all spheres
List<Finger> fingers = hand_.Fingers;
for (int i = 0; i < fingers.Count; i++) {
Finger finger = fingers[i];
for (int j = 0; j < 4; j++) {
int key = getFingerJointIndex((int)finger.Type, j);
Transform sphere = _jointSpheres[key];
sphere.position = finger.Bone((Bone.BoneType)j).NextJoint.ToVector3();
}
}
palmPositionSphere.position = hand_.PalmPosition.ToVector3();
Vector3 wristPos = hand_.PalmPosition.ToVector3();
wristPositionSphere.position = wristPos;
Transform thumbBase = _jointSpheres[THUMB_BASE_INDEX];
Vector3 thumbBaseToPalm = thumbBase.position - hand_.PalmPosition.ToVector3();
mockThumbJointSphere.position = hand_.PalmPosition.ToVector3() + Vector3.Reflect(thumbBaseToPalm, hand_.Basis.xBasis.ToVector3());
}
private void updateArm() {
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;
armFrontRight.position = wrist + right;
armFrontLeft.position = wrist - right;
armBackRight.position = elbow + right;
armBackLeft.position = elbow - right;
}
private void updateCylinders() {
for (int i = 0; i < _cylinderTransforms.Count; i++) {
Transform cylinder = _cylinderTransforms[i];
Transform sphereA = _sphereATransforms[i];
Transform sphereB = _sphereBTransforms[i];
Vector3 delta = sphereA.position - sphereB.position;
if (!_hasGeneratedMeshes) {
MeshFilter filter = cylinder.GetComponent<MeshFilter>();
filter.sharedMesh = generateCylinderMesh(delta.magnitude / transform.lossyScale.x);
}
cylinder.position = sphereA.position;
if (delta.sqrMagnitude <= Mathf.Epsilon) {
//Two spheres are at the same location, no rotation will be found
continue;
}
cylinder.LookAt(sphereB);
}
_hasGeneratedMeshes = true;
}
private void updateArmVisibility() {
for (int i = 0; i < _armRenderers.Count; i++) {
_armRenderers[i].enabled = _showArm;
}
}
//Geometry creation methods
//private void createSpheres() {
// //Create spheres for finger joints
// List<Finger> fingers = hand_.Fingers;
// for (int i = 0; i < fingers.Count; i++) {
// Finger finger = fingers[i];
// for (int j = 0; j < 4; j++) {
// int key = getFingerJointIndex((int)finger.Type, j);
// _jointSpheres[key] = createSphere("Joint", SPHERE_RADIUS);
// }
// }
// mockThumbJointSphere = createSphere("MockJoint", SPHERE_RADIUS);
// palmPositionSphere = createSphere("PalmPosition", PALM_RADIUS);
// wristPositionSphere = createSphere("WristPosition", SPHERE_RADIUS);
// armFrontLeft = createSphere("ArmFrontLeft", SPHERE_RADIUS, true);
// armFrontRight = createSphere("ArmFrontRight", SPHERE_RADIUS, true);
// armBackLeft = createSphere("ArmBackLeft", SPHERE_RADIUS, true);
// armBackRight = createSphere("ArmBackRight", SPHERE_RADIUS, true);
//}
private void BuildSpheres() {
Transform[] joints = GetComponentsInChildren<Transform>().Where(c => c.name == "Joint").ToArray();
int k = 0;
//PlayerColor = new Color(UnityEngine.Random.Range(0f, 1), UnityEngine.Random.Range(0f, 1), UnityEngine.Random.Range(0f, 1));
//Create spheres for finger joints
List<Finger> fingers = hand_.Fingers;
for (int i = 0; i < fingers.Count; i++) {
Finger finger = fingers[i];
for (int j = 0; j < 4; j++) {
int key = getFingerJointIndex((int)finger.Type, j);
_jointSpheres[key] = joints[k++]; /*createSphere("Joint", SPHERE_RADIUS);*/
_jointSpheres[key].GetComponent<Renderer>().material = _material;
_jointSpheres[key].GetComponent<Renderer>().material.color = PlayerColor;
}
}
mockThumbJointSphere = transform.FindChild("MockJoint");//createSphere("MockJoint", SPHERE_RADIUS);
palmPositionSphere = transform.FindChild("PalmPosition");//createSphere("PalmPosition", PALM_RADIUS);
wristPositionSphere = transform.FindChild("WristPosition");//createSphere("WristPosition", SPHERE_RADIUS);
mockThumbJointSphere.GetComponent<Renderer>().material = _material;
palmPositionSphere.GetComponent<Renderer>().material = _material;
wristPositionSphere.GetComponent<Renderer>().material = _material;
mockThumbJointSphere.GetComponent<Renderer>().material.color = PlayerColor;
palmPositionSphere.GetComponent<Renderer>().material.color = PlayerColor;
wristPositionSphere.GetComponent<Renderer>().material.color = PlayerColor;
//armFrontLeft = createSphere("ArmFrontLeft", SPHERE_RADIUS, true);
//armFrontRight = createSphere("ArmFrontRight", SPHERE_RADIUS, true);
//armBackLeft = createSphere("ArmBackLeft", SPHERE_RADIUS, true);
//armBackRight = createSphere("ArmBackRight", SPHERE_RADIUS, true);
}
private void BuildCylinders() {
Transform[] fingerJoints = GetComponentsInChildren<Transform>().Where(c => c.name == "Finger Joint").ToArray();
int k = 0;
//Create 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);
Transform sphereA = _jointSpheres[keyA];
Transform sphereB = _jointSpheres[keyB];
buildCylinder(sphereA, sphereB, fingerJoints[k++]);
//createCylinder("Finger Joint", sphereA, sphereB);
}
}
Transform[] handJoints = GetComponentsInChildren<Transform>().Where(c => c.name == "Hand Joints").ToArray();
k = 0;
//Create cylinders between finger knuckles
for (int i = 0; i < 4; i++) {
int keyA = getFingerJointIndex(i, 0);
int keyB = getFingerJointIndex(i + 1, 0);
Transform sphereA = _jointSpheres[keyA];
Transform sphereB = _jointSpheres[keyB];
buildCylinder(sphereA, sphereB, handJoints[k++]);
//createCylinder("Hand Joints", sphereA, sphereB);
}
//Create the rest of the hand
Transform thumbBase = _jointSpheres[THUMB_BASE_INDEX];
Transform pinkyBase = _jointSpheres[PINKY_BASE_INDEX];
buildCylinder(thumbBase, mockThumbJointSphere, transform.FindChild("Hand Bottom"));
buildCylinder(pinkyBase, mockThumbJointSphere, transform.FindChild("Hand Side"));
//createCylinder("Hand Bottom", thumbBase, mockThumbJointSphere);
//createCylinder("Hand Side", pinkyBase, mockThumbJointSphere);
//createCylinder("ArmFront", armFrontLeft, armFrontRight, true);
//createCylinder("ArmBack", armBackLeft, armBackRight, true);
//createCylinder("ArmLeft", armFrontLeft, armBackLeft, true);
//createCylinder("ArmRight", armFrontRight, armBackRight, true);
}
private void buildCylinder(Transform sphereA, Transform sphereB, Transform joint) {
_sphereATransforms.Add(sphereA);
_sphereBTransforms.Add(sphereB);
_cylinderTransforms.Add(joint);
}
//private void createCylinders() {
// //Create 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);
// Transform sphereA = _jointSpheres[keyA];
// Transform sphereB = _jointSpheres[keyB];
// createCylinder("Finger Joint", sphereA, sphereB);
// }
// }
// //Create cylinders between finger knuckles
// for (int i = 0; i < 4; i++) {
// int keyA = getFingerJointIndex(i, 0);
// int keyB = getFingerJointIndex(i + 1, 0);
// Transform sphereA = _jointSpheres[keyA];
// Transform sphereB = _jointSpheres[keyB];
// createCylinder("Hand Joints", sphereA, sphereB);
// }
// //Create the rest of the hand
// Transform thumbBase = _jointSpheres[THUMB_BASE_INDEX];
// Transform pinkyBase = _jointSpheres[PINKY_BASE_INDEX];
// createCylinder("Hand Bottom", thumbBase, mockThumbJointSphere);
// createCylinder("Hand Side", pinkyBase, mockThumbJointSphere);
// createCylinder("ArmFront", armFrontLeft, armFrontRight, true);
// createCylinder("ArmBack", armBackLeft, armBackRight, true);
// createCylinder("ArmLeft", armFrontLeft, armBackLeft, true);
// createCylinder("ArmRight", armFrontRight, armBackRight, true);
//}
private int getFingerJointIndex(int fingerIndex, int jointIndex) {
return fingerIndex * 4 + jointIndex;
}
//private Transform createSphere(string name, float radius, bool isPartOfArm = false) {
// GameObject sphere = new GameObject(name);
// _serializedTransforms.Add(sphere.transform);
// sphere.AddComponent<MeshFilter>().mesh = _sphereMesh;
// sphere.AddComponent<MeshRenderer>().sharedMaterial = jointMat;
// sphere.transform.parent = transform;
// sphere.transform.localScale = Vector3.one * radius * 2;
// //sphere.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.HideInInspector;
// sphere.layer = gameObject.layer;
// if (isPartOfArm) {
// _armRenderers.Add(sphere.GetComponent<Renderer>());
// }
// return sphere.transform;
//}
//private void createCylinder(string name, Transform jointA, Transform jointB, bool isPartOfArm = false) {
// GameObject cylinder = new GameObject(name);
// _serializedTransforms.Add(cylinder.transform);
// cylinder.AddComponent<MeshFilter>();
// cylinder.AddComponent<MeshRenderer>().sharedMaterial = _material;
// cylinder.transform.parent = transform;
// _cylinderTransforms.Add(cylinder.transform);
// _sphereATransforms.Add(jointA);
// _sphereBTransforms.Add(jointB);
// cylinder.gameObject.layer = gameObject.layer;
// //cylinder.hideFlags = HideFlags.DontSave | HideFlags.HideInHierarchy | HideFlags.HideInInspector;
// if (isPartOfArm) {
// _armRenderers.Add(cylinder.GetComponent<Renderer>());
// }
//}
private Mesh generateCylinderMesh(float length) {
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) * transform.lossyScale.x);
verts.Add((p1 + spoke) * transform.lossyScale.x);
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);
return mesh;
}
}
}