SvrPluginAndroid.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using System;
  4. using System.Collections;
  5. using System.Runtime.InteropServices;
  6. class SvrPluginAndroid : SvrPlugin
  7. {
  8. public enum RenderEvent
  9. {
  10. Initialize,
  11. BeginVr,
  12. EndVr,
  13. BeginEye,
  14. EndEye,
  15. SubmitFrame,
  16. Foveation,
  17. Shutdown,
  18. RecenterTracking,
  19. SetTrackingMode,
  20. SetPerformanceLevels,
  21. OccludeEye,
  22. OcclusionMesh,
  23. AnchorData,
  24. CloudData,
  25. PauseXr,
  26. ResumeXr,
  27. };
  28. public static SvrPluginAndroid Create()
  29. {
  30. Debug.Log("SvrPluginAndroid Create");
  31. if (Application.isEditor)
  32. {
  33. Debug.LogError("SvrPlugin not supported in unity editor!");
  34. throw new InvalidOperationException();
  35. }
  36. return new SvrPluginAndroid ();
  37. }
  38. private SvrPluginAndroid()
  39. {
  40. beginEyeCommandBuffer = new CommandBuffer();
  41. //beginEyeCommandBuffer.ClearRenderTarget(true, true, Color.red);
  42. beginEyeCommandBuffer.IssuePluginEvent(GetRenderEventFunc(), (int)RenderEvent.OccludeEye);
  43. }
  44. internal void IssueEvent(RenderEvent e)
  45. {
  46. // Queue a specific callback to be called on the render thread
  47. GL.IssuePluginEvent(GetRenderEventFunc(), (int)e);
  48. }
  49. public override bool IsInitialized() { return SvrIsInitialized(); }
  50. public override bool IsRunning() { return SvrIsRunning(); }
  51. public override IEnumerator Initialize()
  52. {
  53. //yield return new WaitUntil(() => SvrIsInitialized() == false); // Wait for shutdown
  54. yield return base.Initialize();
  55. #if UNITY_ANDROID && !UNITY_EDITOR
  56. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  57. AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  58. SvrInitializeEventData(activity.GetRawObject());
  59. #endif
  60. SXR.ParamLoader.ParamLoaderInit((int)deviceModel);
  61. IssueEvent(RenderEvent.Initialize);
  62. yield return new WaitUntil (() => SvrIsInitialized () == true);
  63. yield return null; // delay one frame - fix for re-init w multi-threaded rendering
  64. deviceInfo = GetDeviceInfo();
  65. }
  66. public override void BeginVr(int cpuPerfLevel, int gpuPerfLevel, int optionFlags)
  67. {
  68. Debug.Log("=>>BeginVr");
  69. //yield return new WaitUntil(() => SvrIsRunning() == false); // Wait for EndVr
  70. //yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel, optionFlags);
  71. // float[6]: x, y, z, w, u, v
  72. //float[] lowerLeft = { -1f, -1f, 0f, 1f, 0f, 0f };
  73. //float[] upperLeft = { -1f, 1f, 0f, 1f, 0f, 1f };
  74. //float[] upperRight = { 1f, 1f, 0f, 1f, 1f, 1f };
  75. //float[] lowerRight = { 1f, -1f, 0f, 1f, 1f, 0f };
  76. //SvrSetupLayerCoords(-1, lowerLeft, lowerRight, upperLeft, upperRight); // Layers/All
  77. SvrSetPerformanceLevelsEventData(cpuPerfLevel, gpuPerfLevel);
  78. SvrSetOptionFlags(optionFlags);
  79. //if ( ((optionFlags & (int)SvrManager.SvrSettings.eOptionFlags.FoveationSubsampled) != 0) && (SystemInfo.supportsMultisampledTextures == 0))
  80. //{
  81. // Debug.LogWarning("FoveationSampled requested but not supported");
  82. //}
  83. ColorSpace space = QualitySettings.activeColorSpace;
  84. if(space == ColorSpace.Gamma)
  85. {
  86. // 0 == kColorSpaceLinear from svrApi.h
  87. SvrSetColorSpace(0); //Unity will be supplying gamma space eye buffers into warp so we want
  88. //to setup a linear color space display surface so no further gamma
  89. //correction is performed
  90. }
  91. else
  92. {
  93. // 1 == kColorSpaceSRGB from svrApi.h
  94. SvrSetColorSpace(1); //Unity will be supplying linear space eye buffers into warp so we want
  95. //to setup an sRGB color space display surface to properly convert
  96. //incoming linear values into sRGB
  97. }
  98. //yield return new WaitUntil(() => SvrCanBeginVR() == true);
  99. //IssueEvent (RenderEvent.BeginVr);
  100. // yield return null; // delay one frame - fix for multi-threaded rendering
  101. }
  102. public override void EndVr()
  103. {
  104. base.EndVr();
  105. IssueEvent (RenderEvent.EndVr);
  106. }
  107. public override void PauseXr()
  108. {
  109. base.PauseXr();
  110. IssueEvent(RenderEvent.PauseXr);
  111. }
  112. public override void ResumeXr()
  113. {
  114. base.ResumeXr();
  115. IssueEvent(RenderEvent.ResumeXr);
  116. }
  117. public override void BeginEye(int renderIndex, int sideMask, float[] frameDelta)
  118. {
  119. SvrSetFrameOffset(renderIndex, frameDelta); // Enabled for foveation head orientation delta
  120. SvrSetEyeEventData(renderIndex, sideMask, 0);
  121. IssueEvent (RenderEvent.BeginEye);
  122. }
  123. public override void OccludeEye(int renderIndex, Matrix4x4 projection, Matrix4x4 view)
  124. {
  125. //float[] projMtx = { proj.m00, proj.m01, proj.m02, proj.m03,
  126. // proj.m10, proj.m11, proj.m12, proj.m13,
  127. // proj.m20, proj.m21, proj.m22, proj.m23,
  128. // proj.m30, proj.m31, proj.m32, proj.m33};
  129. //float[] viewMtx = { view.m00, view.m01, view.m02, view.m03,
  130. // view.m10, view.m11, view.m12, view.m13,
  131. // view.m20, view.m21, view.m22, view.m23,
  132. // view.m30, view.m31, view.m32, view.m33};
  133. Matrix4x4 proj = GL.GetGPUProjectionMatrix(projection, true);
  134. float[] projMtx = { proj.m00, proj.m10, proj.m20, proj.m30,
  135. proj.m01, proj.m11, proj.m21, proj.m31,
  136. proj.m02, proj.m12, proj.m22, proj.m32,
  137. proj.m03, proj.m13, proj.m23, proj.m33};
  138. float[] viewMtx = { view.m00, view.m10, view.m20, view.m30,
  139. view.m01, view.m11, view.m21, view.m31,
  140. view.m02, view.m12, view.m22, view.m32,
  141. view.m03, view.m13, view.m23, view.m33};
  142. SvrSetEyeRenderData(renderIndex, projMtx, viewMtx);
  143. //IssueEvent(RenderEvent.OccludeEye);
  144. }
  145. public override void EndEye(int renderIndex, int sideMask, int layerMask)
  146. {
  147. SvrSetEyeEventData(renderIndex, sideMask, layerMask);
  148. IssueEvent(RenderEvent.EndEye);
  149. }
  150. public override void SetTrackingMode(int modeMask)
  151. {
  152. Debug.Log("SetTrackingMode:"+ modeMask);
  153. SvrSetTrackingModeEventData(modeMask);
  154. IssueEvent (RenderEvent.SetTrackingMode);
  155. }
  156. public override void SetFoveationParameters(int renderIndex, int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum)
  157. {
  158. SvrSetFoveationParameters(renderIndex, textureId, previousId, focalPointX, focalPointY, foveationGainX, foveationGainY, foveationArea, foveationMinimum);
  159. }
  160. public override void ApplyFoveation()
  161. {
  162. IssueEvent(RenderEvent.Foveation);
  163. }
  164. public override int GetTrackingMode()
  165. {
  166. return SvrGetTrackingMode();
  167. }
  168. public override void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel)
  169. {
  170. SvrSetPerformanceLevelsEventData((int)newCpuPerfLevel, (int)newGpuPerfLevel);
  171. IssueEvent (RenderEvent.SetPerformanceLevels);
  172. }
  173. public override void SetFrameOption(FrameOption frameOption)
  174. {
  175. SvrSetFrameOption((uint)frameOption);
  176. }
  177. public override void UnsetFrameOption(FrameOption frameOption)
  178. {
  179. SvrUnsetFrameOption((uint)frameOption);
  180. }
  181. public override void SetVSyncCount(int vSyncCount)
  182. {
  183. Debug.Log("SetVSyncCount:"+ vSyncCount);
  184. SvrSetVSyncCount(vSyncCount);
  185. }
  186. public override bool RecenterTracking()
  187. {
  188. //IssueEvent (RenderEvent.RecenterTracking);
  189. return SvrRecenterTrackingPose();
  190. }
  191. public override int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex)
  192. {
  193. UInt64 timestamp = 0; // Unused
  194. orientation.z = -orientation.z;
  195. position.x = -position.x;
  196. position.y = -position.y;
  197. int rv = SvrGetPredictedPose(ref timestamp, ref orientation.x, ref orientation.y, ref orientation.z, ref orientation.w,
  198. ref position.x, ref position.y, ref position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  199. orientation.z = -orientation.z;
  200. position.x = -position.x;
  201. position.y = -position.y;
  202. return rv;
  203. }
  204. public override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  205. {
  206. headPose.orientation.z = -headPose.orientation.z;
  207. headPose.position.x = -headPose.position.x;
  208. headPose.position.y = -headPose.position.y;
  209. int rv = SvrGetPredictedPose(ref headPose.timestamp, ref headPose.orientation.x, ref headPose.orientation.y, ref headPose.orientation.z, ref headPose.orientation.w,
  210. ref headPose.position.x, ref headPose.position.y, ref headPose.position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  211. headPose.orientation.z = -headPose.orientation.z;
  212. headPose.position.x = -headPose.position.x;
  213. headPose.position.y = -headPose.position.y;
  214. return rv;
  215. }
  216. public override int GetEyePose(ref EyePose eyePose, int frameIndex = -1)
  217. {
  218. // Transform Unity to SVR by negating z-axis
  219. eyePose.leftDirection.z = -eyePose.leftDirection.z;
  220. eyePose.rightDirection.z = -eyePose.rightDirection.z;
  221. eyePose.combinedDirection.z = -eyePose.combinedDirection.z;
  222. eyePose.leftPosition.z = -eyePose.leftPosition.z;
  223. eyePose.rightPosition.z = -eyePose.rightPosition.z;
  224. eyePose.combinedPosition.z = -eyePose.combinedPosition.z;
  225. int rv = SvrGetEyePose(
  226. ref eyePose.timestamp,
  227. ref eyePose.leftStatus, ref eyePose.rightStatus, ref eyePose.combinedStatus,
  228. ref eyePose.leftBlink, ref eyePose.rightBlink,
  229. ref eyePose.leftOpenness, ref eyePose.rightOpenness,
  230. ref eyePose.leftDilation, ref eyePose.rightDilation,
  231. ref eyePose.leftDirection.x, ref eyePose.leftDirection.y, ref eyePose.leftDirection.z,
  232. ref eyePose.leftPosition.x, ref eyePose.leftPosition.y, ref eyePose.leftPosition.z,
  233. ref eyePose.leftGuide.x, ref eyePose.leftGuide.y, ref eyePose.leftGuide.z,
  234. ref eyePose.rightDirection.x, ref eyePose.rightDirection.y, ref eyePose.rightDirection.z,
  235. ref eyePose.rightPosition.x, ref eyePose.rightPosition.y, ref eyePose.rightPosition.z,
  236. ref eyePose.rightGuide.x, ref eyePose.rightGuide.y, ref eyePose.rightGuide.z,
  237. ref eyePose.combinedDirection.x, ref eyePose.combinedDirection.y, ref eyePose.combinedDirection.z,
  238. ref eyePose.combinedPosition.x, ref eyePose.combinedPosition.y, ref eyePose.combinedPosition.z,
  239. frameIndex);
  240. // Transform SVR to Unity by negating z-axis
  241. eyePose.leftDirection.z = -eyePose.leftDirection.z;
  242. eyePose.rightDirection.z = -eyePose.rightDirection.z;
  243. eyePose.combinedDirection.z = -eyePose.combinedDirection.z;
  244. eyePose.leftPosition.z = -eyePose.leftPosition.z;
  245. eyePose.rightPosition.z = -eyePose.rightPosition.z;
  246. eyePose.combinedPosition.z = -eyePose.combinedPosition.z;
  247. return rv;
  248. }
  249. public override int GetEyeFocalPoint(ref Vector2 focalPoint)
  250. {
  251. ulong timeStamp = 0;
  252. ulong foveatedState = 0;
  253. Vector3 foveatedDirection = Vector3.back;
  254. int rv = SvrGetEyeFocalPoint(
  255. ref timeStamp,
  256. ref foveatedState,
  257. ref foveatedDirection.x, ref foveatedDirection.y, ref foveatedDirection.z);
  258. // Transform SVR to Unity by negating z-axis
  259. foveatedDirection.z = -foveatedDirection.z;
  260. if (foveatedDirection.sqrMagnitude > 0f)
  261. {
  262. foveatedDirection.Normalize();
  263. //Debug.LogFormat("Eye Direction: ({0:F2}, {1:F2}, {2:F2})", foveatedDirection.x, foveatedDirection.y, foveatedDirection.z);
  264. float denominator = Vector3.Dot(foveatedDirection, Vector3.forward);
  265. if (denominator > float.Epsilon)
  266. {
  267. // eye direction intersection with frustum near plane (left)
  268. var eyePoint = foveatedDirection * deviceInfo.targetFrustumLeft.near / denominator;
  269. // size of the frustum near plane (left)
  270. var nearSize = new Vector2(0.5f * (deviceInfo.targetFrustumLeft.right - deviceInfo.targetFrustumLeft.left),
  271. 0.5f * (deviceInfo.targetFrustumLeft.top - deviceInfo.targetFrustumLeft.bottom));
  272. focalPoint.Set(eyePoint.x / nearSize.x, eyePoint.y / nearSize.y); // Normalized [-1,1]
  273. //Debug.LogFormat("[{0}] Eye Focus: {1}", timeStamp, focalPoint.ToString());
  274. }
  275. }
  276. return rv;
  277. }
  278. public override bool Is3drOcclusion()
  279. {
  280. return SvrIs3drOcclusion();
  281. }
  282. public override void GetOcclusionMesh()
  283. {
  284. IssueEvent(RenderEvent.OcclusionMesh);
  285. }
  286. public override DeviceInfo GetDeviceInfo()
  287. {
  288. DeviceInfo info = new DeviceInfo();
  289. info.trackingCalibration = Matrix4x4.identity;
  290. SvrGetDeviceInfo (ref info.displayWidthPixels,
  291. ref info.displayHeightPixels,
  292. ref info.displayRefreshRateHz,
  293. ref info.targetEyeWidthPixels,
  294. ref info.targetEyeHeightPixels,
  295. ref info.targetFovXRad,
  296. ref info.targetFovYRad,
  297. ref info.targetFrustumLeft.left, ref info.targetFrustumLeft.right, ref info.targetFrustumLeft.bottom, ref info.targetFrustumLeft.top, ref info.targetFrustumLeft.near, ref info.targetFrustumLeft.far,
  298. ref info.targetFrustumLeft.position.x, ref info.targetFrustumLeft.position.y, ref info.targetFrustumLeft.position.z,
  299. ref info.targetFrustumLeft.rotation.x, ref info.targetFrustumLeft.rotation.y, ref info.targetFrustumLeft.rotation.z, ref info.targetFrustumLeft.rotation.w,
  300. ref info.targetFrustumRight.left, ref info.targetFrustumRight.right, ref info.targetFrustumRight.bottom, ref info.targetFrustumRight.top, ref info.targetFrustumRight.near, ref info.targetFrustumRight.far,
  301. ref info.targetFrustumRight.position.x, ref info.targetFrustumRight.position.y, ref info.targetFrustumRight.position.z,
  302. ref info.targetFrustumRight.rotation.x, ref info.targetFrustumRight.rotation.y, ref info.targetFrustumRight.rotation.z, ref info.targetFrustumRight.rotation.w,
  303. ref info.targetFrustumConvergence, ref info.targetFrustumPitch,
  304. ref info.lowFoveation.Gain.x, ref info.lowFoveation.Gain.y, ref info.lowFoveation.Area, ref info.lowFoveation.Minimum,
  305. ref info.medFoveation.Gain.x, ref info.medFoveation.Gain.y, ref info.medFoveation.Area, ref info.medFoveation.Minimum,
  306. ref info.highFoveation.Gain.x, ref info.highFoveation.Gain.y, ref info.highFoveation.Area, ref info.highFoveation.Minimum,
  307. ref info.trackingCalibration.m00, ref info.trackingCalibration.m01, ref info.trackingCalibration.m02, ref info.trackingCalibration.m03,
  308. ref info.trackingCalibration.m10, ref info.trackingCalibration.m11, ref info.trackingCalibration.m12, ref info.trackingCalibration.m13,
  309. ref info.trackingCalibration.m20, ref info.trackingCalibration.m21, ref info.trackingCalibration.m22, ref info.trackingCalibration.m23,
  310. ref info.trackingIntrinsics.PrincipalPoint.x, ref info.trackingIntrinsics.PrincipalPoint.y,
  311. ref info.trackingIntrinsics.FocalLength.x, ref info.trackingIntrinsics.FocalLength.y,
  312. ref info.trackingIntrinsics.Distortion0, ref info.trackingIntrinsics.Distortion1, ref info.trackingIntrinsics.Distortion2, ref info.trackingIntrinsics.Distortion3, ref info.trackingIntrinsics.Distortion4, ref info.trackingIntrinsics.Distortion5, ref info.trackingIntrinsics.Distortion6, ref info.trackingIntrinsics.Distortion7,
  313. ref info.trackingCapabilities
  314. );
  315. // Transform coords from SVR to Unity by negating z-axis (negate position.x, rotation.x, and rotation.y)
  316. info.targetFrustumLeft.position.z = -info.targetFrustumLeft.position.z;
  317. info.targetFrustumLeft.rotation.x = -info.targetFrustumLeft.rotation.x;
  318. info.targetFrustumLeft.rotation.y = -info.targetFrustumLeft.rotation.y;
  319. info.targetFrustumRight.position.z = -info.targetFrustumRight.position.z;
  320. info.targetFrustumRight.rotation.x = -info.targetFrustumRight.rotation.x;
  321. info.targetFrustumRight.rotation.y = -info.targetFrustumRight.rotation.y;
  322. return info;
  323. }
  324. public override void Shutdown()
  325. {
  326. IssueEvent (RenderEvent.Shutdown);
  327. base.Shutdown();
  328. }
  329. // public override bool PollEvent(ref SvrManager.SvrEvent frameEvent)
  330. // {
  331. // int dataCount = Marshal.SizeOf(frameEvent.eventData) / sizeof(uint);
  332. // uint[] dataBuffer = new uint[dataCount];
  333. //int eventType = 0;
  334. // bool isEvent = SvrPollEvent(ref eventType, ref frameEvent.deviceId, ref frameEvent.eventTimeStamp, dataCount, dataBuffer);
  335. //frameEvent.eventType = (SvrManager.svrEventType)(eventType);
  336. // switch (frameEvent.eventType)
  337. // {
  338. // case SvrManager.svrEventType.kEventThermal:
  339. // //Debug.LogFormat("PollEvent: data {0} {1}", dataBuffer[0], dataBuffer[1]);
  340. // frameEvent.eventData.thermal.zone = (SvrManager.svrThermalZone)dataBuffer[0];
  341. // frameEvent.eventData.thermal.level = (SvrManager.svrThermalLevel)dataBuffer[1];
  342. // break;
  343. // case SvrManager.svrEventType.kEventProximity:
  344. // //Debug.LogFormat("PollEvent: data {0} {1}", dataBuffer[0], dataBuffer[1]);
  345. // frameEvent.eventData.proximity.distance = (float)dataBuffer[0];
  346. // break;
  347. // case SvrManager.svrEventType.kEventControllerConnected:
  348. // case SvrManager.svrEventType.kEventControllerConnecting:
  349. // case SvrManager.svrEventType.kEventControllerDisconnected:
  350. // frameEvent.eventData.data = dataBuffer[0];
  351. // break;
  352. // }
  353. // return isEvent;
  354. // }
  355. //---------------------------------------------------------------------------------------------
  356. //Controller Apis
  357. //---------------------------------------------------------------------------------------------
  358. /// <summary>
  359. /// Controllers the start tracking.
  360. /// </summary>
  361. /// <returns>The start tracking.</returns>
  362. /// <param name="desc">Desc.</param>
  363. //---------------------------------------------------------------------------------------------
  364. public override int ControllerStartTracking(string desc)
  365. {
  366. return SvrControllerStartTracking(desc);
  367. }
  368. /// <summary>
  369. /// Controllers the stop tracking.
  370. /// </summary>
  371. /// <param name="handle">Handle.</param>
  372. //---------------------------------------------------------------------------------------------
  373. public override void ControllerStopTracking(int handle)
  374. {
  375. SvrControllerStopTracking(handle);
  376. }
  377. /// <summary>
  378. /// Dumps the state.
  379. /// </summary>
  380. /// <param name="state">State.</param>
  381. //---------------------------------------------------------------------------------------------
  382. public override int CloudPointData(CloudPoint[] cloudData)
  383. {
  384. //IssueEvent(RenderEvent.CloudData);
  385. SvrCloudGetData();
  386. int pointCount = 0;
  387. while (pointCount < cloudData.Length &&
  388. SvrCloudGetDataElement(pointCount, ref cloudData[pointCount].id, ref cloudData[pointCount].x, ref cloudData[pointCount].y, ref cloudData[pointCount].z))
  389. {
  390. pointCount++;
  391. }
  392. return pointCount;
  393. }
  394. public override string AnchorToString(AnchorUuid id)
  395. {
  396. return SvrAnchorToString(ref id);
  397. }
  398. public override bool AnchorCreate(Vector3 position, Quaternion rotation, ref AnchorUuid id)
  399. {
  400. // Transform Unity to SXR
  401. return SvrAnchorCreate(-position.x, -position.y, position.z, rotation.x, rotation.y, -rotation.z, rotation.w, ref id);
  402. //return SvrAnchorCreate(position.x, position.y, position.z, rotation.x, rotation.y, rotation.z, rotation.w, ref id);
  403. }
  404. public override bool AnchorDestroy(AnchorUuid id)
  405. {
  406. return SvrAnchorDestroy(id);
  407. }
  408. public override bool AnchorSave(AnchorUuid id)
  409. {
  410. return SvrAnchorSave(id, Application.persistentDataPath);
  411. }
  412. public override int AnchorGetData(AnchorInfo[] anchorData)
  413. {
  414. //IssueEvent(RenderEvent.AnchorData);
  415. SvrAnchorGetData();
  416. int anchorCount = 0;
  417. while (anchorCount < anchorData.Length &&
  418. SvrAnchorGetDataElement(anchorCount, ref anchorData[anchorCount].id, ref anchorData[anchorCount].revision,
  419. ref anchorData[anchorCount].pose.position.x, ref anchorData[anchorCount].pose.position.y, ref anchorData[anchorCount].pose.position.z,
  420. ref anchorData[anchorCount].pose.orientation.x, ref anchorData[anchorCount].pose.orientation.y, ref anchorData[anchorCount].pose.orientation.z, ref anchorData[anchorCount].pose.orientation.w,
  421. ref anchorData[anchorCount].pose.poseQuality))
  422. {
  423. // Transform Unity to SXR
  424. anchorData[anchorCount].pose.orientation.z = -anchorData[anchorCount].pose.orientation.z;
  425. anchorData[anchorCount].pose.position.x = -anchorData[anchorCount].pose.position.x;
  426. anchorData[anchorCount].pose.position.y = -anchorData[anchorCount].pose.position.y;
  427. anchorCount++;
  428. }
  429. return anchorCount;
  430. }
  431. public override bool AnchorStartRelocating()
  432. {
  433. return SvrAnchorRelocatingStart(Application.persistentDataPath);
  434. }
  435. public override bool AnchorStopRelocating()
  436. {
  437. return SvrAnchorRelocatingStop();
  438. }
  439. #region Native-Interfaces
  440. protected static string Tag = "SvrPluginAndroid";
  441. [DllImport("svrplugin")]
  442. internal static extern IntPtr GetRenderEventFunc();
  443. [DllImport("unity_native_api")]
  444. internal static extern void Unity_setReticleTextureId(int reticleTextureID, int texWidth, int texHeight);
  445. [DllImport("unity_native_api")]
  446. internal static extern bool Unity_getReticleRendering();
  447. [DllImport("unity_native_api")]
  448. internal static extern void Unity_setReticleRendering(bool renderFlag);
  449. [DllImport("unity_native_api")]
  450. internal static extern bool Unity_getOverlayRendering();
  451. /// <summary>
  452. /// 开关 overlay rendering 特性
  453. /// </summary>
  454. /// <param name="enableOverlayRendering"></param>
  455. [DllImport("unity_native_api")]
  456. internal static extern void Unity_setOverlayRendering(bool enableOverlayRendering);
  457. /// <summary>
  458. /// 设置 overlay rendering 的 texture id.
  459. /// </summary>
  460. /// <param name="overlayTextureIDLeft"></param>
  461. /// <param name="overlayTextureIDRight"></param>
  462. [DllImport("unity_native_api")]
  463. internal static extern void Unity_setOverlayRenderingTextureId(int overlayTextureIDLeft, int overlayTextureIDRight);
  464. /// <summary>
  465. /// sets world space overlay texture params
  466. /// </summary>
  467. /// <param name="renderingFlag"></param>
  468. /// <param name="layerIndex"></param>
  469. /// <param name="textureId"></param>
  470. /// <param name="width"></param>
  471. /// <param name="height"></param>
  472. /// <param name="anchorMatrix"></param>
  473. [DllImport("unity_native_api")]
  474. internal static extern void Unity_setWorldOverlayTexture(bool renderingFlag, int layerIndex, int textureId, float width, float height, float[] anchorMatrix,
  475. float[] ll, float[] lt, float[] rt, float[] rb);
  476. [DllImport("unity_native_api")]
  477. internal static extern void Unity_setFrame(int frame);
  478. [DllImport("svrplugin")]
  479. private static extern bool SvrIsInitialized();
  480. [DllImport("svrplugin")]
  481. internal static extern bool SvrIsRunning();
  482. [DllImport("svrplugin")]
  483. private static extern bool SvrCanBeginVR();
  484. [DllImport("svrplugin")]
  485. private static extern void SvrInitializeEventData(IntPtr activity);
  486. [DllImport("svrplugin")]
  487. internal static extern void SvrSubmitFrameEventData(int frameIndex, float fieldOfView, int frameType);
  488. [DllImport("svrplugin")]
  489. internal static extern void SvrSetupLayerCoords(int layerIndex, float[] lowerLeft, float[] lowerRight, float[] upperLeft, float[] upperRight);
  490. [DllImport("svrplugin")]
  491. internal static extern void SvrSetupLayerData(int layerIndex, int sideMask, int textureId, int textureType, int layerFlags);
  492. [DllImport("svrplugin")]
  493. private static extern void SvrSetTrackingModeEventData(int mode);
  494. [DllImport("svrplugin")]
  495. private static extern void SvrSetPerformanceLevelsEventData(int newCpuPerfLevel, int newGpuPerfLevel);
  496. [DllImport("svrplugin")]
  497. private static extern void SvrSetEyeEventData(int renderIndex, int sideMask, int layerMask);
  498. [DllImport("svrplugin")]
  499. private static extern void SvrSetEyeRenderData(int renderIndex, float[] eyeProjMat, float[] eyeViewMat);
  500. [DllImport("svrplugin")]
  501. private static extern void SvrSetColorSpace(int colorSpace);
  502. [DllImport("svrplugin")]
  503. private static extern void SvrSetOptionFlags(int optionFlags);
  504. [DllImport("svrplugin")]
  505. private static extern void SvrSetFrameOption(uint frameOption);
  506. [DllImport("svrplugin")]
  507. private static extern void SvrUnsetFrameOption(uint frameOption);
  508. [DllImport("svrplugin")]
  509. private static extern void SvrSetVSyncCount(int vSyncCount);
  510. [DllImport("svrplugin")]
  511. private static extern int SvrGetPredictedPose(ref UInt64 timeStampNs,
  512. ref float rx,
  513. ref float ry,
  514. ref float rz,
  515. ref float rw,
  516. ref float px,
  517. ref float py,
  518. ref float pz,
  519. int frameIndex,
  520. bool isMultiThreadedRender);
  521. [DllImport("svrplugin")]
  522. private static extern int SvrGetEyeFocalPoint(ref UInt64 timeStampNs,
  523. ref UInt64 foveatedStatus,
  524. ref float foveatedDirectionX,
  525. ref float foveatedDirectionY,
  526. ref float foveatedDirectionZ);
  527. [DllImport("svrplugin")]
  528. private static extern int SvrGetEyePose(ref UInt64 timeStampNs,
  529. ref int leftStatus,
  530. ref int rightStatus,
  531. ref int combinedStatus,
  532. ref bool leftBlink,
  533. ref bool rightBlink,
  534. ref float leftOpenness,
  535. ref float rightOpenness,
  536. ref float leftDilation,
  537. ref float rightDilation,
  538. ref float leftDirectionX,
  539. ref float leftDirectionY,
  540. ref float leftDirectionZ,
  541. ref float leftPositionX,
  542. ref float leftPositionY,
  543. ref float leftPositionZ,
  544. ref float leftGuideX,
  545. ref float leftGuideY,
  546. ref float leftGuideZ,
  547. ref float rightDirectionX,
  548. ref float rightDirectionY,
  549. ref float rightDirectionZ,
  550. ref float rightPositionX,
  551. ref float rightPositionY,
  552. ref float rightPositionZ,
  553. ref float rightGuideX,
  554. ref float rightGuideY,
  555. ref float rightGuideZ,
  556. ref float combinedDirectionX,
  557. ref float combinedDirectionY,
  558. ref float combinedDirectionZ,
  559. ref float combinedPositionX,
  560. ref float combinedPositionY,
  561. ref float combinedPositionZ,
  562. int frameIndex);
  563. [DllImport("svrplugin")]
  564. private static extern bool SvrRecenterTrackingPose();
  565. [DllImport("svrplugin")]
  566. private static extern int SvrGetTrackingMode();
  567. [DllImport("svrplugin")]
  568. private static extern bool SvrIs3drOcclusion();
  569. [DllImport("svrplugin")]
  570. private static extern void SvrGetDeviceInfo(ref int displayWidthPixels,
  571. ref int displayHeightPixels,
  572. ref float displayRefreshRateHz,
  573. ref int targetEyeWidthPixels,
  574. ref int targetEyeHeightPixels,
  575. ref float targetFovXRad,
  576. ref float targetFovYRad,
  577. ref float leftFrustumLeft, ref float leftFrustumRight, ref float leftFrustumBottom, ref float leftFrustumTop, ref float leftFrustumNear, ref float leftEyeFrustumFar,
  578. ref float leftFrustumPositionX, ref float leftFrustumPositionY, ref float leftFrustumPositionZ,
  579. ref float leftFrustumRotationX, ref float leftFrustumRotationY, ref float leftFrustumRotationZ, ref float leftFrustumRotationW,
  580. ref float rightFrustumLeft, ref float rightFrustumRight, ref float rightFrustumBottom, ref float rightFrustumTop, ref float rightFrustumNear, ref float rightFrustumFar,
  581. ref float rightFrustumPositionX, ref float rightFrustumPositionY, ref float rightFrustumPositionZ,
  582. ref float rightFrustumRotationX, ref float rightFrustumRotationY, ref float rightFrustumRotationZ, ref float rightFrustumRotationW,
  583. ref float targetfrustumConvergence, ref float targetFrustumPitch,
  584. ref float lowFoveationGainX, ref float lowFoveationGainY, ref float lowFoveationArea, ref float lowFoveationMinimum,
  585. ref float medFoveationGainX, ref float medFoveationGainY, ref float medFoveationArea, ref float medFoveationMinimum,
  586. ref float highFoveationGainX, ref float highFoveationGainY, ref float highFoveationArea, ref float highFoveationMinimum,
  587. ref float trackingCalibrationM00, ref float trackingCalibrationM01, ref float trackingCalibrationM02, ref float trackingCalibrationM03,
  588. ref float trackingCalibrationM10, ref float trackingCalibrationM11, ref float trackingCalibrationM12, ref float trackingCalibrationM13,
  589. ref float trackingCalibrationM20, ref float trackingCalibrationM21, ref float trackingCalibrationM22, ref float trackingCalibrationM23,
  590. ref float trackingPrincipalPointX, ref float trackingPrincipalPointY,
  591. ref float trackingFocalLengthX, ref float trackingFocalLengthY,
  592. ref float trackingDistortion0, ref float trackingDistortion1, ref float trackingDistortion2, ref float trackingDistortion3, ref float trackingDistortion4, ref float trackingDistortion5, ref float trackingDistortion6, ref float trackingDistortion7,
  593. ref ulong trackingCapabilities);
  594. [DllImport("svrplugin")]
  595. private static extern void SvrSetFrameOffset(int renderIndex, float[] delta);
  596. [DllImport("svrplugin")]
  597. private static extern void SvrSetFoveationParameters(int renderIndex, int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum);
  598. [DllImport("svrplugin")]
  599. public static extern bool SvrPollEvent(ref int eventType, ref uint deviceId, ref float eventTimeStamp, int eventDataCount, uint[] eventData);
  600. //---------------------------------------------------------------------------------------------
  601. // Controller Api
  602. //---------------------------------------------------------------------------------------------
  603. [DllImport("svrplugin")]
  604. private static extern int SvrControllerStartTracking(string desc);
  605. [DllImport("svrplugin")]
  606. private static extern void SvrControllerStopTracking(int handle);
  607. [DllImport("svrplugin")]
  608. private static extern void SvrControllerSendMessage(int handle, int what, int arg1, int arg2);
  609. [DllImport("svrplugin")]
  610. private static extern int SvrControllerQuery(int handle, int what, IntPtr mem, int size);
  611. //---------------------------------------------------------------------------------------------
  612. //---------------------------------------------------------------------------------------------
  613. // Anchors Api
  614. //---------------------------------------------------------------------------------------------
  615. [DllImport("svrplugin")]
  616. private static extern string SvrAnchorToString(ref AnchorUuid id);
  617. [DllImport("svrplugin")]
  618. private static extern bool SvrAnchorCreate(float x, float y, float z, float qx, float qy, float qz, float qw, ref AnchorUuid id);
  619. [DllImport("svrplugin")]
  620. private static extern bool SvrAnchorDestroy(AnchorUuid id);
  621. [DllImport("svrplugin")]
  622. private static extern bool SvrAnchorSave(AnchorUuid id, string fmapFolder);
  623. [DllImport("svrplugin")]
  624. private static extern int SvrAnchorGetData();
  625. [DllImport("svrplugin")]
  626. private static extern bool SvrAnchorGetDataElement(int index, ref AnchorUuid id, ref UInt32 revision,
  627. ref float x, ref float y, ref float z, ref float qx, ref float qy, ref float qz, ref float qw, ref float quality);
  628. [DllImport("svrplugin")]
  629. private static extern bool SvrAnchorRelocatingStart(string fmapFolder);
  630. [DllImport("svrplugin")]
  631. private static extern bool SvrAnchorRelocatingStop();
  632. //---------------------------------------------------------------------------------------------
  633. //---------------------------------------------------------------------------------------------
  634. // PointCloud Api
  635. //---------------------------------------------------------------------------------------------
  636. [DllImport("svrplugin")]
  637. private static extern int SvrCloudGetData();
  638. [DllImport("svrplugin")]
  639. private static extern bool SvrCloudGetDataElement(int index, ref UInt32 id, ref float x, ref float y, ref float z);
  640. //---------------------------------------------------------------------------------------------
  641. #endregion
  642. }