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)]