Hololens入门之手势识别(使用Navigation gesture控制物体缩放)

来源:互联网 发布:连云港seo服务 编辑:程序博客网 时间:2024/06/06 09:20

Hololens入门之手势识别(使用Navigation gesture控制物体缩放)

本文示例在  Hololens入门之手势识别(手检测反馈) 示例的基础上进行修改

Navigation gesture :保持点击手势,在一个标准3D立方空间内相对运动
导航手势就像一个虚拟的操纵杆,能够用于UI控件导航,例如弧形菜单。通过点击开始手势,然后在以点击处为中心的标准立方空间中移动手部。你可以沿着X、Y、Z轴移动手部,这会带来数值-1到1的变化,初始位置的值为0.
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_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手势事件
// 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>    /// 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



4、新增修改CubeScript.cs,添加物体缩放函数,当选中物体,左右移动时,物体进行缩放
using UnityEngine;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 framevoid 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 0
原创粉丝点击