Advertisement
plantbae

Play Video

Nov 20th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.23 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using Leap.Unity;
  4. using Leap;
  5.  
  6. [RequireComponent (typeof (AudioSource))]
  7.  
  8. /// <summary>
  9. /// Hand recognition.
  10. /// http://blog.leapmotion.com/getting-started-leap-motion-sdk/
  11. /// </summary>
  12.  
  13. public class HandRecognition : MonoBehaviour
  14. {
  15.     //Manos
  16.     public RigidHand leftHand;
  17.     public RigidHand rightHand;
  18.  
  19.     //Vectores dedos Mano Izquierda
  20.     public Vector3 indexIntBoneRotL;
  21.     public Vector3 middleIntBoneRotL;
  22.     public Vector3 ringIntBoneRotL;
  23.     public Vector3 pinkyIntBoneRotL;
  24.     public Vector3 thumbIntBoneRotL;
  25.     //Vectores dedos Mano Derecha
  26.     //public Vector3 indexIntBoneRotR;
  27.     //public Vector3 middleIntBoneRotR;
  28.     //public Vector3 ringIntBoneRotR;
  29.     //public Vector3 pinkyIntBoneRotR;
  30.     //public Vector3 thumbIntBoneRotR;
  31.  
  32.     //Vectores posición manos Derecha/izq
  33.     public Vector3 LeftHandPosition;
  34.     //public Vector3 RightHandPosition;
  35.  
  36.     //Init Variables
  37.     public float thresholdGesture = 90f;
  38.  
  39.     //Screen en el que se proyectan los videos
  40.     public MovieTexture movieGesture1;
  41.     public Transform Screen;
  42.  
  43.     void Start() //Función para inicializar
  44.     {
  45.         //Se inicializan todas las matrices en cero para corrección de posibles errores al arranque
  46.         indexIntBoneRotL = Vector3.zero;
  47.         middleIntBoneRotL = Vector3.zero;
  48.         ringIntBoneRotL = Vector3.zero;
  49.         pinkyIntBoneRotL = Vector3.zero;
  50.         thumbIntBoneRotL = Vector3.zero;
  51.         //indexIntBoneRotR = Vector3.zero;
  52.         //middleIntBoneRotR = Vector3.zero;
  53.         //ringIntBoneRotR = Vector3.zero;
  54.         //pinkyIntBoneRotR = Vector3.zero;
  55.         //thumbIntBoneRotR = Vector3.zero;
  56.         //LeftHandPosition = Vector3.zero;
  57.         //RightHandPosition = Vector3.zero;
  58.  
  59.         Screen.GetComponent<Renderer>().material.mainTexture = movieGesture1;
  60.         ((MovieTexture)Screen.GetComponent<Renderer>().material.mainTexture).Play();
  61.        
  62.     }
  63.     /*
  64.      *
  65.      * TYPE_THUMB = = 0 -
  66.      * TYPE_INDEX = = 1 -
  67.      * TYPE_MIDDLE = = 2 -
  68.      * TYPE_RING = = 3 -
  69.      * TYPE_PINKY = = 4 -
  70.      *
  71.      *
  72.      *
  73.     */
  74.  
  75.     void Update() // Update se llama una vez por frame
  76.     {
  77.         //Llenar los vectores de los dedos con los grados de separación entre falange media y proximal
  78.         indexIntBoneRotL = leftHand.fingers[1].GetBoneRotation(2).eulerAngles;
  79.         middleIntBoneRotL = leftHand.fingers[2].GetBoneRotation(2).eulerAngles;
  80.         ringIntBoneRotL = leftHand.fingers[3].GetBoneRotation(2).eulerAngles;
  81.         pinkyIntBoneRotL = leftHand.fingers[4].GetBoneRotation(2).eulerAngles;
  82.         thumbIntBoneRotL = leftHand.fingers[0].GetBoneRotation(2).eulerAngles;
  83.         //indexIntBoneRotR = rightHand.fingers[1].GetBoneRotation(2).eulerAngles;
  84.         //middleIntBoneRotR = rightHand.fingers[2].GetBoneRotation(2).eulerAngles;
  85.         //ringIntBoneRotR = rightHand.fingers[3].GetBoneRotation(2).eulerAngles;
  86.         //pinkyIntBoneRotR = rightHand.fingers[4].GetBoneRotation(2).eulerAngles;
  87.         //thumbIntBoneRotR = rightHand.fingers[0].GetBoneRotation(2).eulerAngles;
  88.  
  89.         // indexIntBonePy = leftHand.palm.localPosition;
  90.  
  91.         //Número que determina qué gesto fue aplicado. Con este se selecciona el video que se reproducirá en el screen
  92.         int gestureId = GestureRecognized();
  93.  
  94.         if (gestureId == 1) //Acicalar de lado a lado. Dado por posición de la muñeca en x [-0.1, 0.1]
  95.         {
  96.             /*
  97.                Debug.Log ("Gesto 1 reconocido!");
  98.                Debug.Log(indexIntBonePy);
  99.                Screen.GetComponent<Renderer> ().material.mainTexture = movieGesture1;
  100.                ((MovieTexture)Screen.GetComponent<Renderer> ().material.mainTexture).Play();
  101.                Gato has muecas
  102.                print
  103.             */
  104.         }
  105.         if (gestureId == 2) //Rascar mentón. Dado por palma hacia arriba y dedos 'rascando'
  106.         {
  107.             Debug.Log("Gesto 2 reconocido! Consintiendo mentón");
  108.             Debug.Log(leftHand.GetPalmNormal().y);
  109.            
  110.            
  111.             // play video 2
  112.         }
  113.         if (gestureId == 3) //Rascar cabeza. Dado por palma hacia abajo y dedos 'rascando'
  114.         {
  115.             Debug.Log("Gesto 3 reconocido! Consintiendo cabeza");
  116.             Debug.Log(leftHand.GetPalmNormal().y);
  117.             //
  118.         }
  119.         if (gestureId == 4) //Dar pata. Dado por mano de lado.
  120.         {
  121.             Debug.Log("Gesto 4 reconocido! Dar Pata");
  122.             Debug.Log(leftHand.GetPalmNormal().y);
  123.             //
  124.         }
  125.         if (gestureId == -1)//Posición de reposo. Dado por inexistencia de gestos en cercanías a cámara
  126.         {
  127.             Debug.Log("Nada :v");
  128.             //
  129.         }
  130.     }
  131.  
  132.     int GestureRecognized()
  133.     {
  134.         if (indexIntBoneRotL.x == 190)
  135.         {
  136.             return 1;
  137.         }
  138.  
  139.         else if (leftHand.GetPalmNormal().y >= 0.6) //mentón
  140.         {
  141.             return 2;
  142.         }
  143.         else if (leftHand.GetPalmNormal().y <= -0.6) //cabeza
  144.         {
  145.             return 3;
  146.         }
  147.         else if (leftHand.GetPalmNormal().y < 0.6 && leftHand.GetPalmNormal().y > -0.6) //pata
  148.         {
  149.             return 4;
  150.  
  151.         }
  152.         return -1;
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement