Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
OVRT_TrackedObject.cs
Go to the documentation of this file.
1using UnityEngine;
2using UnityEngine.Events;
3using Valve.VR;
4
5namespace Spelunx.Vive
6{
7 /// <summary>
8 /// Maps tracked OpenVR poses to transform by device index.
9 /// </summary>
11 {
12 public enum EIndex
13 {
14 None = -1,
15 Hmd = (int)OpenVR.k_unTrackedDeviceIndex_Hmd,
16 Device1,
17 Device2,
18 Device3,
19 Device4,
20 Device5,
21 Device6,
22 Device7,
23 Device8,
24 Device9,
32 }
33
34 public EIndex index;
35 [Tooltip("If not set, relative to parent")]
36
37 private UnityAction<TrackedDevicePose_t[]> _onNewPosesAction;
38
39 private void OnDeviceConnected(int index, bool connected)
40 {
41 if ((int)this.index == index)
42 {
44 IsConnected = connected;
45
47 }
48 }
49
50 private void OnNewPoses(TrackedDevicePose_t[] poses)
51 {
52 if (index == EIndex.None)
53 return;
54
55 var i = DeviceIndex;
56
57 IsValid = false;
58
59 if (i < 0 || poses.Length <= i)
60 return;
61
62 if (!poses[i].bDeviceIsConnected)
63 return;
64
65 if (!poses[i].bPoseIsValid)
66 return;
67
68 IsValid = true;
69
70 var pose = new OVRT_Utils.RigidTransform(poses[i].mDeviceToAbsoluteTracking);
71
72 if (origin != null)
73 {
74 transform.position = origin.transform.TransformPoint(pose.pos);
75 transform.rotation = origin.rotation * pose.rot;
76 }
77 else
78 {
79 transform.localPosition = pose.pos;
80 transform.localRotation = pose.rot;
81 }
82 }
83
84 private void Awake()
85 {
86 _onNewPosesAction += OnNewPoses;
87 _onDeviceConnectedAction += OnDeviceConnected;
88 }
89
90 private void OnEnable()
91 {
92 DeviceIndex = (int)index;
94
95 OVRT_Events.NewPoses.AddListener(_onNewPosesAction);
96 OVRT_Events.TrackedDeviceConnected.AddListener(_onDeviceConnectedAction);
97 }
98
99 private void OnDisable()
100 {
101 OVRT_Events.NewPoses.RemoveListener(_onNewPosesAction);
102 IsValid = false;
103 IsConnected = false;
104 }
105 }
106}
UnityAction< int, bool > _onDeviceConnectedAction
Maps tracked OpenVR poses to transform by device index.