BaseARDevice.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. // Copyright 2016 Nibiru. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if !UNITY_EDITOR
  15. #if UNITY_ANDROID
  16. #define ANDROID_DEVICE
  17. #endif
  18. #endif
  19. using UnityEngine;
  20. using System.Collections.Generic;
  21. using System;
  22. /// @cond
  23. namespace Nxr.Internal
  24. {
  25. // Represents a ar device that this plugin interacts with.
  26. public abstract class BaseARDevice
  27. {
  28. private static BaseARDevice device = null;
  29. protected BaseARDevice()
  30. {
  31. Profile = NxrProfile.Default.Clone();
  32. }
  33. public NxrProfile Profile { get; protected set; }
  34. public abstract void Init();
  35. public abstract void SetSplitScreenModeEnabled(bool enabled);
  36. public virtual void AndroidLog(string msg) { Debug.Log(msg); }
  37. public virtual void SetSystemParameters(string key, string value) { }
  38. public virtual void SetIsKeepScreenOn(bool keep) { }
  39. public virtual bool SupportsNativeDistortionCorrection(List<string> diagnostics)
  40. {
  41. return true;
  42. }
  43. public virtual void SetTextureSizeNative(int w, int h) { }
  44. public virtual void SetCpuLevel(NxrOverrideSettings.PerfLevel level) { }
  45. public virtual void SetGpuLevel(NxrOverrideSettings.PerfLevel level) { }
  46. public long nibiruVRServiceId;
  47. public virtual RenderTexture CreateStereoScreen(int w, int h)
  48. {
  49. int width = w > 0 ? w : (int)recommendedTextureSize[0];
  50. int height = h > 0 ? h : (int)recommendedTextureSize[1];
  51. width = width == 0 ? Screen.width : width;
  52. height = height == 0 ? Screen.height : height;
  53. bool useDFT = NxrViewer.USE_DTR && !NxrGlobal.supportDtr;
  54. float DFT_TextureScale = 0.8f;
  55. if (useDFT)
  56. {
  57. TextureQuality textureQuality = NxrViewer.Instance.TextureQuality;
  58. if (NxrGlobal.offaxisDistortionEnabled)
  59. {
  60. // textureQuality = TextureQuality.Best;
  61. }
  62. if (textureQuality == TextureQuality.Best)
  63. {
  64. DFT_TextureScale = 1f;
  65. }
  66. else if (textureQuality == TextureQuality.Good)
  67. {
  68. DFT_TextureScale = 0.75f;
  69. }
  70. else if (textureQuality == TextureQuality.Simple)
  71. {
  72. DFT_TextureScale = 0.6666666666666666f;
  73. }
  74. else if (textureQuality == TextureQuality.Better)
  75. {
  76. DFT_TextureScale = 0.8f;
  77. }
  78. width = (int)(width * DFT_TextureScale);
  79. height = (int)(height * DFT_TextureScale);
  80. }
  81. Debug.Log("antiAliasing."+QualitySettings.antiAliasing + "," + (int)NxrViewer.Instance.TextureMSAA);
  82. var rt = new RenderTexture(width, height, 24, RenderTextureFormat.Default);
  83. rt.anisoLevel = 0;
  84. int antiAliasing = Mathf.Max(QualitySettings.antiAliasing, (int)NxrViewer.Instance.TextureMSAA);
  85. if (NxrGlobal.isVR9Platform)
  86. {
  87. antiAliasing = 1;
  88. }
  89. rt.antiAliasing = antiAliasing < 1 ? 1 : antiAliasing;
  90. rt.Create();
  91. NxrViewer.Instance.AndroidLog("Creating ss tex "
  92. + width + " x " + height + "." + "sInfo : [" + Screen.width + "," + Screen.height + "].DFT_TexScal=" + DFT_TextureScale
  93. + ",TexQuality=" + NxrViewer.Instance.TextureQuality.ToString() + ", Id=" + rt.GetNativeTexturePtr() + ", MSAA=" + rt.antiAliasing);
  94. return rt;
  95. }
  96. public virtual long CreateNibiruVRService()
  97. {
  98. return 0;
  99. }
  100. public virtual void SetCameraNearFar(float near, float far)
  101. {
  102. }
  103. public virtual void StartCapture(string filePath, int seconds)
  104. {
  105. Debug.Log("StartCapture_" + filePath + "_" + seconds);
  106. }
  107. public virtual void StopCapture()
  108. {
  109. }
  110. public virtual void OnDrawFrameCapture(int frameId)
  111. {
  112. }
  113. public virtual void SetDisplayQuality(int level)
  114. {
  115. }
  116. public virtual IntPtr NGetRenderEventFunc() { return IntPtr.Zero; }
  117. public virtual void NIssuePluginEvent(int eventID) { }
  118. public virtual int GetTimewarpViewNumber()
  119. {
  120. return 0;
  121. }
  122. public Pose3D GetHeadPose()
  123. {
  124. return this.headPose;
  125. }
  126. protected MutablePose3D headPose = new MutablePose3D();
  127. public Matrix4x4 GetProjection(NxrViewer.Eye eye,
  128. NxrViewer.Distortion distortion = NxrViewer.Distortion.Distorted)
  129. {
  130. switch (eye)
  131. {
  132. case NxrViewer.Eye.Left:
  133. return distortion == NxrViewer.Distortion.Distorted ?
  134. leftEyeDistortedProjection : leftEyeUndistortedProjection;
  135. case NxrViewer.Eye.Right:
  136. return distortion == NxrViewer.Distortion.Distorted ?
  137. rightEyeDistortedProjection : rightEyeUndistortedProjection;
  138. default:
  139. return Matrix4x4.identity;
  140. }
  141. }
  142. protected Matrix4x4 leftEyeDistortedProjection;
  143. protected Matrix4x4 rightEyeDistortedProjection;
  144. protected Matrix4x4 leftEyeUndistortedProjection;
  145. protected Matrix4x4 rightEyeUndistortedProjection;
  146. public Rect GetViewport(NxrViewer.Eye eye,
  147. NxrViewer.Distortion distortion = NxrViewer.Distortion.Distorted)
  148. {
  149. switch (eye)
  150. {
  151. case NxrViewer.Eye.Left:
  152. return distortion == NxrViewer.Distortion.Distorted ?
  153. leftEyeDistortedViewport : leftEyeUndistortedViewport;
  154. case NxrViewer.Eye.Right:
  155. return distortion == NxrViewer.Distortion.Distorted ?
  156. rightEyeDistortedViewport : rightEyeUndistortedViewport;
  157. default:
  158. return new Rect();
  159. }
  160. }
  161. protected Rect leftEyeDistortedViewport;
  162. protected Rect rightEyeDistortedViewport;
  163. protected Rect leftEyeUndistortedViewport;
  164. protected Rect rightEyeUndistortedViewport;
  165. protected Vector2 recommendedTextureSize;
  166. protected int leftEyeOrientation;
  167. protected int rightEyeOrientation;
  168. public bool profileChanged;
  169. public abstract void UpdateState();
  170. public abstract void UpdateScreenData();
  171. public abstract void Recenter();
  172. public abstract void PostRender(RenderTexture stereoScreen);
  173. public virtual void OnPause(bool pause)
  174. {
  175. if (!pause)
  176. {
  177. UpdateScreenData();
  178. }
  179. }
  180. public virtual void OnApplicationPause(bool pause)
  181. {
  182. if (!pause)
  183. {
  184. UpdateScreenData();
  185. }
  186. }
  187. public virtual void EnterARMode() { }
  188. public virtual void OnFocus(bool focus)
  189. {
  190. // Do nothing.
  191. }
  192. public virtual void OnApplicationQuit()
  193. {
  194. // Do nothing.
  195. }
  196. public virtual void AppQuit() { }
  197. public virtual NibiruService GetNibiruService()
  198. {
  199. return null;
  200. }
  201. public virtual string GetStoragePath() { return null; }
  202. public virtual void ShowVideoPlayer(string path, int type2D3D, int mode, int decode) { }
  203. public virtual void SetTimeWarpEnable(bool enabled) { }
  204. //public virtual void SetEnableSyncFrame(bool enabled) { }
  205. //public virtual string GetSyncFrameUrl() { return null; }
  206. //public virtual bool IsSyncFrameEnabled() { return false; }
  207. //public virtual bool IsSyncFrameSupported() { return false; }
  208. public virtual void SetIpd(float ipd) { }
  209. /// <summary>
  210. /// 1=System split screen,0=Application split screen
  211. /// </summary>
  212. /// <param name="flag"></param>
  213. public virtual void NSetSystemSplitMode(int flag) { }
  214. /// <summary>
  215. /// Lock current screen.
  216. /// </summary>
  217. public virtual void NLockTracker() { }
  218. /// <summary>
  219. /// Unlock
  220. /// </summary>
  221. public virtual void NUnLockTracker() { }
  222. /// <summary> DTR
  223. /// (0=Display point,1=Hide point,2=Set the distance of point,3=Set the size of point,4=Set the color of point)
  224. /// </summary>
  225. /// <param name="tag"></param>
  226. /// <param name="param"></param>
  227. public virtual bool GazeApi(GazeTag tag, String param) { return false; }
  228. public virtual void Destroy()
  229. {
  230. if (device == this)
  231. {
  232. device = null;
  233. }
  234. }
  235. protected void ComputeEyeFrustum(NxrViewer.Eye eyeType, float near, float far, float left, float right, float top, float bottom)
  236. {
  237. if (eyeType == NxrViewer.Eye.Left)
  238. {
  239. leftEyeDistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  240. leftEyeUndistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  241. }
  242. else
  243. {
  244. rightEyeDistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  245. rightEyeUndistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  246. }
  247. }
  248. public void ComputeEyesForWin(NxrViewer.Eye eyeType, float near, float far, float left, float top, float right, float bottom)
  249. {
  250. Debug.Log("ComputeEyesForWin:" + eyeType + " | near=" + near + ",far=" + far + ",left=" + left + ",right=" + right + ",bottom=" + bottom + ",top=" + top);
  251. if (eyeType == NxrViewer.Eye.Left)
  252. {
  253. leftEyeUndistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  254. leftEyeDistortedProjection = leftEyeUndistortedProjection;
  255. }
  256. else
  257. {
  258. rightEyeUndistortedProjection = MakeProjection(left, top, right, bottom, near, far);
  259. rightEyeDistortedProjection = rightEyeUndistortedProjection;
  260. }
  261. recommendedTextureSize = new Vector2(Screen.width, Screen.height);
  262. }
  263. // Helper functions. near=1,far=1000
  264. protected void ComputeEyesFromProfile(float near, float far)
  265. {
  266. // Compute left eye matrices from screen and device params
  267. Matrix4x4 leftEyeView = Matrix4x4.identity;
  268. leftEyeView[0, 3] = -Profile.viewer.lenses.separation / 2;
  269. float[] rect = new float[4];
  270. Profile.GetLeftEyeVisibleTanAngles(rect);
  271. leftEyeDistortedProjection = MakeProjection(rect[0], rect[1], rect[2], rect[3], near, far);
  272. Profile.GetLeftEyeNoLensTanAngles(rect);
  273. leftEyeUndistortedProjection = MakeProjection(rect[0], rect[1], rect[2], rect[3], near, far);
  274. Debug.Log("ComputeEyesFromProfile." + near + "->" + far + "," + rect[0] + "," + rect[1]
  275. + "," + rect[2] + "," + rect[3]);
  276. leftEyeUndistortedViewport = Profile.GetLeftEyeVisibleScreenRect(rect);
  277. leftEyeDistortedViewport = leftEyeUndistortedViewport;
  278. // Right eye matrices same as left ones but for some sign flippage.
  279. Matrix4x4 rightEyeView = leftEyeView;
  280. rightEyeView[0, 3] *= -1;
  281. rightEyeDistortedProjection = leftEyeDistortedProjection;
  282. rightEyeDistortedProjection[0, 2] *= -1;
  283. rightEyeUndistortedProjection = leftEyeUndistortedProjection;
  284. rightEyeUndistortedProjection[0, 2] *= -1;
  285. rightEyeUndistortedViewport = leftEyeUndistortedViewport;
  286. rightEyeUndistortedViewport.x = 1 - rightEyeUndistortedViewport.xMax;
  287. rightEyeDistortedViewport = rightEyeUndistortedViewport;
  288. if (NxrViewer.USE_DTR) return;
  289. float width = Screen.width * (leftEyeUndistortedViewport.width + rightEyeDistortedViewport.width);
  290. float height = Screen.height * Mathf.Max(leftEyeUndistortedViewport.height,
  291. rightEyeUndistortedViewport.height);
  292. recommendedTextureSize = new Vector2(width, height);
  293. Debug.Log("recommendedTextureSize: " + width + "," + height);
  294. }
  295. public static Matrix4x4 MakeProjection(float l, float t, float r, float b, float n, float f)
  296. {
  297. Matrix4x4 m = Matrix4x4.zero;
  298. m[0, 0] = 2 * n / (r - l);
  299. m[1, 1] = 2 * n / (t - b);
  300. m[0, 2] = (r + l) / (r - l);
  301. m[1, 2] = (t + b) / (t - b);
  302. m[2, 2] = (n + f) / (n - f);
  303. m[2, 3] = 2 * n * f / (n - f);
  304. m[3, 2] = -1;
  305. return m;
  306. }
  307. /// <summary>
  308. /// Shutdown
  309. /// </summary>
  310. public virtual void TurnOff() { }
  311. public virtual void Reboot() { }
  312. public static BaseARDevice GetDevice()
  313. {
  314. if (device == null)
  315. {
  316. #if UNITY_EDITOR || UNITY_STANDALONE_WIN
  317. device = new EditorDevice();
  318. #elif ANDROID_DEVICE
  319. device = new AndroidDevice();
  320. #else
  321. throw new InvalidOperationException("Unsupported device.");
  322. #endif
  323. }
  324. return device;
  325. }
  326. public virtual void SetColorspaceType(int colorSpace)
  327. {
  328. }
  329. public virtual void SetControllerSupportMode(ControllerSupportMode csm)
  330. {
  331. }
  332. public virtual NxrInstantNativeApi.NibiruDeviceType GetSixDofControllerPrimaryDeviceType()
  333. {
  334. return NxrInstantNativeApi.NibiruDeviceType.None;
  335. }
  336. public virtual void SetSixDofControllerPrimaryDeviceType(NxrInstantNativeApi.NibiruDeviceType deviceType)
  337. {
  338. }
  339. public virtual int GetControllerTipState()
  340. {
  341. return 0;
  342. }
  343. public virtual void SetControllerTipState(int state)
  344. {
  345. }
  346. public virtual int GetEnableSystemDialog()
  347. {
  348. return 0;
  349. }
  350. public virtual void SetEnableSystemDialog(int enableSystemDialog)
  351. {
  352. }
  353. public virtual void SetMultiThreadedRendering(bool isMultiThreadedRendering)
  354. {
  355. }
  356. public virtual bool IsHeadPoseUpdated()
  357. {
  358. return false;
  359. }
  360. public virtual bool IsSptEyeLocalRotPos()
  361. {
  362. return false;
  363. }
  364. public virtual Quaternion GetEyeLocalRotation(NxrViewer.Eye eye)
  365. {
  366. return Quaternion.identity;
  367. }
  368. public virtual Vector3 GetEyeLocalPosition(NxrViewer.Eye eye)
  369. {
  370. return Vector3.zero;
  371. }
  372. }
  373. }
  374. /// @endcond