GSXRManager.cs 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. using SC.XR.Unity;
  10. public class GSXRManager : MonoBehaviour
  11. {
  12. public static GSXRManager Instance;
  13. //{
  14. // get
  15. // {
  16. // if (instance == null) instance = FindObjectOfType<GSXRManager>();
  17. // if (instance == null) Debug.LogError("GSXRManager object component not found");
  18. // return instance;
  19. // }
  20. //}
  21. //private static GSXRManager instance;
  22. static public int EyeLayerMax = 8; // svrApi SVR_MAX_EYE_LAYERS
  23. static public int OverlayLayerMax = 8; // svrApi SVR_MAX_OVERLAY_LAYERS
  24. static public int RenderLayersMax = 16; // svrApi SVR_MAX_RENDER_LAYERS
  25. public Action SvrInitializedCallBack;
  26. public enum SpecificationType {
  27. SVR,
  28. GSXR,
  29. }
  30. [SerializeField]
  31. private SpecificationType m_SpecificationType = SpecificationType.SVR;
  32. public SpecificationType specificationType {
  33. get { return m_SpecificationType; }
  34. private set { m_SpecificationType = value; }
  35. }
  36. public enum svrEventType
  37. {
  38. kEventNone = 0,
  39. kEventSdkServiceStarting = 1,
  40. kEventSdkServiceStarted = 2,
  41. kEventSdkServiceStopped = 3,
  42. kEventControllerConnecting = 4,
  43. kEventControllerConnected = 5,
  44. kEventControllerDisconnected = 6,
  45. kEventThermal = 7,
  46. kEventVrModeStarted = 8,
  47. kEventVrModeStopping = 9,
  48. kEventVrModeStopped = 10,
  49. kEventSensorError = 11,
  50. kEventMagnometerUncalibrated = 12,
  51. kEventBoundarySystemCollision = 13,
  52. kEvent6dofRelocation = 14,
  53. kEvent6dofWarningFeatureCount = 15,
  54. kEvent6dofWarningLowLight = 16,
  55. kEvent6dofWarningBrightLight = 17,
  56. kEvent6dofWarningCameraCalibration = 18
  57. };
  58. [Serializable]
  59. public class SvrSettings
  60. {
  61. public enum eAntiAliasing
  62. {
  63. k1 = 1,
  64. k2 = 2,
  65. k4 = 4,
  66. };
  67. public enum eDepth
  68. {
  69. k16 = 16,
  70. k24 = 24
  71. };
  72. public enum eChromaticAberrationCorrection
  73. {
  74. kDisable = 0,
  75. kEnable = 1
  76. };
  77. public enum eVSyncCount
  78. {
  79. k0 = 0,
  80. k1 = 1,
  81. k2 = 2,
  82. };
  83. public enum eMasterTextureLimit
  84. {
  85. k0 = 0, // full size
  86. k1 = 1, // half size
  87. k2 = 2, // quarter size
  88. k3 = 3, // ...
  89. k4 = 4 // ...
  90. };
  91. public enum ePerfLevel
  92. {
  93. Minimum = 1,
  94. Medium = 2,
  95. Maximum = 3
  96. };
  97. public enum eFrustumType
  98. {
  99. Camera = 0,
  100. Device = 1,
  101. }
  102. public enum eEyeBufferType
  103. {
  104. //Mono = 0,
  105. StereoSeperate = 1,
  106. //StereoSingle = 2,
  107. //Array = 3,
  108. }
  109. [Tooltip("If head tracking lost, fade the display")]
  110. public bool poseStatusFade = true;
  111. [Tooltip("Use eye tracking (if available)")]
  112. public bool trackEyes = true;
  113. [Tooltip("Use position tracking (if available)")]
  114. public bool trackPosition = true;
  115. [Tooltip("Track position conversion from meters")]
  116. public float trackPositionScale = 1;
  117. [Tooltip("Height of the eyes from base of head")]
  118. public float headHeight = 0.0750f;
  119. [Tooltip("Depth of the eyes from center of head")]
  120. public float headDepth = 0.0805f;
  121. [Tooltip("Distance between the eyes")]
  122. public float interPupilDistance = 0.064f;
  123. //[Tooltip("Distance of line-of-sight convergence (0 disabled)")]
  124. //public float stereoConvergence = 0;
  125. //[Tooltip("Pitch angle to the horizon in degrees")]
  126. //public float horizonElevation = 0;
  127. [Tooltip("Eye field of view render target reprojection margin (% of fov) [0..]")]
  128. public float eyeFovMargin = 0.0f;
  129. [Tooltip("Eye render target scale factor")]
  130. public float eyeResolutionScaleFactor = 1.0f;
  131. [Tooltip("Eye render target depth buffer")]
  132. public eDepth eyeDepth = eDepth.k24;
  133. [Tooltip("Eye render target MSAA value")]
  134. public eAntiAliasing eyeAntiAliasing = eAntiAliasing.k2;
  135. [Tooltip("Overlay render target scale factor")]
  136. public float overlayResolutionScaleFactor = 1.0f;
  137. [Tooltip("Overlay render target depth buffer")]
  138. public eDepth overlayDepth = eDepth.k16;
  139. [Tooltip("Overlay render target MSAA value")]
  140. public eAntiAliasing overlayAntiAliasing = eAntiAliasing.k1;
  141. [Tooltip("Limit refresh rate")]
  142. public eVSyncCount vSyncCount = eVSyncCount.k1;
  143. [Tooltip("Chromatic Aberration Correction")]
  144. public eChromaticAberrationCorrection chromaticAberationCorrection = eChromaticAberrationCorrection.kEnable;
  145. [Tooltip("QualitySettings TextureQuality FullRes, HalfRes, etc.")]
  146. public eMasterTextureLimit masterTextureLimit = eMasterTextureLimit.k0;
  147. [Tooltip("CPU performance level")]
  148. public ePerfLevel cpuPerfLevel = ePerfLevel.Medium;
  149. [Tooltip("GPU performance level")]
  150. public ePerfLevel gpuPerfLevel = ePerfLevel.Medium;
  151. [Tooltip("Foveated render gain [1..], 0/feature disabled")]
  152. public Vector2 foveationGain = new Vector2(0.0f, 0.0f);
  153. [Tooltip("Foveated render hires area [0..]")]
  154. public float foveationArea = 0.0f;
  155. [Tooltip("Foveated render min pixel density [1/16..1]")]
  156. public float foveationMinimum = 0.25f;
  157. [Tooltip("Use perspective of unity camera or device frustum data")]
  158. public eFrustumType frustumType = eFrustumType.Camera;
  159. [Tooltip("Display buffer type (default stereo seperate)")]
  160. public eEyeBufferType displayType = eEyeBufferType.StereoSeperate;
  161. }
  162. [SerializeField]
  163. public SvrSettings settings;
  164. [Serializable]
  165. public class SvrStatus
  166. {
  167. [Tooltip("SnapdragonVR SDK Initialized")]
  168. public bool initialized = false;
  169. [Tooltip("SnapdragonVR SDK Running")]
  170. public bool running = false;
  171. [Tooltip("SnapdragonVR SDK Pose Status: 0/None, 1/Rotation, 2/Position, 3/RotationAndPosition")]
  172. public int pose = 0;
  173. }
  174. [SerializeField]
  175. public SvrStatus status;
  176. public enum eFadeState { FadeIn, FadeOut }
  177. [NonSerialized]
  178. public eFadeState fadeState = eFadeState.FadeIn;
  179. [NonSerialized]
  180. public float fadeDuration = 0.5f;
  181. [Header("Camera Rig")]
  182. public Transform head;
  183. public Transform gaze;
  184. public Camera monoCamera;
  185. public Camera leftCamera;
  186. public Camera rightCamera;
  187. public Camera leftOverlay;
  188. public Camera rightOverlay;
  189. public Camera monoOverlay;
  190. public GSXROverlay fadeOverlay;
  191. public GSXROverlay reticleOverlay;
  192. private bool IsUseOpticsCalibration = true;
  193. public Vector2 FocalPoint { get; set; } // Foveated Rendering Focal Point
  194. public int FrameCount { get { return frameCount; } }
  195. private int frameCount = 0;
  196. private static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
  197. public GSXRPlugin plugin = null;
  198. private float sensorWarmupDuration = 0.25f;
  199. private List<GSXREye> eyes = new List<GSXREye>(EyeLayerMax);
  200. private List<GSXROverlay> overlays = new List<GSXROverlay>(OverlayLayerMax);
  201. private bool disableInput = false;
  202. private Coroutine onResume = null;
  203. private Coroutine submitFrame = null;
  204. private Matrix4x4 headDeltaInitLocalToWorld = Matrix4x4.identity;
  205. /// <summary>
  206. /// Svr event listener.
  207. /// </summary>
  208. public interface SvrEventListener {
  209. /// <summary>
  210. /// Raises the svr event event.
  211. /// </summary>
  212. /// <param name="ev">Ev.</param>
  213. void OnSvrEvent (SvrEvent ev);
  214. };
  215. public enum svrThermalLevel
  216. {
  217. kSafe,
  218. kLevel1,
  219. kLevel2,
  220. kLevel3,
  221. kCritical,
  222. kNumThermalLevels
  223. };
  224. public enum svrThermalZone
  225. {
  226. kCpu,
  227. kGpu,
  228. kSkin,
  229. kNumThermalZones
  230. };
  231. public struct svrEventData_Thermal
  232. {
  233. public svrThermalZone zone; //!< Thermal zone
  234. public svrThermalLevel level; //!< Indication of the current zone thermal level
  235. };
  236. [StructLayout(LayoutKind.Explicit)]
  237. public struct svrEventData
  238. {
  239. [FieldOffset(0)]
  240. public svrEventData_Thermal thermal;
  241. //[FieldOffset(0)]
  242. //public svrEventData_New newData;
  243. }
  244. public struct SvrEvent
  245. {
  246. public svrEventType eventType; //!< Type of event
  247. public uint deviceId; //!< An identifier for the device that generated the event (0 == HMD)
  248. public float eventTimeStamp; //!< Time stamp for the event in seconds since the last svrBeginVr call
  249. public svrEventData eventData; //!< Event specific data payload
  250. };
  251. // david start
  252. public struct SCSingleLayerData
  253. {
  254. public UInt32 layerId;
  255. public UInt32 parentLayerId;
  256. public UInt32 layerTextureId;
  257. [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
  258. public float[] modelMatrixData;
  259. public UInt32 editFlag;
  260. public int z;
  261. [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 12)]
  262. public float[] vertexPosition;
  263. [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 8)]
  264. public float[] vertexUV;
  265. public float alpha;
  266. public byte bOpaque;
  267. public UInt32 taskId;
  268. };
  269. public struct SCAllLayers
  270. {
  271. public UInt32 layerNum;
  272. [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 16)]
  273. public float[] viewMatrixData;
  274. public IntPtr layerData;
  275. };
  276. // david end
  277. private List<SvrEventListener> eventListeners = new List<SvrEventListener>();
  278. public bool Initialized
  279. {
  280. get { return status.initialized; }
  281. }
  282. public bool IsRunning
  283. {
  284. get { return status.running; }
  285. }
  286. public bool DisableInput
  287. {
  288. get { return disableInput; }
  289. set { disableInput = value; }
  290. }
  291. void Awake() {
  292. if (Instance) {
  293. DestroyImmediate(gameObject);
  294. return;
  295. }
  296. Instance = this;
  297. Debug.Log("SpecificationType:" + specificationType);
  298. if (!ValidateReferencedComponents ())
  299. {
  300. enabled = false;
  301. return;
  302. }
  303. RegisterListeners();
  304. Input.backButtonLeavesApp = true;
  305. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  306. Application.targetFrameRate = -1;
  307. if(API_Module_SDKConfiguration.HasKey("Module_Slam", "IsUseOpticsCalibration")){
  308. IsUseOpticsCalibration = API_Module_SDKConfiguration.GetBool("Module_Slam", "IsUseOpticsCalibration", 1);
  309. DebugMy.Log("IsUseOpticsCalibration:" + IsUseOpticsCalibration, this,true);
  310. }
  311. if (API_Module_SDKConfiguration.HasKey("Module_Slam", "trackPositionScale")) {
  312. settings.trackPositionScale = API_Module_SDKConfiguration.GetFloat("Module_Slam", "trackPositionScale", 1);
  313. DebugMy.Log("settings.trackPositionScale:" + settings.trackPositionScale, this, true);
  314. }
  315. }
  316. void OnGUI() {
  317. }
  318. bool ValidateReferencedComponents()
  319. {
  320. plugin = GSXRPlugin.Instance;
  321. if(plugin == null)
  322. {
  323. Debug.LogError("Svr Plugin failed to load. Disabling...");
  324. return false;
  325. }
  326. if(head == null)
  327. {
  328. Debug.LogError("Required head gameobject not found! Disabling...");
  329. return false;
  330. }
  331. if(monoCamera == null && (leftCamera == null || rightCamera == null))
  332. {
  333. Debug.LogError("Required eye components are missing! Disabling...");
  334. return false;
  335. }
  336. return true;
  337. }
  338. #if UNITY_2018
  339. void OnEnable()
  340. {
  341. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  342. {
  343. UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering += OnPreRenderEvent;
  344. }
  345. }
  346. void OnDisable()
  347. {
  348. if (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset)
  349. {
  350. UnityEngine.Experimental.Rendering.RenderPipeline.beginCameraRendering -= OnPreRenderEvent;
  351. }
  352. }
  353. private void OnPreRenderEvent(Camera camera)
  354. {
  355. camera.SendMessage("OnPreRenderEvent", SendMessageOptions.DontRequireReceiver);
  356. }
  357. #endif
  358. IEnumerator Start ()
  359. {
  360. yield return StartCoroutine(Initialize());
  361. status.initialized = plugin.IsInitialized();
  362. SetOverlayFade(eFadeState.FadeIn);
  363. yield return StartCoroutine(plugin.BeginVr((int)settings.cpuPerfLevel, (int)settings.gpuPerfLevel));
  364. if (!plugin.IsRunning())
  365. {
  366. Debug.LogError("Svr failed!");
  367. Application.Quit();
  368. yield return null; // Wait one frame
  369. }
  370. float recenterTimeout = 1f;
  371. while (!plugin.RecenterTracking() && recenterTimeout > 0f)
  372. {
  373. yield return null; // Wait one frame
  374. recenterTimeout -= Time.deltaTime;
  375. }
  376. yield return new WaitForSecondsRealtime(sensorWarmupDuration);
  377. submitFrame = StartCoroutine(SubmitFrame());
  378. status.running = true;
  379. SvrInitializedCallBack?.Invoke();
  380. plugin.SetGlassDisconnectedCallBack(GlassDisconnetedCallBack);
  381. Debug.Log("Svr initialized!");
  382. Vector3 localLeftShoulder=Vector3.zero;
  383. if (API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderX") && API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderY") && API_Module_SDKConfiguration.HasKey("Module_Slam", "LeftShoulderZ")) {
  384. localLeftShoulder = new Vector3(API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderX", 0.0f), API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderY", 0.0f), API_Module_SDKConfiguration.GetFloat("Module_Slam", "LeftShoulderZ", 0.0f));
  385. }
  386. shoulder = new Shoulder(head, localLeftShoulder == Vector3.zero ? new Vector3(-0.15f,-0.08f,-0.1f): localLeftShoulder);
  387. }
  388. [MonoPInvokeCallback(typeof(Action))]
  389. private static void GlassDisconnetedCallBack() {
  390. Debug.Log("GlassDisconnetedCallBack");
  391. Application.Quit();
  392. }
  393. IEnumerator Initialize()
  394. {
  395. // Plugin must be initialized OnStart in order to properly
  396. // get a valid surface
  397. // GameObject mainCameraGo = GameObject.FindWithTag("MainCamera");
  398. // if (mainCameraGo)
  399. // {
  400. // mainCameraGo.SetActive(false);
  401. //
  402. // Debug.Log("Camera with MainCamera tag found.");
  403. // if (!disableInput)
  404. // {
  405. // Debug.Log("Will use translation and orientation from the MainCamera.");
  406. // transform.position = mainCameraGo.transform.position;
  407. // transform.rotation = mainCameraGo.transform.rotation;
  408. // }
  409. //
  410. // Debug.Log("Disabling Camera with MainCamera tag");
  411. // }
  412. GL.Clear(false, true, Color.black);
  413. yield return StartCoroutine(plugin.Initialize());
  414. InitializeCameras();
  415. InitializeEyes();
  416. InitializeOverlays();
  417. int trackingMode = (int)GSXRPlugin.TrackingMode.kTrackingOrientation;
  418. if (API_Module_SDKConfiguration.HasKey("Module_Slam", "IsSlamUse3Dof")) {
  419. if (API_Module_SDKConfiguration.GetBool("Module_Slam", "IsSlamUse3Dof", 0)) {
  420. DebugMy.Log("Slam Use3Dof", this, true);
  421. settings.trackPosition = false;
  422. }
  423. }
  424. if (settings.trackPosition)
  425. trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingPosition;
  426. if (settings.trackEyes)
  427. trackingMode |= (int)GSXRPlugin.TrackingMode.kTrackingEye;
  428. plugin.SetTrackingMode(trackingMode);
  429. plugin.SetVSyncCount((int)settings.vSyncCount);
  430. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  431. }
  432. Matrix4x4 invertZ = new Matrix4x4(
  433. new Vector4(1, 0, 0, 0),
  434. new Vector4(0, 1, 0, 0),
  435. new Vector4(0, 0, -1, 0),
  436. new Vector4(0, 0, 0, 1));
  437. private void InitializeCameras()
  438. {
  439. float stereoConvergence = plugin.deviceInfo.targetFrustumConvergence; //settings.stereoConvergence
  440. float horizonElevation = plugin.deviceInfo.targetFrustumPitch; //settings.horizonElevation
  441. float convergenceAngle = 0f;
  442. if (stereoConvergence > Mathf.Epsilon) convergenceAngle = Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, stereoConvergence);
  443. else if (stereoConvergence < -Mathf.Epsilon) convergenceAngle = -Mathf.Rad2Deg * Mathf.Atan2(0.5f * settings.interPupilDistance, -stereoConvergence);
  444. // left
  445. Vector3 eyePos;
  446. eyePos.x = -0.5f * settings.interPupilDistance;
  447. eyePos.y = (!settings.trackPosition ? settings.headHeight : 0);
  448. eyePos.z = (!settings.trackPosition ? settings.headDepth : 0);
  449. eyePos += head.transform.localPosition;
  450. Quaternion eyeRot;
  451. eyeRot = Quaternion.Euler(horizonElevation, convergenceAngle, 0);
  452. if (leftCamera != null)
  453. {
  454. leftCamera.transform.localPosition = eyePos;
  455. leftCamera.transform.localRotation = eyeRot;
  456. }
  457. if (leftOverlay != null)
  458. {
  459. leftOverlay.transform.localPosition = eyePos;
  460. leftOverlay.transform.localRotation = eyeRot;
  461. }
  462. // right
  463. eyePos.x *= -1;
  464. eyeRot = Quaternion.Euler(horizonElevation, -convergenceAngle, 0);
  465. if (rightCamera != null)
  466. {
  467. rightCamera.transform.localPosition = eyePos;
  468. rightCamera.transform.localRotation = eyeRot;
  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, GSXREye.eSide side)
  490. {
  491. bool enableCamera = false;
  492. var eyesFound = cam.gameObject.GetComponents<GSXREye>();
  493. for (int i = 0; i < eyesFound.Length; i++)
  494. {
  495. eyesFound[i].Side = side;
  496. if (eyesFound[i].imageType == GSXREye.eType.RenderTexture) enableCamera = true;
  497. }
  498. eyes.AddRange(eyesFound);
  499. if (eyesFound.Length == 0)
  500. {
  501. var eye = cam.gameObject.AddComponent<GSXREye>();
  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, GSXREye.eSide.Both);
  520. }
  521. if (leftCamera != null && leftCamera.gameObject.activeSelf)
  522. {
  523. AddEyes(leftCamera, GSXREye.eSide.Left);
  524. }
  525. if (rightCamera != null && rightCamera.gameObject.activeSelf)
  526. {
  527. AddEyes(rightCamera, GSXREye.eSide.Right);
  528. }
  529. for (int i = 0; i < GSXREye.Instances.Count; i++)
  530. {
  531. var eye = GSXREye.Instances[i];
  532. if (!eyes.Contains(eye))
  533. {
  534. eyes.Add(eye); // Add eyes found outside of svr camera hierarchy
  535. }
  536. }
  537. GSXRPlugin.DeviceInfo info = plugin.deviceInfo;
  538. foreach(GSXREye 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, GSXROverlay.eSide side)
  554. {
  555. bool enableCamera = false;
  556. var overlaysFound = cam.gameObject.GetComponents<GSXROverlay>();
  557. for (int i = 0; i < overlaysFound.Length; i++)
  558. {
  559. overlaysFound[i].Side = side;
  560. if (overlaysFound[i].imageType == GSXROverlay.eType.RenderTexture) enableCamera = true;
  561. }
  562. overlays.AddRange(overlaysFound);
  563. if (overlaysFound.Length == 0)
  564. {
  565. var overlay = cam.gameObject.AddComponent<GSXROverlay>();
  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, GSXROverlay.eSide.Left);
  584. }
  585. if (rightOverlay != null && rightOverlay.gameObject.activeSelf)
  586. {
  587. AddOverlays(rightOverlay, GSXROverlay.eSide.Right);
  588. }
  589. if (monoOverlay != null && monoOverlay.gameObject.activeSelf)
  590. {
  591. AddOverlays(monoOverlay, GSXROverlay.eSide.Both);
  592. }
  593. for (int i = 0; i < GSXROverlay.Instances.Count; i++)
  594. {
  595. var overlay = GSXROverlay.Instances[i];
  596. if (!overlays.Contains(overlay))
  597. {
  598. overlays.Add(overlay); // Add overlays found outside of svr camera hierarchy
  599. }
  600. }
  601. GSXRPlugin.DeviceInfo info = plugin.deviceInfo;
  602. foreach (GSXROverlay 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)GSXRPlugin.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)GSXRPlugin.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)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
  670. if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
  671. //if ((eyePose.combinedStatus & (int)GSXRPlugin.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 = GSXROverrideSettings.FoveateGain == Vector2.zero ?
  688. settings.foveationGain : GSXROverrideSettings.FoveateGain;
  689. var currentArea = GSXROverrideSettings.FoveateArea == 0f ?
  690. settings.foveationArea : GSXROverrideSettings.FoveateArea;
  691. var currentMinimum = GSXROverrideSettings.FoveateMinimum == 0f ?
  692. settings.foveationMinimum : GSXROverrideSettings.FoveateMinimum;
  693. for (int i = 0; i < eyes.Count; i++)
  694. {
  695. var eye = eyes[i];
  696. if (eye.TextureId > 0 && eye.ImageType == GSXREye.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 = GSXROverrideSettings.FoveateGain == Vector2.zero ?
  722. settings.foveationGain : GSXROverrideSettings.FoveateGain;
  723. var currentArea = GSXROverrideSettings.FoveateArea == 0f ?
  724. settings.foveationArea : GSXROverrideSettings.FoveateArea;
  725. var currentMinimum = GSXROverrideSettings.FoveateMinimum == 0f ?
  726. settings.foveationMinimum : GSXROverrideSettings.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("GSXRManager.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("GSXRManager.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 GSXRPlugin.HeadPose headPose;
  778. [NonSerialized]
  779. public GSXRPlugin.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. Matrix4x4 leftcalib = Matrix4x4.identity;
  793. Matrix4x4 rightCalib = Matrix4x4.identity;
  794. void LateUpdate()
  795. {
  796. if (!IsRunning)
  797. {
  798. return;
  799. }
  800. int trackingMode = plugin.GetTrackingMode();
  801. var prevOrientation = headPose.orientation;
  802. status.pose = plugin.GetHeadPose(ref headPose, frameCount);
  803. if ((trackingMode & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0)
  804. {
  805. status.pose |= plugin.GetEyePose(ref eyePose, frameCount);
  806. }
  807. if (!disableInput)
  808. {
  809. if ((status.pose & (int)GSXRPlugin.TrackingMode.kTrackingOrientation) != 0)
  810. {
  811. head.transform.localRotation = headPose.orientation;
  812. // delta orientation screen space x, y offset for foveated rendering
  813. var deltaOrientation = Quaternion.Inverse(prevOrientation) * headPose.orientation;
  814. var lookDirection = deltaOrientation * Vector3.forward;
  815. //Debug.LogFormat("Look Direction: {0}", lookDirection.ToString());
  816. lookDirection *= plugin.deviceInfo.targetFrustumLeft.near / lookDirection.z;
  817. float xTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left);
  818. float xAdjust = (xTotal != 0.0f) ? lookDirection.x / xTotal : 0.0f;
  819. float yTotal = 0.5f * (plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom);
  820. float yAdjust = (yTotal != 0.0f) ? lookDirection.y / yTotal : 0.0f;
  821. //xAdjust *= 0.5f * plugin.deviceInfo.targetEyeWidthPixels;
  822. //yAdjust *= 0.5f * plugin.deviceInfo.targetEyeHeightPixels;
  823. // rotation around z [cos(z), sin(z), 0], [-sin(z), cos(z), 0], [0, 0, 1]
  824. Vector3 deltaEulers = deltaOrientation.eulerAngles;
  825. float cosZ = Mathf.Cos(deltaEulers.z * Mathf.Deg2Rad);
  826. float sinZ = Mathf.Sin(deltaEulers.z * Mathf.Deg2Rad);
  827. // Output rotation and translation
  828. frameDelta[0] = cosZ;
  829. frameDelta[1] = sinZ;
  830. frameDelta[2] = 0.0f;
  831. frameDelta[3] = -sinZ;
  832. frameDelta[4] = cosZ;
  833. frameDelta[5] = 0.0f;
  834. frameDelta[6] = xAdjust;
  835. frameDelta[7] = yAdjust;
  836. frameDelta[8] = 1.0f;
  837. }
  838. if (settings.trackPosition && (status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0)
  839. {
  840. head.transform.localPosition = headPose.position * settings.trackPositionScale;
  841. }
  842. if ((status.pose & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0)
  843. {
  844. //Debug.LogFormat("Left Eye Position: {0}, Direction: {1}", eyePose.leftPosition.ToString(), eyePose.leftDirection.ToString());
  845. //Debug.LogFormat("Right Eye Position: {0}, Direction: {1}", eyePose.rightPosition.ToString(), eyePose.rightDirection.ToString());
  846. //Debug.LogFormat("Combined Eye Position: {0}, Direction: {1}", eyePose.combinedPosition.ToString(), eyePose.combinedDirection.ToString());
  847. var combinedDirection = Vector3.zero;
  848. if ((eyePose.leftStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.leftDirection;
  849. if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.rightDirection;
  850. //if ((eyePose.combinedStatus & (int)GSXRPlugin.EyePoseStatus.kGazeVectorValid) != 0) combinedDirection += eyePose.combinedDirection;
  851. if (combinedDirection.sqrMagnitude > 0f)
  852. {
  853. combinedDirection.Normalize();
  854. //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", combinedDirection.x, combinedDirection.y, combinedDirection.z);
  855. eyeDirection = eyeSmoother > 0.001f ? Vector3.Lerp(eyeDirection, combinedDirection, eyeSmoother) : combinedDirection;
  856. //var combinedPosition = Vector3.zero;
  857. //if ((eyePose.leftStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.leftPosition;
  858. //if ((eyePose.rightStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.rightPosition;
  859. ////if ((eyePose.combinedStatus & (int)GSXRPlugin.EyePoseStatus.kGazePointValid) != 0) combinedPosition += eyePose.combinedPosition;
  860. gaze.localPosition = monoCamera.transform.localPosition;
  861. gaze.localRotation = Quaternion.LookRotation(eyeDirection, Vector3.up);
  862. float denominator = Vector3.Dot(eyeDirection, Vector3.forward);
  863. if (denominator > float.Epsilon)
  864. {
  865. // eye direction intersection with frustum near plane (left)
  866. var eyePoint = eyeDirection * plugin.deviceInfo.targetFrustumLeft.near / denominator;
  867. // size of the frustum near plane (left)
  868. var nearSize = new Vector2(0.5f*(plugin.deviceInfo.targetFrustumLeft.right - plugin.deviceInfo.targetFrustumLeft.left),
  869. 0.5f*(plugin.deviceInfo.targetFrustumLeft.top - plugin.deviceInfo.targetFrustumLeft.bottom));
  870. eyeFocus.Set(eyePoint.x / nearSize.x, eyePoint.y / nearSize.y); // Normalized [-1,1]
  871. //Debug.LogFormat("Eye Focus: {0}", eyeFocus.ToString());
  872. FocalPoint = eyeFocus; // Cache for foveated rendering
  873. }
  874. }
  875. }
  876. }
  877. //var isValid = true;
  878. //if (settings.poseStatusFade)
  879. //{
  880. // isValid = !settings.trackPosition
  881. // || ((trackingMode & (int)GSXRPlugin.TrackingMode.kTrackingPosition) == 0)
  882. // || (status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0;
  883. //}
  884. //var targetAlpha = fadeState == eFadeState.FadeOut || !isValid ? 1f : 0f;
  885. var targetAlpha = fadeState == eFadeState.FadeOut ? 1f : 0f;
  886. UpdateOverlayFade(targetAlpha, Time.deltaTime / fadeDuration);
  887. if (plugin.GSXR_Is_SupportOpticsCalibration()) {
  888. if (leftcalib == Matrix4x4.identity || rightCalib == Matrix4x4.identity) {
  889. plugin.GSXR_Get_TransformMatrix(ref outBload, outTransformArray);
  890. if (outBload) {
  891. leftcalib.SetColumn(0, new Vector4(outTransformArray[0], outTransformArray[1], outTransformArray[2], outTransformArray[3]));
  892. leftcalib.SetColumn(1, new Vector4(outTransformArray[4], outTransformArray[5], outTransformArray[6], outTransformArray[7]));
  893. leftcalib.SetColumn(2, new Vector4(outTransformArray[8], outTransformArray[9], outTransformArray[10], outTransformArray[11]));
  894. leftcalib.SetColumn(3, new Vector4(outTransformArray[12], outTransformArray[13], outTransformArray[14], outTransformArray[15]));
  895. rightCalib.SetColumn(0, new Vector4(outTransformArray[16], outTransformArray[17], outTransformArray[18], outTransformArray[19]));
  896. rightCalib.SetColumn(1, new Vector4(outTransformArray[20], outTransformArray[21], outTransformArray[22], outTransformArray[23]));
  897. rightCalib.SetColumn(2, new Vector4(outTransformArray[24], outTransformArray[25], outTransformArray[26], outTransformArray[27]));
  898. rightCalib.SetColumn(3, new Vector4(outTransformArray[28], outTransformArray[29], outTransformArray[30], outTransformArray[31]));
  899. Debug.Log($"leftcalib [{leftcalib.m00}, {leftcalib.m01}, {leftcalib.m02}, {leftcalib.m03};" +
  900. $"{leftcalib.m10}, {leftcalib.m11}, {leftcalib.m12}, {leftcalib.m13};" +
  901. $"{leftcalib.m20}, {leftcalib.m21}, {leftcalib.m22}, {leftcalib.m23};" +
  902. $"{leftcalib.m30}, {leftcalib.m31}, {leftcalib.m32}, {leftcalib.m33}]");
  903. Debug.Log($"rightCalib [{rightCalib.m00}, {rightCalib.m01}, {rightCalib.m02}, {rightCalib.m03};" +
  904. $"{rightCalib.m10}, {rightCalib.m11}, {rightCalib.m12}, {rightCalib.m13};" +
  905. $"{rightCalib.m20}, {rightCalib.m21}, {rightCalib.m22}, {rightCalib.m23};" +
  906. $"{rightCalib.m30}, {rightCalib.m31}, {rightCalib.m32}, {rightCalib.m33}]");
  907. }
  908. }
  909. //if (outBload) {
  910. // leftCamera.worldToCameraMatrix = leftcalib * invertZ * Matrix4x4.Inverse(Matrix4x4.TRS(leftCamera.transform.position, leftCamera.transform.rotation, leftCamera.transform.lossyScale));
  911. // rightCamera.worldToCameraMatrix = rightCalib * invertZ * Matrix4x4.Inverse(Matrix4x4.TRS(rightCamera.transform.position, rightCamera.transform.rotation, rightCamera.transform.lossyScale));
  912. //}
  913. if (Application.platform == RuntimePlatform.Android) {
  914. if (head.position != head.localPosition || head.rotation != head.localRotation) {
  915. ///for If Head parent not at origin,must mul the delta Matirx for Camera ViewMatrix
  916. //MD*Ml*P = Mw*P ==> MD == Mw * (M1 -1 ni)
  917. headDeltaInitLocalToWorld = Matrix4x4.TRS(head.position, head.rotation, Vector3.one) * Matrix4x4.Inverse(Matrix4x4.TRS(head.localPosition, head.localRotation, Vector3.one));
  918. }
  919. leftCamera.worldToCameraMatrix = plugin.leftViewMatrix * invertZ;
  920. leftCamera.worldToCameraMatrix = Matrix4x4.Inverse(headDeltaInitLocalToWorld * leftCamera.cameraToWorldMatrix/* not worldToCameraMatrix*/);
  921. rightCamera.worldToCameraMatrix = plugin.rightViewMatrix * invertZ;
  922. rightCamera.worldToCameraMatrix = Matrix4x4.Inverse(headDeltaInitLocalToWorld * rightCamera.cameraToWorldMatrix/* not worldToCameraMatrix*/);
  923. }
  924. }
  925. }
  926. public void Shutdown()
  927. {
  928. Debug.Log("GSXRManager.Shutdown()");
  929. status.running = false;
  930. if (submitFrame != null) { StopCoroutine(submitFrame); submitFrame = null; }
  931. if (plugin.IsRunning()) plugin.EndVr();
  932. if (plugin.IsInitialized()) plugin.Shutdown();
  933. status.initialized = false;
  934. }
  935. void OnDestroy() {
  936. if (Instance != this)
  937. return;
  938. UnregisterListeners();
  939. Shutdown();
  940. }
  941. public delegate void OnApplicationPauseDele(bool pause);
  942. public static event OnApplicationPauseDele onApplicationPauseDele;
  943. void OnApplicationPause(bool pause)
  944. {
  945. Debug.LogFormat("GSXRManager.OnApplicationPause({0})",pause);
  946. onApplicationPauseDele?.Invoke(pause);
  947. SetPause(pause);
  948. }
  949. void OnApplicationQuit()
  950. {
  951. //Debug.Log("GSXRManager.OnApplicationQuit()");
  952. //Shutdown();
  953. }
  954. static public Matrix4x4 Perspective(float left, float right, float bottom, float top, float near, float far)
  955. {
  956. float x = 2.0F * near / (right - left);
  957. float y = 2.0F * near / (top - bottom);
  958. float a = (right + left) / (right - left);
  959. float b = (top + bottom) / (top - bottom);
  960. float c = -(far + near) / (far - near);
  961. float d = -(2.0F * far * near) / (far - near);
  962. float e = -1.0F;
  963. Matrix4x4 m = new Matrix4x4();
  964. m[0, 0] = x;
  965. m[0, 1] = 0;
  966. m[0, 2] = a;
  967. m[0, 3] = 0;
  968. m[1, 0] = 0;
  969. m[1, 1] = y;
  970. m[1, 2] = b;
  971. m[1, 3] = 0;
  972. m[2, 0] = 0;
  973. m[2, 1] = 0;
  974. m[2, 2] = c;
  975. m[2, 3] = d;
  976. m[3, 0] = 0;
  977. m[3, 1] = 0;
  978. m[3, 2] = e;
  979. m[3, 3] = 0;
  980. return m;
  981. }
  982. void RegisterListeners()
  983. {
  984. GSXROverrideSettings.OnEyeAntiAliasingChangedEvent += OnEyeAntiAliasingChanged;
  985. GSXROverrideSettings.OnEyeDepthChangedEvent += OnEyeDepthChanged;
  986. GSXROverrideSettings.OnEyeResolutionScaleFactorChangedEvent += OnEyeResolutionScaleFactorChanged;
  987. GSXROverrideSettings.OnOverlayAntiAliasingChangedEvent += OnOverlayAntiAliasingChanged;
  988. GSXROverrideSettings.OnOverlayDepthChangedEvent += OnOverlayDepthChanged;
  989. GSXROverrideSettings.OnOverlayResolutionScaleFactorChangedEvent += OnOverlayResolutionScaleFactorChanged;
  990. GSXROverrideSettings.OnChromaticAberrationCorrectionChangedEvent += OnChromaticAberrationCorrectionChanged;
  991. GSXROverrideSettings.OnVSyncCountChangedEvent += OnVSyncCountChanged;
  992. GSXROverrideSettings.OnMasterTextureLimitChangedEvent += OnMasterTextureLimitChanged;
  993. GSXROverrideSettings.OnPerfLevelChangedEvent += OnPerfLevelChanged;
  994. GSXROverrideSettings.OnFoveateChangedEvent += OnFoveateChanged;
  995. }
  996. void UnregisterListeners()
  997. {
  998. GSXROverrideSettings.OnEyeAntiAliasingChangedEvent -= OnEyeAntiAliasingChanged;
  999. GSXROverrideSettings.OnEyeDepthChangedEvent -= OnEyeDepthChanged;
  1000. GSXROverrideSettings.OnEyeResolutionScaleFactorChangedEvent -= OnEyeResolutionScaleFactorChanged;
  1001. GSXROverrideSettings.OnOverlayAntiAliasingChangedEvent -= OnOverlayAntiAliasingChanged;
  1002. GSXROverrideSettings.OnOverlayDepthChangedEvent -= OnOverlayDepthChanged;
  1003. GSXROverrideSettings.OnOverlayResolutionScaleFactorChangedEvent -= OnOverlayResolutionScaleFactorChanged;
  1004. GSXROverrideSettings.OnChromaticAberrationCorrectionChangedEvent -= OnChromaticAberrationCorrectionChanged;
  1005. GSXROverrideSettings.OnVSyncCountChangedEvent -= OnVSyncCountChanged;
  1006. GSXROverrideSettings.OnMasterTextureLimitChangedEvent -= OnMasterTextureLimitChanged;
  1007. GSXROverrideSettings.OnPerfLevelChangedEvent -= OnPerfLevelChanged;
  1008. GSXROverrideSettings.OnFoveateChangedEvent -= OnFoveateChanged;
  1009. }
  1010. void OnEyeAntiAliasingChanged(GSXROverrideSettings.eAntiAliasing antiAliasing)
  1011. {
  1012. foreach (GSXREye eye in eyes)
  1013. {
  1014. eye.AntiAliasing = antiAliasing == GSXROverrideSettings.eAntiAliasing.NoOverride ?
  1015. (int)settings.eyeAntiAliasing : (int)antiAliasing;
  1016. }
  1017. }
  1018. void OnEyeDepthChanged(GSXROverrideSettings.eDepth depth)
  1019. {
  1020. foreach (GSXREye eye in eyes)
  1021. {
  1022. eye.Depth = depth == GSXROverrideSettings.eDepth.NoOverride ?
  1023. (int)settings.eyeDepth : (int)depth;
  1024. }
  1025. }
  1026. void OnEyeResolutionScaleFactorChanged(float scaleFactor)
  1027. {
  1028. foreach (GSXREye eye in eyes)
  1029. {
  1030. eye.ResolutionScaleFactor = scaleFactor <= 0 ? settings.eyeResolutionScaleFactor : scaleFactor;
  1031. }
  1032. }
  1033. void OnOverlayAntiAliasingChanged(GSXROverrideSettings.eAntiAliasing antiAliasing)
  1034. {
  1035. foreach (GSXROverlay overlay in overlays)
  1036. {
  1037. overlay.AntiAliasing = antiAliasing == GSXROverrideSettings.eAntiAliasing.NoOverride ?
  1038. (int)settings.overlayAntiAliasing : (int)antiAliasing;
  1039. }
  1040. }
  1041. void OnOverlayDepthChanged(GSXROverrideSettings.eDepth depth)
  1042. {
  1043. foreach (GSXROverlay overlay in overlays)
  1044. {
  1045. overlay.Depth = depth == GSXROverrideSettings.eDepth.NoOverride ?
  1046. (int)settings.overlayDepth : (int)depth;
  1047. }
  1048. }
  1049. void OnOverlayResolutionScaleFactorChanged(float scaleFactor)
  1050. {
  1051. foreach (GSXROverlay overlay in overlays)
  1052. {
  1053. overlay.ResolutionScaleFactor = scaleFactor <= 0 ? settings.overlayResolutionScaleFactor : scaleFactor;
  1054. }
  1055. }
  1056. void OnChromaticAberrationCorrectionChanged(GSXROverrideSettings.eChromaticAberrationCorrection aberrationCorrection)
  1057. {
  1058. if(aberrationCorrection == GSXROverrideSettings.eChromaticAberrationCorrection.kDisable)
  1059. {
  1060. plugin.SetFrameOption(GSXRPlugin.FrameOption.kDisableChromaticCorrection);
  1061. }
  1062. else
  1063. {
  1064. plugin.UnsetFrameOption(GSXRPlugin.FrameOption.kDisableChromaticCorrection);
  1065. }
  1066. }
  1067. void OnVSyncCountChanged(GSXROverrideSettings.eVSyncCount vSyncCount)
  1068. {
  1069. if (vSyncCount == GSXROverrideSettings.eVSyncCount.NoOverride)
  1070. {
  1071. plugin.SetVSyncCount((int)settings.vSyncCount);
  1072. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  1073. }
  1074. else
  1075. {
  1076. plugin.SetVSyncCount((int)vSyncCount);
  1077. QualitySettings.vSyncCount = (int)settings.vSyncCount;
  1078. }
  1079. }
  1080. void OnMasterTextureLimitChanged(GSXROverrideSettings.eMasterTextureLimit masterTextureLimit)
  1081. {
  1082. QualitySettings.masterTextureLimit = masterTextureLimit == GSXROverrideSettings.eMasterTextureLimit.NoOverride ?
  1083. (int)settings.masterTextureLimit : (int)masterTextureLimit;
  1084. }
  1085. void OnPerfLevelChanged(GSXROverrideSettings.ePerfLevel cpuPerfLevel, GSXROverrideSettings.ePerfLevel gpuPerfLevel)
  1086. {
  1087. int currentCpuPerfLevel = cpuPerfLevel == GSXROverrideSettings.ePerfLevel.NoOverride ?
  1088. (int)settings.cpuPerfLevel : (int)GSXROverrideSettings.CpuPerfLevel;
  1089. int currentGpuPerfLevel = gpuPerfLevel == GSXROverrideSettings.ePerfLevel.NoOverride ?
  1090. (int)settings.gpuPerfLevel : (int)GSXROverrideSettings.GpuPerfLevel;
  1091. plugin.SetPerformanceLevels(currentCpuPerfLevel, currentGpuPerfLevel);
  1092. }
  1093. void OnFoveateChanged(Vector2 gain, float area, float minPixelDensity)
  1094. {
  1095. var point = Vector2.zero;
  1096. var currentGain = gain == Vector2.zero ?
  1097. settings.foveationGain : GSXROverrideSettings.FoveateGain;
  1098. var currentArea = area == 0f ?
  1099. settings.foveationArea : GSXROverrideSettings.FoveateArea;
  1100. var currentMinimum = minPixelDensity == 0f ?
  1101. settings.foveationMinimum : GSXROverrideSettings.FoveateMinimum;
  1102. plugin.SetFoveationParameters(0, 0, point.x, point.y, currentGain.x, currentGain.y, currentArea, currentMinimum);
  1103. }
  1104. /// <summary>
  1105. /// Update this instance.
  1106. /// </summary>
  1107. //---------------------------------------------------------------------------------------------
  1108. void Update()
  1109. {
  1110. SvrEvent frameEvent = new SvrEvent ();
  1111. while (plugin.PollEvent(ref frameEvent))
  1112. {
  1113. //Debug.LogFormat("SvrEvent: {0}", frameEvent.eventType.ToString());
  1114. for (int i = 0; i < eventListeners.Count; i++) {
  1115. eventListeners [i].OnSvrEvent (frameEvent);
  1116. }
  1117. }
  1118. }
  1119. /// <summary>
  1120. /// Adds the event listener.
  1121. /// </summary>
  1122. /// <param name="listener">Listener.</param>
  1123. //---------------------------------------------------------------------------------------------
  1124. public void AddEventListener(SvrEventListener listener)
  1125. {
  1126. if (listener != null) {
  1127. eventListeners.Insert (0, listener);
  1128. }
  1129. }
  1130. /// <summary>
  1131. /// Start Tracking
  1132. /// </summary>
  1133. /// <returns>Handle to the controller</returns>
  1134. /// <param name="desc">Desc.</param>
  1135. //---------------------------------------------------------------------------------------------
  1136. public int ControllerStartTracking(string desc)
  1137. {
  1138. return plugin.ControllerStartTracking(desc);
  1139. }
  1140. /// <summary>
  1141. /// Stop Tracking
  1142. /// </summary>
  1143. /// <param name="handle">Handle.</param>
  1144. //---------------------------------------------------------------------------------------------
  1145. public void ControllerStopTracking(int handle)
  1146. {
  1147. plugin.ControllerStopTracking(handle);
  1148. }
  1149. /// <summary>
  1150. /// Get current state
  1151. /// </summary>
  1152. /// <returns>Controller State.</returns>
  1153. /// <param name="handle">Handle.</param>
  1154. //---------------------------------------------------------------------------------------------
  1155. public GSXRControllerState ControllerGetState(int handle, int space = 0)
  1156. {
  1157. return plugin.ControllerGetState(handle, space);
  1158. }
  1159. /// <summary>
  1160. /// Send an event to the controller
  1161. /// </summary>
  1162. /// <param name="handle">Handle.</param>
  1163. /// <param name="what">What.</param>
  1164. /// <param name="arg1">Arg1.</param>
  1165. /// <param name="arg2">Arg2.</param>
  1166. //---------------------------------------------------------------------------------------------
  1167. public void ControllerSendMessage(int handle, GSXRController.svrControllerMessageType what, int arg1, int arg2)
  1168. {
  1169. plugin.ControllerSendMessage (handle, what, arg1, arg2);
  1170. }
  1171. /// <summary>
  1172. /// Controllers the query.
  1173. /// </summary>
  1174. /// <returns>The query.</returns>
  1175. /// <param name="handle">Handle.</param>
  1176. /// <param name="what">What.</param>
  1177. /// <param name="mem">Mem.</param>
  1178. /// <param name="size">Size.</param>
  1179. //---------------------------------------------------------------------------------------------
  1180. public object ControllerQuery(int handle, GSXRController.svrControllerQueryType what)
  1181. {
  1182. return plugin.ControllerQuery (handle, what);
  1183. }
  1184. Coroutine startSlameCor;
  1185. public void StartSlam() {
  1186. if (startSlameCor == null) {
  1187. Debug.Log("StartSlam Coroutine Start");
  1188. startSlameCor = StartCoroutine(startSlam());
  1189. } else {
  1190. Debug.Log("startSlame Coroutine Not Null");
  1191. }
  1192. }
  1193. IEnumerator startSlam() {
  1194. yield return new WaitUntil(() => GSXRManager.Instance != null);
  1195. yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
  1196. GSXRManager.Instance.SetPause(false);
  1197. yield return new WaitUntil(() => IsTrackingValid);
  1198. startSlameCor = null;
  1199. }
  1200. Coroutine stopSlameCor;
  1201. //when stopSlam you must be sure no StartCorutine run before!
  1202. public void StopSlam() {
  1203. if (stopSlameCor == null && startSlameCor == null) {
  1204. Debug.Log("stopSlame Coroutine Start");
  1205. stopSlameCor = StartCoroutine(stopSlam());
  1206. } else if (stopSlameCor != null) {
  1207. Debug.Log("stopSlame Coroutine Not Null");
  1208. } else if (startSlameCor != null) {
  1209. Debug.Log("stopSlame: startSlame Coroutine Not Null");
  1210. }
  1211. }
  1212. IEnumerator stopSlam() {
  1213. yield return new WaitUntil(() => GSXRManager.Instance != null);
  1214. yield return new WaitUntil(() => GSXRManager.Instance.IsRunning);
  1215. GSXRManager.Instance.SetPause(true);
  1216. yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
  1217. stopSlameCor = null;
  1218. }
  1219. Coroutine resetSlameCor;
  1220. public void ResetSlam() {
  1221. if (resetSlameCor == null) {
  1222. Debug.Log("resetSlameCor Coroutine Start");
  1223. resetSlameCor = StartCoroutine(resetSlam());
  1224. } else {
  1225. Debug.Log("resetSlameCor Coroutine Not Null");
  1226. }
  1227. }
  1228. IEnumerator resetSlam() {
  1229. Debug.Log("ResetSlam Coroutine Step 0: CheckFlag...");
  1230. yield return new WaitUntil(() => GSXRManager.Instance != null);
  1231. yield return new WaitUntil(() => GSXRManager.Instance.IsRunning);
  1232. Debug.Log("ResetSlam Coroutine Step 1: StopSlam");
  1233. GSXRManager.Instance.SetPause(true);
  1234. yield return new WaitUntil(() => !GSXRManager.Instance.IsRunning);
  1235. Debug.Log("ResetSlam Coroutine Step 2: StartSlam");
  1236. GSXRManager.Instance.SetPause(false);
  1237. yield return new WaitUntil(() => IsTrackingValid);
  1238. Debug.Log("ResetSlam Coroutine Finish");
  1239. resetSlameCor = null;
  1240. }
  1241. public bool IsTrackingValid {
  1242. get {
  1243. if (GSXRManager.Instance && GSXRManager.Instance.IsRunning) {
  1244. return !GSXRManager.Instance.settings.trackPosition
  1245. || ((GSXRManager.Instance.plugin.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingPosition) == 0)
  1246. || (GSXRManager.Instance.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0;
  1247. }
  1248. return false;
  1249. }
  1250. }
  1251. public Shoulder shoulder;
  1252. public class Shoulder {
  1253. public Transform Left { get; private set; }
  1254. public Transform Right { get; private set; }
  1255. private Transform head;
  1256. private Vector3 leftOffset;
  1257. public Shoulder(Transform head,Vector3 leftOffset) {
  1258. this.head = head;
  1259. this.leftOffset = leftOffset;
  1260. if (head) {
  1261. Left = new GameObject("LeftShoulder").transform;
  1262. Left.SetParent(head,false);
  1263. Left.localPosition = leftOffset;
  1264. Right = new GameObject("RightShoulder").transform;
  1265. Right.SetParent(head, false);
  1266. Right.localPosition = new Vector3(-leftOffset.x, leftOffset.y, leftOffset.z) ;
  1267. }
  1268. }
  1269. }
  1270. }