comparison Orchestland/Assets/LeapMotion/DemoResources/Scripts/HandDetector.cs @ 3:0030a1b971fb default tip

merge
author Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:23:43 +0900
parents f7675884f2a1
children
comparison
equal deleted inserted replaced
2:fdab88fc2cb9 3:0030a1b971fb
1 using UnityEngine;
2 using System.Collections;
3 using Leap;
4
5 public class HandDetector : MonoBehaviour {
6
7 public HandController leap_controller_;
8
9 HandModel GetHand(Collider other)
10 {
11 HandModel hand_model = null;
12 // Navigate a maximum of 3 levels to find the HandModel component.
13 int level = 1;
14 Transform parent = other.transform.parent;
15 while (parent != null && level < 3) {
16 hand_model = parent.GetComponent<HandModel>();
17 if (hand_model != null) {
18 break;
19 }
20 parent = parent.parent;
21 }
22
23 return hand_model;
24 }
25
26 // Finds the first instance (by depth-firstrecursion)
27 // of a child with the specified name
28 Transform FindPart(Transform parent, string name) {
29 if (parent == null) {
30 return parent;
31 }
32 if (parent.name == name) {
33 return parent;
34 }
35 for (int c = 0; c < parent.childCount; c++) {
36 Transform part = FindPart(parent.GetChild(c), name);
37 if (part != null) {
38 return part;
39 }
40 }
41 return null;
42 }
43
44 void OnTriggerEnter(Collider other)
45 {
46 HandModel hand_model = GetHand(other);
47 if (hand_model != null)
48 {
49 int handID = hand_model.GetLeapHand().Id;
50 HandModel[] hand_models = leap_controller_.GetAllGraphicsHands();
51 for (int i = 0; i < hand_models.Length; ++i)
52 {
53 if (hand_models[i].GetLeapHand().Id == handID)
54 {
55 Transform part = null;
56 if (other.transform.parent.GetComponent<HandModel>() != null) {
57 // Palm or Forearm components
58 part = FindPart(hand_models[i].transform, other.name);
59 } else if (other.transform.parent.GetComponent<FingerModel>() != null) {
60 // Bone in a finger
61 part = FindPart(FindPart(hand_models[i].transform, other.transform.parent.name), other.name);
62 }
63 //Debug.Log ("Detected: " + other.transform.parent.name + "/" + other.gameObject.name);
64 if (part != null) {
65 Renderer[] renderers = part.GetComponentsInChildren<Renderer>();
66 foreach(Renderer renderer in renderers) {
67 //Debug.Log ("Marked: " + renderer.gameObject.transform.parent.name + "/" + renderer.gameObject.name);
68 renderer.material.color = Color.red;
69 }
70 }
71 }
72 }
73 }
74 }
75 }