SHARE
TWEET

Untitled

a guest May 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.MagicLeap;
  5. using UnityEngine.UI;
  6.  
  7. public class GestureScript : MonoBehaviour
  8. {
  9.     private bool OKHandPoseDetected = false;
  10.     private float speed = 30.0f;  // Speed of our cube
  11.     private float distance = 2.0f; // Distance between Main Camera and Cube
  12.     private GameObject cube; // Reference to our Cube
  13.     private List<MLHandKeyPose> gestures; // Holds the different gestures we will look for
  14.  
  15.     private static bool awakened = false;
  16.    
  17.     // The default DirectionalLight only illuminates half the scene. Objects
  18.     // that fall in the other half do not get lit and appear invisible in
  19.     // additive HMDs. This boolean will auto-add a Point Light at the center
  20.     // of the scene to help with global illumination, but set it to false if
  21.     // the Scene already contains proper lighting.
  22.     //
  23.     public bool CreatePointLight = true;
  24.  
  25.     void Awake()
  26.     {
  27.         if (awakened)
  28.         {
  29.             ShowMessage("Multiple Gesture Scripts detected. Please only use one. This one is on GameObject \"" + name + "\"");
  30.             return;
  31.         }
  32.         awakened = true;
  33.  
  34.         if (Camera.main == null)
  35.         {
  36.             ShowMessage("No MainCamera object in the scene. Please replace the scene camera with the \"Main Camera\" prefab from " +
  37.                 "Assets\\MagicLeap\\CoreComponents");
  38.             return;
  39.         }
  40.  
  41.         if (Camera.main.GetComponent<UnityEngine.XR.MagicLeap.Rendering.MagicLeapCamera>() == null)
  42.         {
  43.             ShowMessage("No MagicLeapCamera component attached to the Main Camera. Please replace the scene camera with the \"Main Camera\" prefab from " +
  44.                 "Assets\\MagicLeap\\CoreComponents");
  45.             return;
  46.         }
  47.  
  48.         // First find and hide the cube
  49.         cube = GameObject.Find("Cube");
  50.         if (cube == null)
  51.         {
  52.             ShowMessage("Could not find GameObject named \"Cube\". Please add a " +
  53.                 "3d model to the hierarchy and name it \"Cube\" (no quotes)");
  54.             return;
  55.         }
  56.         cube.SetActive(false);
  57.  
  58.         if (CreatePointLight)
  59.         {
  60.             GameObject pointLight = new GameObject("Light");
  61.             pointLight.AddComponent<Light>();
  62.         }
  63.  
  64.         // Then verify gestures are available
  65.         if (!MLHands.Start().IsOk)
  66.         {
  67. //#if !UNITY_EDITOR
  68.             ShowMessage("Gesture permissions not granted. Please relocate the manifest from the default location " +
  69.                 "Assets\\MagicLeap\\Examples\\Plugins\\Lumin\\manifest.xml to Assets\\Plugins\\Lumin\\manifest.xml");
  70. //#endif
  71.             return;
  72.         }
  73.  
  74.         gestures = new List<MLHandKeyPose>();
  75.  
  76.         // To recognize more gestures, add to this list.
  77.         gestures.Add(MLHandKeyPose.Ok);
  78.         gestures.Add(MLHandKeyPose.Fist);
  79.         gestures.Add(MLHandKeyPose.OpenHandBack);
  80.         gestures.Add(MLHandKeyPose.Finger);
  81.         gestures.Add(MLHandKeyPose.L);
  82.  
  83.         MLHands.KeyPoseManager.EnableKeyPoses(gestures.ToArray(), true, false);
  84.         ShowMessage("Ready for OK Gesture");
  85.     }
  86.  
  87.     private void OnDisable()
  88.     {
  89.         if (this.name.Equals("Cube"))
  90.         {
  91.             ShowMessage("Please do not put this script directly on the Cube object.");
  92.         }
  93.     }
  94.  
  95.     void OnDestroy()
  96.     {
  97.         if (MLHands.IsStarted)
  98.         {
  99.             MLHands.Stop();
  100.         }
  101.     }
  102.  
  103.     void Update()
  104.     {
  105.         // Do nothing if the cube does not exist
  106.         if (cube == null)
  107.         {
  108.             return;
  109.         }
  110.  
  111.         // If the OK HandPose was detected, check for pose modifiers
  112.         if (OKHandPoseDetected)
  113.         {
  114.             // Open Hand Back (Shift-B in Editor): Either hand rotates counter-clockwise around the Y-Axis.
  115.             if ((GetLeftHandGesture(MLHandKeyPose.OpenHandBack) || GetRightHandGesture(MLHandKeyPose.OpenHandBack)))
  116.             {
  117.                 cube.transform.Rotate(Vector3.up, +speed * Time.deltaTime);
  118.             }
  119.  
  120.             // Fist (Shift-G in Editor): Either hand rotates clockwise around the Y-Axis.
  121.             if (GetLeftHandGesture(MLHandKeyPose.Fist) || GetRightHandGesture(MLHandKeyPose.Fist))
  122.                 cube.transform.Rotate(Vector3.up, -speed * Time.deltaTime);
  123.  
  124.             // Finger (LShift-F in Editor): Left hand rotates down under the X-Axis.
  125.             if (GetLeftHandGesture(MLHandKeyPose.Finger))
  126.                 cube.transform.Rotate(Vector3.right, +speed * Time.deltaTime);
  127.  
  128.             // Finger (RShift-F in Editor): Right hand rotates up over the X-Axis.
  129.             if (GetRightHandGesture(MLHandKeyPose.Finger))
  130.                 cube.transform.Rotate(Vector3.right, -speed * Time.deltaTime);
  131.  
  132.             // Add other responses here as desired, making sure to add their recognition in Awake()
  133.  
  134.             // L with both hands resets the rotation
  135.             if ((GetLeftHandGesture(MLHandKeyPose.L) && GetRightHandGesture(MLHandKeyPose.L)))
  136.             {
  137.                 cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward * distance;
  138.                 cube.transform.rotation = Camera.main.transform.rotation;
  139.             }
  140.  
  141.         }
  142.         else
  143.         {
  144.             if (GetLeftHandGesture(MLHandKeyPose.Ok) || GetRightHandGesture(MLHandKeyPose.Ok))
  145.             {
  146.                 OKHandPoseDetected = true;
  147.                 cube.SetActive(true);
  148.                 cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward * distance;
  149.                 cube.transform.rotation = Camera.main.transform.rotation;
  150.  
  151.                 if (_canvasObject != null)
  152.                 {
  153.                     _canvasObject.SetActive(false);
  154.                 }
  155.             }
  156.         }
  157.     }
  158.  
  159.     bool GetLeftHandGesture(MLHandKeyPose type)
  160.     {
  161.         return GetGesture(false, type);
  162.     }
  163.  
  164.     bool GetRightHandGesture(MLHandKeyPose type)
  165.     {
  166.         return GetGesture(true, type);
  167.     }
  168.  
  169.     bool GetGesture(bool handIsRight, MLHandKeyPose type)
  170.     {
  171. #if UNITY_EDITOR
  172.         if (GetEditorGesture(handIsRight, type))
  173.         {
  174.             return true;
  175.         }
  176. #endif
  177.         if (!MLHands.IsStarted)
  178.         {
  179.             return false;
  180.         }
  181.  
  182.         MLHand hand = handIsRight ? MLHands.Right : MLHands.Left;
  183.         if (hand != null)
  184.         {
  185.             if (hand.KeyPose == type)
  186.             {
  187.                 if (hand.KeyPoseConfidence > 0.9f)
  188.                 {
  189.                     return true;
  190.                 }
  191.             }
  192.         }
  193.         return false;
  194.     }
  195.  
  196.  
  197.     /* Only runs in the Unity Editor as a way to test code prior to headset deployment.
  198.      *
  199.      *  Allows you to hold left shift and type in a corresponding keycode to implement
  200.      *  a left-hand gesture, or right shift to do that gesture with the right hand.
  201.      *
  202.      *  Hold both shift keys to do the same gesture with both hands.
  203.      *
  204.      * Note that this example does not allow for both hands to be doing different
  205.      *  identified key poses in the editor, but the deployed app will be able to do
  206.      *  so on the headset.    
  207.      */
  208.     bool GetEditorGesture(bool handIsRight, MLHandKeyPose type)
  209.     {
  210. #if UNITY_EDITOR
  211.         if (!handIsRight && !Input.GetKey(KeyCode.LeftShift))
  212.         {
  213.             return false;
  214.         }
  215.  
  216.         if (handIsRight && !Input.GetKey(KeyCode.RightShift))
  217.         {
  218.             return false;
  219.         }
  220.  
  221.         switch (type)
  222.         {
  223.             case MLHandKeyPose.C:
  224.                 return Input.GetKey(KeyCode.C);
  225.  
  226.             case MLHandKeyPose.Finger:
  227.                 return Input.GetKey(KeyCode.F);
  228.  
  229.             case MLHandKeyPose.Fist:
  230.                 return Input.GetKey(KeyCode.G);
  231.  
  232.             case MLHandKeyPose.L:
  233.                 return Input.GetKey(KeyCode.L);
  234.  
  235.             case MLHandKeyPose.NoPose:
  236.                 return Input.GetKey(KeyCode.X);
  237.  
  238.             case MLHandKeyPose.Ok:
  239.                 return Input.GetKey(KeyCode.O);
  240.  
  241.             case MLHandKeyPose.OpenHandBack:
  242.                 return Input.GetKey(KeyCode.B);
  243.  
  244.             case MLHandKeyPose.Pinch:
  245.                 return Input.GetKey(KeyCode.P);
  246.  
  247.             case MLHandKeyPose.Thumb:
  248.                 return Input.GetKey(KeyCode.T);
  249.         }
  250. #endif
  251.         return type == MLHandKeyPose.NoHand;
  252.     }
  253.  
  254.  
  255.     /* The below manages errors by dynamically constructing a canvas
  256.      * to display an error to the user when wearing the Leap.
  257.      *
  258.      * It can be ignored for the purpose of this lesson.
  259.      */
  260.     GameObject _canvasObject;
  261.     GameObject CanvasObject
  262.     {
  263.         get
  264.         {
  265.             if (_canvasObject == null)
  266.             {
  267.                 Vector3 forward = Camera.main.transform.forward;
  268.                 forward.y = 0;
  269.  
  270.                 _canvasObject = new GameObject("World Canvas");
  271.                 Canvas canvas = _canvasObject.AddComponent<Canvas>();
  272.                 canvas.renderMode = RenderMode.WorldSpace;
  273.                 canvas.GetComponent<RectTransform>().position = forward.normalized * 2;
  274.                 canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(840, 600);
  275.                 canvas.GetComponent<RectTransform>().localScale = Vector3.one * .001f;
  276.                 canvas.transform.SetParent(Camera.main.transform, false);
  277.  
  278.                 RawImage background = canvas.gameObject.AddComponent<RawImage>();
  279.                 background.color = new Color32(20, 20, 20, 128);
  280.  
  281.                 GameObject textObject = new GameObject("Text");
  282.                 Text text = textObject.AddComponent<Text>();
  283.                 text.GetComponent<RectTransform>().sizeDelta = new Vector2(720, 480);
  284.                 text.color = Color.white;
  285.                 text.resizeTextForBestFit = true;
  286.                 text.resizeTextMaxSize = 48;
  287.                 text.resizeTextMinSize = 24;
  288.                 text.alignment = TextAnchor.MiddleLeft;
  289.  
  290.                 text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
  291.  
  292.                 textObject.transform.SetParent(canvas.transform, false);
  293.             }
  294.             return _canvasObject;
  295.         }
  296.     }
  297.  
  298.     void ShowMessage(string message)
  299.     {
  300.         CanvasObject.GetComponentInChildren<Text>().text = message;
  301.         CanvasObject.SetActive(true);
  302.     }
  303. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top