Hololens入門之手勢識別(使用Navigation gesture控制物體縮放)

Hololens入門之手勢識別(使用Navigation gesture控制物體縮放)
本文示例在 Hololens入門之手勢識別(手檢測反饋) 示例的基礎上進行修改Navigation gesture :保持點擊手勢,在一個標準3D立方空間內相對運動導航手勢就像一個虛擬的操縱桿,能夠用于UI控件導航,例如弧形菜單。通過點擊開始手勢,然后在以點擊處為中心的標準立方空間中移動手部。你可以沿著X、Y、Z軸移動手部,這會帶來數值-1到1的變化,初始位置的值為0.1、修改HandsManager.cs,添加InteractionManager.SourcePressed,InteractionManager.SourceReleased處理函數,用于識別物體被點擊和被釋放的事件HandsManager.cs完整代碼如下:

[csharp] view plain copy

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

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_SourceReleased(InteractionSourceState state)  
        {  
            FocusedGameObject = null;  
        }  
        //識別到手指按下時,將凝視射線關注的物體置為當前手勢操作的對象    
        private void InteractionManager_SourcePressed(InteractionSourceState state)  
        {  
            if (GazeManager.Instance.FocusedObject != null)  
            {  
                FocusedGameObject = GazeManager.Instance.FocusedObject;  
            }  
        }  
  
        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;  
        }  
  
        void OnDestroy()  
        {  
            InteractionManager.SourceDetected -= InteractionManager_SourceDetected;  
            InteractionManager.SourceLost -= InteractionManager_SourceLost;  
  
            InteractionManager.SourceReleased -= InteractionManager_SourceReleased;  
            InteractionManager.SourcePressed -= InteractionManager_SourcePressed;  
        }  
    }  
}  

2、修改GestureManager.cs,訂閱Navigation手勢事件****[csharp] view plain copy

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

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;  
        }  
  
        /// <summary>  
        /// Gets the currently focused object, or null if none.  
        /// </summary>  
        public GameObject FocusedObject  
        {  
            get { return focusedObject; }  
        }  
  
        public bool IsNavigating { get; private set; }  
        public Vector3 NavigationPosition { get; private set; }  
  
        private GestureRecognizer gestureRecognizer;  
        private GameObject focusedObject;  
  
        void Start()  
        {  
            //  創建GestureRecognizer實例    
            gestureRecognizer = new GestureRecognizer();  
            //  注冊指定的手勢類型    
            gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap  
                | GestureSettings.NavigationX);  
            //  訂閱手勢事件    
            gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;  
  
            //添加Navigation手勢事件    
            gestureRecognizer.NavigationStartedEvent += GestureRecognizer_NavigationStartedEvent;  
            gestureRecognizer.NavigationUpdatedEvent += GestureRecognizer_NavigationUpdatedEvent;  
            gestureRecognizer.NavigationCompletedEvent += GestureRecognizer_NavigationCompletedEvent;  
            gestureRecognizer.NavigationCanceledEvent += GestureRecognizer_NavigationCanceledEvent;  
            //  開始手勢識別    
            gestureRecognizer.StartCapturingGestures();  
        }  
  
        private void GestureRecognizer_NavigationCanceledEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)  
        {  
            IsNavigating = false;  
        }  
  
        private void GestureRecognizer_NavigationCompletedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)  
        {  
            IsNavigating = false;  
        }  
  
        private void GestureRecognizer_NavigationUpdatedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)  
        {  
            if (HandsManager.Instance.FocusedGameObject != null)  
            {  
                IsNavigating = true;  
                NavigationPosition = normalizedOffset;  
                HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformZoomUpdate", normalizedOffset, SendMessageOptions.DontRequireReceiver);  
            }  
        }  
  
        private void GestureRecognizer_NavigationStartedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)  
        {  
            if (HandsManager.Instance.FocusedGameObject != null)  
            {  
                IsNavigating = true;  
                NavigationPosition = normalizedOffset;  
                HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformNavigationStart", normalizedOffset, SendMessageOptions.DontRequireReceiver);  
            }  
        }  
  
        private void OnTap()  
        {  
            if (focusedObject != null)  
            {  
                focusedObject.SendMessage("OnSelect", SendMessageOptions.DontRequireReceiver);  
            }  
        }  
  
        private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)  
        {  
            OnTap();  
        }  
  
        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();  
            //}  
 
#if UNITY_EDITOR  
            if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(EditorSelectKey))  
            {  
                OnTap();  
            }  
#endif  
        }  
  
        void OnDestroy()  
        {  
            gestureRecognizer.StopCapturingGestures();  
            gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;  
            gestureRecognizer.NavigationStartedEvent -= GestureRecognizer_NavigationStartedEvent;  
            gestureRecognizer.NavigationUpdatedEvent -= GestureRecognizer_NavigationUpdatedEvent;  
            gestureRecognizer.NavigationCompletedEvent -= GestureRecognizer_NavigationCompletedEvent;  
            gestureRecognizer.NavigationCanceledEvent -= GestureRecognizer_NavigationCanceledEvent;  
        }  
    }  
}  

3、新增測試用Cube
[圖片上傳中。。。(1)]


4、新增修改CubeScript.cs,添加物體縮放函數,當選中物體,左右移動時,物體進行縮放[csharp] view plain copy

using System.Collections;  
using HoloToolkit.Unity;  
  
public class CubeScript : MonoBehaviour {  
  
    private Vector3 navigationPreviousPosition;  
    public float MaxScale = 2f;  
    public float MinScale = 0.1f;  
      
    // Use this for initialization  
    void Start () {  
      
    }  
      
    // Update is called once per frame  
    void Update () {  
      
    }  
  
    void PerformNavigationStart(Vector3 position)  
    {  
        //設置初始位置    
        navigationPreviousPosition = position;  
    }  
  
    void PerformZoomUpdate(Vector3 position)  
    {  
        if (GestureManager.Instance.IsNavigating && HandsManager.Instance.FocusedGameObject == gameObject)  
        {  
            Vector3 deltaScale = Vector3.zero;  
            float ScaleValue = 0.01f;  
            //設置每一幀縮放的大小  
            if (position.x < 0)  
            {  
                ScaleValue = -1 * ScaleValue;  
            }  
            //當縮放超出設置的最大,最小范圍時直接返回  
            if (transform.localScale.x >= MaxScale && ScaleValue > 0)  
            {  
                return;  
            }  
            else if (transform.localScale.x <= MinScale && ScaleValue < 0)  
            {  
                return;  
            }  
            //根據比例計算每個方向上的縮放大小  
            deltaScale.x = ScaleValue;  
            deltaScale.y = (transform.localScale.y / transform.localScale.x) * ScaleValue;  
            deltaScale.z = (transform.localScale.z / transform.localScale.x) * ScaleValue;  
            transform.localScale += deltaScale;  
        }  
    }  
}  

5、運行測試啟動后手勢向左滑動,物體縮小
[圖片上傳中。。。(2)]
手勢向右滑動,物體放大
[圖片上傳中。。。(3)]

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,996評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容