当前位置:   article > 正文

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

hololens滑动

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

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

Navigation gesture :保持点击手势,在一个标准3D立方空间内相对运动
导航手势就像一个虚拟的操纵杆,能够用于UI控件导航,例如弧形菜单。通过点击开始手势,然后在以点击处为中心的标准立方空间中移动手部。你可以沿着X、Y、Z轴移动手部,这会带来数值-1到1的变化,初始位置的值为0.
1、修改HandsManager.cs,添加InteractionManager.SourcePressed,InteractionManager.SourceReleased处理函数,用于识别物体被点击和被释放的事件

HandsManager.cs完整代码如下:

  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.VR.WSA.Input;
  6. namespace HoloToolkit.Unity
  7. {
  8. /// <summary>
  9. /// HandsManager determines if the hand is currently detected or not.
  10. /// </summary>
  11. public partial class HandsManager : Singleton<HandsManager>
  12. {
  13. /// <summary>
  14. /// HandDetected tracks the hand detected state.
  15. /// Returns true if the list of tracked hands is not empty.
  16. /// </summary>
  17. public bool HandDetected
  18. {
  19. get { return trackedHands.Count > 0; }
  20. }
  21. private HashSet<uint> trackedHands = new HashSet<uint>();
  22. public GameObject FocusedGameObject { get; private set; }
  23. void Awake()
  24. {
  25. InteractionManager.SourceDetected += InteractionManager_SourceDetected;
  26. InteractionManager.SourceLost += InteractionManager_SourceLost;
  27. //来源被按下
  28. InteractionManager.SourcePressed += InteractionManager_SourcePressed;
  29. //被释放
  30. InteractionManager.SourceReleased += InteractionManager_SourceReleased;
  31. FocusedGameObject = null;
  32. }
  33. //手势释放时,将被关注的物体置空
  34. private void InteractionManager_SourceReleased(InteractionSourceState state)
  35. {
  36. FocusedGameObject = null;
  37. }
  38. //识别到手指按下时,将凝视射线关注的物体置为当前手势操作的对象
  39. private void InteractionManager_SourcePressed(InteractionSourceState state)
  40. {
  41. if (GazeManager.Instance.FocusedObject != null)
  42. {
  43. FocusedGameObject = GazeManager.Instance.FocusedObject;
  44. }
  45. }
  46. private void InteractionManager_SourceDetected(InteractionSourceState state)
  47. {
  48. // Check to see that the source is a hand.
  49. if (state.source.kind != InteractionSourceKind.Hand)
  50. {
  51. return;
  52. }
  53. trackedHands.Add(state.source.id);
  54. }
  55. private void InteractionManager_SourceLost(InteractionSourceState state)
  56. {
  57. // Check to see that the source is a hand.
  58. if (state.source.kind != InteractionSourceKind.Hand)
  59. {
  60. return;
  61. }
  62. if (trackedHands.Contains(state.source.id))
  63. {
  64. trackedHands.Remove(state.source.id);
  65. }
  66. FocusedGameObject = null;
  67. }
  68. void OnDestroy()
  69. {
  70. InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
  71. InteractionManager.SourceLost -= InteractionManager_SourceLost;
  72. InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
  73. InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
  74. }
  75. }
  76. }
2、修改GestureManager.cs,订阅Navigation手势事件
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. using UnityEngine;
  4. using UnityEngine.VR.WSA.Input;
  5. namespace HoloToolkit.Unity
  6. {
  7. /// <summary>
  8. /// GestureManager creates a gesture recognizer and signs up for a tap gesture.
  9. /// When a tap gesture is detected, GestureManager uses GazeManager to find the game object.
  10. /// GestureManager then sends a message to that game object.
  11. /// </summary>
  12. [RequireComponent(typeof(GazeManager))]
  13. public partial class GestureManager : Singleton<GestureManager>
  14. {
  15. /// <summary>
  16. /// Key to press in the editor to select the currently gazed hologram
  17. /// </summary>
  18. public KeyCode EditorSelectKey = KeyCode.Space;
  19. /// <summary>
  20. /// To select even when a hologram is not being gazed at,
  21. /// set the override focused object.
  22. /// If its null, then the gazed at object will be selected.
  23. /// </summary>
  24. public GameObject OverrideFocusedObject
  25. {
  26. get; set;
  27. }
  28. /// <summary>
  29. /// Gets the currently focused object, or null if none.
  30. /// </summary>
  31. public GameObject FocusedObject
  32. {
  33. get { return focusedObject; }
  34. }
  35. public bool IsNavigating { get; private set; }
  36. public Vector3 NavigationPosition { get; private set; }
  37. private GestureRecognizer gestureRecognizer;
  38. private GameObject focusedObject;
  39. void Start()
  40. {
  41. // 创建GestureRecognizer实例
  42. gestureRecognizer = new GestureRecognizer();
  43. // 注册指定的手势类型
  44. gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
  45. | GestureSettings.NavigationX);
  46. // 订阅手势事件
  47. gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;
  48. //添加Navigation手势事件
  49. gestureRecognizer.NavigationStartedEvent += GestureRecognizer_NavigationStartedEvent;
  50. gestureRecognizer.NavigationUpdatedEvent += GestureRecognizer_NavigationUpdatedEvent;
  51. gestureRecognizer.NavigationCompletedEvent += GestureRecognizer_NavigationCompletedEvent;
  52. gestureRecognizer.NavigationCanceledEvent += GestureRecognizer_NavigationCanceledEvent;
  53. // 开始手势识别
  54. gestureRecognizer.StartCapturingGestures();
  55. }
  56. private void GestureRecognizer_NavigationCanceledEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  57. {
  58. IsNavigating = false;
  59. }
  60. private void GestureRecognizer_NavigationCompletedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  61. {
  62. IsNavigating = false;
  63. }
  64. private void GestureRecognizer_NavigationUpdatedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  65. {
  66. if (HandsManager.Instance.FocusedGameObject != null)
  67. {
  68. IsNavigating = true;
  69. NavigationPosition = normalizedOffset;
  70. HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformZoomUpdate", normalizedOffset, SendMessageOptions.DontRequireReceiver);
  71. }
  72. }
  73. private void GestureRecognizer_NavigationStartedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  74. {
  75. if (HandsManager.Instance.FocusedGameObject != null)
  76. {
  77. IsNavigating = true;
  78. NavigationPosition = normalizedOffset;
  79. HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformNavigationStart", normalizedOffset, SendMessageOptions.DontRequireReceiver);
  80. }
  81. }
  82. private void OnTap()
  83. {
  84. if (focusedObject != null)
  85. {
  86. focusedObject.SendMessage("OnSelect", SendMessageOptions.DontRequireReceiver);
  87. }
  88. }
  89. private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
  90. {
  91. OnTap();
  92. }
  93. void LateUpdate()
  94. {
  95. GameObject oldFocusedObject = focusedObject;
  96. if (GazeManager.Instance.Hit &&
  97. OverrideFocusedObject == null &&
  98. GazeManager.Instance.HitInfo.collider != null)
  99. {
  100. // If gaze hits a hologram, set the focused object to that game object.
  101. // Also if the caller has not decided to override the focused object.
  102. focusedObject = GazeManager.Instance.HitInfo.collider.gameObject;
  103. }
  104. else
  105. {
  106. // If our gaze doesn't hit a hologram, set the focused object to null or override focused object.
  107. focusedObject = OverrideFocusedObject;
  108. }
  109. //if (focusedObject != oldFocusedObject)
  110. //{
  111. // // If the currently focused object doesn't match the old focused object, cancel the current gesture.
  112. // // Start looking for new gestures. This is to prevent applying gestures from one hologram to another.
  113. // gestureRecognizer.CancelGestures();
  114. // gestureRecognizer.StartCapturingGestures();
  115. //}
  116. #if UNITY_EDITOR
  117. if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(EditorSelectKey))
  118. {
  119. OnTap();
  120. }
  121. #endif
  122. }
  123. void OnDestroy()
  124. {
  125. gestureRecognizer.StopCapturingGestures();
  126. gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;
  127. gestureRecognizer.NavigationStartedEvent -= GestureRecognizer_NavigationStartedEvent;
  128. gestureRecognizer.NavigationUpdatedEvent -= GestureRecognizer_NavigationUpdatedEvent;
  129. gestureRecognizer.NavigationCompletedEvent -= GestureRecognizer_NavigationCompletedEvent;
  130. gestureRecognizer.NavigationCanceledEvent -= GestureRecognizer_NavigationCanceledEvent;
  131. }
  132. }
  133. }

3、新增测试用Cube



4、新增修改CubeScript.cs,添加物体缩放函数,当选中物体,左右移动时,物体进行缩放
  1. using UnityEngine;
  2. using System.Collections;
  3. using HoloToolkit.Unity;
  4. public class CubeScript : MonoBehaviour {
  5. private Vector3 navigationPreviousPosition;
  6. public float MaxScale = 2f;
  7. public float MinScale = 0.1f;
  8. // Use this for initialization
  9. void Start () {
  10. }
  11. // Update is called once per frame
  12. void Update () {
  13. }
  14. void PerformNavigationStart(Vector3 position)
  15. {
  16. //设置初始位置
  17. navigationPreviousPosition = position;
  18. }
  19. void PerformZoomUpdate(Vector3 position)
  20. {
  21. if (GestureManager.Instance.IsNavigating && HandsManager.Instance.FocusedGameObject == gameObject)
  22. {
  23. Vector3 deltaScale = Vector3.zero;
  24. float ScaleValue = 0.01f;
  25. //设置每一帧缩放的大小
  26. if (position.x < 0)
  27. {
  28. ScaleValue = -1 * ScaleValue;
  29. }
  30. //当缩放超出设置的最大,最小范围时直接返回
  31. if (transform.localScale.x >= MaxScale && ScaleValue > 0)
  32. {
  33. return;
  34. }
  35. else if (transform.localScale.x <= MinScale && ScaleValue < 0)
  36. {
  37. return;
  38. }
  39. //根据比例计算每个方向上的缩放大小
  40. deltaScale.x = ScaleValue;
  41. deltaScale.y = (transform.localScale.y / transform.localScale.x) * ScaleValue;
  42. deltaScale.z = (transform.localScale.z / transform.localScale.x) * ScaleValue;
  43. transform.localScale += deltaScale;
  44. }
  45. }
  46. }

5、运行测试

启动后手势向左滑动,物体缩小


手势向右滑动,物体放大


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/89388
推荐阅读
相关标签
  

闽ICP备14008679号