OVRDisplay.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /************************************************************************************
  2. Copyright : Copyright 2017 Oculus VR, LLC. All Rights reserved.
  3. Licensed under the Oculus VR Rift SDK License Version 3.4.1 (the "License");
  4. you may not use the Oculus VR Rift SDK except in compliance with the License,
  5. which is provided at the time of installation or download, or which
  6. otherwise accompanies this software in either electronic or hard copy form.
  7. You may obtain a copy of the License at
  8. https://developer.oculus.com/licenses/sdk-3.4.1
  9. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ************************************************************************************/
  15. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Text.RegularExpressions;
  18. using UnityEngine;
  19. /// <summary>
  20. /// Manages an Oculus Rift head-mounted display (HMD).
  21. /// </summary>
  22. public class OVRDisplay
  23. {
  24. /// <summary>
  25. /// Contains full fov information per eye
  26. /// Under Symmetric Fov mode, UpFov == DownFov and LeftFov == RightFov.
  27. /// </summary>
  28. public struct EyeFov
  29. {
  30. public float UpFov;
  31. public float DownFov;
  32. public float LeftFov;
  33. public float RightFov;
  34. }
  35. /// <summary>
  36. /// Specifies the size and field-of-view for one eye texture.
  37. /// </summary>
  38. public struct EyeRenderDesc
  39. {
  40. /// <summary>
  41. /// The horizontal and vertical size of the texture.
  42. /// </summary>
  43. public Vector2 resolution;
  44. /// <summary>
  45. /// The angle of the horizontal and vertical field of view in degrees.
  46. /// For Symmetric FOV interface compatibility
  47. /// Note this includes the fov angle from both sides
  48. /// </summary>
  49. public Vector2 fov;
  50. /// <summary>
  51. /// The full information of field of view in degrees.
  52. /// When Asymmetric FOV isn't enabled, this returns the maximum fov angle
  53. /// </summary>
  54. public EyeFov fullFov;
  55. }
  56. /// <summary>
  57. /// Contains latency measurements for a single frame of rendering.
  58. /// </summary>
  59. public struct LatencyData
  60. {
  61. /// <summary>
  62. /// The time it took to render both eyes in seconds.
  63. /// </summary>
  64. public float render;
  65. /// <summary>
  66. /// The time it took to perform TimeWarp in seconds.
  67. /// </summary>
  68. public float timeWarp;
  69. /// <summary>
  70. /// The time between the end of TimeWarp and scan-out in seconds.
  71. /// </summary>
  72. public float postPresent;
  73. public float renderError;
  74. public float timeWarpError;
  75. }
  76. private bool needsConfigureTexture;
  77. private EyeRenderDesc[] eyeDescs = new EyeRenderDesc[2];
  78. private bool recenterRequested = false;
  79. private int recenterRequestedFrameCount = int.MaxValue;
  80. /// <summary>
  81. /// Creates an instance of OVRDisplay. Called by OVRManager.
  82. /// </summary>
  83. public OVRDisplay()
  84. {
  85. UpdateTextures();
  86. }
  87. /// <summary>
  88. /// Updates the internal state of the OVRDisplay. Called by OVRManager.
  89. /// </summary>
  90. public void Update()
  91. {
  92. UpdateTextures();
  93. if (recenterRequested && Time.frameCount > recenterRequestedFrameCount)
  94. {
  95. if (RecenteredPose != null)
  96. {
  97. RecenteredPose();
  98. }
  99. recenterRequested = false;
  100. recenterRequestedFrameCount = int.MaxValue;
  101. }
  102. }
  103. /// <summary>
  104. /// Occurs when the head pose is reset.
  105. /// </summary>
  106. public event System.Action RecenteredPose;
  107. /// <summary>
  108. /// Recenters the head pose.
  109. /// </summary>
  110. public void RecenterPose()
  111. {
  112. #if UNITY_2017_2_OR_NEWER
  113. UnityEngine.XR.InputTracking.Recenter();
  114. #else
  115. UnityEngine.VR.InputTracking.Recenter();
  116. #endif
  117. // The current poses are cached for the current frame and won't be updated immediately
  118. // after UnityEngine.VR.InputTracking.Recenter(). So we need to wait until next frame
  119. // to trigger the RecenteredPose delegate. The application could expect the correct pose
  120. // when the RecenteredPose delegate get called.
  121. recenterRequested = true;
  122. recenterRequestedFrameCount = Time.frameCount;
  123. #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
  124. OVRMixedReality.RecenterPose();
  125. #endif
  126. }
  127. /// <summary>
  128. /// Gets the current linear acceleration of the head.
  129. /// </summary>
  130. public Vector3 acceleration
  131. {
  132. get {
  133. if (!OVRManager.isHmdPresent)
  134. return Vector3.zero;
  135. return OVRPlugin.GetNodeAcceleration(OVRPlugin.Node.Head, OVRPlugin.Step.Render).FromFlippedZVector3f();
  136. }
  137. }
  138. /// <summary>
  139. /// Gets the current angular acceleration of the head.
  140. /// </summary>
  141. public Vector3 angularAcceleration
  142. {
  143. get
  144. {
  145. if (!OVRManager.isHmdPresent)
  146. return Vector3.zero;
  147. return OVRPlugin.GetNodeAngularAcceleration(OVRPlugin.Node.Head, OVRPlugin.Step.Render).FromFlippedZVector3f() * Mathf.Rad2Deg;
  148. }
  149. }
  150. /// <summary>
  151. /// Gets the current linear velocity of the head.
  152. /// </summary>
  153. public Vector3 velocity
  154. {
  155. get
  156. {
  157. if (!OVRManager.isHmdPresent)
  158. return Vector3.zero;
  159. return OVRPlugin.GetNodeVelocity(OVRPlugin.Node.Head, OVRPlugin.Step.Render).FromFlippedZVector3f();
  160. }
  161. }
  162. /// <summary>
  163. /// Gets the current angular velocity of the head.
  164. /// </summary>
  165. public Vector3 angularVelocity
  166. {
  167. get {
  168. if (!OVRManager.isHmdPresent)
  169. return Vector3.zero;
  170. return OVRPlugin.GetNodeAngularVelocity(OVRPlugin.Node.Head, OVRPlugin.Step.Render).FromFlippedZVector3f() * Mathf.Rad2Deg;
  171. }
  172. }
  173. /// <summary>
  174. /// Gets the resolution and field of view for the given eye.
  175. /// </summary>
  176. #if UNITY_2017_2_OR_NEWER
  177. public EyeRenderDesc GetEyeRenderDesc(UnityEngine.XR.XRNode eye)
  178. #else
  179. public EyeRenderDesc GetEyeRenderDesc(UnityEngine.VR.VRNode eye)
  180. #endif
  181. {
  182. return eyeDescs[(int)eye];
  183. }
  184. /// <summary>
  185. /// Gets the current measured latency values.
  186. /// </summary>
  187. public LatencyData latency
  188. {
  189. get {
  190. if (!OVRManager.isHmdPresent)
  191. return new LatencyData();
  192. string latency = OVRPlugin.latency;
  193. var r = new Regex("Render: ([0-9]+[.][0-9]+)ms, TimeWarp: ([0-9]+[.][0-9]+)ms, PostPresent: ([0-9]+[.][0-9]+)ms", RegexOptions.None);
  194. var ret = new LatencyData();
  195. Match match = r.Match(latency);
  196. if (match.Success)
  197. {
  198. ret.render = float.Parse(match.Groups[1].Value);
  199. ret.timeWarp = float.Parse(match.Groups[2].Value);
  200. ret.postPresent = float.Parse(match.Groups[3].Value);
  201. }
  202. return ret;
  203. }
  204. }
  205. /// <summary>
  206. /// Gets application's frame rate reported by oculus plugin
  207. /// </summary>
  208. public float appFramerate
  209. {
  210. get
  211. {
  212. if (!OVRManager.isHmdPresent)
  213. return 0;
  214. return OVRPlugin.GetAppFramerate();
  215. }
  216. }
  217. /// <summary>
  218. /// Gets the recommended MSAA level for optimal quality/performance the current device.
  219. /// </summary>
  220. public int recommendedMSAALevel
  221. {
  222. get
  223. {
  224. int result = OVRPlugin.recommendedMSAALevel;
  225. if (result == 1)
  226. result = 0;
  227. return result;
  228. }
  229. }
  230. /// <summary>
  231. /// Gets the list of available display frequencies supported by this hardware.
  232. /// </summary>
  233. public float[] displayFrequenciesAvailable
  234. {
  235. get { return OVRPlugin.systemDisplayFrequenciesAvailable; }
  236. }
  237. /// <summary>
  238. /// Gets and sets the current display frequency.
  239. /// </summary>
  240. public float displayFrequency
  241. {
  242. get
  243. {
  244. return OVRPlugin.systemDisplayFrequency;
  245. }
  246. set
  247. {
  248. OVRPlugin.systemDisplayFrequency = value;
  249. }
  250. }
  251. private void UpdateTextures()
  252. {
  253. #if UNITY_2017_2_OR_NEWER
  254. ConfigureEyeDesc(UnityEngine.XR.XRNode.LeftEye);
  255. ConfigureEyeDesc(UnityEngine.XR.XRNode.RightEye);
  256. #else
  257. ConfigureEyeDesc(UnityEngine.VR.VRNode.LeftEye);
  258. ConfigureEyeDesc(UnityEngine.VR.VRNode.RightEye);
  259. #endif
  260. }
  261. #if UNITY_2017_2_OR_NEWER
  262. private void ConfigureEyeDesc(UnityEngine.XR.XRNode eye)
  263. #else
  264. private void ConfigureEyeDesc(UnityEngine.VR.VRNode eye)
  265. #endif
  266. {
  267. if (!OVRManager.isHmdPresent)
  268. return;
  269. OVRPlugin.Sizei size = OVRPlugin.GetEyeTextureSize((OVRPlugin.Eye)eye);
  270. eyeDescs[(int)eye] = new EyeRenderDesc();
  271. eyeDescs[(int)eye].resolution = new Vector2(size.w, size.h);
  272. OVRPlugin.Frustumf2 frust;
  273. if (OVRPlugin.GetNodeFrustum2((OVRPlugin.Node)eye, out frust))
  274. {
  275. eyeDescs[(int)eye].fullFov.LeftFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.LeftTan);
  276. eyeDescs[(int)eye].fullFov.RightFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.RightTan);
  277. eyeDescs[(int)eye].fullFov.UpFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.UpTan);
  278. eyeDescs[(int)eye].fullFov.DownFov = Mathf.Rad2Deg * Mathf.Atan(frust.Fov.DownTan);
  279. }
  280. else
  281. {
  282. OVRPlugin.Frustumf frustOld = OVRPlugin.GetEyeFrustum((OVRPlugin.Eye)eye);
  283. eyeDescs[(int)eye].fullFov.LeftFov = Mathf.Rad2Deg * frustOld.fovX * 0.5f;
  284. eyeDescs[(int)eye].fullFov.RightFov = Mathf.Rad2Deg * frustOld.fovX * 0.5f;
  285. eyeDescs[(int)eye].fullFov.UpFov = Mathf.Rad2Deg * frustOld.fovY * 0.5f;
  286. eyeDescs[(int)eye].fullFov.DownFov = Mathf.Rad2Deg * frustOld.fovY * 0.5f;
  287. }
  288. // Symmetric Fov uses the maximum fov angle
  289. float maxFovX = Mathf.Max(eyeDescs[(int)eye].fullFov.LeftFov, eyeDescs[(int)eye].fullFov.RightFov);
  290. float maxFovY = Mathf.Max(eyeDescs[(int)eye].fullFov.UpFov, eyeDescs[(int)eye].fullFov.DownFov);
  291. eyeDescs[(int)eye].fov.x = maxFovX * 2.0f;
  292. eyeDescs[(int)eye].fov.y = maxFovY * 2.0f;
  293. if (!OVRPlugin.AsymmetricFovEnabled)
  294. {
  295. eyeDescs[(int)eye].fullFov.LeftFov = maxFovX;
  296. eyeDescs[(int)eye].fullFov.RightFov = maxFovX;
  297. eyeDescs[(int)eye].fullFov.UpFov = maxFovY;
  298. eyeDescs[(int)eye].fullFov.DownFov = maxFovY;
  299. }
  300. }
  301. }