SteamVR_TrackedObject.cs
2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//======= Copyright (c) Valve Corporation, All rights reserved. ===============
//
// Purpose: For controlling in-game objects with tracked devices.
//
//=============================================================================
using UnityEngine;
using Valve.VR;
namespace 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,
Device16
}
public EIndex index;
[Tooltip("If not set, relative to parent")]
public Transform origin;
public bool isValid { get; private set; }
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;
SteamVR_TrackedObject()
{
newPosesAction = SteamVR_Events.NewPosesAction(OnNewPoses);
}
private void Awake()
{
OnEnable();
}
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;
}
}
}