天天看點

Hololens開發筆記之Gesture手勢識别(Manipulation手勢控制物體平移)

Manipulation gesture:保持點選手勢,在3D世界中絕對運動

當你想要全息圖像1:1響應使用者手部移動時,操縱手勢能被用于移動、縮放或旋轉全息圖像。如此的一個用處是使得使用者可以在世界中繪制圖像或作畫。使用所有的手勢時,操縱手勢的初始目标應該通過凝視來選中。一旦點選手勢開始,通過手部移動的任何對對象的操作都能夠被處理,在使用者操作全息圖像時,進而使得使用者得以自由地四處張望。

1、修改HandsManager.cs,添加InteractionManager.SourcePressed,InteractionManager.SourceReleased處理函數,用于識别物體被點選和被釋放的事件

HandsManager.cs完整代碼如下:

// Copyright (c) Microsoft Corporation. All rights reserved.  
// Licensed under the MIT License. See LICENSE in the project root for license information.  
  
using System.Collections.Generic;  
using UnityEngine;  
using UnityEngine.VR.WSA.Input;  
  
namespace HoloToolkit.Unity  
{  
    /// <summary>  
    /// HandsManager determines if the hand is currently detected or not.  
    /// </summary>  
    public partial class HandsManager : Singleton<HandsManager>  
    {  
        /// <summary>  
        /// HandDetected tracks the hand detected state.  
        /// Returns true if the list of tracked hands is not empty.  
        /// </summary>  
        public bool HandDetected  
        {  
            get { return trackedHands.Count > 0; }  
        }  
  
        private HashSet<uint> trackedHands = new HashSet<uint>();  
  
        public GameObject FocusedGameObject { get; private set; }  
  
        void Awake()  
        {  
            //識别到來源  
            InteractionManager.SourceDetected += InteractionManager_SourceDetected;  
            //來源丢失  
            InteractionManager.SourceLost += InteractionManager_SourceLost;  
            //來源被按下  
            InteractionManager.SourcePressed += InteractionManager_SourcePressed;  
            //被釋放  
            InteractionManager.SourceReleased += InteractionManager_SourceReleased;  
            FocusedGameObject = null;  
        }  
  
        private void InteractionManager_SourceDetected(InteractionSourceState state)  
        {  
            // Check to see that the source is a hand.  
            if (state.source.kind != InteractionSourceKind.Hand)  
            {  
                return;  
            }  
  
            trackedHands.Add(state.source.id);  
        }  
  
        private void InteractionManager_SourceLost(InteractionSourceState state)  
        {  
            // Check to see that the source is a hand.  
            if (state.source.kind != InteractionSourceKind.Hand)  
            {  
                return;  
            }  
  
            if (trackedHands.Contains(state.source.id))  
            {  
                trackedHands.Remove(state.source.id);  
            }  
            FocusedGameObject = null;  
        }  
  
        //手勢釋放時,将被關注的物體置空  
        private void InteractionManager_SourceReleased(InteractionSourceState state)  
        {  
            FocusedGameObject = null;  
        }  
  
        //識别到手指按下時,将凝視射線關注的物體置為目前手勢操作的對象  
        private void InteractionManager_SourcePressed(InteractionSourceState state)  
        {  
            if (GazeManager.Instance.FocusedObject != null)  
            {  
                FocusedGameObject = GazeManager.Instance.FocusedObject;  
            }  
        }  
  
        void OnDestroy()  
        {  
            InteractionManager.SourceDetected -= InteractionManager_SourceDetected;  
            InteractionManager.SourceLost -= InteractionManager_SourceLost;  
            InteractionManager.SourceReleased -= InteractionManager_SourceReleased;  
            InteractionManager.SourcePressed -= InteractionManager_SourcePressed;  
        }  
    }  
}        

2、修改GestureManager.cs,訂閱Manipulation gesture事件

// Copyright (c) Microsoft Corporation. All rights reserved.  
// Licensed under the MIT License. See LICENSE in the project root for license information.  
  
using System;  
using UnityEngine;  
using UnityEngine.VR.WSA.Input;  
  
namespace HoloToolkit.Unity  
{  
    /// <summary>  
    /// GestureManager creates a gesture recognizer and signs up for a tap gesture.  
    /// When a tap gesture is detected, GestureManager uses GazeManager to find the game object.  
    /// GestureManager then sends a message to that game object.  
    /// </summary>  
    [RequireComponent(typeof(GazeManager))]  
    public partial class GestureManager : Singleton<GestureManager>  
    {  
        /// <summary>  
        /// Key to press in the editor to select the currently gazed hologram  
        /// </summary>  
        public KeyCode EditorSelectKey = KeyCode.Space;  
  
        /// <summary>  
        /// To select even when a hologram is not being gazed at,  
        /// set the override focused object.  
        /// If its null, then the gazed at object will be selected.  
        /// </summary>  
        public GameObject OverrideFocusedObject  
        {  
            get; set;  
        }  
  
        public bool IsManipulating { get; private set; }  
        public Vector3 ManipulationPosition { get; private set; }  
  
        /// <summary>  
        /// Gets the currently focused object, or null if none.  
        /// </summary>  
        public GameObject FocusedObject  
        {  
            get { return focusedObject; }  
        }  
  
        private GestureRecognizer gestureRecognizer;  
        private GameObject focusedObject;  
  
        void Start()  
        {  
            // Create a new GestureRecognizer. Sign up for tapped events.  
            gestureRecognizer = new GestureRecognizer();  
            gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap  
                | GestureSettings.DoubleTap  
                | GestureSettings.ManipulationTranslate);  
  
            gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;  
            //訂閱Manipulation gesture事件  
            gestureRecognizer.ManipulationStartedEvent += GestureRecognizer_ManipulationStartedEvent;  
            gestureRecognizer.ManipulationUpdatedEvent += GestureRecognizer_ManipulationUpdatedEvent;  
            gestureRecognizer.ManipulationCompletedEvent += GestureRecognizer_ManipulationCompletedEvent;  
            gestureRecognizer.ManipulationCanceledEvent += GestureRecognizer_ManipulationCanceledEvent;  
  
            // Start looking for gestures.  
            gestureRecognizer.StartCapturingGestures();  
        }  
  
        private void GestureRecognizer_ManipulationStartedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)  
        {  
            //當被關注的對象非空時,設定初始位置  
            if (HandsManager.Instance.FocusedGameObject != null)  
            {  
                IsManipulating = true;  
  
                ManipulationPosition = cumulativeDelta;  
  
                HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformManipulationStart", cumulativeDelta);  
            }  
        }  
  
        private void GestureRecognizer_ManipulationUpdatedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)  
        {  
            //當被關注的對象非空時,更新新的位置  
            if (HandsManager.Instance.FocusedGameObject != null)  
            {  
                IsManipulating = true;  
  
                ManipulationPosition = cumulativeDelta;  
  
                HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformManipulationUpdate", cumulativeDelta);  
            }  
        }  
  
        private void GestureRecognizer_ManipulationCompletedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)  
        {  
            IsManipulating = false;  
        }  
  
        private void GestureRecognizer_ManipulationCanceledEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)  
        {  
            IsManipulating = false;  
        }  
  
        private void OnTap()  
        {  
            if (focusedObject != null)  
            {  
                focusedObject.SendMessage("OnTap");  
            }  
        }  
  
        private void OnDoubleTap()  
        {  
            if (focusedObject != null)  
            {  
                focusedObject.SendMessage("OnDoubleTap");  
            }  
        }  
  
        private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)  
        {  
            if (tapCount == 1)  
            {  
                OnTap();  
            }  
            else  
            {  
                OnDoubleTap();  
            }  
        }  
  
        void LateUpdate()  
        {  
            GameObject oldFocusedObject = focusedObject;  
  
            if (GazeManager.Instance.Hit &&  
                OverrideFocusedObject == null &&  
                GazeManager.Instance.HitInfo.collider != null)  
            {  
                // If gaze hits a hologram, set the focused object to that game object.  
                // Also if the caller has not decided to override the focused object.  
                focusedObject = GazeManager.Instance.HitInfo.collider.gameObject;  
            }  
            else  
            {  
                // If our gaze doesn't hit a hologram, set the focused object to null or override focused object.  
                focusedObject = OverrideFocusedObject;  
            }  
  
            //if (focusedObject != oldFocusedObject)  
            //{  
            //    // If the currently focused object doesn't match the old focused object, cancel the current gesture.  
            //    // Start looking for new gestures.  This is to prevent applying gestures from one hologram to another.  
            //    gestureRecognizer.CancelGestures();  
            //    gestureRecognizer.StartCapturingGestures();  
            //}  
        }  
  
        void OnDestroy()  
        {  
            gestureRecognizer.StopCapturingGestures();  
            //取消訂閱  
            gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;  
            gestureRecognizer.ManipulationStartedEvent -= GestureRecognizer_ManipulationStartedEvent;  
            gestureRecognizer.ManipulationUpdatedEvent -= GestureRecognizer_ManipulationUpdatedEvent;  
            gestureRecognizer.ManipulationCompletedEvent -= GestureRecognizer_ManipulationCompletedEvent;  
            gestureRecognizer.ManipulationCanceledEvent -= GestureRecognizer_ManipulationCanceledEvent;  
        }  
    }  
}        

3、添加ManipulationTranslate腳本,計算相對位移,然後進行更新物體的新的位置。

using UnityEngine;  
using System.Collections;  
using HoloToolkit.Unity;  
  
public class ManipulationTranslate: MonoBehaviour {  
  
    private Vector3 manipulationPreviousPosition;  
  
    // Use this for initialization  
    void Start () {  
      
    }  
      
    // Update is called once per frame  
    void Update () {  
          
    }  
  
    void PerformManipulationStart(Vector3 position)  
    {  
        //設定初始位置  
        manipulationPreviousPosition = position;  
    }  
  
    void PerformManipulationUpdate(Vector3 position)  
    {  
        if (GestureManager.Instance.IsManipulating)  
        {  
            //計算相對位移,然後更新物體的位置     
            Vector3 moveVector = Vector3.zero;  
            moveVector = position - manipulationPreviousPosition;  
            manipulationPreviousPosition = position;  
            transform.position += 2f * moveVector;  
        }  
    }        

4、運作測試

将凝視射線投射到對象上,舉起右手使食指和拇指碰觸在一起,然後進行左右移動,可以看到物體的位置将移動。

轉載于:https://www.cnblogs.com/unity3ds/p/6039475.html

繼續閱讀