diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Orchestland/Assets/LeapMotion/DemoResources/Scripts/HandDetector.cs	Fri Jul 17 23:23:43 2015 +0900
@@ -0,0 +1,75 @@
+using UnityEngine;
+using System.Collections;
+using Leap;
+
+public class HandDetector : MonoBehaviour {
+
+  public HandController leap_controller_;
+
+  HandModel GetHand(Collider other)
+  {
+    HandModel hand_model = null;
+    // Navigate a maximum of 3 levels to find the HandModel component.
+    int level = 1;
+    Transform parent = other.transform.parent;
+    while (parent != null && level < 3) {
+      hand_model = parent.GetComponent<HandModel>();
+      if (hand_model != null) {
+        break;
+      }
+      parent = parent.parent;
+    }
+
+    return hand_model;
+  }
+
+  // Finds the first instance (by depth-firstrecursion)
+  // of a child with the specified name
+  Transform FindPart(Transform parent, string name) {
+    if (parent == null) {
+      return parent;
+    }
+    if (parent.name == name) {
+      return parent;
+    }
+    for (int c = 0; c < parent.childCount; c++) {
+      Transform part = FindPart(parent.GetChild(c), name);
+      if (part != null) {
+        return part;
+      }
+    }
+    return null;
+  }
+
+  void OnTriggerEnter(Collider other)
+  {
+    HandModel hand_model = GetHand(other);
+    if (hand_model != null)
+    {
+      int handID = hand_model.GetLeapHand().Id;
+      HandModel[] hand_models = leap_controller_.GetAllGraphicsHands();
+      for (int i = 0; i < hand_models.Length; ++i)
+      {
+        if (hand_models[i].GetLeapHand().Id == handID)
+        {
+          Transform part = null;
+          if (other.transform.parent.GetComponent<HandModel>() != null) {
+            // Palm or Forearm components
+            part = FindPart(hand_models[i].transform, other.name);
+          } else if (other.transform.parent.GetComponent<FingerModel>() != null) {
+            // Bone in a finger
+            part = FindPart(FindPart(hand_models[i].transform, other.transform.parent.name), other.name);
+          }
+          //Debug.Log ("Detected: " + other.transform.parent.name + "/" + other.gameObject.name);
+          if (part != null) {
+            Renderer[] renderers = part.GetComponentsInChildren<Renderer>();
+            foreach(Renderer renderer in renderers) {
+              //Debug.Log ("Marked: " + renderer.gameObject.transform.parent.name + "/" + renderer.gameObject.name);
+              renderer.material.color = Color.red;
+            }
+          }
+        }
+      }
+    }
+  }
+}