http://blog.csdn.net/ystistheking/article/details/51553237

想交流的朋友我们可以微博互粉,我的微博黑石铸造厂厂长 ,缺粉丝啊 。。。。。求粉求粉

研究了几天htc vive的接口,总算是把基本的按键功能研究出来了,这里分享一下,一来当做笔记,二来也希望对大家有所帮助。

如何导入Steam_VR那个包什么的我就不说了,网上有几个前辈已经教了,蛮牛论坛啥的上面都有,这里只把比较详细的按键功能分享一下,不知啥高端的东西,也算一段时间劳动成果啦,所以转载的帮我留个名写个转,谢谢啦。

个人感觉手柄上开始比较难搞明白的就是那个圆盘键,这个键是一个以中心为(0,0)点的直角坐标系,四个端长度都是1,可接收触摸和按压两种事件,大体就是下图这个意思(手绘水平略渣,见谅见谅),触摸touch或按压press会通过GetAxis方法返回一个坐标系中的点,可以判断你按在哪里,触发不同的事件,可以根据角度或各种方法来切分按键为n个按钮(就像切蛋糕一样)

 

这里用的是C#脚本 ,直接上代码了,我个人写注释比较话唠,适合新手看:

  1. using UnityEngine;
  2. using System.Collections;
  3. //检测手柄功能的脚本 这个脚本挂到手柄上(controler(right)和controler(left))上
  4. public class ButtonTouchAction : MonoBehaviour {
  5. //手柄
  6. SteamVR_TrackedObject trackdeObjec;
  7. void Awake() {
  8. //获取手柄上的这个组件
  9. trackdeObjec = GetComponent<SteamVR_TrackedObject>();
  10. }
  11. // Use this for initialization
  12. void Start () {
  13. }
  14. void FixedUpdate()
  15. {   //获取手柄输入
  16. var device = SteamVR_Controller.Input((int)trackdeObjec.index);
  17. //以下是api中复制出来的按键列表
  18. /*       public class ButtonMask
  19. {
  20. public const ulong System = (1ul << (int)EVRButtonId.k_EButton_System); // reserved
  21. public const ulong ApplicationMenu = (1ul << (int)EVRButtonId.k_EButton_ApplicationMenu);
  22. public const ulong Grip = (1ul << (int)EVRButtonId.k_EButton_Grip);
  23. public const ulong Axis0 = (1ul << (int)EVRButtonId.k_EButton_Axis0);
  24. public const ulong Axis1 = (1ul << (int)EVRButtonId.k_EButton_Axis1);
  25. public const ulong Axis2 = (1ul << (int)EVRButtonId.k_EButton_Axis2);
  26. public const ulong Axis3 = (1ul << (int)EVRButtonId.k_EButton_Axis3);
  27. public const ulong Axis4 = (1ul << (int)EVRButtonId.k_EButton_Axis4);
  28. public const ulong Touchpad = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Touchpad);
  29. public const ulong Trigger = (1ul << (int)EVRButtonId.k_EButton_SteamVR_Trigger);
  30. }
  31. */
  32. //同样是三种按键方式,以后不做赘述
  33. if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger)) {
  34. Debug.Log("按了 “trigger” “扳机键”");
  35. //右手震动
  36. //拉弓类似操作应该就是按住trigger(扳机)gettouch时持续调用震动方法模拟弓弦绷紧的感觉。
  37. var deviceIndex2 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
  38. SteamVR_Controller.Input(deviceIndex2).TriggerHapticPulse(500);
  39. }
  40. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
  41. {   Debug.Log("按下了 “trigger” “扳机键”");
  42. }
  43. if (device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {
  44. Debug.Log("松开了 “trigger” “扳机键”");
  45. //左手震动
  46. var deviceIndex = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);
  47. SteamVR_Controller.Input(deviceIndex).TriggerHapticPulse(3000);
  48. //右手震动
  49. var deviceIndex1 = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
  50. SteamVR_Controller.Input(deviceIndex1).TriggerHapticPulse(3000);
  51. }
  52. //这三种也能检测到 后面不做赘述
  53. if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)) {
  54. Debug.Log("用press按下了 “trigger” “扳机键”");
  55. }
  56. if (device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
  57. {
  58. Debug.Log("用press按了 “trigger” “扳机键”");
  59. }
  60. if (device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger))
  61. {
  62. Debug.Log("用press松开了 “trigger” “扳机键”");
  63. }
  64. //system键 圆盘下面那个键
  65. // reserved 为Steam系统保留,用来调出Steam系统菜单 因此貌似自己加的功能没啥用
  66. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.System))
  67. {
  68. Debug.Log("按下了 “system” “系统按钮/Steam”");
  69. }
  70. if (device.GetPressDown(SteamVR_Controller.ButtonMask.System))
  71. {
  72. Debug.Log("用press按下了 “System” “系统按钮/Steam”");
  73. }
  74. //ApplicationMenu键 带菜单标志的那个按键(在方向圆盘上面)
  75. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  76. {
  77. Debug.Log("按下了 “ApplicationMenu” “菜单键”");
  78. }
  79. if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
  80. {
  81. Debug.Log("用press按下了 “ApplicationMenu” “菜单键”");
  82. }
  83. //Grip键 两侧的键 (vive雇佣兵游戏中的换弹键),每个手柄左右各一功能相同,同一手柄两个键是一个键。
  84. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Grip))
  85. {
  86. Debug.Log("按下了 “Grip” “ ”");
  87. }
  88. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
  89. {
  90. Debug.Log("用press按下了 “Grip” “ ”");
  91. }
  92. //Axis0键 与圆盘有交互 与圆盘有关
  93. //触摸触发
  94. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
  95. {
  96. Debug.Log("按下了 “Axis0” “方向 ”");
  97. }
  98. //按动触发
  99. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
  100. {
  101. Debug.Log("用press按下了 “Axis0” “方向 ”");
  102. }
  103. //Axis1键  目前未发现按键位置
  104. //触摸触发
  105. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
  106. {
  107. Debug.Log("按下了 “Axis1” “ ”");
  108. }
  109. //按动触发
  110. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
  111. {
  112. Debug.Log("用press按下了 “Axis1” “ ”");
  113. }
  114. //Axis2键 目前未发现按键位置
  115. //触摸触发
  116. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
  117. {
  118. Debug.Log("按下了 “Axis2” “ ”");
  119. }
  120. //按动触发
  121. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
  122. {
  123. Debug.Log("用press按下了 “Axis2” “ ”");
  124. }
  125. //Axis3键  未目前未发现按键位置
  126. //触摸触发
  127. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
  128. {
  129. Debug.Log("按下了 “Axis3” “ ”");
  130. }
  131. //按动触发
  132. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
  133. {
  134. Debug.Log("用press按下了 “Axis3” “ ”");
  135. }
  136. //Axis4键  目前未发现按键位置
  137. //触摸触发
  138. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
  139. {
  140. Debug.Log("按下了 “Axis4” “ ”");
  141. }
  142. //按动触发
  143. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
  144. {
  145. Debug.Log("用press按下了 “Axis4” “ ”");
  146. }
  147. <pre name="code" class="csharp">       //方向圆盘:
  148. //这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动<pre name="code" class="csharp"> //Axis0键 与圆盘有交互 与圆盘有关
  149. //触摸触发
  150. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis0))
  151. {
  152. Debug.Log("按下了 “Axis0” “方向 ”");
  153. }
  154. //按动触发
  155. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis0))
  156. {
  157. Debug.Log("用press按下了 “Axis0” “方向 ”");
  158. }
  159. //Axis1键  目前未发现按键位置
  160. //触摸触发
  161. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis1))
  162. {
  163. Debug.Log("按下了 “Axis1” “ ”");
  164. }
  165. //按动触发
  166. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis1))
  167. {
  168. Debug.Log("用press按下了 “Axis1” “ ”");
  169. }
  170. //Axis2键 目前未发现按键位置
  171. //触摸触发
  172. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis2))
  173. {
  174. Debug.Log("按下了 “Axis2” “ ”");
  175. }
  176. //按动触发
  177. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis2))
  178. {
  179. Debug.Log("用press按下了 “Axis2” “ ”");
  180. }
  181. //Axis3键  未目前未发现按键位置
  182. //触摸触发
  183. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis3))
  184. {
  185. Debug.Log("按下了 “Axis3” “ ”");
  186. }
  187. //按动触发
  188. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis3))
  189. {
  190. Debug.Log("用press按下了 “Axis3” “ ”");
  191. }
  192. //Axis4键  目前未发现按键位置
  193. //触摸触发
  194. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Axis4))
  195. {
  196. Debug.Log("按下了 “Axis4” “ ”");
  197. }
  198. //按动触发
  199. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Axis4))
  200. {
  201. Debug.Log("用press按下了 “Axis4” “ ”");
  202. }
  203. //ATouchpad键 圆盘交互
  204. //触摸触发
  205. if (device.GetTouchDown(SteamVR_Controller.ButtonMask.Touchpad))
  206. {
  207. Debug.Log("按下了 “Touchpad” “ ”");
  208. //方法返回一个坐标 接触圆盘位置
  209. Vector2 cc = device.GetAxis();
  210. Debug.Log(cc);
  211. // 例子:圆盘分成上下左右
  212. float jiaodu = VectorAngle(new Vector2(1, 0), cc);
  213. Debug.Log(jiaodu);
  214. //下
  215. if (jiaodu > 45 && jiaodu < 135)
  216. {
  217. Debug.Log("下");
  218. }
  219. //上
  220. if (jiaodu < -45 && jiaodu > -135)
  221. {
  222. Debug.Log("上");
  223. }
  224. //左
  225. if ((jiaodu < 180 && jiaodu > 135) || (jiaodu < -135 && jiaodu > -180))
  226. {
  227. Debug.Log("左");
  228. }
  229. //右
  230. if ((jiaodu > 0 && jiaodu < 45) || (jiaodu > -45 && jiaodu < 0))
  231. {
  232. Debug.Log("右");
  233. }
  234. }
  235. //按动触发
  236. if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
  237. {
  238. Debug.Log("用press按下了 “Touchpad” “ ”");
  239. }
  240. }
  241. // Update is called once per frame
  242. void Update () {
  243. }
  244. //方向圆盘最好配合这个使用 圆盘的.GetAxis()会检测返回一个二位向量,可用角度划分圆盘按键数量
  245. //这个函数输入两个二维向量会返回一个夹角 180 到 -180
  246. float VectorAngle(Vector2 from, Vector2 to)
  247. {
  248. float angle;
  249. Vector3 cross = Vector3.Cross(from, to);
  250. angle = Vector2.Angle(from, to);
  251. return cross.z > 0 ? -angle : angle;
  252. }
  253. }

Unity的HTC VIVE SDK研究(手柄按键功能的研究,比较详细)的更多相关文章

  1. 用 Unity 和 HTC Vive 实现高级 VR 机制(1)

    原文:Advanced VR Mechanics With Unity and the HTC Vive Part 1 作者:Eric Van de Kerckhove 译者:kmyhy VR 从来没 ...

  2. HTC VIVE SDK 中的例子 hellovr_opengl 程序流程分析

    最近Vive的VR头盔设备很火,恰逢项目需求,所以对 SDK 中的例子 hellovr_opengl 做了比较细致的代码分析,先将流程图绘制如下,便于大家理解. 在ViVe头盔中实现立体效果的技术核心 ...

  3. 用Unity开发HTC VIVE——手柄控制篇

    写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会.所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中 ...

  4. 用Unity开发HTC VIVE——移动漫游篇

    这篇文章主要写的是通过手柄控制移动在场景中漫游.在通过手柄控制移动时,我主要写了两个脚本一个ChildTransform.cs.Move.cs;1. ChildTransform这个脚本主要是为了获取 ...

  5. Unity 5.4大赞:HTC Vive经典The lab渲染器开源

    HTC Vive提供了一个不错的免费VR demo,最近1周仔细体验了一番. 仔细看了其安装文件,竟然是Unity 5.4beta版本(通过查log,知道Valve公司用的是最新的5.4.0b11版本 ...

  6. HTC vive VR设备软硬件安装+运行unity开发的VR程序

    总结在HTC vive VR开发过程中的HTC vive的安装调试 1.首先确保电脑的配置满足要求: 进入官网,测试电脑是否满足要求 链接:https://www.vive.com/us/produc ...

  7. Unity正式发布首个“实验性”VR编辑器,支持HTC Vive和Oculus Rift

    Unity今天正式推出"实验性"VR编辑器.据悉,EditorVR是Unity游戏引擎中的一个组件,可让开发者在虚拟现实环境中开发游戏.为何要称之为"实验性"? ...

  8. unity htc vive使用

    本文介绍如何在Unity中使用HTC vive设备,当前VR作为市场比较火热的热点,HTC VIVE设备作为三大供应商之一,许多人购买了该设备,却不知道如何使用,本文通过图文并茂的形式,进行手把手的讲 ...

  9. 如何低成本的打造HTC Vive虚拟演播室直播MR视频?

    http://m.toutiao.com/i6298923859378700802/?tt_from=weixin&utm_campaign=client_share&from=gro ...

随机推荐

  1. Java中自定义异常

    /*下面做了归纳总结,欢迎批评指正*/ /*自定义异常*/ class ChushulingException extends Exception { public ChushulingExcepti ...

  2. VC++ 工程添加 Unicode Debug和Unicode Release编译支持

    转载:http://blog.csdn.net/djhdu/article/details/171766 转载:http://blog.163.com/long_lh/blog/static/2769 ...

  3. JavaScript运算符

    JavaScript运算符 1.算数运算符 设定a = 5. 运算符 描述 例子 结果 + 加 b=a+2 b=7 - 减 b=a-2 b=3 * 乘 b=a*2 b=10 / 除 b=a/2 b=2 ...

  4. 【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree

    #-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init ...

  5. 使用Hibernate框架,新增数据后如何返回新增数据的全部信息

    一.需求描述:一个修改事物(TRANSACTION)先后包括子事物修改和子事物新增,修改事物完成后返回其子事物新增的全部数据  二.实现:若实现修改对象后还需要新增一个新对象,并返回新对象的需求,保存 ...

  6. Highlighting Text Item On Entry In Oracle Forms

    Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...

  7. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  8. 列联表(Crosstabs)

    四格表(2*2的列联表): Tmin为最小的频数:N为频数之和. 1 当 Tmin≥5,N≥40时, 用普通卡方检验公式;2 当1≦Tmin≦5, N≥40时, 用校正卡方检验公式;3 Tmin< ...

  9. Java编程思想读书笔记

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  10. CA*Layer(CAShapeLayer--CATextLayer)

    CAShapeLayer CAShapeLayer是一个通过矢量图形而不是bitmap来绘制的图层子类.你指定诸如颜色和线宽等属性,用CGPath来定义想要绘制的图 形,最后CAShapeLayer就 ...