| 1 | /************************************************************************************ |
| 2 | Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. |
| 3 | |
| 4 | Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use |
| 5 | the Utilities SDK except in compliance with the License, which is provided at the time of installation |
| 6 | or download, or which otherwise accompanies this software in either electronic or hard copy form. |
| 7 | |
| 8 | You may obtain a copy of the License at |
| 9 | https://developer.oculus.com/licenses/utilities-1.31 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, the Utilities SDK distributed |
| 12 | under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 13 | ANY KIND, either express or implied. See the License for the specific language governing |
| 14 | permissions and limitations under the License. |
| 15 | ************************************************************************************/ |
| 16 | |
| 17 | using System; |
| 18 | using System.Runtime.InteropServices; |
| 19 | using UnityEngine; |
| 20 | using VR = UnityEngine.VR; |
| 21 | |
| 22 | /// <summary> |
| 23 | /// An infrared camera that tracks the position of a head-mounted display. |
| 24 | /// </summary> |
| 25 | public class OVRTracker |
| 26 | { |
| 27 | /// <summary> |
| 28 | /// The (symmetric) visible area in front of the sensor. |
| 29 | /// </summary> |
| 30 | public struct Frustum |
| 31 | { |
| 32 | /// <summary> |
| 33 | /// The sensor's minimum supported distance to the HMD. |
| 34 | /// </summary> |
| 35 | public float nearZ; |
| 36 | /// <summary> |
| 37 | /// The sensor's maximum supported distance to the HMD. |
| 38 | /// </summary> |
| 39 | public float farZ; |
| 40 | /// <summary> |
| 41 | /// The sensor's horizontal and vertical fields of view in degrees. |
| 42 | /// </summary> |
| 43 | public Vector2 fov; |
| 44 | } |
| 45 | |
| 46 | /// <summary> |
| 47 | /// If true, a sensor is attached to the system. |
| 48 | /// </summary> |
| 49 | public bool isPresent |
| 50 | { |
| 51 | get { |
| 52 | if (!OVRManager.isHmdPresent) |
| 53 | return false; |
| 54 | |
| 55 | return OVRPlugin.positionSupported; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// <summary> |
| 60 | /// If true, the sensor is actively tracking the HMD's position. Otherwise the HMD may be temporarily occluded, the system may not support position tracking, etc. |
| 61 | /// </summary> |
| 62 | public bool isPositionTracked |
| 63 | { |
| 64 | get { |
| 65 | return OVRPlugin.positionTracked; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// <summary> |
| 70 | /// If this is true and a sensor is available, the system will use position tracking when isPositionTracked is also true. |
| 71 | /// </summary> |
| 72 | public bool isEnabled |
| 73 | { |
| 74 | get { |
| 75 | if (!OVRManager.isHmdPresent) |
| 76 | return false; |
| 77 | |
| 78 | return OVRPlugin.position; |
| 79 | } |
| 80 | |
| 81 | set { |
| 82 | if (!OVRManager.isHmdPresent) |
| 83 | return; |
| 84 | |
| 85 | OVRPlugin.position = value; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Returns the number of sensors currently connected to the system. |
| 91 | /// </summary> |
| 92 | public int count |
| 93 | { |
| 94 | get { |
| 95 | int count = 0; |
| 96 | |
| 97 | for (int i = 0; i < (int)OVRPlugin.Tracker.Count; ++i) |
| 98 | { |
| 99 | if (GetPresent(i)) |
| 100 | count++; |
| 101 | } |
| 102 | |
| 103 | return count; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// <summary> |
| 108 | /// Gets the sensor's viewing frustum. |
| 109 | /// </summary> |
| 110 | public Frustum GetFrustum(int tracker = 0) |
| 111 | { |
| 112 | if (!OVRManager.isHmdPresent) |
| 113 | return new Frustum(); |
| 114 | |
| 115 | return OVRPlugin.GetTrackerFrustum((OVRPlugin.Tracker)tracker).ToFrustum(); |
| 116 | } |
| 117 | |
| 118 | /// <summary> |
| 119 | /// Gets the sensor's pose, relative to the head's pose at the time of the last pose recentering. |
| 120 | /// </summary> |
| 121 | public OVRPose GetPose(int tracker = 0) |
| 122 | { |
| 123 | if (!OVRManager.isHmdPresent) |
| 124 | return OVRPose.identity; |
| 125 | |
| 126 | OVRPose p; |
| 127 | switch (tracker) |
| 128 | { |
| 129 | case 0: |
| 130 | p = OVRPlugin.GetNodePose(OVRPlugin.Node.TrackerZero, OVRPlugin.Step.Render).ToOVRPose(); |
| 131 | break; |
| 132 | case 1: |
| 133 | p = OVRPlugin.GetNodePose(OVRPlugin.Node.TrackerOne, OVRPlugin.Step.Render).ToOVRPose(); |
| 134 | break; |
| 135 | case 2: |
| 136 | p = OVRPlugin.GetNodePose(OVRPlugin.Node.TrackerTwo, OVRPlugin.Step.Render).ToOVRPose(); |
| 137 | break; |
| 138 | case 3: |
| 139 | p = OVRPlugin.GetNodePose(OVRPlugin.Node.TrackerThree, OVRPlugin.Step.Render).ToOVRPose(); |
| 140 | break; |
| 141 | default: |
| 142 | return OVRPose.identity; |
| 143 | } |
| 144 | |
| 145 | return new OVRPose() |
| 146 | { |
| 147 | position = p.position, |
| 148 | orientation = p.orientation * Quaternion.Euler(0, 180, 0) |
| 149 | }; |
| 150 | } |
| 151 | |
| 152 | /// <summary> |
| 153 | /// If true, the pose of the sensor is valid and is ready to be queried. |
| 154 | /// </summary> |
| 155 | public bool GetPoseValid(int tracker = 0) |
| 156 | { |
| 157 | if (!OVRManager.isHmdPresent) |
| 158 | return false; |
| 159 | |
| 160 | switch (tracker) |
| 161 | { |
| 162 | case 0: |
| 163 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.TrackerZero); |
| 164 | case 1: |
| 165 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.TrackerOne); |
| 166 | case 2: |
| 167 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.TrackerTwo); |
| 168 | case 3: |
| 169 | return OVRPlugin.GetNodePositionTracked(OVRPlugin.Node.TrackerThree); |
| 170 | default: |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | public bool GetPresent(int tracker = 0) |
| 176 | { |
| 177 | if (!OVRManager.isHmdPresent) |
| 178 | return false; |
| 179 | |
| 180 | switch (tracker) |
| 181 | { |
| 182 | case 0: |
| 183 | return OVRPlugin.GetNodePresent(OVRPlugin.Node.TrackerZero); |
| 184 | case 1: |
| 185 | return OVRPlugin.GetNodePresent(OVRPlugin.Node.TrackerOne); |
| 186 | case 2: |
| 187 | return OVRPlugin.GetNodePresent(OVRPlugin.Node.TrackerTwo); |
| 188 | case 3: |
| 189 | return OVRPlugin.GetNodePresent(OVRPlugin.Node.TrackerThree); |
| 190 | default: |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | } |