StereoRenderer.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. using EZXR.Glass.SixDof;
  2. using AOT;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using EZXR.Glass.Device;
  10. using EZXR.Glass.Core;
  11. namespace EZXR.Glass.Rendering
  12. {
  13. public class StereoRenderer : MonoBehaviour
  14. {
  15. public enum RenderMode
  16. {
  17. Native,
  18. Unity,
  19. }
  20. public RenderMode renderMode;
  21. private static DisplaySize ds = new DisplaySize();
  22. private static DisplaySize targetRTSize = new DisplaySize();
  23. private static CamParams cp = new CamParams();
  24. private static StereoRenderer currentInstance = null;
  25. // 解决屏幕的问题
  26. // 备用,防止Native渲染链路无法建立。
  27. private static int mainDisplayIdx = -1;
  28. private static int targetDisplayIdx = -1;
  29. /// <summary> Renders the event delegate described by eventID. </summary>
  30. /// <param name="eventID"> Identifier for the event.</param>
  31. private delegate void RenderEventDelegate(int eventID);
  32. /// <summary> Handle of the render thread. </summary>
  33. private static RenderEventDelegate RenderThreadHandle = new RenderEventDelegate(RunOnRenderThread);
  34. /// <summary> The render thread handle pointer. </summary>
  35. private static IntPtr RenderThreadHandlePtr = Marshal.GetFunctionPointerForDelegate(RenderThreadHandle);
  36. private const int SETRENDERTEXTUREEVENT = 0x0001;
  37. private const int STARTNATIVERENDEREVENT = 0x0002;
  38. private const int RESUMENATIVERENDEREVENT = 0x0003;
  39. private const int PAUSENATIVERENDEREVENT = 0x0004;
  40. private const int STOPNATIVERENDEREVENT = 0x0005;
  41. private const int RECHANGENATIVERENDEREVENT = 0x0006;
  42. /// <summary> Gets or sets the native renderring. </summary>
  43. /// <value> The m native renderring. </value>
  44. private static EZXRNativeRenderring m_NativeRenderring;
  45. static EZXRNativeRenderring NativeRenderring
  46. {
  47. get
  48. {
  49. if (m_NativeRenderring == null)
  50. {
  51. m_NativeRenderring = new EZXRNativeRenderring();
  52. }
  53. return m_NativeRenderring;
  54. }
  55. set
  56. {
  57. m_NativeRenderring = value;
  58. }
  59. }
  60. /// <summary> Values that represent eyes. </summary>
  61. public enum Eyes
  62. {
  63. /// <summary> An enum constant representing the left option. </summary>
  64. Left = 0,
  65. /// <summary> An enum constant representing the right option. </summary>
  66. Right = 1,
  67. /// <summary> An enum constant representing the count option. </summary>
  68. Count = 2
  69. }
  70. private static int _TextureBufferSize = 4;
  71. /// <summary> Number of eye textures. </summary>
  72. private static int EyeTextureCount = _TextureBufferSize * (int)Eyes.Count;
  73. /// <summary> The eye textures. </summary>
  74. private RenderTexture[] eyeTextures;
  75. /// <summary> Dictionary of rights. </summary>
  76. private Dictionary<RenderTexture, IntPtr> m_RTDict = new Dictionary<RenderTexture, IntPtr>();
  77. /// <summary> Values that represent renderer states. </summary>
  78. public enum RendererState
  79. {
  80. UnInitialized = 0,
  81. InitializedFailed = 1,
  82. Initialized = 2,
  83. Running = 3,
  84. Paused = 4,
  85. Destroyed = 5,
  86. Unknown = 6
  87. }
  88. /// <summary> The current state. </summary>
  89. internal RendererState m_CurrentState = RendererState.UnInitialized;
  90. /// <summary> Gets the current state. </summary>
  91. /// <value> The current state. </value>
  92. public RendererState currentState
  93. {
  94. get
  95. {
  96. return m_CurrentState;
  97. }
  98. }
  99. private int currentEyeTextureIdx = 0;
  100. private void Awake()
  101. {
  102. currentInstance = this;
  103. Debug.Log("=============Unity Log=============== StereoRenderer:" + this.GetInstanceID() + " -- Awake display length " + Display.displays.Length);
  104. Screen.sleepTimeout = SleepTimeout.NeverSleep;
  105. for (int i = 0; i < Display.displays.Length; i++)
  106. {
  107. Display.displays[i].Activate();
  108. }
  109. if (!Application.isEditor)
  110. {
  111. if (renderMode == RenderMode.Unity)
  112. {
  113. ARConfig.DefaultConfig.unsetMTPMode_openMTP();
  114. //StartUp_Unity();
  115. StartCoroutine(StartUp_Unity());
  116. }
  117. }
  118. m_CurrentState = RendererState.UnInitialized;
  119. Camera.onPreRender += OnPreRenderCallback;
  120. }
  121. private void Start()
  122. {
  123. Debug.Log("=============Unity Log=============== StereoRenderer:" + this.GetInstanceID() + " -- Start");
  124. if (!Application.isEditor)
  125. {
  126. if (renderMode == RenderMode.Native)
  127. {
  128. StartCoroutine(StartUp_Native());
  129. }
  130. }
  131. }
  132. private void Update()
  133. {
  134. if (!Application.isEditor)
  135. {
  136. if (m_CurrentState == RendererState.Running && ARFrame.SessionStatus == EZVIOState.EZVIOCameraState_Tracking)
  137. {
  138. if (renderMode == RenderMode.Native)
  139. {
  140. HMDPoseTracker.Instance.leftCamera.targetTexture = eyeTextures[currentEyeTextureIdx];
  141. HMDPoseTracker.Instance.rightCamera.targetTexture = eyeTextures[currentEyeTextureIdx + 1];
  142. currentEyeTextureIdx = ((currentEyeTextureIdx + 2) % EyeTextureCount + EyeTextureCount) % EyeTextureCount;
  143. }
  144. Matrix4x4 leftProj = new Matrix4x4();
  145. Matrix4x4 rightProj = new Matrix4x4();
  146. for (int i = 0; i < 4; i++)
  147. for (int j = 0; j < 4; j++)
  148. {
  149. leftProj[i, j] = cp.leftProjection[i * 4 + j];
  150. rightProj[i, j] = cp.rightProjection[i * 4 + j];
  151. }
  152. //Debug.Log("=============Unity Log=============== StereoRenderer Update, left proj "
  153. // + leftProj[0, 0] + " " + leftProj[0, 1] + " " + leftProj[0, 2] + " " + leftProj[0, 3] + " "
  154. // + leftProj[1, 0] + " " + leftProj[1, 1] + " " + leftProj[1, 2] + " " + leftProj[1, 3] + " "
  155. // + leftProj[2, 0] + " " + leftProj[2, 1] + " " + leftProj[2, 2] + " " + leftProj[2, 3] + " "
  156. // + leftProj[3, 0] + " " + leftProj[3, 1] + " " + leftProj[3, 2] + " " + leftProj[3, 3]);
  157. //Debug.Log("=============Unity Log=============== StereoRenderer Update, right proj "
  158. // + rightProj[0, 0] + " " + rightProj[0, 1] + " " + rightProj[0, 2] + " " + rightProj[0, 3] + " "
  159. // + rightProj[1, 0] + " " + rightProj[1, 1] + " " + rightProj[1, 2] + " " + rightProj[1, 3] + " "
  160. // + rightProj[2, 0] + " " + rightProj[2, 1] + " " + rightProj[2, 2] + " " + rightProj[2, 3] + " "
  161. // + rightProj[3, 0] + " " + rightProj[3, 1] + " " + rightProj[3, 2] + " " + rightProj[3, 3]);
  162. HMDPoseTracker.Instance.leftCamera.projectionMatrix = leftProj;
  163. HMDPoseTracker.Instance.rightCamera.projectionMatrix = rightProj;
  164. //Debug.Log("=============Unity Log=============== StereoRenderer Update, HMDPoseTracker.Instance.leftCamera.projectionMatrix "
  165. // + HMDPoseTracker.Instance.leftCamera.projectionMatrix[0, 0] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[0, 1] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[0, 2] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[0, 3] + " "
  166. // + HMDPoseTracker.Instance.leftCamera.projectionMatrix[1, 0] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[1, 1] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[1, 2] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[1, 3] + " "
  167. // + HMDPoseTracker.Instance.leftCamera.projectionMatrix[2, 0] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[2, 1] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[2, 2] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[2, 3] + " "
  168. // + HMDPoseTracker.Instance.leftCamera.projectionMatrix[3, 0] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[3, 1] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[3, 2] + " " + HMDPoseTracker.Instance.leftCamera.projectionMatrix[3, 3]);
  169. //Debug.Log("=============Unity Log=============== StereoRenderer Update, right proj "
  170. // + HMDPoseTracker.Instance.rightCamera.projectionMatrix[0, 0] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[0, 1] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[0, 2] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[0, 3] + " "
  171. // + HMDPoseTracker.Instance.rightCamera.projectionMatrix[1, 0] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[1, 1] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[1, 2] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[1, 3] + " "
  172. // + HMDPoseTracker.Instance.rightCamera.projectionMatrix[2, 0] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[2, 1] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[2, 2] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[2, 3] + " "
  173. // + HMDPoseTracker.Instance.rightCamera.projectionMatrix[3, 0] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[3, 1] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[3, 2] + " " + HMDPoseTracker.Instance.rightCamera.projectionMatrix[3, 3]);
  174. //Debug.Log("=============Unity Log=============== StereoRenderer Update, leftCamera.fieldOfView "
  175. // + HMDPoseTracker.Instance.leftCamera.fieldOfView);
  176. //Debug.Log("=============Unity Log=============== StereoRenderer Update, rightCamera.fieldOfView "
  177. // + HMDPoseTracker.Instance.rightCamera.fieldOfView);
  178. }
  179. }
  180. }
  181. private double cachedPreRenderTime = 0.0f;
  182. void OnPreRenderCallback(Camera cam)
  183. {
  184. if (cam.name == "LeftCamera")
  185. {
  186. cachedPreRenderTime = NativeTracking.GetSystemTime() / 1e9;
  187. //Debug.Log("=============Unity Log=============== StereoRenderer: + OnPreRenderCallback() cursystime: " + NativeTracking.GetSystemTime() + " cachedPreRenderTime: " + cachedPreRenderTime);
  188. }
  189. }
  190. private void OnDestroy()
  191. {
  192. Debug.Log("=============Unity Log=============== StereoRenderer:" + this.GetInstanceID() + " -- OnDestroy begins");
  193. currentInstance = null;
  194. if (renderMode == RenderMode.Native)
  195. {
  196. if (m_CurrentState != RendererState.Destroyed)
  197. {
  198. //GL.IssuePluginEvent(RenderThreadHandlePtr, STOPNATIVERENDEREVENT);
  199. NativeRenderring?.Stop();
  200. NativeRenderring = null;
  201. m_CurrentState = RendererState.Destroyed;
  202. }
  203. }
  204. Camera.onPreRender -= OnPreRenderCallback;
  205. Debug.Log("=============Unity Log=============== StereoRenderer:" + this.GetInstanceID() + " -- OnDestroy ends");
  206. }
  207. //private void StartUp_Unity()
  208. private IEnumerator StartUp_Unity()
  209. {
  210. //HMDPoseTracker.Instance.leftCamera.rect = new Rect(0, 0, 0.5f, 1);
  211. //HMDPoseTracker.Instance.rightCamera.rect = new Rect(0.5f, 0, 0.5f, 1);
  212. //yield return new WaitForEndOfFrame();
  213. //yield return new WaitForEndOfFrame();
  214. //yield return new WaitForEndOfFrame();
  215. while (!NativeTracking.GetIsARSessionInited())
  216. {
  217. yield return new WaitForEndOfFrame();
  218. }
  219. Debug.Log("=============Unity Log=============== StereoRenderer -- StartUp_Unity get started");
  220. NativeTracking.GetCameraParams(ref cp);
  221. Debug.Log("=============Unity Log=============== StereoRenderer -- StartUp_Unity CameraParams: " + cp.width + ", " + cp.height + ", fov=(" + cp.fov[0] + ", " + cp.fov[1] + ")");
  222. m_CurrentState = RendererState.Running;
  223. }
  224. /// <summary> Prepares this object for use. </summary>
  225. /// <returns> An IEnumerator. </returns>
  226. private IEnumerator StartUp_Native()
  227. {
  228. yield return new WaitForEndOfFrame();
  229. yield return new WaitForEndOfFrame();
  230. yield return new WaitForEndOfFrame();
  231. while (!NativeTracking.GetIsARSessionInited())
  232. {
  233. yield return new WaitForEndOfFrame();
  234. }
  235. Debug.Log("=============Unity Log=============== StereoRenderer -- StartUp get started");
  236. NativeTracking.GetGlassDisplaySize(ref ds);
  237. NativeTracking.GetTargetRenderSize(ref targetRTSize);
  238. NativeTracking.GetCameraParams(ref cp);
  239. Debug.Log("=============Unity Log=============== StereoRenderer -- StartUp desired glass size: " + ds.width + " " + ds.height + " target RenderTexture Size:(" + targetRTSize.width + "," + targetRTSize.height + ")");
  240. Debug.Log("=============Unity Log=============== StereoRenderer -- StartUp desired CameraParams: " + cp.width + ", " + cp.height + ", fov=(" + cp.fov[0] + ", " + cp.fov[1] + ")");
  241. CreateRenderTextures();
  242. StartCoroutine(RenderCoroutine());
  243. GL.IssuePluginEvent(RenderThreadHandlePtr, STARTNATIVERENDEREVENT);
  244. }
  245. /// <summary> Creates render textures. </summary>
  246. private void CreateRenderTextures()
  247. {
  248. EyeTextureCount = _TextureBufferSize * (int)Eyes.Count;
  249. eyeTextures = new RenderTexture[EyeTextureCount];
  250. for (int i = 0; i < EyeTextureCount; i++)
  251. {
  252. eyeTextures[i] = EZGlassARRendererUtility.CreateRenderTexture(targetRTSize.width, targetRTSize.height, 24, RenderTextureFormat.DefaultHDR, false); // default may ARGB
  253. m_RTDict.Add(eyeTextures[i], eyeTextures[i].GetNativeTexturePtr());
  254. }
  255. HMDPoseTracker.Instance.leftCamera.targetTexture = eyeTextures[0];
  256. HMDPoseTracker.Instance.rightCamera.targetTexture = eyeTextures[1];
  257. }
  258. /// <summary> Renders the coroutine. </summary>
  259. /// <returns> An IEnumerator. </returns>
  260. private IEnumerator RenderCoroutine()
  261. {
  262. WaitForEndOfFrame delay = new WaitForEndOfFrame();
  263. WaitForSecondsRealtime delayMoment = new WaitForSecondsRealtime(0.2f);
  264. yield return delay;
  265. bool isWaitMoreTime = false;
  266. while (true)
  267. {
  268. if (isWaitMoreTime)
  269. {
  270. isWaitMoreTime = false;
  271. yield return delayMoment;
  272. }
  273. else
  274. {
  275. yield return delay;
  276. }
  277. //Debug.Log("=============Unity Log=============== StereoRenderer -- RenderCoroutine m_CurrentState " + m_CurrentState);
  278. if (m_CurrentState == RendererState.Initialized)
  279. {
  280. Debug.Log("=============Unity Log=============== StereoRenderer -- RenderCoroutine renderer state would come from Initialized into Running");
  281. m_CurrentState = RendererState.Running;
  282. }
  283. if (m_CurrentState != RendererState.Running)
  284. {
  285. Debug.Log("=============Unity Log=============== StereoRenderer -- RenderCoroutine m_CurrentState is not Running, passed");
  286. isWaitMoreTime = true;
  287. continue;
  288. }
  289. //Debug.Log("=============Unity Log=============== StereoRenderer -- RenderCoroutine SessionStatus " + ARFrame.SessionStatus);
  290. if (ARFrame.SessionStatus != EZVIOState.EZVIOCameraState_Tracking)
  291. {
  292. Debug.Log("=============Unity Log=============== StereoRenderer -- RenderCoroutine ARFrame.SessionStatus is not Tracking, passed");
  293. isWaitMoreTime = true;
  294. continue;
  295. }
  296. FrameInfo info = new FrameInfo(IntPtr.Zero, IntPtr.Zero, ARFrame.OriginVIOResult.Twc, ARFrame.OriginVIOResult.timestamp, cachedPreRenderTime, ARFrame.VIOResultFetchTimeNS / 1e9);
  297. IntPtr left_target, right_target;
  298. if (!m_RTDict.TryGetValue(HMDPoseTracker.Instance.leftCamera.targetTexture, out left_target)) continue;
  299. if (!m_RTDict.TryGetValue(HMDPoseTracker.Instance.rightCamera.targetTexture, out right_target)) continue;
  300. info.leftTex = left_target;
  301. info.rightTex = right_target;
  302. //Debug.Log("=============Unity Log=============== UICamController -- RenderCoroutine before SetRenderFrameInfo");
  303. SetRenderFrameInfo(info);
  304. }
  305. }
  306. /// <summary> Sets render frame information. </summary>
  307. /// <param name="frame"> The frame.</param>
  308. private static void SetRenderFrameInfo(FrameInfo frame)
  309. {
  310. //Debug.Log("=============Unity Log=============== UICamController -- SetRenderFrameInfo frameinfo " + frame.leftTex + " " + frame.rightTex + " " + frame.Twc + " " + frame.TwcTime);
  311. NativeRenderring.WriteFrameData(frame);
  312. GL.IssuePluginEvent(RenderThreadHandlePtr, SETRENDERTEXTUREEVENT);
  313. }
  314. public static void OnHMDPoseTrackerDofChanged(HMDPoseTracker.DegreeOfFreedom dof)
  315. {
  316. EZXRRendererHotConfig cfg = new EZXRRendererHotConfig(HMDPoseTracker.Instance.degreeOfFreedom != HMDPoseTracker.DegreeOfFreedom.ZeroDof);
  317. NativeRenderring.WriteRendererHotConfig(cfg);
  318. GL.IssuePluginEvent(RenderThreadHandlePtr, RECHANGENATIVERENDEREVENT);
  319. }
  320. /// <summary> Executes the 'on render thread' operation. </summary>
  321. /// <param name="eventID"> Identifier for the event.</param>
  322. [MonoPInvokeCallback(typeof(RenderEventDelegate))]
  323. private static void RunOnRenderThread(int eventID)
  324. {
  325. if (eventID == STARTNATIVERENDEREVENT)
  326. {
  327. int result = NativeRenderring.Start();
  328. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread STARTNATIVERENDEREVENT result: " + result);
  329. if (result != 0 && result != -3)
  330. {
  331. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread STARTNATIVERENDEREVENT start failed");
  332. if (StereoRenderer.currentInstance != null)
  333. StereoRenderer.currentInstance.m_CurrentState = RendererState.InitializedFailed;
  334. return;
  335. }
  336. if (StereoRenderer.currentInstance != null)
  337. StereoRenderer.currentInstance.m_CurrentState = RendererState.Initialized;
  338. // @xunighao: assumed openMTP is set on at now;
  339. if (SessionManager.Instance.ARInitConfig.getMTPMode_warping_1() ==
  340. (HMDPoseTracker.Instance.degreeOfFreedom == HMDPoseTracker.DegreeOfFreedom.SixDof ||
  341. HMDPoseTracker.Instance.degreeOfFreedom == HMDPoseTracker.DegreeOfFreedom.ThreeDof))
  342. {
  343. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread STARTNATIVERENDEREVENT, no need to do hotupdate to EZXRRenderer; " +
  344. " ARInitConfig‘s warp: " + SessionManager.Instance.ARInitConfig.getMTPMode_warping_1() +
  345. " camera instance dof: " + HMDPoseTracker.Instance.degreeOfFreedom);
  346. }
  347. else
  348. {
  349. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread STARTNATIVERENDEREVENT, to do hotupdate to EZXRRenderer; " +
  350. " ARInitConfig‘s warp: " + SessionManager.Instance.ARInitConfig.getMTPMode_warping_1() +
  351. " camera instance dof: " + HMDPoseTracker.Instance.degreeOfFreedom);
  352. EZXRRendererHotConfig cfg = new EZXRRendererHotConfig(HMDPoseTracker.Instance.degreeOfFreedom != HMDPoseTracker.DegreeOfFreedom.ZeroDof);
  353. NativeRenderring.WriteRendererHotConfig(cfg);
  354. NativeRenderring.HotUpdateRenderer();
  355. }
  356. HMDPoseTracker.Instance.PosetrackerChangedListener += OnHMDPoseTrackerDofChanged;
  357. }
  358. else if (eventID == RESUMENATIVERENDEREVENT)
  359. {
  360. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RESUMENATIVERENDEREVENT");
  361. #if EZXRCS
  362. if (StereoRenderer.currentInstance != null)
  363. {
  364. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RESUMENATIVERENDEREVENT current renderer state: " + StereoRenderer.currentInstance.m_CurrentState);
  365. if (StereoRenderer.currentInstance.m_CurrentState == RendererState.Paused)
  366. {
  367. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RESUMENATIVERENDEREVENT resumeMtp called");
  368. NativeTracking.resumeMtp();
  369. StereoRenderer.currentInstance.m_CurrentState = RendererState.Initialized;
  370. }
  371. else
  372. {
  373. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RESUMENATIVERENDEREVENT resumeMtp not called, reason is the renderer state");
  374. }
  375. }
  376. else
  377. {
  378. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RESUMENATIVERENDEREVENT unknown error");
  379. }
  380. #endif
  381. }
  382. else if (eventID == PAUSENATIVERENDEREVENT)
  383. {
  384. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread PAUSENATIVERENDEREVENT");
  385. #if EZXRCS
  386. if (StereoRenderer.currentInstance != null)
  387. {
  388. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread PAUSENATIVERENDEREVENT current renderer state: " + StereoRenderer.currentInstance.m_CurrentState);
  389. if (StereoRenderer.currentInstance.m_CurrentState == RendererState.Initialized || StereoRenderer.currentInstance.m_CurrentState == RendererState.Running)
  390. {
  391. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread PAUSENATIVERENDEREVENT pauseMTP called");
  392. NativeTracking.pauseMtp();
  393. StereoRenderer.currentInstance.m_CurrentState = RendererState.Paused;
  394. }
  395. else
  396. {
  397. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread PAUSENATIVERENDEREVENT pauseMTP not called, reason is the renderer state");
  398. }
  399. }
  400. else
  401. {
  402. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread PAUSENATIVERENDEREVENT unknown error");
  403. }
  404. #endif
  405. }
  406. else if (eventID == STOPNATIVERENDEREVENT)
  407. {
  408. //NativeRenderring?.Stop();
  409. //NativeRenderring = null;
  410. }
  411. else if (eventID == SETRENDERTEXTUREEVENT)
  412. {
  413. NativeRenderring?.DoExtendedRenderring();
  414. }
  415. else if (eventID == RECHANGENATIVERENDEREVENT)
  416. {
  417. Debug.Log("=============Unity Log=============== StereoRenderer -- RunOnRenderThread RECHANGENATIVERENDEREVENT HotUpdateRenderer ");
  418. NativeRenderring?.HotUpdateRenderer();
  419. }
  420. }
  421. private void OnApplicationPause(bool pause)
  422. {
  423. if (!Application.isEditor)
  424. {
  425. if (pause) // pause
  426. {
  427. Pause();
  428. }
  429. else // resume
  430. {
  431. // delay resume
  432. //Invoke("Resume", 0.3f);
  433. // resume immediately
  434. Resume();
  435. }
  436. }
  437. }
  438. /// <summary> Pause render. </summary>
  439. public void Pause()
  440. {
  441. if (renderMode == RenderMode.Native)
  442. {
  443. GL.IssuePluginEvent(RenderThreadHandlePtr, PAUSENATIVERENDEREVENT);
  444. }
  445. }
  446. /// <summary> Resume render. </summary>
  447. public void Resume()
  448. {
  449. if (renderMode == RenderMode.Native)
  450. {
  451. GL.IssuePluginEvent(RenderThreadHandlePtr, RESUMENATIVERENDEREVENT);
  452. }
  453. }
  454. public void OnClickReCenterButton()
  455. {
  456. Debug.Log("=============Unity Log=============== StereoRenderer -- OnClickReCenterButton");
  457. if (ARFrame.SessionStatus != EZVIOState.EZVIOCameraState_Tracking)
  458. return;
  459. Debug.Log("=============Unity Log=============== StereoRenderer -- Start ReCenter");
  460. ARFrame.ReCenter();
  461. }
  462. }
  463. }