Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.XR.MagicLeap;
  5.  
  6. public class HandTrackScript : MonoBehaviour {
  7. #region Public Variables
  8. public enum HandPoses { Ok, Finger, Thumb, OpenHandBack, Fist, NoPose };
  9. public HandPoses pose = HandPoses.NoPose;
  10. public Vector3[] pos;
  11. public GameObject sphereThumb, sphereIndex, sphereWrist;
  12. #endregion
  13.  
  14. #region Private Variables
  15. private MLHandKeyPose[] _gestures;
  16. #endregion
  17.  
  18. #region Unity Methods
  19. private void Awake() {
  20. MLHands.Start();
  21. _gestures = new MLHandKeyPose[5];
  22. _gestures[0] = MLHandKeyPose.Ok;
  23. _gestures[1] = MLHandKeyPose.Finger;
  24. _gestures[2] = MLHandKeyPose.OpenHandBack;
  25. _gestures[3] = MLHandKeyPose.Fist;
  26. _gestures[4] = MLHandKeyPose.Thumb;
  27. MLHands.KeyPoseManager.EnableKeyPoses(_gestures, true, false);
  28. pos = new Vector3[3];
  29. }
  30. private void OnDestroy() {
  31. MLHands.Stop();
  32. }
  33.  
  34. private void Update() {
  35. if (GetGesture(MLHands.Left, MLHandKeyPose.Ok)) {
  36. pose = HandPoses.Ok;
  37. }
  38. else if (GetGesture(MLHands.Left, MLHandKeyPose.Finger)) {
  39. pose = HandPoses.Finger;
  40. }
  41. else if (GetGesture(MLHands.Left, MLHandKeyPose.OpenHandBack)) {
  42. pose = HandPoses.OpenHandBack;
  43. }
  44. else if (GetGesture(MLHands.Left, MLHandKeyPose.Fist)) {
  45. pose = HandPoses.Fist;
  46. }
  47. else if (GetGesture(MLHands.Left, MLHandKeyPose.Thumb)) {
  48. pose = HandPoses.Thumb;
  49. }
  50. else {
  51. pose = HandPoses.NoPose;
  52. }
  53.  
  54. if (pose != HandPoses.NoPose) ShowPoints();
  55. }
  56. #endregion
  57.  
  58. #region Private Methods
  59. private void ShowPoints() {
  60. // Left Hand Thumb tip
  61. pos[0] = MLHands.Left.Thumb.KeyPoints[0].Position;
  62. // Left Hand Index finger tip
  63. pos[1] = MLHands.Left.Index.KeyPoints[0].Position;
  64. // Left Hand Wrist
  65. pos[2] = MLHands.Left.Wrist.KeyPoints[0].Position;
  66. sphereThumb.transform.position = pos[0];
  67. sphereIndex.transform.position = pos[1];
  68. sphereWrist.transform.position = pos[2];
  69. }
  70.  
  71. private bool GetGesture(MLHand hand, MLHandKeyPose type) {
  72. if (hand != null) {
  73. if (hand.KeyPose == type) {
  74. if (hand.KeyPoseConfidence > 0.9f) {
  75. return true;
  76. }
  77. }
  78. }
  79. return false;
  80. }
  81. #endregion
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement