123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- using System;
- using UnityEngine;
- using Valve.VR;
- public class SteamVR_Controller
- {
- public static SteamVR_Controller.Device Input(int deviceIndex)
- {
- if (SteamVR_Controller.devices == null)
- {
- SteamVR_Controller.devices = new SteamVR_Controller.Device[16];
- uint num = 0U;
- while ((ulong)num < (ulong)((long)SteamVR_Controller.devices.Length))
- {
- SteamVR_Controller.devices[(int)((UIntPtr)num)] = new SteamVR_Controller.Device(num);
- num += 1U;
- }
- }
- return SteamVR_Controller.devices[deviceIndex];
- }
- public static void Update()
- {
- int num = 0;
- while ((long)num < 16L)
- {
- SteamVR_Controller.Input(num).Update();
- num++;
- }
- }
- public static int GetDeviceIndex(SteamVR_Controller.DeviceRelation relation, ETrackedDeviceClass deviceClass = ETrackedDeviceClass.Controller, int relativeTo = 0)
- {
- int result = -1;
- SteamVR_Utils.RigidTransform t = (relativeTo >= 16) ? SteamVR_Utils.RigidTransform.identity : SteamVR_Controller.Input(relativeTo).transform.GetInverse();
- CVRSystem system = OpenVR.System;
- if (system == null)
- {
- return result;
- }
- float num = float.MinValue;
- int num2 = 0;
- while ((long)num2 < 16L)
- {
- if (num2 != relativeTo && system.GetTrackedDeviceClass((uint)num2) == deviceClass)
- {
- SteamVR_Controller.Device device = SteamVR_Controller.Input(num2);
- if (device.connected)
- {
- if (relation == SteamVR_Controller.DeviceRelation.First)
- {
- return num2;
- }
- Vector3 vector = t * device.transform.pos;
- float num3;
- if (relation == SteamVR_Controller.DeviceRelation.FarthestRight)
- {
- num3 = vector.x;
- }
- else if (relation == SteamVR_Controller.DeviceRelation.FarthestLeft)
- {
- num3 = -vector.x;
- }
- else
- {
- Vector3 vector2 = new Vector3(vector.x, 0f, vector.z);
- Vector3 normalized = vector2.normalized;
- float num4 = Vector3.Dot(normalized, Vector3.forward);
- Vector3 vector3 = Vector3.Cross(normalized, Vector3.forward);
- if (relation == SteamVR_Controller.DeviceRelation.Leftmost)
- {
- num3 = ((vector3.y <= 0f) ? num4 : (2f - num4));
- }
- else
- {
- num3 = ((vector3.y >= 0f) ? num4 : (2f - num4));
- }
- }
- if (num3 > num)
- {
- result = num2;
- num = num3;
- }
- }
- }
- num2++;
- }
- return result;
- }
- private static SteamVR_Controller.Device[] devices;
- public class ButtonMask
- {
- public const ulong System = 1UL;
- public const ulong ApplicationMenu = 2UL;
- public const ulong Grip = 4UL;
- public const ulong Axis0 = 4294967296UL;
- public const ulong Axis1 = 8589934592UL;
- public const ulong Axis2 = 17179869184UL;
- public const ulong Axis3 = 34359738368UL;
- public const ulong Axis4 = 68719476736UL;
- public const ulong Touchpad = 4294967296UL;
- public const ulong Trigger = 8589934592UL;
- }
- public class Device
- {
- public Device(uint i)
- {
- this.index = i;
- }
- public uint index { get; private set; }
- public bool valid { get; private set; }
- public bool connected
- {
- get
- {
- this.Update();
- return this.pose.bDeviceIsConnected;
- }
- }
- public bool hasTracking
- {
- get
- {
- this.Update();
- return this.pose.bPoseIsValid;
- }
- }
- public bool outOfRange
- {
- get
- {
- this.Update();
- return this.pose.eTrackingResult == ETrackingResult.Running_OutOfRange || this.pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
- }
- }
- public bool calibrating
- {
- get
- {
- this.Update();
- return this.pose.eTrackingResult == ETrackingResult.Calibrating_InProgress || this.pose.eTrackingResult == ETrackingResult.Calibrating_OutOfRange;
- }
- }
- public bool uninitialized
- {
- get
- {
- this.Update();
- return this.pose.eTrackingResult == ETrackingResult.Uninitialized;
- }
- }
- public SteamVR_Utils.RigidTransform transform
- {
- get
- {
- this.Update();
- return new SteamVR_Utils.RigidTransform(this.pose.mDeviceToAbsoluteTracking);
- }
- }
- public Vector3 velocity
- {
- get
- {
- this.Update();
- return new Vector3(this.pose.vVelocity.v0, this.pose.vVelocity.v1, -this.pose.vVelocity.v2);
- }
- }
- public Vector3 angularVelocity
- {
- get
- {
- this.Update();
- return new Vector3(-this.pose.vAngularVelocity.v0, -this.pose.vAngularVelocity.v1, this.pose.vAngularVelocity.v2);
- }
- }
- public VRControllerState_t GetState()
- {
- this.Update();
- return this.state;
- }
- public VRControllerState_t GetPrevState()
- {
- this.Update();
- return this.prevState;
- }
- public TrackedDevicePose_t GetPose()
- {
- this.Update();
- return this.pose;
- }
- public void Update()
- {
- if (Time.frameCount != this.prevFrameCount)
- {
- this.prevFrameCount = Time.frameCount;
- this.prevState = this.state;
- CVRSystem system = OpenVR.System;
- if (system != null)
- {
- this.valid = system.GetControllerStateWithPose(SteamVR_Render.instance.trackingSpace, this.index, ref this.state, ref this.pose);
- this.UpdateHairTrigger();
- }
- }
- }
- public bool GetPress(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonPressed & buttonMask) != 0UL;
- }
- public bool GetPressDown(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonPressed & buttonMask) != 0UL && (this.prevState.ulButtonPressed & buttonMask) == 0UL;
- }
- public bool GetPressUp(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonPressed & buttonMask) == 0UL && (this.prevState.ulButtonPressed & buttonMask) != 0UL;
- }
- public bool GetPress(EVRButtonId buttonId)
- {
- return this.GetPress(1UL << (int)buttonId);
- }
- public bool GetPressDown(EVRButtonId buttonId)
- {
- return this.GetPressDown(1UL << (int)buttonId);
- }
- public bool GetPressUp(EVRButtonId buttonId)
- {
- return this.GetPressUp(1UL << (int)buttonId);
- }
- public bool GetTouch(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonTouched & buttonMask) != 0UL;
- }
- public bool GetTouchDown(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonTouched & buttonMask) != 0UL && (this.prevState.ulButtonTouched & buttonMask) == 0UL;
- }
- public bool GetTouchUp(ulong buttonMask)
- {
- this.Update();
- return (this.state.ulButtonTouched & buttonMask) == 0UL && (this.prevState.ulButtonTouched & buttonMask) != 0UL;
- }
- public bool GetTouch(EVRButtonId buttonId)
- {
- return this.GetTouch(1UL << (int)buttonId);
- }
- public bool GetTouchDown(EVRButtonId buttonId)
- {
- return this.GetTouchDown(1UL << (int)buttonId);
- }
- public bool GetTouchUp(EVRButtonId buttonId)
- {
- return this.GetTouchUp(1UL << (int)buttonId);
- }
- public Vector2 GetAxis(EVRButtonId buttonId = EVRButtonId.k_EButton_Axis0)
- {
- this.Update();
- switch (buttonId)
- {
- case EVRButtonId.k_EButton_Axis0:
- return new Vector2(this.state.rAxis0.x, this.state.rAxis0.y);
- case EVRButtonId.k_EButton_Axis1:
- return new Vector2(this.state.rAxis1.x, this.state.rAxis1.y);
- case EVRButtonId.k_EButton_Axis2:
- return new Vector2(this.state.rAxis2.x, this.state.rAxis2.y);
- case EVRButtonId.k_EButton_Axis3:
- return new Vector2(this.state.rAxis3.x, this.state.rAxis3.y);
- case EVRButtonId.k_EButton_Axis4:
- return new Vector2(this.state.rAxis4.x, this.state.rAxis4.y);
- default:
- return Vector2.zero;
- }
- }
- public void TriggerHapticPulse(ushort durationMicroSec = 500, EVRButtonId buttonId = EVRButtonId.k_EButton_Axis0)
- {
- CVRSystem system = OpenVR.System;
- if (system != null)
- {
- uint unAxisId = (uint)(buttonId - EVRButtonId.k_EButton_Axis0);
- system.TriggerHapticPulse(this.index, unAxisId, (char)durationMicroSec);
- }
- }
- private void UpdateHairTrigger()
- {
- this.hairTriggerPrevState = this.hairTriggerState;
- float x = this.state.rAxis1.x;
- if (this.hairTriggerState)
- {
- if (x < this.hairTriggerLimit - this.hairTriggerDelta || x <= 0f)
- {
- this.hairTriggerState = false;
- }
- }
- else if (x > this.hairTriggerLimit + this.hairTriggerDelta || x >= 1f)
- {
- this.hairTriggerState = true;
- }
- this.hairTriggerLimit = ((!this.hairTriggerState) ? Mathf.Min(this.hairTriggerLimit, x) : Mathf.Max(this.hairTriggerLimit, x));
- }
- public bool GetHairTrigger()
- {
- this.Update();
- return this.hairTriggerState;
- }
- public bool GetHairTriggerDown()
- {
- this.Update();
- return this.hairTriggerState && !this.hairTriggerPrevState;
- }
- public bool GetHairTriggerUp()
- {
- this.Update();
- return !this.hairTriggerState && this.hairTriggerPrevState;
- }
- private VRControllerState_t state;
- private VRControllerState_t prevState;
- private TrackedDevicePose_t pose;
- private int prevFrameCount = -1;
- public float hairTriggerDelta = 0.1f;
- private float hairTriggerLimit;
- private bool hairTriggerState;
- private bool hairTriggerPrevState;
- }
- public enum DeviceRelation
- {
- First,
- Leftmost,
- Rightmost,
- FarthestLeft,
- FarthestRight
- }
- }
|