重要类(ButtonMask、Device、Input、Update、DeviceRelation与GetDeviceIndex)
SteamVR_Controller是非Monobehavior的脚本,并没有挂在场景下,其运行是通过SteamVR_Render脚本对于其中Update()函数的调用从而一直循环获取手柄相关信息。而SteamVR_Render脚本是在程序运行时自动加载至场景的。先来看看脚本的全貌吧。
先来看看这几个重要类

ButtonMask

1 - 菜单键
6 - 系统键(按下后手柄断开连接,再次按下手柄再次连接上)
7 - 扳机键
8 - 握持键
9 - 触摸板键

Device
首先先看这前面这几个简单函数的解释把
public class Device//最重要的类,封装了跟踪设备的全部名称
{
public Device(uint i) { index = i; }
public uint index { get; private set; }
public bool valid { get; private set; }//GetControllerStateWithPose()函数调用是否成功;
public bool connected { get { Update(); return pose.bDeviceIsConnected; } }//判断设备是否连接
public bool hasTracking { get { Update(); return pose.bPoseIsValid; } }//判断设备是否跟踪正常
//判断设备是否超出范围
public bool outOfRange { get { Update(); return pose.eTrackingResult == ETrackingResult.Running_OutOfRange || pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange; } }
//判断设备是否正在校正
public bool calibrating { get { Update(); return pose.eTrackingResult == ETrackingResult.Calibrating_InProgress || pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange; } }
//判断设备是否未初始化
public bool uninitialized { get { Update(); return pose.eTrackingResult == ETrackingResult.Uninitialized; } }
// These values are only accurate for the last controller state change (e.g. trigger release), and by definition, will always lag behind
// the predicted visual poses that drive SteamVR_TrackedObjects since they are sync'd to the input timestamp that caused them to update.
//获取的结果是包含12个元素的一维数组,通过SteamVR_Utils.RigidTransform函数
//将12个元素重组为3X4矩阵并针对Unity的坐标系进行修正,同时添加了对position和rotation方便的引用
public SteamVR_Utils.RigidTransform transform { get { Update(); return new SteamVR_Utils.RigidTransform(pose.mDeviceToAbsoluteTracking); } }
//velocity和angularVelocity :这两个速度也针对Unity的坐标系进行修正,lighthouse跟踪的空间轴方向与Unity存在偏差。
public Vector3 velocity { get { Update(); return new Vector3(pose.vVelocity.v0, pose.vVelocity.v1, -pose.vVelocity.v2); } }
public Vector3 angularVelocity { get { Update(); return new Vector3(-pose.vAngularVelocity.v0, -pose.vAngularVelocity.v1, pose.vAngularVelocity.v2); } }
下面我们再来看看下面这几个按键函数的应用

针对按键的三个函数,每种函数有两个重载,参数为ButtonMask或EVRButtonId类,第二种形式会将EVRButtonId转换为ButtonMask后调用第一种得到结果。
针对Touchpad的三个函数,一样的两种形式,同上不解释。
// 按下了扳机按钮调用
if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
{
Debug.Log("GetTouch");
}
//第二种方法
if (device.GetPressDown(Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger))
{
print("GetPressDown");
}
关于GetAxis

关于TriggerHapticPulse

关于这GetHairTrigger/GetHairTriggerDown/GetHairTriggerUp

检测自定义的一种操作:轻按Trigger ,这三个函数代表的是我们用手指轻轻触碰到触碰板并不需要按下及会产生反应,HairTrigger是检测当你握住扳手超过一个固定值(0.1,可调)时即触发的状态。相当于利用Trigger的变化量来做一个功能的触发,很棒不是吗。轻轻碰一下Trigger使得行程大于0.1即可触发一个功能。
Device中的Update

网友评论