核心类简介:
SteamVR_TrackedObject
此类用于根据硬件设备,并为硬件设备分配相应的索引
SteamVR_Controller.Device
最重要的类,封装了跟踪设备的全部信息,例如手柄的各种交互相应勾动扳机等。
SteamVR_Controller.ButtonMask
手柄各按键的名称
SteamVR_Controller
此类通常使用静态方法Input根据设备索引值获取对应的设备(Device)对象
每个按钮的名称都在ButtonMask中定义好了:
public class SteamVR_Controller
{
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);
}
}
实际上在SteamVR_TrackedObject类枚举类型EIndex中,已经预先定义了16个设备索引,头盔默认索引为0
using UnityEngine;
using Valve.VR;
public class SteamVR_TrackedObject : MonoBehaviour
{
public enum EIndex
{
None = -1,
Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
Device1,
Device2,
Device3,
Device4,
Device5,
Device6,
Device7,
Device8,
Device9,
Device10,
Device11,
Device12,
Device13,
Device14,
Device15
}
}
简单示例:
1,通过手柄游戏对象上的SteamVR_TrackedObject组件进行获取,,,
//获取跟踪对象 此时通过trackObj.index 就可以访问到设备对象
SteamVR_TrackedObject trackObj = this.GetComponent();
2,通过找到索引获取到设备(Device)对象,
这时就用到了SteamVR_Controller.Input方法传入上面获取的到设备索引,就可以访问到设备中的方法,相应手柄操作,,,
//通过设备索引获取到相应设备对象Device
SteamVR_Controller.Device device= SteamVR_Controller.Input((int)trackObj.index);
3,调用Divice中的相应函数,来获取手柄的按键操作,,,
按键: Device.GetPressDown(按下) / GetPressUp(抬起) / GetPress(一直按)
触摸键:Device.GetTouchDown / GetTouchUp / GetTouch
void Update
{
//按下Tigger键
if(device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger)
{
Debug.Log("您按下了Trigger");
}
}
触摸板:
通过获取用户触摸操作,然后根据当前的状态进行获取,,,Touchpad圆盘坐标是和平面直角坐标系一致,,上下和左右都是(-1,1)值,,,
void Update
{
//按下Touchpad键
if(device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad)
{
Debug.Log("您按下了Touchpad");
}
}
手柄的震动: TriggerHapicPulse
手柄震动是通过TriggerHapicPulsefangf ,第一个参数可以理解为震动强度,默认500,不要超过4000,,第二个参数默认就好,不用填写
简例:按下Trigger触发震动:
private SteamVR_Controller.Device device;
void Strar(){
//获取设备
device = SteamVR_Contorller.Input((int)GetComponent().index)
}
void Update{
if(device.GetPress(SteamVR_Controller.ButtonMask.Trigger))
{
device.Trigger.TriggerHapicPulse(1000);
}
}
Device常用属性:
valid:GetControllerStateWithPose() 函数调用是否成功;
connected:判断设备是否连接
hasTracking :判断设备是否正常跟踪
结果得到三个参数:
outOfRange:判断设备是否超出范围
calibrating:判断设备是否正在校正
uninitialized:判断设备是否未初始化
transform:获取的结果是包含12个元素的一维数组,通过SteamVR_Utils.RigidTransform函数将12元素重组为3×4的矩阵针对Unity的坐标系进行修正,同时添加了怼position和rotation的方便的引用
velocity和angularVelocity:两个速度也针对Unity的坐标系进行修正,Lighthouse跟踪的空间轴方向与Unity存在偏差,,,
上面描述所对应的SteamVR插件中写好的类:
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; } }
//获取的结果是包含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); } }