您的位置:首页 > 其它

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

2016-09-02 10:16 531 查看
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;
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)
{
// 检测来源是否为手,如果是手则加入跟踪集合
if (state.source.kind != InteractionSourceKind.Hand)
{
return;
}

trackedHands.Add(state.source.id);
}

private void InteractionManager_SourceLost(InteractionSourceState state)
{
// 检测丢失的来源是否为手,如果是手则从跟踪集合中去除
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 System;
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; }
}

private GestureRecognizer gestureRecognizer;
private GameObject focusedObject;

public bool IsNavigating { get; private set; }
public Vector3 NavigationPosition { get; private set; }

void Start()
{
//  创建GestureRecognizer实例
gestureRecognizer = new GestureRecognizer();
//  注册指定的手势类型
gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
| GestureSettings.DoubleTap
| 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)
{
IsNavigating = true;
//偏移量
NavigationPosition = normalizedOffset;
}

private void GestureRecognizer_NavigationStartedEvent(InteractionSourceKind source, Vector3 normalizedOffset, Ray headRay)
{
IsNavigating = true;
NavigationPosition = normalizedOffset;
}

private void OnTap()
{
if (focusedObject != null)
{
focusedObject.SendMessage("OnTap");
}
}

private void OnDoubleTap()
{
if (focusedObject != null)
{
focusedObject.SendMessage("OnDoubleTap");
}
}

private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
{
if (tapCount == 1)
{
OnTap();
}
else
{
OnDoubleTap();
}
}

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();
//}
}

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、修改CubeScript.cs,添加物体旋转函数,当选中物体,左右移动时,物体将发生绕着y轴进行旋转

using UnityEngine;
using System.Collections;
using HoloToolkit.Unity;

public class CubeScript : MonoBehaviour {

[Tooltip("Rotation max speed controls amount of rotation.")]
public float RotationSensitivity = 10.0f;

private float rotationFactorX;
private float rotationFactorY;
private float rotationFactorZ;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
PerformRotation();
}

private void PerformRotation()
{
//当处于Navigating Gesture时且当前物体为被手势识别追踪的物体时进行旋转
if (GestureManager.Instance.IsNavigating && HandsManager.Instance.FocusedGameObject == gameObject)
{
//计算旋转角度
rotationFactorX = GestureManager.Instance.NavigationPosition.x * RotationSensitivity;
//绕着y轴进行旋转
transform.Rotate(new Vector3(0, -1 * rotationFactorX, 0));
}
}

private void OnTap()
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
}

private void OnDoubleTap()
{
gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
}
}


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



5、运行测试

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



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐