SvrManager.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Runtime.InteropServices;
  8. using AOT;
  9. public class SvrManager : MonoBehaviour
  10. {
  11. public static SvrManager Instance;
  12. //{
  13. // get
  14. // {
  15. // if (instance == null) instance = FindObjectOfType<SvrManager>();
  16. // if (instance == null) Debug.LogError("SvrManager object component not found");
  17. // return instance;
  18. // }
  19. //}
  20. //private static SvrManager instance;
  21. static public int EyeLayerMax = 8; // svrApi SVR_MAX_EYE_LAYERS
  22. static public int OverlayLayerMax = 8; // svrApi SVR_MAX_OVERLAY_LAYERS
  23. static public int RenderLayersMax = 16; // svrApi SVR_MAX_RENDER_LAYERS
  24. public Action SvrInitializedCallBack;
  25. public enum svrEventType
  26. {
  27. kEventNone = 0,
  28. kEventSdkServiceStarting = 1,
  29. kEventSdkServiceStarted = 2,
  30. kEventSdkServiceStopped = 3,
  31. kEventControllerConnecting = 4,
  32. kEventControllerConnected = 5,
  33. kEventControllerDisconnected = 6,
  34. kEventThermal = 7,
  35. kEventVrModeStarted = 8,
  36. kEventVrModeStopping = 9,
  37. kEventVrModeStopped = 10,
  38. kEventSensorError = 11,
  39. kEventMagnometerUncalibrated = 12,
  40. kEventBoundarySystemCollision = 13,
  41. kEvent6dofRelocation = 14,
  42. kEvent6dofWarningFeatureCount = 15,
  43. kEvent6dofWarningLowLight = 16,
  44. kEvent6dofWarningBrightLight = 17,
  45. kEvent6dofWarningCameraCalibration = 18
  46. };
  47. [Serializable]
  48. public class SvrSettings
  49. {
  50. public enum eAntiAliasing
  51. {
  52. k1 = 1,
  53. k2 = 2,
  54. k4 = 4,
  55. };
  56. public enum eDepth
  57. {
  58. k16 = 16,
  59. k24 = 24
  60. };
  61. public enum eChromaticAberrationCorrection
  62. {
  63. kDisable = 0,
  64. kEnable = 1
  65. };
  66. public enum eVSyncCount
  67. {
  68. k0 = 0,
  69. k1 = 1,
  70. k2 = 2,
  71. };
  72. public enum eMasterTextureLimit
  73. {
  74. k0 = 0, // full size
  75. k1 = 1, // half size
  76. k2 = 2, // quarter size
  77. k3 = 3, // ...
  78. k4 = 4 // ...
  79. };
  80. public enum ePerfLevel
  81. {
  82. Minimum = 1,
  83. Medium = 2,
  84. Maximum = 3
  85. };
  86. public enum eFrustumType
  87. {
  88. Camera = 0,
  89. Device = 1,
  90. }
  91. public enum eEyeBufferType
  92. {
  93. //Mono = 0,
  94. StereoSeperate = 1,
  95. //StereoSingle = 2,
  96. //Array = 3,
  97. }
  98. [Tooltip("If head tracking lost, fade the display")]
  99. public bool poseStatusFade = true;
  100. [Tooltip("Use eye tracking (if available)")]
  101. public bool trackEyes = true;
  102. [Tooltip("Use position tracking (if available)")]
  103. public bool trackPosition = true;
  104. [Tooltip("Track position conversion from meters")]
  105. public float trackPositionScale = 1;
  106. [Tooltip("Height of the eyes from base of head")]
  107. public float headHeight = 0.0750f;
  108. [Tooltip("Depth of the eyes from center of head")]
  109. public float headDepth = 0.0805f;
  110. [Tooltip("Distance between the eyes")]
  111. public float interPupilDistance = 0.064f;
  112. //[Tooltip("Distance of line-of-sight convergence (0 disabled)")]
  113. //public float stereoConvergence = 0;
  114. //[Tooltip("Pitch angle to the horizon in degrees")]
  115. //public float horizonElevation = 0;
  116. [Tooltip("Eye field of view render target reprojection margin (% of fov) [0..]")]
  117. public float eyeFovMargin = 0.0f;
  118. [Tooltip("Eye render target scale factor")]
  119. public float eyeResolutionScaleFactor = 1.0f;
  120. [Tooltip("Eye render target depth buffer")]
  121. public eDepth eyeDepth = eDepth.k24;
  122. [Tooltip("Eye render target MSAA value")]
  123. public eAntiAliasing eyeAntiAliasing = eAntiAliasing.k2;
  124. [Tooltip("Overlay render target scale factor")]
  125. public float overlayResolutionScaleFactor = 1.0f;
  126. [Tooltip("Overlay render target depth buffer")]
  127. public eDepth overlayDepth = eDepth.k16;
  128. [Tooltip("Overlay render target MSAA value")]
  129. public eAntiAliasing overlayAntiAliasing = eAntiAliasing.k1;
  130. [Tooltip("Limit refresh rate")]
  131. public eVSyncCount vSyncCount = eVSyncCount.k1;
  132. [Tooltip("Chromatic Aberration Correction")]
  133. public eChromaticAberrationCorrection chromaticAberationCorrection = eChromaticAberrationCorrection.kEnable;
  134. [Tooltip("QualitySettings TextureQuality FullRes, HalfRes, etc.")]
  135. public eMasterTextureLimit masterTextureLimit = eMasterTextureLimit.k0;
  136. [Tooltip("CPU performance level")]
  137. public ePerfLevel cpuPerfLevel = ePerfLevel.Medium;
  138. [Tooltip("GPU performance level")]
  139. public ePerfLevel gpuPerfLevel = ePerfLevel.Medium;
  140. [Tooltip("Foveated render gain [1..], 0/feature disabled")]
  141. public Vector2 foveationGain = new Vector2(0.0f, 0.0f);
  142. [Tooltip("Foveated render hires area [0..]")]
  143. public float foveationArea = 0.0f;
  144. [Tooltip("Foveated render min pixel density [1/16..1]")]
  145. public float foveationMinimum = 0.25f;
  146. [Tooltip("Use perspective of unity camera or device frustum data")]
  147. public eFrustumType frustumType = eFrustumType.Camera;
  148. [Tooltip("Display buffer type (default stereo seperate)")]
  149. public eEyeBufferType displayType = eEyeBufferType.StereoSeperate;
  150. }
  151. [SerializeField]
  152. public SvrSettings settings;
  153. [Serializable]
  154. public class SvrStatus
  155. {
  156. [Tooltip("SnapdragonVR SDK Initialized")]
  157. public bool initialized = false;
  158. [Tooltip("SnapdragonVR SDK Running")]
  159. public bool running = false;
  160. [Tooltip("SnapdragonVR SDK Pose Status: 0/None, 1/Rotation, 2/Position, 3/RotationAndPosition")]
  161. public int pose = 0;
  162. public int poseModify = 0;
  163. }
  164. [SerializeField]
  165. public SvrStatus status;
  166. public enum eFadeState { FadeIn, FadeOut }
  167. [NonSerialized]
  168. public eFadeState fadeState = eFadeState.FadeIn;
  169. [NonSerialized]
  170. public float fadeDuration = 2f;
  171. [Header("Camera Rig")]
  172. public Transform head;
  173. public Transform gaze;
  174. public Camera monoCamera;
  175. public Camera leftCamera;
  176. public Camera rightCamera;
  177. public Camera leftOverlay;
  178. public Camera rightOverlay;
  179. public Camera monoOverlay;
  180. public SvrOverlay fadeOverlay;
  181. public SvrOverlay reticleOverlay;
  182. public Vector3 leftCameraOffsetPostion= new Vector3 (0,0,0);
  183. public Vector3 leftCameraOffsetAngles= new Vector3 (0,2.5f,0);
  184. public Vector3 rightCameraOffsetPostion= new Vector3 (0,0,0);
  185. public Vector3 rightCameraOffsetAngles= new Vector3 (0,-2.5f,0);
  186. public static bool init;
  187. public Vector2 FocalPoint { get; set; } // Foveated Rendering Focal Point
  188. public int FrameCount { get { return frameCount; } }
  189. private int frameCount = 0;
  190. private static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
  191. public SvrPlugin plugin = null;
  192. private float sensorWarmupDuration = 0.25f;
  193. private List<SvrEye> eyes = new List<SvrEye>(EyeLayerMax);
  194. private List<SvrOverlay> overlays = new List<SvrOverlay>(OverlayLayerMax);
  195. private bool disableInput = false;
  196. private Coroutine onResume = null;
  197. private Coroutine submitFrame = null;
  198. /// <summary>
  199. /// Svr event listener.
  200. /// </summary>
  201. public interface SvrEventListener {
  202. /// <summary>
  203. /// Raises the svr event event.
  204. /// </summary>
  205. /// <param name="ev">Ev.</param>
  206. void OnSvrEvent (SvrEvent ev);
  207. };
  208. public enum svrThermalLevel
  209. {
  210. kSafe,
  211. kLevel1,
  212. kLevel2,
  213. kLevel3,
  214. kCritical,
  215. kNumThermalLevels
  216. };
  217. public enum svrThermalZone
  218. {
  219. kCpu,
  220. kGpu,
  221. kSkin,
  222. kNumThermalZones
  223. };
  224. public struct svrEventData_Thermal
  225. {
  226. public svrThermalZone zone; //!< Thermal zone
  227. public svrThermalLevel level; //!< Indication of the current zone thermal level
  228. };
  229. [StructLayout(LayoutKind.Explicit)]
  230. public struct svrEventData
  231. {
  232. [FieldOffset(0)]
  233. public svrEventData_Thermal thermal;
  234. //[FieldOffset(0)]
  235. //public svrEventData_New newData;
  236. }
  237. public struct SvrEvent
  238. {
  239. public svrEventType eventType; //!< Type of event
  240. public uint deviceId; //!< An identifier for the device that generated the event (0 == HMD)
  241. public float eventTimeStamp; //!< Time stamp for the event in seconds since the last svrBeginVr call
  242. public svrEventData eventData; //!< Event specific data payload
  243. };
  244. private List<SvrEventListener> eventListeners = new List<SvrEventListener>();
  245. public bool Initialized
  246. {
  247. get { return status.initialized; }
  248. }
  249. public bool IsRunning
  250. {
  251. get { return status.running; }
  252. }
  253. public bool DisableInput
  254. {
  255. get { return disableInput; }
  256. set { disableInput = value; }
  257. }
  258. void Awake()
  259. {
  260. if(Instance) {
  261. DestroyImmediate(gameObject);
  262. return;
  263. }
  264. Instance = this;
  265. if (!ValidateReferencedComponents ())
  266. {
  267. enabled = false;
  268. return;
  269. }
  270. RegisterListeners();
  271. Input.backButtonLeavesApp = true;
  272. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  273. Application.targetFrameRate = -1;
  274. }
  275. void OnGUI() {
  276. }
  277. bool ValidateReferencedComponents()
  278. {
  279. plugin = SvrPlugin.Instance;
  280. if(plugin == null)
  281. {
  282. Debug.LogError("Svr Plugin failed to load. Disabling...");
  283. return false;
  284. }
  285. if(head == null)
  286. {
  287. Debug.LogError("Required head gameobject not found! Disabling...");
  288. return false;
  289. }
  290. if(monoCamera == null && (leftCamera == null || rightCamera == null))
  291. {
  292. Debug.LogError("Required eye components are missing! Disabling...");
  293. return false;
  294. }
  295. return true;
  296. }
  297. #if UNITY_2018
  298. void OnEnable()
  299. {
  300. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  301. {
  302. UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering += OnPreRenderEvent;
  303. }
  304. }
  305. void OnDisable()
  306. {
  307. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  308. {
  309. UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering -= OnPreRenderEvent;
  310. }
  311. }
  312. private void OnPreRenderEvent(Camera camera)
  313. {
  314. camera.SendMessage("OnPreRenderEvent", SendMessageOptions.DontRequireReceiver);
  315. }
  316. #endif
  317. IEnumerator Start ()
  318. {
  319. yield return StartCoroutine(Initialize());
  320. status.initialized = plugin.IsInitialized();
  321. SetOverlayFade(eFadeState.FadeIn);
  322. yield return StartCoroutine(plugin.BeginVr((int)settings.cpuPerfLevel, (int)settings.gpuPerfLevel));
  323. if (!plugin.IsRunning())
  324. {
  325. Debug.LogError("Svr failed!");
  326. Application.Quit();
  327. yield return null; // Wait one frame
  328. }
  329. float recenterTimeout = 1f;
  330. while (!plugin.RecenterTracking() && recenterTimeout > 0f)
  331. {
  332. yield return null; // Wait one frame
  333. recenterTimeout -= Time.deltaTime;
  334. }
  335. yield return new WaitForSecondsRealtime(sensorWarmupDuration);
  336. submitFrame = StartCoroutine(SubmitFrame());
  337. status.running = true;
  338. SvrInitializedCallBack?.Invoke();
  339. plugin.SetGlassDisconnectedCallBack(GlassDisconnetedCallBack);
  340. Debug.Log("Svr initialized!");
  341. }
  342. [MonoPInvokeCallback(typeof(SvrPlugin.GlassDisconnectedCallBack))]
  343. private static void GlassDisconnetedCallBack() {
  344. Debug.Log("GlassDisconnetedCallBack");
  345. Application.Quit();
  346. }
  347. IEnumerator Initialize()
  348. {
  349. // Plugin must be initialized OnStart in order to properly
  350. // get a valid surface
  351. // GameObject mainCameraGo = GameObject.FindWithTag("MainCamera");
  352. // if (mainCameraGo)
  353. // {
  354. // mainCameraGo.SetActive(false);
  355. //
  356. // Debug.Log("Camera with MainCamera tag found.");
  357. // if (!disableInput)
  358. // {
  359. // Debug.Log("Will use translation and orientation from the MainCamera.");
  360. // transform.position = mainCameraGo.transform.position;
  361. // transform.rotation = mainCameraGo.transform.rotation;
  362. // }
  363. //
  364. // Debug.Log("Disabling Camera with MainCamera tag");
  365. // }
  366. GL.Clear(false, true, Color.black);
  367. yield return StartCoroutine(plugin.Initialize());
  368. InitializeCameras();
  369. InitializeEyes();
  370. InitializeOverlays();
  371. int trackingMode = (int)SvrPlugin.TrackingMode.kTrackingOrientation;
  372. if (settings.trackPosition)
  373. trackingMode |= (int)SvrPlugin.TrackingMode.kTrackingPosition;
  374. if (settings.trackEyes)
  375. trackingMode |= (int)SvrPlugin.TrackingMode.kTrackingEye;
  376. plugin.SetTrackingMode(trackingMode);
  377. plugin.SetVSyncCount((int)settings.vSyncCount);
  378. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  379. }
  380. Matrix4x4 leftcalibration = new Matrix4x4(
  381. new Vector4(0.999928f, -0.0108193f, 0.00518164f, 0),
  382. new Vector4(0.0110425f, 0.998918f, -0.0451751f, 0),
  383. new Vector4(-0.00468727f, 0.045229f, 0.998966f, 0),
  384. new Vector4(-0.0182249f, 0.01366f, -0.0578625f, 1));
  385. Matrix4x4 leftXIE = new Matrix4x4(
  386. new Vector4(0.999928f, -0.010819f, 0.00518164f, 0),
  387. new Vector4(0.011042f, 0.998918f, -0.0451751f, 0),
  388. new Vector4(-0.004687f, 0.045229f, 0.998966f, 0),
  389. new Vector4(0.018460f, -0.031915f, -1.056663f, 1));
  390. Matrix4x4 rigthXIE = new Matrix4x4(
  391. new Vector4(0.99994f, 0.00570579f, -0.00933801f, 0),
  392. new Vector4(-0.00613274f, 0.998906f, -0.0463502f, 0),
  393. new Vector4(0.00906334f, 0.0464047f, 0.998882f, 0),
  394. new Vector4(-0.057191f, -0.028896f, -1.048315f, 1));
  395. // 11-30 19:32:56.734 7534 7534 I willie_native_log: Controller::onMouseDown mViewMatrixArray[0] = [0.999928, 0.011042, -0.004687, 0.018460; -0.010819, 0.998918, 0.045229, -0.031915; 0.005182, -0.045175, 0.998966, -1.056663; 0.000000, 0.000000, 0.000000, 1.000000]
  396. //11-30 19:32:56.734 7534 7534 I willie_native_log: Controller::onMouseDown mViewMatrixArray[1] =
  397. //[0.999940, -0.006133, 0.009063, -0.057191; 0.005706, 0.998906, 0.046405, -0.028896; -0.009338, -0.046350, 0.998882, -1.048315; 0.000000, 0.000000, 0.000000, 1.000000]
  398. Matrix4x4 rightcalibration = new Matrix4x4(
  399. new Vector4(0.99994f, 0.00570579f, -0.00933801f, 0),
  400. new Vector4(-0.00613274f, 0.998906f, -0.0463502f, 0),
  401. new Vector4(0.00906334f, 0.0464047f, 0.998882f, 0),
  402. new Vector4(-0.0161295f, 0.0176911f, -0.0497315f, 1));
  403. //Matrix4x4 initView = new Matrix4x4(
  404. // new Vector4(0.707f, 0f, 0.707f, 0),
  405. // new Vector4(0f, 1f, 0f, 0),
  406. // new Vector4(-0.707f, 0f, 0.707f, 0),
  407. // new Vector4(-0f, 0f, -1.414f, 1));
  408. Matrix4x4 initView = new Matrix4x4(
  409. new Vector4(1f, 0f, 0f, 0),
  410. new Vector4(0f, 1f, 0f, 0),
  411. new Vector4(0f, 0f, 1f, 0),
  412. new Vector4(0f, 0f, -1f, 1));
  413. Matrix4x4 leftPD = new Matrix4x4(
  414. new Vector4(1f, 0f, 0f, 0f),
  415. new Vector4(0f, 1f, 0f, 0),
  416. new Vector4(0f, 0f, 1f, 0),
  417. new Vector4(0.032f, 0f, 0f, 1f));
  418. Matrix4x4 rightPD = new Matrix4x4(
  419. new Vector4(1f, 0f, 0f, 0),
  420. new Vector4(0f, 1f, 0f, 0),
  421. new Vector4(0f, 0f, 1f, 0),
  422. new Vector4(-0.032f, 0f, 0f, 1));
  423. Matrix4x4 invertZ = new Matrix4x4(
  424. new Vector4(1, 0, 0, 0),
  425. new Vector4(0, 1, 0, 0),
  426. new Vector4(0, 0, -1, 0),
  427. new Vector4(0, 0, 0, 1));
  428. private void InitializeCameras()
  429. {
  430. float stereoConvergence = plugin.deviceInfo.targetFrustumConvergence; //settings.stereoConvergence
  431. float horizonElevation = plugin.deviceInfo.targetFrustumPitch; //settings.horizonElevation
  432. float convergenceAngle = 0f;
  433. if (stereoConvergence > Mathf.Epsilon) convergenceAngle = Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, stereoConvergence);
  434. else if (stereoConvergence < -Mathf.Epsilon) convergenceAngle = -Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, -stereoConvergence);
  435. // left
  436. Vector3 eyePos;
  437. eyePos.x = -0.5f * settings.interPupilDistance;
  438. eyePos.y = (!settings.trackPosition ? settings.headHeight : 0);
  439. eyePos.z = (!settings.trackPosition ? settings.headDepth : 0);
  440. eyePos += head.transform.localPosition;
  441. Quaternion eyeRot;
  442. eyeRot = Quaternion.Euler(horizonElevation, convergenceAngle, 0);
  443. if (leftCamera != null)
  444. {
  445. leftCamera.transform.localPosition = eyePos+leftCameraOffsetPostion;
  446. leftCamera.transform.localRotation = eyeRot;
  447. leftCamera.transform.localEulerAngles = leftCamera.transform.localEulerAngles+leftCameraOffsetAngles;
  448. Debug.Log(leftCamera.worldToCameraMatrix);
  449. //leftCamera.worldToCameraMatrix = leftcalibration * leftCamera.worldToCameraMatrix;
  450. // Matrix4x4 xx =invertZ* Matrix4x4.Inverse( Matrix4x4.TRS(leftCamera.transform.position, leftCamera.transform.rotation, leftCamera.transform.lossyScale));
  451. Matrix4x4 xx = (Matrix4x4.TRS(leftCamera.transform.position, leftCamera.transform.rotation, leftCamera.transform.lossyScale));
  452. Debug.Log(xx);
  453. Debug.Log(leftCamera.transform.position.x+ " "+leftCamera.transform.position.y+" " + leftCamera.transform.position.z);
  454. Debug.Log(leftCamera.transform.rotation.x + " " + leftCamera.transform.rotation.y + " " + leftCamera.transform.rotation.z + " " + leftCamera.transform.rotation.w);
  455. }
  456. if (leftOverlay != null)
  457. {
  458. leftOverlay.transform.localPosition = eyePos;
  459. leftOverlay.transform.localRotation = eyeRot;
  460. }
  461. // right
  462. eyePos.x *= -1;
  463. eyeRot = Quaternion.Euler(horizonElevation, -convergenceAngle, 0);
  464. if (rightCamera != null)
  465. {
  466. rightCamera.transform.localPosition = eyePos+rightCameraOffsetPostion;
  467. rightCamera.transform.localRotation = eyeRot;
  468. rightCamera.transform.localEulerAngles = rightCamera.transform.localEulerAngles+rightCameraOffsetAngles;
  469. }
  470. if (rightOverlay != null)
  471. {
  472. rightOverlay.transform.localPosition = eyePos;
  473. rightOverlay.transform.localRotation = eyeRot;
  474. }
  475. // mono
  476. eyePos.x = 0.0f;
  477. eyeRot = Quaternion.Euler(horizonElevation, 0, 0);
  478. if (monoCamera != null)
  479. {
  480. monoCamera.transform.localPosition = eyePos;
  481. monoCamera.transform.localRotation = eyeRot;
  482. }
  483. if (monoOverlay != null)
  484. {
  485. monoOverlay.transform.localPosition = eyePos;
  486. monoOverlay.transform.localRotation = eyeRot;
  487. }
  488. }
  489. private void AddEyes(Camera cam, SvrEye.eSide side)
  490. {
  491. bool enableCamera = false;
  492. var eyesFound = cam.gameObject.GetComponents<SvrEye>();
  493. for (int i = 0; i < eyesFound.Length; i++)
  494. {
  495. eyesFound[i].Side = side;
  496. if (eyesFound[i].imageType == SvrEye.eType.RenderTexture) enableCamera = true;
  497. }
  498. eyes.AddRange(eyesFound);
  499. if (eyesFound.Length == 0)
  500. {
  501. var eye = cam.gameObject.AddComponent<SvrEye>();
  502. eye.Side = side;
  503. eyes.Add(eye);
  504. enableCamera = true;
  505. }
  506. #if UNITY_5_4 || UNITY_5_5
  507. cam.hdr = false;
  508. #else // UNITY_5_6 plus
  509. cam.allowHDR = false;
  510. cam.allowMSAA = false;
  511. #endif
  512. cam.enabled = enableCamera;
  513. }
  514. private void InitializeEyes()
  515. {
  516. eyes.Clear();
  517. if (monoCamera != null && monoCamera.gameObject.activeSelf)
  518. {
  519. AddEyes(monoCamera, SvrEye.eSide.Both);
  520. }
  521. if (leftCamera != null && leftCamera.gameObject.activeSelf)
  522. {
  523. AddEyes(leftCamera, SvrEye.eSide.Left);
  524. }
  525. if (rightCamera != null && rightCamera.gameObject.activeSelf)
  526. {
  527. AddEyes(rightCamera, SvrEye.eSide.Right);
  528. }
  529. for (int i = 0; i < SvrEye.Instances.Count; i++)
  530. {
  531. var eye = SvrEye.Instances[i];
  532. if (!eyes.Contains(eye))
  533. {
  534. eyes.Add(eye); // Add eyes found outside of svr camera hierarchy
  535. }
  536. }
  537. SvrPlugin.DeviceInfo info = plugin.deviceInfo;
  538. foreach(SvrEye eye in eyes)
  539. {
  540. if (eye == null) continue;
  541. eye.FovMargin = settings.eyeFovMargin;
  542. eye.Format = RenderTextureFormat.Default;
  543. eye.Resolution = new Vector2(info.targetEyeWidthPixels, info.targetEyeHeightPixels);
  544. eye.ResolutionScaleFactor = settings.eyeResolutionScaleFactor;
  545. eye.Depth = (int)settings.eyeDepth;
  546. eye.AntiAliasing = (int)settings.eyeAntiAliasing; // hdr not supported with antialiasing
  547. eye.FrustumType = (int)settings.frustumType;
  548. eye.OnPreRenderListener = OnPreRenderListener;
  549. eye.OnPostRenderListener = OnPostRenderListener;
  550. eye.Initialize();
  551. }
  552. }
  553. private void AddOverlays(Camera cam, SvrOverlay.eSide side)
  554. {
  555. bool enableCamera = false;
  556. var overlaysFound = cam.gameObject.GetComponents<SvrOverlay>();
  557. for (int i = 0; i < overlaysFound.Length; i++)
  558. {
  559. overlaysFound[i].Side = side;
  560. if (overlaysFound[i].imageType == SvrOverlay.eType.RenderTexture) enableCamera = true;
  561. }
  562. overlays.AddRange(overlaysFound);
  563. if (overlaysFound.Length == 0)
  564. {
  565. var overlay = cam.gameObject.AddComponent<SvrOverlay>();
  566. overlay.Side = side;
  567. overlays.Add(overlay);
  568. enableCamera = true;
  569. }
  570. #if UNITY_5_4 || UNITY_5_5
  571. cam.hdr = false;
  572. #else // UNITY_5_6 plus
  573. cam.allowHDR = false;
  574. cam.allowMSAA = false;
  575. #endif
  576. cam.enabled = enableCamera;
  577. }
  578. private void InitializeOverlays()
  579. {
  580. overlays.Clear();
  581. if (leftOverlay != null && leftOverlay.gameObject.activeSelf)
  582. {
  583. AddOverlays(leftOverlay, SvrOverlay.eSide.Left);
  584. }
  585. if (rightOverlay != null && rightOverlay.gameObject.activeSelf)
  586. {
  587. AddOverlays(rightOverlay, SvrOverlay.eSide.Right);
  588. }
  589. if (monoOverlay != null && monoOverlay.gameObject.activeSelf)
  590. {
  591. AddOverlays(monoOverlay, SvrOverlay.eSide.Both);
  592. }
  593. for (int i = 0; i < SvrOverlay.Instances.Count; i++)
  594. {
  595. var overlay = SvrOverlay.Instances[i];
  596. if (!overlays.Contains(overlay))
  597. {
  598. overlays.Add(overlay); // Add overlays found outside of svr camera hierarchy
  599. }
  600. }
  601. SvrPlugin.DeviceInfo info = plugin.deviceInfo;
  602. foreach (SvrOverlay overlay in overlays)
  603. {
  604. if (overlay == null) continue;
  605. overlay.Format = RenderTextureFormat.Default;
  606. overlay.Resolution = new Vector2(info.targetEyeWidthPixels, info.targetEyeHeightPixels);
  607. overlay.ResolutionScaleFactor = settings.overlayResolutionScaleFactor;
  608. overlay.Depth = (int)settings.overlayDepth;
  609. overlay.AntiAliasing = (int)settings.overlayAntiAliasing; // hdr not supported with antialiasing
  610. overlay.FrustumType = (int)settings.frustumType;
  611. overlay.OnPreRenderListener = OnPreRenderListener;
  612. overlay.OnPostRenderListener = OnPostRenderListener;
  613. overlay.Initialize();
  614. }
  615. }
  616. public void SetOverlayFade(eFadeState fadeValue)
  617. {
  618. fadeState = fadeValue;
  619. var startAlpha = fadeState == eFadeState.FadeIn ? 1f : 0f;
  620. UpdateOverlayFade(startAlpha);
  621. }
  622. public bool IsOverlayFading()
  623. {
  624. return !Mathf.Approximately((float)fadeState, fadeAlpha);
  625. }
  626. private float fadeAlpha = 0f;
  627. private void UpdateOverlayFade(float targetAlpha, float rate = 0)
  628. {
  629. if (fadeOverlay == null) return;
  630. fadeAlpha = rate > 0 ? Mathf.MoveTowards(fadeAlpha, targetAlpha, rate) : targetAlpha;
  631. var fadeTexture = fadeOverlay.imageTexture as Texture2D;
  632. if (fadeTexture != null)
  633. {
  634. var fadeColors = fadeTexture.GetPixels();
  635. for (int i = 0; i < fadeColors.Length; ++i)
  636. {
  637. fadeColors[i].a = fadeAlpha;
  638. }
  639. fadeTexture.SetPixels(fadeColors);
  640. fadeTexture.Apply(false);
  641. }
  642. var isActive = fadeAlpha > 0.0f;
  643. if (fadeOverlay.enabled != isActive)
  644. {
  645. fadeOverlay.enabled = isActive;
  646. }
  647. }
  648. IEnumerator SubmitFrame ()
  649. {
  650. Vector3 frustumSize = Vector3.zero;
  651. frustumSize.x = 0.5f * (plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left);
  652. frustumSize.y = 0.5f * (plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom);
  653. frustumSize.z = plugin.deviceInfo.targetFrustumLeft.near;
  654. //Debug.LogFormat("SubmitFrame: Frustum Size ({0:F2}, {1:F2}, {2:F2})", frustumSize.x, frustumSize.y, frustumSize.z);
  655. while (true)
  656. {
  657. yield return waitForEndOfFrame;
  658. if ((plugin.GetTrackingMode() & (int)SvrPlugin.TrackingMode.kTrackingEye) != 0) // Request eye pose
  659. {
  660. status.pose |= plugin.GetEyePose(ref eyePose);
  661. }
  662. var eyePoint = Vector3.zero;
  663. if ((status.pose & (int)SvrPlugin.TrackingMode.kTrackingEye) != 0) // Valid eye pose
  664. {
  665. //Debug.LogFormat("Left Eye Position: {0}, Direction: {1}", eyePose.leftPosition.ToString(), eyePose.leftDirection.ToString());
  666. //Debug.LogFormat("Right Eye Position: {0}, Direction: {1}", eyePose.rightPosition.ToString(), eyePose.rightDirection.ToString());
  667. //Debug.LogFormat("Combined Eye Position: {0}, Direction: {1}", eyePose.combinedPosition.ToString(), eyePose.combinedDirection.ToString());
  668. var combinedDirection = Vector3.zero;
  669. if ((eyePose.leftStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
  670. if ((eyePose.rightStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
  671. //if ((eyePose.combinedStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.combinedDirection;
  672. if (combinedDirection.sqrMagnitude > 0f)
  673. {
  674. combinedDirection.Normalize();
  675. //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", combinedDirection.x, combinedDirection.y, combinedDirection.z);
  676. float denominator = Vector3.Dot(combinedDirection, Vector3.forward);
  677. if (denominator > float.Epsilon)
  678. {
  679. // eye direction intersection with frustum near plane (left)
  680. eyePoint = combinedDirection * frustumSize.z / denominator;
  681. eyePoint.x /= frustumSize.x; // [-1..1]
  682. eyePoint.y /= frustumSize.y; // [-1..1]
  683. //Debug.LogFormat("Eye Point: ({0:F2}, {1:F2})", eyePoint.x, eyePoint.y);
  684. }
  685. }
  686. }
  687. var currentGain = SvrOverrideSettings.FoveateGain == Vector2.zero ?
  688. settings.foveationGain : SvrOverrideSettings.FoveateGain;
  689. var currentArea = SvrOverrideSettings.FoveateArea == 0f ?
  690. settings.foveationArea : SvrOverrideSettings.FoveateArea;
  691. var currentMinimum = SvrOverrideSettings.FoveateMinimum == 0f ?
  692. settings.foveationMinimum : SvrOverrideSettings.FoveateMinimum;
  693. for (int i = 0; i < eyes.Count; i++)
  694. {
  695. var eye = eyes[i];
  696. if (eye.TextureId > 0 && eye.ImageType == SvrEye.eType.RenderTexture)
  697. {
  698. plugin.SetFoveationParameters(eye.TextureId, eye.PreviousId, eyePoint.x, eyePoint.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
  699. plugin.ApplyFoveation();
  700. }
  701. }
  702. var horizontalFieldOfView = 0f;
  703. if (settings.eyeFovMargin > 0f)
  704. {
  705. horizontalFieldOfView = (monoCamera.enabled ? monoCamera.fieldOfView / monoCamera.aspect : leftCamera.fieldOfView / leftCamera.aspect) * Mathf.Deg2Rad;
  706. }
  707. plugin.SubmitFrame(frameCount, horizontalFieldOfView, (int)settings.displayType);
  708. frameCount++;
  709. }
  710. }
  711. public bool RecenterTracking()
  712. {
  713. if (!Initialized)
  714. return false;
  715. return plugin.RecenterTracking();
  716. }
  717. void OnPreRenderListener (int sideMask, int textureId, int previousId)
  718. {
  719. if (!IsRunning)
  720. return;
  721. var currentGain = SvrOverrideSettings.FoveateGain == Vector2.zero ?
  722. settings.foveationGain : SvrOverrideSettings.FoveateGain;
  723. var currentArea = SvrOverrideSettings.FoveateArea == 0f ?
  724. settings.foveationArea : SvrOverrideSettings.FoveateArea;
  725. var currentMinimum = SvrOverrideSettings.FoveateMinimum == 0f ?
  726. settings.foveationMinimum : SvrOverrideSettings.FoveateMinimum;
  727. plugin.SetFoveationParameters(textureId, previousId, FocalPoint.x, FocalPoint.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
  728. plugin.BeginEye(sideMask, frameDelta);
  729. }
  730. void OnPostRenderListener (int sideMask, int layerMask)
  731. {
  732. if (!IsRunning)
  733. return;
  734. plugin.EndEye (sideMask, layerMask);
  735. }
  736. public void SetPause(bool pause)
  737. {
  738. if (!Initialized)
  739. return;
  740. if (pause)
  741. {
  742. OnPause();
  743. }
  744. else
  745. {
  746. onResume = StartCoroutine(OnResume());
  747. }
  748. }
  749. void OnPause()
  750. {
  751. //Debug.Log("SvrManager.OnPause()");
  752. status.running = false;
  753. if (onResume != null) {
  754. StopCoroutine(onResume);
  755. onResume = null;
  756. }
  757. if (submitFrame != null) { StopCoroutine(submitFrame); submitFrame = null; }
  758. if (plugin.IsRunning()) plugin.EndVr();
  759. }
  760. IEnumerator OnResume()
  761. {
  762. //Debug.Log("SvrManager.OnResume()");
  763. SetOverlayFade(eFadeState.FadeIn);
  764. yield return StartCoroutine(plugin.BeginVr((int)settings.cpuPerfLevel, (int)settings.gpuPerfLevel));
  765. float recenterTimeout = 1f;
  766. while (!plugin.RecenterTracking() && recenterTimeout > 0f)
  767. {
  768. yield return null; // Wait one frame
  769. recenterTimeout -= Time.deltaTime;
  770. }
  771. yield return new WaitForSecondsRealtime(sensorWarmupDuration);
  772. submitFrame = StartCoroutine (SubmitFrame ());
  773. status.running = plugin.IsRunning();
  774. onResume = null;
  775. }
  776. [NonSerialized]
  777. public SvrPlugin.HeadPose headPose;
  778. [NonSerialized]
  779. public SvrPlugin.EyePose eyePose;
  780. [NonSerialized]
  781. public Vector3 eyeDirection = Vector3.forward;
  782. [NonSerialized]
  783. public Vector2 eyeFocus = Vector2.zero;
  784. [NonSerialized]
  785. public float eyeSmoother = 0.2f;
  786. [NonSerialized]
  787. public float[] frameDelta = new float[9];
  788. public Vector3 modifyPosition = Vector3.zero;
  789. public Quaternion modifyOrientation = Quaternion.identity;
  790. bool outBload = false;
  791. float[] outTransformArray = new float[32];
  792. void LateUpdate()
  793. {
  794. if (!IsRunning)
  795. {
  796. return;
  797. }
  798. int trackingMode = plugin.GetTrackingMode();
  799. var prevOrientation = headPose.orientation;
  800. //status.poseModify = plugin.GetPredictedPoseModify(ref modifyOrientation, ref modifyPosition, frameCount);
  801. status.pose = plugin.GetHeadPose(ref headPose, frameCount);
  802. if ((trackingMode & (int)SvrPlugin.TrackingMode.kTrackingEye) != 0)
  803. {
  804. status.pose |= plugin.GetEyePose(ref eyePose, frameCount);
  805. }
  806. if (!disableInput)
  807. {
  808. if ((status.pose & (int)SvrPlugin.TrackingMode.kTrackingOrientation) != 0)
  809. {
  810. head.transform.localRotation = headPose.orientation;
  811. // delta orientation screen space x, y offset for foveated rendering
  812. var deltaOrientation = Quaternion.Inverse(prevOrientation) * headPose.orientation;
  813. var lookDirection = deltaOrientation * Vector3.forward;
  814. //Debug.LogFormat("Look Direction: {0}", lookDirection.ToString());
  815. lookDirection *= plugin.deviceInfo.targetFrustumLeft.near / lookDirection.z;
  816. float xTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left);
  817. float xAdjust = (xTotal != 0.0f) ? lookDirection.x / xTotal : 0.0f;
  818. float yTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom);
  819. float yAdjust = (yTotal != 0.0f) ? lookDirection.y / yTotal : 0.0f;
  820. //xAdjust *= 0.5f * plugin.deviceInfo.targetEyeWidthPixels;
  821. //yAdjust *= 0.5f * plugin.deviceInfo.targetEyeHeightPixels;
  822. // rotation around z [cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]
  823. Vector3 deltaEulers = deltaOrientation.eulerAngles;
  824. float cosZ = Mathf.Cos(deltaEulers.z * Mathf.Deg2Rad);
  825. float sinZ = Mathf.Sin(deltaEulers.z * Mathf.Deg2Rad);
  826. // Output rotation and translation
  827. frameDelta[0] = cosZ;
  828. frameDelta[1] = sinZ;
  829. frameDelta[2] = 0.0f;
  830. frameDelta[3] = -sinZ;
  831. frameDelta[4] = cosZ;
  832. frameDelta[5] = 0.0f;
  833. frameDelta[6] = xAdjust;
  834. frameDelta[7] = yAdjust;
  835. frameDelta[8] = 1.0f;
  836. }
  837. if (settings.trackPosition && (status.pose & (int)SvrPlugin.TrackingMode.kTrackingPosition) != 0)
  838. {
  839. head.transform.localPosition = headPose.position * settings.trackPositionScale;
  840. }
  841. if ((status.pose & (int)SvrPlugin.TrackingMode.kTrackingEye) != 0)
  842. {
  843. //Debug.LogFormat("Left Eye Position: {0}, Direction: {1}", eyePose.leftPosition.ToString(), eyePose.leftDirection.ToString());
  844. //Debug.LogFormat("Right Eye Position: {0}, Direction: {1}", eyePose.rightPosition.ToString(), eyePose.rightDirection.ToString());
  845. //Debug.LogFormat("Combined Eye Position: {0}, Direction: {1}", eyePose.combinedPosition.ToString(), eyePose.combinedDirection.ToString());
  846. var combinedDirection = Vector3.zero;
  847. if ((eyePose.leftStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
  848. if ((eyePose.rightStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
  849. //if ((eyePose.combinedStatus & (int)SvrPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.combinedDirection;
  850. if (combinedDirection.sqrMagnitude > 0f)
  851. {
  852. combinedDirection.Normalize();
  853. //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", combinedDirection.x, combinedDirection.y, combinedDirection.z);
  854. eyeDirection = eyeSmoother > 0.001f ? Vector3.Lerp(eyeDirection, combinedDirection, eyeSmoother) : combinedDirection;
  855. //var combinedPosition = Vector3.zero;
  856. //if ((eyePose.leftStatus & (int)SvrPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.leftPosition;
  857. //if ((eyePose.rightStatus & (int)SvrPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.rightPosition;
  858. ////if ((eyePose.combinedStatus & (int)SvrPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.combinedPosition;
  859. gaze.localPosition = monoCamera.transform.localPosition;
  860. gaze.localRotation = Quaternion.LookRotation(eyeDirection, Vector3.up);
  861. float denominator = Vector3.Dot(eyeDirection, Vector3.forward);
  862. if (denominator > float.Epsilon)
  863. {
  864. // eye direction intersection with frustum near plane (left)
  865. var eyePoint = eyeDirection * plugin.deviceInfo.targetFrustumLeft.near / denominator;
  866. // size of the frustum near plane (left)
  867. var nearSize = new Vector2(0.5f*(plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left),
  868. 0.5f*(plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom));
  869. eyeFocus.Set(eyePoint.x / nearSize.x, eyePoint.y / nearSize.y); // Normalized [-1,1]
  870. //Debug.LogFormat("Eye Focus: {0}", eyeFocus.ToString());
  871. FocalPoint = eyeFocus; // Cache for foveated rendering
  872. }
  873. }
  874. }
  875. }
  876. var isValid = true;
  877. if (settings.poseStatusFade)
  878. {
  879. isValid = !settings.trackPosition
  880. || ((trackingMode & (int)SvrPlugin.TrackingMode.kTrackingPosition) == 0)
  881. || (status.pose & (int)SvrPlugin.TrackingMode.kTrackingPosition) != 0;
  882. }
  883. var targetAlpha = fadeState == eFadeState.FadeOut || !isValid ? 1f : 0f;
  884. UpdateOverlayFade(targetAlpha, Time.deltaTime / fadeDuration);
  885. if (Application.platform == RuntimePlatform.Android) {
  886. if (Input.GetMouseButton(0)) {
  887. leftCamera.ResetWorldToCameraMatrix();
  888. rightCamera.ResetWorldToCameraMatrix();
  889. } else {
  890. leftCamera.worldToCameraMatrix = plugin.leftViewMatrix * invertZ;
  891. rightCamera.worldToCameraMatrix = plugin.rightViewMatrix * invertZ;
  892. }
  893. }
  894. }
  895. public void Shutdown()
  896. {
  897. //Debug.Log("SvrManager.Shutdown()");
  898. status.running = false;
  899. if (submitFrame != null) { StopCoroutine(submitFrame); submitFrame = null; }
  900. if (plugin.IsRunning()) plugin.EndVr();
  901. if (plugin.IsInitialized()) plugin.Shutdown();
  902. status.initialized = false;
  903. }
  904. void OnDestory()
  905. {
  906. //Debug.Log("SvrManager.OnDestroy()");
  907. UnregisterListeners();
  908. Shutdown();
  909. }
  910. public delegate void OnApplicationPauseDele(bool pause);
  911. public static event OnApplicationPauseDele onApplicationPauseDele;
  912. void OnApplicationPause(bool pause)
  913. {
  914. onApplicationPauseDele?.Invoke(pause);
  915. Debug.LogFormat("SvrManager.OnApplicationPause({0})",pause);
  916. SetPause(pause);
  917. }
  918. void OnApplicationQuit()
  919. {
  920. //Debug.Log("SvrManager.OnApplicationQuit()");
  921. Shutdown();
  922. }
  923. static public Matrix4x4 Perspective(float left, float right, float bottom, float top, float near, float far)
  924. {
  925. float x = 2.0F * near / (right - left);
  926. float y = 2.0F * near / (top - bottom);
  927. float a = (right + left) / (right - left);
  928. float b = (top + bottom) / (top - bottom);
  929. float c = -(far + near) / (far - near);
  930. float d = -(2.0F * far * near) / (far - near);
  931. float e = -1.0F;
  932. Matrix4x4 m = new Matrix4x4();
  933. m[0, 0] = x;
  934. m[0, 1] = 0;
  935. m[0, 2] = a;
  936. m[0, 3] = 0;
  937. m[1, 0] = 0;
  938. m[1, 1] = y;
  939. m[1, 2] = b;
  940. m[1, 3] = 0;
  941. m[2, 0] = 0;
  942. m[2, 1] = 0;
  943. m[2, 2] = c;
  944. m[2, 3] = d;
  945. m[3, 0] = 0;
  946. m[3, 1] = 0;
  947. m[3, 2] = e;
  948. m[3, 3] = 0;
  949. return m;
  950. }
  951. void RegisterListeners()
  952. {
  953. SvrOverrideSettings.OnEyeAntiAliasingChangedEvent += OnEyeAntiAliasingChanged;
  954. SvrOverrideSettings.OnEyeDepthChangedEvent += OnEyeDepthChanged;
  955. SvrOverrideSettings.OnEyeResolutionScaleFactorChangedEvent += OnEyeResolutionScaleFactorChanged;
  956. SvrOverrideSettings.OnOverlayAntiAliasingChangedEvent += OnOverlayAntiAliasingChanged;
  957. SvrOverrideSettings.OnOverlayDepthChangedEvent += OnOverlayDepthChanged;
  958. SvrOverrideSettings.OnOverlayResolutionScaleFactorChangedEvent += OnOverlayResolutionScaleFactorChanged;
  959. SvrOverrideSettings.OnChromaticAberrationCorrectionChangedEvent += OnChromaticAberrationCorrectionChanged;
  960. SvrOverrideSettings.OnVSyncCountChangedEvent += OnVSyncCountChanged;
  961. SvrOverrideSettings.OnMasterTextureLimitChangedEvent += OnMasterTextureLimitChanged;
  962. SvrOverrideSettings.OnPerfLevelChangedEvent += OnPerfLevelChanged;
  963. SvrOverrideSettings.OnFoveateChangedEvent += OnFoveateChanged;
  964. }
  965. void UnregisterListeners()
  966. {
  967. SvrOverrideSettings.OnEyeAntiAliasingChangedEvent -= OnEyeAntiAliasingChanged;
  968. SvrOverrideSettings.OnEyeDepthChangedEvent -= OnEyeDepthChanged;
  969. SvrOverrideSettings.OnEyeResolutionScaleFactorChangedEvent -= OnEyeResolutionScaleFactorChanged;
  970. SvrOverrideSettings.OnOverlayAntiAliasingChangedEvent -= OnOverlayAntiAliasingChanged;
  971. SvrOverrideSettings.OnOverlayDepthChangedEvent -= OnOverlayDepthChanged;
  972. SvrOverrideSettings.OnOverlayResolutionScaleFactorChangedEvent -= OnOverlayResolutionScaleFactorChanged;
  973. SvrOverrideSettings.OnChromaticAberrationCorrectionChangedEvent -= OnChromaticAberrationCorrectionChanged;
  974. SvrOverrideSettings.OnVSyncCountChangedEvent -= OnVSyncCountChanged;
  975. SvrOverrideSettings.OnMasterTextureLimitChangedEvent -= OnMasterTextureLimitChanged;
  976. SvrOverrideSettings.OnPerfLevelChangedEvent -= OnPerfLevelChanged;
  977. SvrOverrideSettings.OnFoveateChangedEvent -= OnFoveateChanged;
  978. }
  979. void OnEyeAntiAliasingChanged(SvrOverrideSettings.eAntiAliasing antiAliasing)
  980. {
  981. foreach (SvrEye eye in eyes)
  982. {
  983. eye.AntiAliasing = antiAliasing == SvrOverrideSettings.eAntiAliasing.NoOverride ?
  984. (int)settings.eyeAntiAliasing : (int)antiAliasing;
  985. }
  986. }
  987. void OnEyeDepthChanged(SvrOverrideSettings.eDepth depth)
  988. {
  989. foreach (SvrEye eye in eyes)
  990. {
  991. eye.Depth = depth == SvrOverrideSettings.eDepth.NoOverride ?
  992. (int)settings.eyeDepth : (int)depth;
  993. }
  994. }
  995. void OnEyeResolutionScaleFactorChanged(float scaleFactor)
  996. {
  997. foreach (SvrEye eye in eyes)
  998. {
  999. eye.ResolutionScaleFactor = scaleFactor <= 0 ? settings.eyeResolutionScaleFactor : scaleFactor;
  1000. }
  1001. }
  1002. void OnOverlayAntiAliasingChanged(SvrOverrideSettings.eAntiAliasing antiAliasing)
  1003. {
  1004. foreach (SvrOverlay overlay in overlays)
  1005. {
  1006. overlay.AntiAliasing = antiAliasing == SvrOverrideSettings.eAntiAliasing.NoOverride ?
  1007. (int)settings.overlayAntiAliasing : (int)antiAliasing;
  1008. }
  1009. }
  1010. void OnOverlayDepthChanged(SvrOverrideSettings.eDepth depth)
  1011. {
  1012. foreach (SvrOverlay overlay in overlays)
  1013. {
  1014. overlay.Depth = depth == SvrOverrideSettings.eDepth.NoOverride ?
  1015. (int)settings.overlayDepth : (int)depth;
  1016. }
  1017. }
  1018. void OnOverlayResolutionScaleFactorChanged(float scaleFactor)
  1019. {
  1020. foreach (SvrOverlay overlay in overlays)
  1021. {
  1022. overlay.ResolutionScaleFactor = scaleFactor <= 0 ? settings.overlayResolutionScaleFactor : scaleFactor;
  1023. }
  1024. }
  1025. void OnChromaticAberrationCorrectionChanged(SvrOverrideSettings.eChromaticAberrationCorrection aberrationCorrection)
  1026. {
  1027. if(aberrationCorrection == SvrOverrideSettings.eChromaticAberrationCorrection.kDisable)
  1028. {
  1029. plugin.SetFrameOption(SvrPlugin.FrameOption.kDisableChromaticCorrection);
  1030. }
  1031. else
  1032. {
  1033. plugin.UnsetFrameOption(SvrPlugin.FrameOption.kDisableChromaticCorrection);
  1034. }
  1035. }
  1036. void OnVSyncCountChanged(SvrOverrideSettings.eVSyncCount vSyncCount)
  1037. {
  1038. if (vSyncCount == SvrOverrideSettings.eVSyncCount.NoOverride)
  1039. {
  1040. plugin.SetVSyncCount((int)settings.vSyncCount);
  1041. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  1042. }
  1043. else
  1044. {
  1045. plugin.SetVSyncCount((int)vSyncCount);
  1046. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  1047. }
  1048. }
  1049. void OnMasterTextureLimitChanged(SvrOverrideSettings.eMasterTextureLimit masterTextureLimit)
  1050. {
  1051. QualitySettings.masterTextureLimit = masterTextureLimit == SvrOverrideSettings.eMasterTextureLimit.NoOverride ?
  1052. (int)settings.masterTextureLimit : (int)masterTextureLimit;
  1053. }
  1054. void OnPerfLevelChanged(SvrOverrideSettings.ePerfLevel cpuPerfLevel, SvrOverrideSettings.ePerfLevel gpuPerfLevel)
  1055. {
  1056. int currentCpuPerfLevel = cpuPerfLevel == SvrOverrideSettings.ePerfLevel.NoOverride ?
  1057. (int)settings.cpuPerfLevel : (int)SvrOverrideSettings.CpuPerfLevel;
  1058. int currentGpuPerfLevel = gpuPerfLevel == SvrOverrideSettings.ePerfLevel.NoOverride ?
  1059. (int)settings.gpuPerfLevel : (int)SvrOverrideSettings.GpuPerfLevel;
  1060. plugin.SetPerformanceLevels(currentCpuPerfLevel, currentGpuPerfLevel);
  1061. }
  1062. void OnFoveateChanged(Vector2 gain, float area, float minPixelDensity)
  1063. {
  1064. var point = Vector2.zero;
  1065. var currentGain = gain == Vector2.zero ?
  1066. settings.foveationGain : SvrOverrideSettings.FoveateGain;
  1067. var currentArea = area == 0f ?
  1068. settings.foveationArea : SvrOverrideSettings.FoveateArea;
  1069. var currentMinimum = minPixelDensity == 0f ?
  1070. settings.foveationMinimum : SvrOverrideSettings.FoveateMinimum;
  1071. plugin.SetFoveationParameters(0, 0, point.x, point.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
  1072. }
  1073. /// <summary>
  1074. /// Update this instance.
  1075. /// </summary>
  1076. //---------------------------------------------------------------------------------------------
  1077. void Update()
  1078. {
  1079. SvrEvent frameEvent = new SvrEvent ();
  1080. while (plugin.PollEvent(ref frameEvent))
  1081. {
  1082. Debug.LogFormat("SvrEvent: {0}", frameEvent.eventType.ToString());
  1083. for (int i = 0; i < eventListeners.Count; i++) {
  1084. eventListeners [i].OnSvrEvent (frameEvent);
  1085. }
  1086. }
  1087. }
  1088. /// <summary>
  1089. /// Adds the event listener.
  1090. /// </summary>
  1091. /// <param name="listener">Listener.</param>
  1092. //---------------------------------------------------------------------------------------------
  1093. public void AddEventListener(SvrEventListener listener)
  1094. {
  1095. if (listener != null) {
  1096. eventListeners.Insert (0, listener);
  1097. }
  1098. }
  1099. /// <summary>
  1100. /// Start Tracking
  1101. /// </summary>
  1102. /// <returns>Handle to the controller</returns>
  1103. /// <param name="desc">Desc.</param>
  1104. //---------------------------------------------------------------------------------------------
  1105. public int ControllerStartTracking(string desc)
  1106. {
  1107. return plugin.ControllerStartTracking(desc);
  1108. }
  1109. /// <summary>
  1110. /// Stop Tracking
  1111. /// </summary>
  1112. /// <param name="handle">Handle.</param>
  1113. //---------------------------------------------------------------------------------------------
  1114. public void ControllerStopTracking(int handle)
  1115. {
  1116. plugin.ControllerStopTracking(handle);
  1117. }
  1118. /// <summary>
  1119. /// Get current state
  1120. /// </summary>
  1121. /// <returns>Controller State.</returns>
  1122. /// <param name="handle">Handle.</param>
  1123. //---------------------------------------------------------------------------------------------
  1124. public SvrControllerState ControllerGetState(int handle, int space = 0)
  1125. {
  1126. return plugin.ControllerGetState(handle, space);
  1127. }
  1128. /// <summary>
  1129. /// Send an event to the controller
  1130. /// </summary>
  1131. /// <param name="handle">Handle.</param>
  1132. /// <param name="what">What.</param>
  1133. /// <param name="arg1">Arg1.</param>
  1134. /// <param name="arg2">Arg2.</param>
  1135. //---------------------------------------------------------------------------------------------
  1136. public void ControllerSendMessage(int handle, SvrController.svrControllerMessageType what, int arg1, int arg2)
  1137. {
  1138. plugin.ControllerSendMessage (handle, what, arg1, arg2);
  1139. }
  1140. /// <summary>
  1141. /// Controllers the query.
  1142. /// </summary>
  1143. /// <returns>The query.</returns>
  1144. /// <param name="handle">Handle.</param>
  1145. /// <param name="what">What.</param>
  1146. /// <param name="mem">Mem.</param>
  1147. /// <param name="size">Size.</param>
  1148. //---------------------------------------------------------------------------------------------
  1149. public object ControllerQuery(int handle, SvrController.svrControllerQueryType what)
  1150. {
  1151. return plugin.ControllerQuery (handle, what);
  1152. }
  1153. }