Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)
http://blog.csdn.net/ystistheking/article/details/51553237
想交流的朋友我们可以微博互粉,我的微博黑石铸造厂厂长 ,缺粉丝啊 。。。。。求粉求粉
研究了几天htc vive的接口,总算是把基本的按键功能研究出来了,这里分享一下,一来当做笔记,二来也希望对大家有所帮助。
如何导入Steam_VR那个包什么的我就不说了,网上有几个前辈已经教了,蛮牛论坛啥的上面都有,这里只把比较详细的按键功能分享一下,不知啥高端的东西,也算一段时间劳动成果啦,所以转载的帮我留个名写个转,谢谢啦。
个人感觉手柄上开始比较难搞明白的就是那个圆盘键,这个键是一个以中心为(0,0)点的直角坐标系,四个端长度都是1,可接收触摸和按压两种事件,大体就是下图这个意思(手绘水平略渣,见谅见谅),触摸touch或按压press会通过GetAxis方法返回一个坐标系中的点,可以判断你按在哪里,触发不同的事件,可以根据角度或各种方法来切分按键为n个按钮(就像切蛋糕一样)
这里用的是C#脚本 ,直接上代码了,我个人写注释比较话唠,适合新手看:
- using UnityEngine;
- using System.Collections;
- //检测手柄功能的脚本 这个脚本挂到手柄上(controler(right)和controler(left))上
- public class ButtonTouchAction : MonoBehaviour {
- //手柄
- SteamVR_TrackedObject trackdeObjec;
- void Awake() {
- //获取手柄上的这个组件
- trackdeObjec = GetComponent<SteamVR_TrackedObject>();
- }
- // Use this for initialization
- void Start () {
- }
- void FixedUpdate()
- { //获取手柄输入
- var device = SteamVR_Controller.Input((int)trackdeObjec.index);
- //以下是api中复制出来的按键列表
- /* public class ButtonMask
- {
- public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved
- public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu);
- public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip);
- public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0);
- public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1);
- public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2);
- public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3);
- public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4);
- public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad);
- public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger);
- }
- */
- //同样是三种按键方式,以后不做赘述
- if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) {
- Debug.Log("按了 “trigger” “扳机键”");
- //右手震动
- //拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。
- var deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
- SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);
- }
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
- { Debug.Log("按下了 “trigger” “扳机键”");
- }
- if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {
- Debug.Log("松开了 “trigger” “扳机键”");
- //左手震动
- var deviceIndex = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);
- SteamVR_Controller.Input(deviceIndex).TriggerHapticPulse(3000);
- //右手震动
- var deviceIndex1 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
- SteamVR_Controller.Input(deviceIndex1).TriggerHapticPulse(3000);
- }
- //这三种也能检测到 后面不做赘述
- if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
- Debug.Log("用press按下了 “trigger” “扳机键”");
- }
- if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
- {
- Debug.Log("用press按了 “trigger” “扳机键”");
- }
- if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
- {
- Debug.Log("用press松开了 “trigger” “扳机键”");
- }
- //system键 圆盘下面那个键
- // reserved 为Steam系统保留,用来调出Steam系统菜单 因此貌似自己加的功能没啥用
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.System))
- {
- Debug.Log("按下了 “system” “系统按钮/Steam”");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.System))
- {
- Debug.Log("用press按下了 “System” “系统按钮/Steam”");
- }
- //ApplicationMenu键 带菜单标志的那个按键(在方向圆盘上面)
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
- {
- Debug.Log("按下了 “ApplicationMenu” “菜单键”");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
- {
- Debug.Log("用press按下了 “ApplicationMenu” “菜单键”");
- }
- //Grip键 两侧的键 (vive雇佣兵游戏中的换弹键),每个手柄左右各一功能相同,同一手柄两个键是一个键。
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Grip))
- {
- Debug.Log("按下了 “Grip” “ ”");
- }
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
- {
- Debug.Log("用press按下了 “Grip” “ ”");
- }
- //Axis0键 与圆盘有交互 与圆盘有关
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
- {
- Debug.Log("按下了 “Axis0” “方向 ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
- {
- Debug.Log("用press按下了 “Axis0” “方向 ”");
- }
- //Axis1键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
- {
- Debug.Log("按下了 “Axis1” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
- {
- Debug.Log("用press按下了 “Axis1” “ ”");
- }
- //Axis2键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
- {
- Debug.Log("按下了 “Axis2” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
- {
- Debug.Log("用press按下了 “Axis2” “ ”");
- }
- //Axis3键 未目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
- {
- Debug.Log("按下了 “Axis3” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
- {
- Debug.Log("用press按下了 “Axis3” “ ”");
- }
- //Axis4键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
- {
- Debug.Log("按下了 “Axis4” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
- {
- Debug.Log("用press按下了 “Axis4” “ ”");
- }
- <pre name="code" class="csharp"> //方向圆盘:
- //这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动<pre name="code" class="csharp"> //Axis0键 与圆盘有交互 与圆盘有关
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
- {
- Debug.Log("按下了 “Axis0” “方向 ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
- {
- Debug.Log("用press按下了 “Axis0” “方向 ”");
- }
- //Axis1键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
- {
- Debug.Log("按下了 “Axis1” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
- {
- Debug.Log("用press按下了 “Axis1” “ ”");
- }
- //Axis2键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
- {
- Debug.Log("按下了 “Axis2” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
- {
- Debug.Log("用press按下了 “Axis2” “ ”");
- }
- //Axis3键 未目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
- {
- Debug.Log("按下了 “Axis3” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
- {
- Debug.Log("用press按下了 “Axis3” “ ”");
- }
- //Axis4键 目前未发现按键位置
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
- {
- Debug.Log("按下了 “Axis4” “ ”");
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
- {
- Debug.Log("用press按下了 “Axis4” “ ”");
- }
- //ATouchpad键 圆盘交互
- //触摸触发
- if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
- {
- Debug.Log("按下了 “Touchpad” “ ”");
- //方法返回一个坐标 接触圆盘位置
- Vector2 cc = device.GetAxis();
- Debug.Log(cc);
- // 例子:圆盘分成上下左右
- float jiaodu = VectorAngle(new Vector2(1, 0), cc);
- Debug.Log(jiaodu);
- //下
- if (jiaodu > 45 && jiaodu < 135)
- {
- Debug.Log("下");
- }
- //上
- if (jiaodu < -45 && jiaodu > -135)
- {
- Debug.Log("上");
- }
- //左
- if ((jiaodu < 180 && jiaodu > 135) || (jiaodu < -135 && jiaodu > -180))
- {
- Debug.Log("左");
- }
- //右
- if ((jiaodu > 0 && jiaodu < 45) || (jiaodu > -45 && jiaodu < 0))
- {
- Debug.Log("右");
- }
- }
- //按动触发
- if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
- {
- Debug.Log("用press按下了 “Touchpad” “ ”");
- }
- }
- // Update is called once per frame
- void Update () {
- }
- //方向圆盘最好配合这个使用 圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量
- //这个函数输入两个二维向量会返回一个夹角 180 到 -180
- float VectorAngle(Vector2 from, Vector2 to)
- {
- float angle;
- Vector3 cross = Vector3.Cross(from, to);
- angle = Vector2.Angle(from, to);
- return cross.z > 0 ? -angle : angle;
- }
- }
Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)的更多相关文章
- 用 Unity 和 HTC Vive 实现高级 VR 机制(1)
原文:Advanced VR Mechanics With Unity and the HTC Vive Part 1 作者:Eric Van de Kerckhove 译者:kmyhy VR 从来没 ...
- HTC VIVE SDK 中的例子 hellovr_opengl 程序流程分析
最近Vive的VR头盔设备很火,恰逢项目需求,所以对 SDK 中的例子 hellovr_opengl 做了比较细致的代码分析,先将流程图绘制如下,便于大家理解. 在ViVe头盔中实现立体效果的技术核心 ...
- 用Unity开发HTC VIVE——手柄控制篇
写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会.所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中 ...
- 用Unity开发HTC VIVE——移动漫游篇
这篇文章主要写的是通过手柄控制移动在场景中漫游.在通过手柄控制移动时,我主要写了两个脚本一个ChildTransform.cs.Move.cs;1. ChildTransform这个脚本主要是为了获取 ...
- Unity 5.4大赞:HTC Vive经典The lab渲染器开源
HTC Vive提供了一个不错的免费VR demo,最近1周仔细体验了一番. 仔细看了其安装文件,竟然是Unity 5.4beta版本(通过查log,知道Valve公司用的是最新的5.4.0b11版本 ...
- HTC vive VR设备软硬件安装+运行unity开发的VR程序
总结在HTC vive VR开发过程中的HTC vive的安装调试 1.首先确保电脑的配置满足要求: 进入官网,测试电脑是否满足要求 链接:https://www.vive.com/us/produc ...
- Unity正式发布首个“实验性”VR编辑器,支持HTC Vive和Oculus Rift
Unity今天正式推出"实验性"VR编辑器.据悉,EditorVR是Unity游戏引擎中的一个组件,可让开发者在虚拟现实环境中开发游戏.为何要称之为"实验性"? ...
- unity htc vive使用
本文介绍如何在Unity中使用HTC vive设备,当前VR作为市场比较火热的热点,HTC VIVE设备作为三大供应商之一,许多人购买了该设备,却不知道如何使用,本文通过图文并茂的形式,进行手把手的讲 ...
- 如何低成本的打造HTC Vive虚拟演播室直播MR视频?
http://m.toutiao.com/i6298923859378700802/?tt_from=weixin&utm_campaign=client_share&from=gro ...
随机推荐
- Struts2的异常处理
Struts2的异常处理 1.异常处理机制(1)发送请求到控制器(Action); (2)Action出现异常后,依照所捕捉的不同异常转入不同的视图资源. 2.异常捕捉 (1)在Action的处理逻辑 ...
- DataTables warning : Requested unknown parameter '5' from the data source for row 0
在该项目中我使用了jquery.dataTables.js来作为我的前端数据表格. 表格的官网地址:https://www.datatables.net/ 一.jsp部分代码片段如下: <tab ...
- CentOS最小化安装后,增加GNOME桌面
背景:下载CentOS 7的安装包后,在虚拟机上安装. 上来就遇到一个问题:提示需要开启intel vt-x. 这个进入BIOS,在CPU的设置中开启即可. 然后怀着兴奋的心情,开始各种下一步的安装, ...
- How To PLAY_SOUND in Oracle Forms
Play_sound is used to play audio files in Oracle Forms, Play_Sound plays the sound object in the spe ...
- geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
# sample data d <- data.frame(expand.grid(x=letters[1:4], g=factor(1:2)), y=rnorm(8)) # Figure 1a ...
- ImageMagick远程命令执行工具检测工具
ImageMagick这个漏洞昨天晚上就出来了,今天才有时间研究一下,今天自己也测试了一下 效果图: ======================= 用lua写了一个检测脚本 print (" ...
- ubuntu安装miniconda
系统:ubuntu15.04 64位 wget -c http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh chm ...
- 用jq移除某一个特定样式
用JQ移除样式中的某条单独的样式,实际移除不了的,只能将其赋值为空$(function(){ $(".tableStyle").css("background-color ...
- JS——ajax login test
1.新建一个webproject,我用的是myeclipse10,建立如下的LoginServlet.java文件 2.编写java文件 import java.io.IOException; imp ...
- FLASH CC 2015 CANVAS 中 gotoAndStop、gotoAndPlay() 不起作用
哎 话不多说先看我的代码: //舞台上 放着sp0.sp1....sp8,9个mc,每个mc都有几帧, //帧上有如下代码 var S=this; S.stop() inIt1();//not wor ...