0817 : Hand Tracking
* 지난 주 요약
1. 큐브
2. 비둘기
3. 손 트레킹
* 이번 주 할 일
1. 비둘기 밥주기
canIGrab 랑 canIPlace 기능이 뭔지 까먹었어,,.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.MagicLeap; public class HandTrackScript : MonoBehaviour { public enum HandPoses { Ok, Finger, Thumb, OpenHand, Pinch, NoPose }; public HandPoses pose = HandPoses.NoPose; public Vector3[] pos; public GameObject sphereThumb, sphereIndex, sphereWrist, ObjectToPlace; private MLHandTracking.HandKeyPose[] _gestures; bool canIPlace = false; bool canIGrab = false; public GameObject selectedGameObject; private void Start() { MLHandTracking.Start(); _gestures = new MLHandTracking.HandKeyPose[5]; _gestures[0] = MLHandTracking.HandKeyPose.Ok; _gestures[1] = MLHandTracking.HandKeyPose.Finger; _gestures[2] = MLHandTracking.HandKeyPose.OpenHand; _gestures[3] = MLHandTracking.HandKeyPose.Fist; _gestures[4] = MLHandTracking.HandKeyPose.Thumb; MLHandTracking.KeyPoseManager.EnableKeyPoses(_gestures, true, false); pos = new Vector3[3]; } private void OnDestroy() { MLHandTracking.Stop(); } private void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Interactable") { if (canIGrab == false) { selectedGameObject = other.gameObject; canIGrab = true; other.gameObject.GetComponent<Renderer>().material.color = Color.red; } } } private void OnTriggerExit(Collider other) { if (other.gameObject.tag == "Interactable") { canIGrab = false; other.gameObject.GetComponent<Renderer>().material.color = Color.white; } } private void Update() { ShowPoints(); transform.position = pos[1]; if (GetGesture(MLHandTracking.Left, MLHandTracking.HandKeyPose.Ok)) { pose = HandPoses.Ok; } else if (GetGesture(MLHandTracking.Left, MLHandTracking.HandKeyPose.Finger)) { pose = HandPoses.Finger; } else if (GetGesture(MLHandTracking.Left, MLHandTracking.HandKeyPose.OpenHand)) { pose = HandPoses.OpenHand; } else if (GetGesture(MLHandTracking.Left, MLHandTracking.HandKeyPose.Pinch)) { pose = HandPoses.Pinch; if (canIGrab) { selectedGameObject.transform.position = transform.position; selectedGameObject.GetComponent<SphereCollider>().radius = 0.1f; } } else if (GetGesture(MLHandTracking.Left, MLHandTracking.HandKeyPose.Thumb)) { pose = HandPoses.Thumb; if (canIPlace == true) { GameObject placedObject = Instantiate(ObjectToPlace, pos[0], transform.rotation); canIPlace = false; } } else { pose = HandPoses.NoPose; canIPlace = true; if (selectedGameObject && canIGrab == false) { selectedGameObject = null; gameObject.GetComponent<SphereCollider>().radius = 0.01f; } } } private void ShowPoints() { // Left Hand Thumb tip pos[0] = MLHandTracking.Left.Thumb.KeyPoints[2].Position; // Left Hand Index finger tip ./ pos[1] = MLHandTracking.Left.Index.KeyPoints[2].Position; // Left Hand Wrist pos[2] = MLHandTracking.Left.Wrist.KeyPoints[0].Position; sphereThumb.transform.position = pos[0]; sphereIndex.transform.position = pos[1]; sphereWrist.transform.position = pos[2]; } private bool GetGesture(MLHandTracking.Hand hand, MLHandTracking.HandKeyPose type) { if (hand != null) { if (hand.KeyPose == type) { if (hand.HandKeyPoseConfidence > 0.9f) { return true; } } } return false; } }
댓글
댓글 쓰기