当前位置:   article > 正文

Hololens入门之手势识别(使用Navigation gesture控制物体旋转)_normalizedoffset

normalizedoffset

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;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.VR.WSA.Input;
  7. namespace HoloToolkit.Unity
  8. {
  9. /// <summary>
  10. /// HandsManager determines if the hand is currently detected or not.
  11. /// </summary>
  12. public partial class HandsManager : Singleton<HandsManager>
  13. {
  14. /// <summary>
  15. /// HandDetected tracks the hand detected state.
  16. /// Returns true if the list of tracked hands is not empty.
  17. /// </summary>
  18. public bool HandDetected
  19. {
  20. get { return trackedHands.Count > 0; }
  21. }
  22. private HashSet<uint> trackedHands = new HashSet<uint>();
  23. public GameObject FocusedGameObject { get; private set; }
  24. void Awake()
  25. {
  26. //识别到来源
  27. InteractionManager.SourceDetected += InteractionManager_SourceDetected;
  28. //来源丢失
  29. InteractionManager.SourceLost += InteractionManager_SourceLost;
  30. //来源被按下
  31. InteractionManager.SourcePressed += InteractionManager_SourcePressed;
  32. //被释放
  33. InteractionManager.SourceReleased += InteractionManager_SourceReleased;
  34. FocusedGameObject = null;
  35. }
  36. //手势释放时,将被关注的物体置空
  37. private void InteractionManager_SourceReleased(InteractionSourceState state)
  38. {
  39. FocusedGameObject = null;
  40. }
  41. //识别到手指按下时,将凝视射线关注的物体置为当前手势操作的对象
  42. private void InteractionManager_SourcePressed(InteractionSourceState state)
  43. {
  44. if (GazeManager.Instance.FocusedObject != null)
  45. {
  46. FocusedGameObject = GazeManager.Instance.FocusedObject;
  47. }
  48. }
  49. private void InteractionManager_SourceDetected(InteractionSourceState state)
  50. {
  51. // 检测来源是否为手,如果是手则加入跟踪集合
  52. if (state.source.kind != InteractionSourceKind.Hand)
  53. {
  54. return;
  55. }
  56. trackedHands.Add(state.source.id);
  57. }
  58. private void InteractionManager_SourceLost(InteractionSourceState state)
  59. {
  60. // 检测丢失的来源是否为手,如果是手则从跟踪集合中去除
  61. if (state.source.kind != InteractionSourceKind.Hand)
  62. {
  63. return;
  64. }
  65. if (trackedHands.Contains(state.source.id))
  66. {
  67. trackedHands.Remove(state.source.id);
  68. }
  69. FocusedGameObject = null;
  70. }
  71. void OnDestroy()
  72. {
  73. InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
  74. InteractionManager.SourceLost -= InteractionManager_SourceLost;
  75. InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
  76. InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
  77. }
  78. }
  79. }

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 System;
  4. using UnityEngine;
  5. using UnityEngine.VR.WSA.Input;
  6. namespace HoloToolkit.Unity
  7. {
  8. /// <summary>
  9. /// GestureManager creates a gesture recognizer and signs up for a tap gesture.
  10. /// When a tap gesture is detected, GestureManager uses GazeManager to find the game object.
  11. /// GestureManager then sends a message to that game object.
  12. /// </summary>
  13. [RequireComponent(typeof(GazeManager))]
  14. public partial class GestureManager : Singleton<GestureManager>
  15. {
  16. /// <summary>
  17. /// Key to press in the editor to select the currently gazed hologram
  18. /// </summary>
  19. public KeyCode EditorSelectKey = KeyCode.Space;
  20. /// <summary>
  21. /// To select even when a hologram is not being gazed at,
  22. /// set the override focused object.
  23. /// If its null, then the gazed at object will be selected.
  24. /// </summary>
  25. public GameObject OverrideFocusedObject
  26. {
  27. get; set;
  28. }
  29. /// <summary>
  30. /// Gets the currently focused object, or null if none.
  31. /// </summary>
  32. public GameObject FocusedObject
  33. {
  34. get { return focusedObject; }
  35. }
  36. private GestureRecognizer gestureRecognizer;
  37. private GameObject focusedObject;
  38. public bool IsNavigating { get; private set; }
  39. public Vector3 NavigationPosition { get; private set; }
  40. void Start()
  41. {
  42. // 创建GestureRecognizer实例
  43. gestureRecognizer = new GestureRecognizer();
  44. // 注册指定的手势类型
  45. gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
  46. | GestureSettings.DoubleTap
  47. | GestureSettings.NavigationX);
  48. // 订阅手势事件
  49. gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;
  50. //添加Navigation手势事件
  51. gestureRecognizer.NavigationStartedEvent += GestureRecognizer_NavigationStartedEvent;
  52. gestureRecognizer.NavigationUpdatedEvent += GestureRecognizer_NavigationUpdatedEvent;
  53. gestureRecognizer.NavigationCompletedEvent += GestureRecognizer_NavigationCompletedEvent;
  54. gestureRecognizer.NavigationCanceledEvent += GestureRecognizer_NavigationCanceledEvent;
  55. // 开始手势识别
  56. gestureRecognizer.StartCapturingGestures();
  57. }
  58. private void GestureRecognizer_NavigationCanceledEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  59. {
  60. IsNavigating = false;
  61. }
  62. private void GestureRecognizer_NavigationCompletedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  63. {
  64. IsNavigating = false;
  65. }
  66. private void GestureRecognizer_NavigationUpdatedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  67. {
  68. IsNavigating = true;
  69. //偏移量
  70. NavigationPosition = normalizedOffset;
  71. }
  72. private void GestureRecognizer_NavigationStartedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
  73. {
  74. IsNavigating = true;
  75. NavigationPosition = normalizedOffset;
  76. }
  77. private void OnTap()
  78. {
  79. if (focusedObject != null)
  80. {
  81. focusedObject.SendMessage("OnTap");
  82. }
  83. }
  84. private void OnDoubleTap()
  85. {
  86. if (focusedObject != null)
  87. {
  88. focusedObject.SendMessage("OnDoubleTap");
  89. }
  90. }
  91. private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
  92. {
  93. if (tapCount == 1)
  94. {
  95. OnTap();
  96. }
  97. else
  98. {
  99. OnDoubleTap();
  100. }
  101. }
  102. void LateUpdate()
  103. {
  104. GameObject oldFocusedObject = focusedObject;
  105. if (GazeManager.Instance.Hit &&
  106. OverrideFocusedObject == null &&
  107. GazeManager.Instance.HitInfo.collider != null)
  108. {
  109. // If gaze hits a hologram, set the focused object to that game object.
  110. // Also if the caller has not decided to override the focused object.
  111. focusedObject = GazeManager.Instance.HitInfo.collider.gameObject;
  112. }
  113. else
  114. {
  115. // If our gaze doesn't hit a hologram, set the focused object to null or override focused object.
  116. focusedObject = OverrideFocusedObject;
  117. }
  118. //放开该代码将重置手势识别
  119. //if (focusedObject != oldFocusedObject)
  120. //{
  121. // // If the currently focused object doesn't match the old focused object, cancel the current gesture.
  122. // // Start looking for new gestures. This is to prevent applying gestures from one hologram to another.
  123. // gestureRecognizer.CancelGestures();
  124. // gestureRecognizer.StartCapturingGestures();
  125. //}
  126. }
  127. void OnDestroy()
  128. {
  129. gestureRecognizer.StopCapturingGestures();
  130. gestureRecognizer.TappedEvent -= GestureRecognizer_TappedEvent;
  131. gestureRecognizer.NavigationStartedEvent -= GestureRecognizer_NavigationStartedEvent;
  132. gestureRecognizer.NavigationUpdatedEvent -= GestureRecognizer_NavigationUpdatedEvent;
  133. gestureRecognizer.NavigationCompletedEvent -= GestureRecognizer_NavigationCompletedEvent;
  134. gestureRecognizer.NavigationCanceledEvent -= GestureRecognizer_NavigationCanceledEvent;
  135. }
  136. }
  137. }

3、修改CubeScript.cs,添加物体旋转函数,当选中物体,左右移动时,物体将发生绕着y轴进行旋转

  1. using UnityEngine;
  2. using System.Collections;
  3. using HoloToolkit.Unity;
  4. public class CubeScript : MonoBehaviour {
  5. [Tooltip("Rotation max speed controls amount of rotation.")]
  6. public float RotationSensitivity = 10.0f;
  7. private float rotationFactorX;
  8. private float rotationFactorY;
  9. private float rotationFactorZ;
  10. // Use this for initialization
  11. void Start () {
  12. }
  13. // Update is called once per frame
  14. void Update () {
  15. PerformRotation();
  16. }
  17. private void PerformRotation()
  18. {
  19. //当处于Navigating Gesture时且当前物体为被手势识别追踪的物体时进行旋转
  20. if (GestureManager.Instance.IsNavigating && HandsManager.Instance.FocusedGameObject == gameObject)
  21. {
  22. //计算旋转角度
  23. rotationFactorX = GestureManager.Instance.NavigationPosition.x * RotationSensitivity;
  24. //绕着y轴进行旋转
  25. transform.Rotate(new Vector3(0, -1 * rotationFactorX, 0));
  26. }
  27. }
  28. private void OnTap()
  29. {
  30. gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
  31. }
  32. private void OnDoubleTap()
  33. {
  34. gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
  35. }
  36. }

4、新增一个cube,与之前的一个cube相同,用于测试当选中不同的物体时,物体单独进行选择,不影响其它物体



5、运行测试

模拟器中按住鼠标右键选中物体,不放开鼠标右键,左右移动鼠标,被选中物体将绕着y轴进行旋转(如下图绿色Cube的角度将发生变化)



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号