-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathFromXRHmdDataSource.cs
168 lines (133 loc) · 4.9 KB
/
FromXRHmdDataSource.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Meta Platforms, Inc. and affiliates.
#if HAS_META_INTERACTION
using Oculus.Interaction;
using Oculus.Interaction.Input;
using UnityEngine;
using UnityEngine.Assertions;
namespace Meta.Utilities.Input
{
[DefaultExecutionOrder(-80)]
public class FromXRHmdDataSource : DataSource<HmdDataAsset>
{
[SerializeField]
private Vector3 m_rootOffset;
[SerializeField]
private Vector3 m_rootAngleOffset;
[Header("XR Data Source")]
[SerializeField, Interface(typeof(IOVRCameraRigRef))]
private MonoBehaviour m_cameraRigRef;
private IOVRCameraRigRef CameraRigRef { get; set; }
[SerializeField]
private bool m_processLateUpdates;
[Header("Shared Configuration")]
[SerializeField, Interface(typeof(ITrackingToWorldTransformer))]
private MonoBehaviour m_trackingToWorldTransformer;
private ITrackingToWorldTransformer TrackingToWorldTransformer { get; set; }
#if HAS_NAUGHTY_ATTRIBUTES
[NaughtyAttributes.ShowNativeProperty]
#endif
private Vector3 CurrentRootPosition => DataAsset?.Root.position ?? Vector3.zero;
public bool ProcessLateUpdates
{
get => m_processLateUpdates;
set => m_processLateUpdates = value;
}
private readonly HmdDataAsset m_hmdDataAsset = new();
private HmdDataSourceConfig m_config;
private Pose m_poseOffset;
protected override HmdDataAsset DataAsset => m_hmdDataAsset;
protected void Awake()
{
TrackingToWorldTransformer = m_trackingToWorldTransformer as ITrackingToWorldTransformer;
CameraRigRef = m_cameraRigRef as IOVRCameraRigRef;
UpdateConfig();
}
protected override void Start()
{
this.BeginStart(ref _started, base.Start);
Assert.IsNotNull(CameraRigRef);
Assert.IsNotNull(TrackingToWorldTransformer);
var offset = new Pose(m_rootOffset, Quaternion.Euler(m_rootAngleOffset));
m_poseOffset = offset;
UpdateConfig();
this.EndStart(ref _started);
}
protected override void OnEnable()
{
base.OnEnable();
if (_started)
{
CameraRigRef.WhenInputDataDirtied += HandleInputDataDirtied;
}
}
protected override void OnDisable()
{
if (_started)
{
CameraRigRef.WhenInputDataDirtied -= HandleInputDataDirtied;
}
base.OnDisable();
}
private void HandleInputDataDirtied(bool isLateUpdate)
{
if (isLateUpdate && !m_processLateUpdates)
{
return;
}
MarkInputDataRequiresUpdate();
}
private HmdDataSourceConfig Config
{
get
{
if (m_config != null)
{
return m_config;
}
m_config = new();
return m_config;
}
}
private void UpdateConfig()
{
Config.TrackingToWorldTransformer = TrackingToWorldTransformer;
}
protected override void UpdateData()
{
m_hmdDataAsset.Config = Config;
m_hmdDataAsset.IsTracked = true;
m_hmdDataAsset.FrameId = Time.frameCount;
// Convert controller pose from world to tracking space.
var centerEyeAnchor = CameraRigRef.CameraRig.centerEyeAnchor;
var pose = new Pose(centerEyeAnchor.position, centerEyeAnchor.rotation);
pose = Config.TrackingToWorldTransformer.ToTrackingPose(pose);
PoseUtils.Multiply(pose, m_poseOffset, ref m_hmdDataAsset.Root);
}
#region Inject
public void InjectAllFromOVRControllerHandDataSource(UpdateModeFlags updateMode, IDataSource updateAfter,
ITrackingToWorldTransformer trackingToWorldTransformer,
IDataSource<HmdDataAsset> hmdData,
Vector3 rootOffset, Vector3 rootAngleOffset)
{
InjectAllDataSource(updateMode, updateAfter);
InjectTrackingToWorldTransformer(trackingToWorldTransformer);
InjectRootOffset(rootOffset);
InjectRootAngleOffset(rootAngleOffset);
}
public void InjectTrackingToWorldTransformer(ITrackingToWorldTransformer trackingToWorldTransformer)
{
m_trackingToWorldTransformer = trackingToWorldTransformer as MonoBehaviour;
TrackingToWorldTransformer = trackingToWorldTransformer;
}
public void InjectRootOffset(Vector3 rootOffset)
{
m_rootOffset = rootOffset;
}
public void InjectRootAngleOffset(Vector3 rootAngleOffset)
{
m_rootAngleOffset = rootAngleOffset;
}
#endregion
}
}
#endif