Manipulation gesture:保持点击手势,在3D世界中绝对运动

当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动、缩放或旋转全息图像。如此的一个用处是使得用户可以在世界中绘制图像或作画。使用所有的手势时,操纵手势的初始目标应该通过凝视来选中。一旦点击手势开始,通过手部移动的任何对对象的操作都能够被处理,在用户操作全息图像时,从而使得用户得以自由地四处张望。

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 > ; }
} 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_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;
} //手势释放时,将被关注的物体置空
private void InteractionManager_SourceReleased(InteractionSourceState state)
{
FocusedGameObject = null;
} //识别到手指按下时,将凝视射线关注的物体置为当前手势操作的对象
private void InteractionManager_SourcePressed(InteractionSourceState state)
{
if (GazeManager.Instance.FocusedObject != null)
{
FocusedGameObject = GazeManager.Instance.FocusedObject;
}
} void OnDestroy()
{
InteractionManager.SourceDetected -= InteractionManager_SourceDetected;
InteractionManager.SourceLost -= InteractionManager_SourceLost;
InteractionManager.SourceReleased -= InteractionManager_SourceReleased;
InteractionManager.SourcePressed -= InteractionManager_SourcePressed;
}
}
}

2、修改GestureManager.cs,订阅Manipulation gesture事件

// 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;
} public bool IsManipulating { get; private set; }
public Vector3 ManipulationPosition { get; private set; } /// <summary>
/// Gets the currently focused object, or null if none.
/// </summary>
public GameObject FocusedObject
{
get { return focusedObject; }
} private GestureRecognizer gestureRecognizer;
private GameObject focusedObject; void Start()
{
// Create a new GestureRecognizer. Sign up for tapped events.
gestureRecognizer = new GestureRecognizer();
gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap
| GestureSettings.DoubleTap
| GestureSettings.ManipulationTranslate); gestureRecognizer.TappedEvent += GestureRecognizer_TappedEvent;
//订阅Manipulation gesture事件
gestureRecognizer.ManipulationStartedEvent += GestureRecognizer_ManipulationStartedEvent;
gestureRecognizer.ManipulationUpdatedEvent += GestureRecognizer_ManipulationUpdatedEvent;
gestureRecognizer.ManipulationCompletedEvent += GestureRecognizer_ManipulationCompletedEvent;
gestureRecognizer.ManipulationCanceledEvent += GestureRecognizer_ManipulationCanceledEvent; // Start looking for gestures.
gestureRecognizer.StartCapturingGestures();
} private void GestureRecognizer_ManipulationStartedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)
{
//当被关注的对象非空时,设置初始位置
if (HandsManager.Instance.FocusedGameObject != null)
{
IsManipulating = true; ManipulationPosition = cumulativeDelta; HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformManipulationStart", cumulativeDelta);
}
} private void GestureRecognizer_ManipulationUpdatedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)
{
//当被关注的对象非空时,更新新的位置
if (HandsManager.Instance.FocusedGameObject != null)
{
IsManipulating = true; ManipulationPosition = cumulativeDelta; HandsManager.Instance.FocusedGameObject.SendMessageUpwards("PerformManipulationUpdate", cumulativeDelta);
}
} private void GestureRecognizer_ManipulationCompletedEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)
{
IsManipulating = false;
} private void GestureRecognizer_ManipulationCanceledEvent(InteractionSourceKind source, Vector3 cumulativeDelta, Ray headRay)
{
IsManipulating = false;
} 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 == )
{
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.ManipulationStartedEvent -= GestureRecognizer_ManipulationStartedEvent;
gestureRecognizer.ManipulationUpdatedEvent -= GestureRecognizer_ManipulationUpdatedEvent;
gestureRecognizer.ManipulationCompletedEvent -= GestureRecognizer_ManipulationCompletedEvent;
gestureRecognizer.ManipulationCanceledEvent -= GestureRecognizer_ManipulationCanceledEvent;
}
}
}

3、添加ManipulationRotate脚本,实现对物体进行旋转。

using UnityEngine;
using System.Collections;
using HoloToolkit.Unity; public class ManipulationRotate : MonoBehaviour { public float RotationSensitivity = 25.0f; private float rotationFactorX; // Use this for initialization
void PerformManipulationStart()
{ } void PerformManipulationUpdate(Vector3 position)
{
if (GestureManager.Instance.IsManipulating)
{
try
{
if (GazeManager.Instance.HitInfo.collider.gameObject.tag == "Rotate")
{ //绕X轴进行旋转
rotationFactorX = GestureManager.Instance.ManipulationPosition.x * RotationSensitivity;
transform.Rotate(new Vector3(rotationFactorX, , )); }
}
catch
{ }
}
}
}

4、运行测试

将凝视射线投射到对象上,举起右手使食指和拇指碰触在一起,然后进行左右移动,可以看到物体将绕X轴旋转。

 

Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体旋转)的更多相关文章

  1. Hololens开发笔记之Gesture手势识别(手势检测反馈)

    本文实现当使用者手出现在Hololens视野范围内时,跟踪手并给出反馈的效果. 1.在Manager上添加HandsManager脚本组件,用于追踪识别手 HandsManager.cs如下(直接使用 ...

  2. Hololens开发笔记之Gesture手势识别(Manipulation手势控制物体平移)

    Manipulation gesture:保持点击手势,在3D世界中绝对运动 当你想要全息图像1:1响应用户手部移动时,操纵手势能被用于移动.缩放或旋转全息图像.如此的一个用处是使得用户可以在世界中绘 ...

  3. Hololens开发笔记之Gesture手势识别(单击,双击)

    本文使用手势识别实现识别单击及双击手势的功能,当单击Cube时改变颜色为蓝色,当双击Cube时改变颜色为绿色. 手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层 ...

  4. Hololens开发笔记之Gesture手势识别(基本介绍)

    手势识别是HoloLens交互的重要输入方法之一.HoloLens提供了底层API和高层API,可以满足不同的手势定制需求.底层API能够获取手的位置和速度信息,高层API则借助手势识别器来识别预设的 ...

  5. HoloLens开发笔记之Gesture input手势输入

    手势是HoloLens三个首要输入形式之一.一旦你使用凝视定位了一个全息图像,手势允许你与它交互.手势输入允许你使用手或者点击器原生地与全息图像交互. 手势之外,你也可以在应用中使用语音输入来交互. ...

  6. Hololens开发笔记之使用Unity开发一个简单的应用

    一.Hololens概述 Hololens有以下特性 1.空间映射借助微软特殊定制的全息处理单元(HPU),HoloLens 实现了对周边环境的快速扫描和空间匹配.这保证了 HoloLens能够准确地 ...

  7. Hololens开发笔记之连接PC实现资源共享

    官网原文介绍:https://developer.microsoft.com/en-us/windows/holographic/using_the_windows_device_portal Hol ...

  8. Hololens开发笔记:UDP接收数据

    Hololens的应用需要与其他设备通信的时候,UDP是比较方便的一种方式,Unity3d 2017.3 C#开发的时候可以用Windows.Networking.Sockets.DatagramSo ...

  9. Hololens开发笔记之Gaze凝视射线

    凝视是HoloLens首要输入方式,形式功能类似于桌面系统的光标,用于选择操作全息对象.然而在Unity中并没有明确的Gaze API或者组件. 概念上来说,Gaze是通过用户头部两眼之间发出一条向前 ...

随机推荐

  1. ubuntu安装rpm包

    ubuntu下的rpm包的安装方法 一般是不能够直接安装的,我们需要一个工具叫alien,先install它吧.然后按照下面择所需. 1.直接安装: alien -i -c filename.rpm ...

  2. Day25_多线程第二天

    1.单例); } 4.线程之间的通信(多个线程共享同一数据的问题) ; public void print1() throws InterruptedException { synchronized( ...

  3. serialVersionUID的作用

    Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的.在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体(类)的seri ...

  4. bootstrap-15

    缩略图:缩略图在网站中最常用的地方就是产品列表页面,一行显示几张图片,有的在图片底下(左侧或者右侧)带有标题.描述信息.Bootstrap框架将这一部独立成一个模块组件,并通过thumbnail样式配 ...

  5. php捕捉来自搜索引擎的用户IP地址时间和访问路径

    刚才看demo看到很有意思的地方记录一下 $zz_page=$_SERVER['REQUEST_URI']; $zz_name=$_SERVER['HTTP_USER_AGENT']; $zz_ip= ...

  6. 2016HUAS_ACM暑假集训4M - 基础DP

    简单的0-1背包问题,大家都会做的.题意不想解释太多. 简述题目的案例及以几个关键 Sample Input 1                            //测试组数T 5 10     ...

  7. C++ 中静态成员函数访问非静态成员变量的方法

    最近在 VS2010 里开发出厂烧写工具,遇到一个问题: 我创建了一个线程,在这个线程里要访问非静态成员,而这个线程函数是静态的.最后找到的办法是用对象指针来做. sourcecode: #test. ...

  8. AutoLearnSkills.lua --升级自动学习技能

    --[[作者信息: Auto Learn SKills (升级自动学习技能) 作者QQ:247321453 作者Email:247321453@qq.com 修改日期:2014-3-12 功能:在玩家 ...

  9. OpenStack Mitaka安装

    http://egon09.blog.51cto.com/9161406/1839667 前言: openstack的部署非常简单,简单的前提建立在扎实的理论功底,本人一直觉得,玩技术一定是理论指导实 ...

  10. 10天学会phpWeChat——第一天:核心框架的目录结构

    phpWeCaht是一款优秀的PC网站+微信公共号开发框架. 本博客系列将图文结合,详细介绍phpWeChat 的使用方法,今天进入phpWeChat系列教程之<10天学会phpWeChat&g ...