BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / VR / Scripts / OVRCameraRig.cs

OVRCameraRig.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
OVRCameraRig.cs 16.9 KiB Download Raw
1/************************************************************************************
2Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
3
4Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
5the Utilities SDK except in compliance with the License, which is provided at the time of installation
6or download, or which otherwise accompanies this software in either electronic or hard copy form.
7
8You may obtain a copy of the License at
9https://developer.oculus.com/licenses/utilities-1.31
10
11Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
12under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
13ANY KIND, either express or implied. See the License for the specific language governing
14permissions and limitations under the License.
15************************************************************************************/
16
17using System;
18using System.Collections;
19using System.Collections.Generic;
20using UnityEngine;
21
22#if UNITY_2017_2_OR_NEWER
23using InputTracking = UnityEngine.XR.InputTracking;
24using Node = UnityEngine.XR.XRNode;
25#else
26using InputTracking = UnityEngine.VR.InputTracking;
27using Node = UnityEngine.VR.VRNode;
28#endif
29
30/// <summary>
31/// A head-tracked stereoscopic virtual reality camera rig.
32/// </summary>
33[ExecuteInEditMode]
34public class OVRCameraRig : MonoBehaviour
35{
36 /// <summary>
37 /// The left eye camera.
38 /// </summary>
39 public Camera leftEyeCamera { get { return (usePerEyeCameras) ? _leftEyeCamera : _centerEyeCamera; } }
40 /// <summary>
41 /// The right eye camera.
42 /// </summary>
43 public Camera rightEyeCamera { get { return (usePerEyeCameras) ? _rightEyeCamera : _centerEyeCamera; } }
44 /// <summary>
45 /// Provides a root transform for all anchors in tracking space.
46 /// </summary>
47 public Transform trackingSpace { get; private set; }
48 /// <summary>
49 /// Always coincides with the pose of the left eye.
50 /// </summary>
51 public Transform leftEyeAnchor { get; private set; }
52 /// <summary>
53 /// Always coincides with average of the left and right eye poses.
54 /// </summary>
55 public Transform centerEyeAnchor { get; private set; }
56 /// <summary>
57 /// Always coincides with the pose of the right eye.
58 /// </summary>
59 public Transform rightEyeAnchor { get; private set; }
60 /// <summary>
61 /// Always coincides with the pose of the left hand.
62 /// </summary>
63 public Transform leftHandAnchor { get; private set; }
64 /// <summary>
65 /// Always coincides with the pose of the right hand.
66 /// </summary>
67 public Transform rightHandAnchor { get; private set; }
68 /// <summary>
69 /// Anchors controller pose to fix offset issues for the left hand.
70 /// </summary>
71 public Transform leftControllerAnchor { get; private set; }
72 /// <summary>
73 /// Anchors controller pose to fix offset issues for the right hand.
74 /// </summary>
75 public Transform rightControllerAnchor { get; private set; }
76 /// <summary>
77 /// Always coincides with the pose of the sensor.
78 /// </summary>
79 public Transform trackerAnchor { get; private set; }
80 /// <summary>
81 /// Occurs when the eye pose anchors have been set.
82 /// </summary>
83 public event System.Action<OVRCameraRig> UpdatedAnchors;
84 /// <summary>
85 /// If true, separate cameras will be used for the left and right eyes.
86 /// </summary>
87 public bool usePerEyeCameras = false;
88 /// <summary>
89 /// If true, all tracked anchors are updated in FixedUpdate instead of Update to favor physics fidelity.
90 /// \note: If the fixed update rate doesn't match the rendering framerate (OVRManager.display.appFramerate), the anchors will visibly judder.
91 /// </summary>
92 public bool useFixedUpdateForTracking = false;
93 /// <summary>
94 /// If true, the cameras on the eyeAnchors will be disabled.
95 /// \note: The main camera of the game will be used to provide VR rendering. And the tracking space anchors will still be updated to provide reference poses.
96 /// </summary>
97 public bool disableEyeAnchorCameras = false;
98
99
100 protected bool _skipUpdate = false;
101 protected readonly string trackingSpaceName = "TrackingSpace";
102 protected readonly string trackerAnchorName = "TrackerAnchor";
103 protected readonly string leftEyeAnchorName = "LeftEyeAnchor";
104 protected readonly string centerEyeAnchorName = "CenterEyeAnchor";
105 protected readonly string rightEyeAnchorName = "RightEyeAnchor";
106 protected readonly string leftHandAnchorName = "LeftHandAnchor";
107 protected readonly string rightHandAnchorName = "RightHandAnchor";
108 protected readonly string leftControllerAnchorName = "LeftControllerAnchor";
109 protected readonly string rightControllerAnchorName = "RightControllerAnchor";
110 protected Camera _centerEyeCamera;
111 protected Camera _leftEyeCamera;
112 protected Camera _rightEyeCamera;
113
114#region Unity Messages
115 protected virtual void Awake()
116 {
117 _skipUpdate = true;
118 EnsureGameObjectIntegrity();
119 }
120
121 protected virtual void Start()
122 {
123 UpdateAnchors(true, true);
124 Application.onBeforeRender += OnBeforeRenderCallback;
125 }
126
127 protected virtual void FixedUpdate()
128 {
129 if (useFixedUpdateForTracking)
130 UpdateAnchors(true, true);
131 }
132
133 protected virtual void Update()
134 {
135 _skipUpdate = false;
136
137 if (!useFixedUpdateForTracking)
138 UpdateAnchors(true, true);
139 }
140
141 protected virtual void OnDestroy()
142 {
143 Application.onBeforeRender -= OnBeforeRenderCallback;
144 }
145#endregion
146
147 protected virtual void UpdateAnchors(bool updateEyeAnchors, bool updateHandAnchors)
148 {
149 if (!OVRManager.OVRManagerinitialized)
150 return;
151
152 EnsureGameObjectIntegrity();
153
154 if (!Application.isPlaying)
155 return;
156
157 if (_skipUpdate)
158 {
159 centerEyeAnchor.FromOVRPose(OVRPose.identity, true);
160 leftEyeAnchor.FromOVRPose(OVRPose.identity, true);
161 rightEyeAnchor.FromOVRPose(OVRPose.identity, true);
162
163 return;
164 }
165
166 bool monoscopic = OVRManager.instance.monoscopic;
167 bool hmdPresent = OVRNodeStateProperties.IsHmdPresent();
168
169 OVRPose tracker = OVRManager.tracker.GetPose();
170
171 trackerAnchor.localRotation = tracker.orientation;
172
173 Quaternion emulatedRotation = Quaternion.Euler(-OVRManager.instance.headPoseRelativeOffsetRotation.x, -OVRManager.instance.headPoseRelativeOffsetRotation.y, OVRManager.instance.headPoseRelativeOffsetRotation.z);
174
175 //Note: in the below code, when using UnityEngine's API, we only update anchor transforms if we have a new, fresh value this frame.
176 //If we don't, it could mean that tracking is lost, etc. so the pose should not change in the virtual world.
177 //This can be thought of as similar to calling InputTracking GetLocalPosition and Rotation, but only for doing so when the pose is valid.
178 //If false is returned for any of these calls, then a new pose is not valid and thus should not be updated.
179 if (updateEyeAnchors)
180 {
181 if (hmdPresent)
182 {
183 Vector3 centerEyePosition = Vector3.zero;
184 Quaternion centerEyeRotation = Quaternion.identity;
185
186 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.CenterEye, NodeStatePropertyType.Position, OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyePosition))
187 centerEyeAnchor.localPosition = centerEyePosition;
188 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.CenterEye, NodeStatePropertyType.Orientation, OVRPlugin.Node.EyeCenter, OVRPlugin.Step.Render, out centerEyeRotation))
189 centerEyeAnchor.localRotation = centerEyeRotation;
190 }
191 else
192 {
193 centerEyeAnchor.localRotation = emulatedRotation;
194 centerEyeAnchor.localPosition = OVRManager.instance.headPoseRelativeOffsetTranslation;
195 }
196
197 if (!hmdPresent || monoscopic)
198 {
199 leftEyeAnchor.localPosition = centerEyeAnchor.localPosition;
200 rightEyeAnchor.localPosition = centerEyeAnchor.localPosition;
201 leftEyeAnchor.localRotation = centerEyeAnchor.localRotation;
202 rightEyeAnchor.localRotation = centerEyeAnchor.localRotation;
203 }
204 else
205 {
206 Vector3 leftEyePosition = Vector3.zero;
207 Vector3 rightEyePosition = Vector3.zero;
208 Quaternion leftEyeRotation = Quaternion.identity;
209 Quaternion rightEyeRotation = Quaternion.identity;
210
211 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftEye, NodeStatePropertyType.Position, OVRPlugin.Node.EyeLeft, OVRPlugin.Step.Render, out leftEyePosition))
212 leftEyeAnchor.localPosition = leftEyePosition;
213 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightEye, NodeStatePropertyType.Position, OVRPlugin.Node.EyeRight, OVRPlugin.Step.Render, out rightEyePosition))
214 rightEyeAnchor.localPosition = rightEyePosition;
215 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftEye, NodeStatePropertyType.Orientation, OVRPlugin.Node.EyeLeft, OVRPlugin.Step.Render, out leftEyeRotation))
216 leftEyeAnchor.localRotation = leftEyeRotation;
217 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.RightEye, NodeStatePropertyType.Orientation, OVRPlugin.Node.EyeRight, OVRPlugin.Step.Render, out rightEyeRotation))
218 rightEyeAnchor.localRotation = rightEyeRotation;
219 }
220 }
221
222 if (updateHandAnchors)
223 {
224 //Need this for controller offset because if we're on OpenVR, we want to set the local poses as specified by Unity, but if we're not, OVRInput local position is the right anchor
225 if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
226 {
227 Vector3 leftPos = Vector3.zero;
228 Vector3 rightPos = Vector3.zero;
229 Quaternion leftQuat = Quaternion.identity;
230 Quaternion rightQuat = Quaternion.identity;
231
232 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.LeftHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandLeft, OVRPlugin.Step.Render, out leftPos))
233 leftHandAnchor.localPosition = leftPos;
234 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.RightHand, NodeStatePropertyType.Position, OVRPlugin.Node.HandRight, OVRPlugin.Step.Render, out rightPos))
235 rightHandAnchor.localPosition = rightPos;
236 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.LeftHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandLeft, OVRPlugin.Step.Render, out leftQuat))
237 leftHandAnchor.localRotation = leftQuat;
238 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.RightHand, NodeStatePropertyType.Orientation, OVRPlugin.Node.HandRight, OVRPlugin.Step.Render, out rightQuat))
239 rightHandAnchor.localRotation = rightQuat;
240
241 }
242 else
243 {
244 leftHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.LTouch);
245 rightHandAnchor.localPosition = OVRInput.GetLocalControllerPosition(OVRInput.Controller.RTouch);
246 leftHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.LTouch);
247 rightHandAnchor.localRotation = OVRInput.GetLocalControllerRotation(OVRInput.Controller.RTouch);
248 }
249
250 trackerAnchor.localPosition = tracker.position;
251
252 OVRPose leftOffsetPose = OVRPose.identity;
253 OVRPose rightOffsetPose = OVRPose.identity;
254 if (OVRManager.loadedXRDevice == OVRManager.XRDevice.OpenVR)
255 {
256 leftOffsetPose = OVRManager.GetOpenVRControllerOffset(Node.LeftHand);
257 rightOffsetPose = OVRManager.GetOpenVRControllerOffset(Node.RightHand);
258
259 //Sets poses of left and right nodes, local to the tracking space.
260 OVRManager.SetOpenVRLocalPose(trackingSpace.InverseTransformPoint(leftControllerAnchor.position),
261 trackingSpace.InverseTransformPoint(rightControllerAnchor.position),
262 Quaternion.Inverse(trackingSpace.rotation) * leftControllerAnchor.rotation,
263 Quaternion.Inverse(trackingSpace.rotation) * rightControllerAnchor.rotation);
264 }
265 rightControllerAnchor.localPosition = rightOffsetPose.position;
266 rightControllerAnchor.localRotation = rightOffsetPose.orientation;
267 leftControllerAnchor.localPosition = leftOffsetPose.position;
268 leftControllerAnchor.localRotation = leftOffsetPose.orientation;
269 }
270
271 RaiseUpdatedAnchorsEvent();
272 }
273
274 protected virtual void OnBeforeRenderCallback()
275 {
276 if (OVRManager.loadedXRDevice == OVRManager.XRDevice.Oculus) //Restrict late-update to only Oculus devices
277 {
278 bool controllersNeedUpdate = OVRManager.instance.LateControllerUpdate;
279#if USING_XR_SDK
280 //For the XR SDK, we need to late update head pose, not just the controllers, because the functionality
281 //is no longer built-in to the Engine. Under legacy, late camera update is done by default. In the XR SDK, you must use
282 //Tracked Pose Driver to get this by default, which we do not use. So, we have to manually late update camera poses.
283 UpdateAnchors(true, controllersNeedUpdate);
284#else
285 if (controllersNeedUpdate)
286 UpdateAnchors(false, true);
287#endif
288 }
289 }
290
291 protected virtual void RaiseUpdatedAnchorsEvent()
292 {
293 if (UpdatedAnchors != null)
294 {
295 UpdatedAnchors(this);
296 }
297 }
298
299 public virtual void EnsureGameObjectIntegrity()
300 {
301 bool monoscopic = OVRManager.instance != null ? OVRManager.instance.monoscopic : false;
302
303 if (trackingSpace == null)
304 trackingSpace = ConfigureAnchor(null, trackingSpaceName);
305
306 if (leftEyeAnchor == null)
307 leftEyeAnchor = ConfigureAnchor(trackingSpace, leftEyeAnchorName);
308
309 if (centerEyeAnchor == null)
310 centerEyeAnchor = ConfigureAnchor(trackingSpace, centerEyeAnchorName);
311
312 if (rightEyeAnchor == null)
313 rightEyeAnchor = ConfigureAnchor(trackingSpace, rightEyeAnchorName);
314
315 if (leftHandAnchor == null)
316 leftHandAnchor = ConfigureAnchor(trackingSpace, leftHandAnchorName);
317
318 if (rightHandAnchor == null)
319 rightHandAnchor = ConfigureAnchor(trackingSpace, rightHandAnchorName);
320
321 if (trackerAnchor == null)
322 trackerAnchor = ConfigureAnchor(trackingSpace, trackerAnchorName);
323
324 if (leftControllerAnchor == null)
325 leftControllerAnchor = ConfigureAnchor(leftHandAnchor, leftControllerAnchorName);
326
327 if (rightControllerAnchor == null)
328 rightControllerAnchor = ConfigureAnchor(rightHandAnchor, rightControllerAnchorName);
329
330 if (_centerEyeCamera == null || _leftEyeCamera == null || _rightEyeCamera == null)
331 {
332 _centerEyeCamera = centerEyeAnchor.GetComponent<Camera>();
333 _leftEyeCamera = leftEyeAnchor.GetComponent<Camera>();
334 _rightEyeCamera = rightEyeAnchor.GetComponent<Camera>();
335
336 if (_centerEyeCamera == null)
337 {
338 _centerEyeCamera = centerEyeAnchor.gameObject.AddComponent<Camera>();
339 _centerEyeCamera.tag = "MainCamera";
340 }
341
342 if (_leftEyeCamera == null)
343 {
344 _leftEyeCamera = leftEyeAnchor.gameObject.AddComponent<Camera>();
345 _leftEyeCamera.tag = "MainCamera";
346 }
347
348 if (_rightEyeCamera == null)
349 {
350 _rightEyeCamera = rightEyeAnchor.gameObject.AddComponent<Camera>();
351 _rightEyeCamera.tag = "MainCamera";
352 }
353
354 _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
355 _leftEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
356 _rightEyeCamera.stereoTargetEye = StereoTargetEyeMask.Right;
357 }
358
359 if (monoscopic && !OVRPlugin.EyeTextureArrayEnabled)
360 {
361 // Output to left eye only when in monoscopic mode
362 if (_centerEyeCamera.stereoTargetEye != StereoTargetEyeMask.Left)
363 {
364 _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Left;
365 }
366 }
367 else
368 {
369 if (_centerEyeCamera.stereoTargetEye != StereoTargetEyeMask.Both)
370 {
371 _centerEyeCamera.stereoTargetEye = StereoTargetEyeMask.Both;
372 }
373 }
374
375 if (disableEyeAnchorCameras)
376 {
377 _centerEyeCamera.enabled = false;
378 _leftEyeCamera.enabled = false;
379 _rightEyeCamera.enabled = false;
380 }
381 else
382 {
383 // disable the right eye camera when in monoscopic mode
384 if (_centerEyeCamera.enabled == usePerEyeCameras ||
385 _leftEyeCamera.enabled == !usePerEyeCameras ||
386 _rightEyeCamera.enabled == !(usePerEyeCameras && (!monoscopic || OVRPlugin.EyeTextureArrayEnabled)))
387 {
388 _skipUpdate = true;
389 }
390
391 _centerEyeCamera.enabled = !usePerEyeCameras;
392 _leftEyeCamera.enabled = usePerEyeCameras;
393 _rightEyeCamera.enabled = (usePerEyeCameras && (!monoscopic || OVRPlugin.EyeTextureArrayEnabled));
394
395 }
396 }
397
398 protected virtual Transform ConfigureAnchor(Transform root, string name)
399 {
400 Transform anchor = (root != null) ? root.Find(name) : null;
401
402 if (anchor == null)
403 {
404 anchor = transform.Find(name);
405 }
406
407 if (anchor == null)
408 {
409 anchor = new GameObject(name).transform;
410 }
411
412 anchor.name = name;
413 anchor.parent = (root != null) ? root : transform;
414 anchor.localScale = Vector3.one;
415 anchor.localPosition = Vector3.zero;
416 anchor.localRotation = Quaternion.identity;
417
418 return anchor;
419 }
420
421 public virtual Matrix4x4 ComputeTrackReferenceMatrix()
422 {
423 if (centerEyeAnchor == null)
424 {
425 Debug.LogError("centerEyeAnchor is required");
426 return Matrix4x4.identity;
427 }
428
429 // The ideal approach would be using UnityEngine.VR.VRNode.TrackingReference, then we would not have to depend on the OVRCameraRig. Unfortunately, it is not available in Unity 5.4.3
430
431 OVRPose headPose = OVRPose.identity;
432
433 Vector3 pos;
434 Quaternion rot;
435 if (OVRNodeStateProperties.GetNodeStatePropertyVector3(Node.Head, NodeStatePropertyType.Position, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out pos))
436 headPose.position = pos;
437 if (OVRNodeStateProperties.GetNodeStatePropertyQuaternion(Node.Head, NodeStatePropertyType.Orientation, OVRPlugin.Node.Head, OVRPlugin.Step.Render, out rot))
438 headPose.orientation = rot;
439
440 OVRPose invHeadPose = headPose.Inverse();
441 Matrix4x4 invHeadMatrix = Matrix4x4.TRS(invHeadPose.position, invHeadPose.orientation, Vector3.one);
442
443 Matrix4x4 ret = centerEyeAnchor.localToWorldMatrix * invHeadMatrix;
444
445 return ret;
446 }
447}