一、HTC Vive简介##
二、一套完整的HTC Vive设备##
三、HTC Vive手柄解析##
SteamVR_TrackedObject脚本详解
SteamVR_TrackedObject 脚本展示详解:
using UnityEngine;
using Valve.VR;
public class SteamVR_TrackedObject : MonoBehaviour
{
public enum EIndex//track追踪,手柄设备的各个按钮信息的获取
{
None = -1,
Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
Device1,
Device2,
Device3,
Device4,
Device5,
Device6,
Device7,
Device8,
Device9,
Device10,
Device11,
Device12,
Device13,
Device14,
Device15
}
public EIndex index;
public Transform origin; // if not set, relative to parent
public bool isValid = false;
private void OnNewPoses(TrackedDevicePose_t[] poses)
{
if (index == EIndex.None)
return;
var i = (int)index;
isValid = false;
if (poses.Length <= i)
return;
if (!poses[i].bDeviceIsConnected)
return;
if (!poses[i].bPoseIsValid)
return;
isValid = true;
var pose = new SteamVR_Utils.RigidTransform(poses[i].mDeviceToAbsoluteTracking);
if (origin != null)
{
transform.position = origin.transform.TransformPoint(pose.pos);
transform.rotation = origin.rotation * pose.rot;
}
else
{
transform.localPosition = pose.pos;
transform.localRotation = pose.rot;
}
}
SteamVR_Events.Action newPosesAction;
void Awake()
{
newPosesAction = SteamVR_Events.NewPosesAction(OnNewPoses);
}
void OnEnable()
{
var render = SteamVR_Render.instance;
if (render == null)
{
enabled = false;
return;
}
newPosesAction.enabled = true;
}
void OnDisable()
{
newPosesAction.enabled = false;
isValid = false;
}
public void SetDeviceIndex(int index)
{
if (System.Enum.IsDefined(typeof(EIndex), index))
this.index = (EIndex)index;
}
}
SteamVR_Controller 脚本控制:获取手柄的信息及交互的方法
using UnityEngine;
using Valve.VR;
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);
}
public class Device
{
public Device(uint i) { index = i; }
public uint index { get; private set; }
public bool valid { get; private set; }
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.
public SteamVR_Utils.RigidTransform transform { get { Update(); return new SteamVR_Utils.RigidTransform(pose.mDeviceToAbsoluteTracking); } }
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); } }
public VRControllerState_t GetState() { Update(); return state; }
public VRControllerState_t GetPrevState() { Update(); return prevState; }
public TrackedDevicePose_t GetPose() { Update(); return pose; }
VRControllerState_t state, prevState;
TrackedDevicePose_t pose;
int prevFrameCount = -1;
public void Update()
{
if (Time.frameCount != prevFrameCount)
{
prevFrameCount = Time.frameCount;
prevState = state;
var system = OpenVR.System;
if (system != null)
{
valid = system.GetControllerStateWithPose(SteamVR_Render.instance.trackingSpace, index, ref state, (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(VRControllerState_t)), ref pose);
UpdateHairTrigger();
}
}
}
//调用的交互方法
public bool GetPress(ulong buttonMask) { Update(); return (state.ulButtonPressed & buttonMask) != 0; }
public bool GetPressDown(ulong buttonMask) { Update(); return (state.ulButtonPressed & buttonMask) != 0 && (prevState.ulButtonPressed & buttonMask) == 0; }
public bool GetPressUp(ulong buttonMask) { Update(); return (state.ulButtonPressed & buttonMask) == 0 && (prevState.ulButtonPressed & buttonMask) != 0; }
public bool GetPress(EVRButtonId buttonId) { return GetPress(1ul << (int)buttonId); }
public bool GetPressDown(EVRButtonId buttonId) { return GetPressDown(1ul << (int)buttonId); }
public bool GetPressUp(EVRButtonId buttonId) { return GetPressUp(1ul << (int)buttonId); }
public bool GetTouch(ulong buttonMask) { Update(); return (state.ulButtonTouched & buttonMask) != 0; }
public bool GetTouchDown(ulong buttonMask) { Update(); return (state.ulButtonTouched & buttonMask) != 0 && (prevState.ulButtonTouched & buttonMask) == 0; }
public bool GetTouchUp(ulong buttonMask) { Update(); return (state.ulButtonTouched & buttonMask) == 0 && (prevState.ulButtonTouched & buttonMask) != 0; }
public bool GetTouch(EVRButtonId buttonId) { return GetTouch(1ul << (int)buttonId); }
public bool GetTouchDown(EVRButtonId buttonId) { return GetTouchDown(1ul << (int)buttonId); }
public bool GetTouchUp(EVRButtonId buttonId) { return GetTouchUp(1ul << (int)buttonId); }
public Vector2 GetAxis(EVRButtonId buttonId = EVRButtonId.k_EButton_SteamVR_Touchpad)
{
Update();
var axisId = (uint)buttonId - (uint)EVRButtonId.k_EButton_Axis0;
switch (axisId)
{
case 0: return new Vector2(state.rAxis0.x, state.rAxis0.y);
case 1: return new Vector2(state.rAxis1.x, state.rAxis1.y);
case 2: return new Vector2(state.rAxis2.x, state.rAxis2.y);
case 3: return new Vector2(state.rAxis3.x, state.rAxis3.y);
case 4: return new Vector2(state.rAxis4.x, state.rAxis4.y);
}
return Vector2.zero;
}
public void TriggerHapticPulse(ushort durationMicroSec = 500, EVRButtonId buttonId = EVRButtonId.k_EButton_SteamVR_Touchpad)
{
var system = OpenVR.System;
if (system != null)
{
var axisId = (uint)buttonId - (uint)EVRButtonId.k_EButton_Axis0;
system.TriggerHapticPulse(index, axisId, (char)durationMicroSec);
}
}
public float hairTriggerDelta = 0.1f; // amount trigger must be pulled or released to change state
float hairTriggerLimit;
bool hairTriggerState, hairTriggerPrevState;
void UpdateHairTrigger()
{
hairTriggerPrevState = hairTriggerState;
var value = state.rAxis1.x; // trigger
if (hairTriggerState)
{
if (value < hairTriggerLimit - hairTriggerDelta || value <= 0.0f)
hairTriggerState = false;
}
else
{
if (value > hairTriggerLimit + hairTriggerDelta || value >= 1.0f)
hairTriggerState = true;
}
hairTriggerLimit = hairTriggerState ? Mathf.Max(hairTriggerLimit, value) : Mathf.Min(hairTriggerLimit, value);
}
public bool GetHairTrigger() { Update(); return hairTriggerState; }
public bool GetHairTriggerDown() { Update(); return hairTriggerState && !hairTriggerPrevState; }
public bool GetHairTriggerUp() { Update(); return !hairTriggerState && hairTriggerPrevState; }
}
private static Device[] devices;
public static Device Input(int deviceIndex)
{
if (devices == null)
{
devices = new Device[OpenVR.k_unMaxTrackedDeviceCount];
for (uint i = 0; i < devices.Length; i++)
devices[i] = new Device(i);
}
return devices[deviceIndex];
}
public static void Update()
{
for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
Input(i).Update();
}
// This helper can be used in a variety of ways. Beware that indices may change
// as new devices are dynamically added or removed, controllers are physically
// swapped between hands, arms crossed, etc.
public enum DeviceRelation
{
First,
// radially
Leftmost,
Rightmost,
// distance - also see vr.hmd.GetSortedTrackedDeviceIndicesOfClass
FarthestLeft,
FarthestRight,
}
public static int GetDeviceIndex(DeviceRelation relation,
ETrackedDeviceClass deviceClass = ETrackedDeviceClass.Controller,
int relativeTo = (int)OpenVR.k_unTrackedDeviceIndex_Hmd) // use -1 for absolute tracking space
{
var result = -1;
var invXform = ((uint)relativeTo < OpenVR.k_unMaxTrackedDeviceCount) ?
Input(relativeTo).transform.GetInverse() : SteamVR_Utils.RigidTransform.identity;
var system = OpenVR.System;
if (system == null)
return result;
var best = -float.MaxValue;
for (int i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
{
if (i == relativeTo || system.GetTrackedDeviceClass((uint)i) != deviceClass)
continue;
var device = Input(i);
if (!device.connected)
continue;
if (relation == DeviceRelation.First)
return i;
float score;
var pos = invXform * device.transform.pos;
if (relation == DeviceRelation.FarthestRight)
{
score = pos.x;
}
else if (relation == DeviceRelation.FarthestLeft)
{
score = -pos.x;
}
else
{
var dir = new Vector3(pos.x, 0.0f, pos.z).normalized;
var dot = Vector3.Dot(dir, Vector3.forward);
var cross = Vector3.Cross(dir, Vector3.forward);
if (relation == DeviceRelation.Leftmost)
{
score = (cross.y > 0.0f) ? 2.0f - dot : dot;
}
else
{
score = (cross.y < 0.0f) ? 2.0f - dot : dot;
}
}
if (score > best)
{
result = i;
best = score;
}
}
return result;
}
}
using UnityEngine;
using System.Collections;
//检测手柄功能的脚本 这个脚本挂到手柄上(controler(right)和controler(left))上
public class Test_TrackObject : MonoBehaviour {
//手柄
SteamVR_TrackedObject trackdeObjec;
void Awake() {
//获取手柄上的这个组件
trackdeObjec = GetComponent();
}
// Use this for initialization
void Start () {
}
void FixedUpdate()
{ //获取手柄输入
var device = SteamVR_Controller.Input((int)trackdeObjec.index);
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” “ ”");
}
//方向圆盘:
//这里开始区分了press检测与touch检测的不同之处,圆盘可以触摸,顾名思义,touch检测的是触摸,press检测的是按动 //Axis0键 与圆盘有交互 与圆盘有关 . //触摸触发
if (device.GetTouchDown(SteamVR_Conler.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;
}
}