GSXRPluginAndroid.cs 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Runtime.InteropServices;
  5. using SC.XR.Unity;
  6. class GSXRPluginAndroid : GSXRPlugin
  7. {
  8. private const string DLLName = "gsxrplugin";
  9. [DllImport(DLLName)]
  10. private static extern IntPtr GetRenderEventFunc();
  11. [DllImport(DLLName)]
  12. private static extern bool GSXRIsInitialized();
  13. [DllImport(DLLName)]
  14. private static extern bool GSXRIsRunning();
  15. [DllImport(DLLName)]
  16. private static extern bool GSXRCanBeginVR();
  17. [DllImport(DLLName)]
  18. private static extern void GSXRInitializeEventData(IntPtr activity);
  19. [DllImport(DLLName)]
  20. private static extern void GSXRSubmitFrameEventData(int frameIndex, float fieldOfView, int frameType);
  21. [DllImport(DLLName)]
  22. private static extern void GSXRSetupLayerCoords(int layerIndex, float[] lowerLeft, float[] lowerRight, float[] upperLeft, float[] upperRight);
  23. [DllImport(DLLName)]
  24. private static extern void GSXRSetupLayerData(int layerIndex, int sideMask, int textureId, int textureType, int layerFlags, int depthId);
  25. [DllImport(DLLName)]
  26. private static extern void GSXRSetTrackingModeEventData(int mode);
  27. [DllImport(DLLName)]
  28. private static extern void GSXRSetPerformanceLevelsEventData(int newCpuPerfLevel, int newGpuPerfLevel);
  29. [DllImport(DLLName)]
  30. private static extern void GSXRSetEyeEventData(int sideMask, int layerMask);
  31. [DllImport(DLLName)]
  32. private static extern void GSXRSetColorSpace(int colorSpace);
  33. [DllImport(DLLName)]
  34. private static extern void GSXRSetFrameOption(uint frameOption);
  35. [DllImport(DLLName)]
  36. private static extern void GSXRUnsetFrameOption(uint frameOption);
  37. [DllImport(DLLName)]
  38. private static extern void GSXRSetVSyncCount(int vSyncCount);
  39. [DllImport(DLLName)]
  40. private static extern int GSXRGetPredictedPose(ref float rx,
  41. ref float ry,
  42. ref float rz,
  43. ref float rw,
  44. ref float px,
  45. ref float py,
  46. ref float pz,
  47. int frameIndex,
  48. bool isMultiThreadedRender);
  49. [DllImport(DLLName)]
  50. private static extern bool GSXRRecenterTrackingPose();
  51. [DllImport(DLLName)]
  52. private static extern int GSXRGetTrackingMode();
  53. [DllImport(DLLName)]
  54. private static extern void GSXRGetDeviceInfo(ref int displayWidthPixels,
  55. ref int displayHeightPixels,
  56. ref float displayRefreshRateHz,
  57. ref int targetEyeWidthPixels,
  58. ref int targetEyeHeightPixels,
  59. ref float targetFovXRad,
  60. ref float targetFovYRad,
  61. ref float leftFrustumLeft, ref float leftFrustumRight, ref float leftFrustumBottom, ref float leftFrustumTop, ref float leftFrustumNear, ref float leftEyeFrustumFar,
  62. ref float rightFrustumLeft, ref float rightFrustumRight, ref float rightFrustumBottom, ref float rightFrustumTop, ref float rightFrustumNear, ref float rightFrustumFar,
  63. ref float targetfrustumConvergence, ref float targetFrustumPitch);
  64. [DllImport(DLLName)]
  65. private static extern void GSXRSetFrameOffset(float[] delta);
  66. [DllImport(DLLName)]
  67. private static extern void GSXRSetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum);
  68. [DllImport(DLLName)]
  69. private static extern bool GSXRPollEvent(ref int eventType, ref uint deviceId, ref float eventTimeStamp, int eventDataCount, uint[] eventData);
  70. //---------------------------------------------------------------------------------------------
  71. // Conotroller Apis
  72. //---------------------------------------------------------------------------------------------
  73. [DllImport(DLLName)]
  74. private static extern int GSXRControllerStartTracking(string desc);
  75. [DllImport(DLLName)]
  76. private static extern void GSXRControllerStopTracking(int handle);
  77. [DllImport(DLLName)]
  78. private static extern void GSXRControllerGetState(int handle, int space, ref GSXRControllerState state);
  79. [DllImport(DLLName)]
  80. private static extern void GSXRControllerSendMessage(int handle, int what, int arg1, int arg2);
  81. [DllImport(DLLName)]
  82. private static extern int GSXRControllerQuery(int handle, int what, IntPtr mem, int size);
  83. private enum RenderEvent
  84. {
  85. Initialize,
  86. BeginVr,
  87. EndVr,
  88. BeginEye,
  89. EndEye,
  90. SubmitFrame,
  91. Foveation,
  92. Shutdown,
  93. RecenterTracking,
  94. SetTrackingMode,
  95. SetPerformanceLevels,
  96. StartLayerRendering
  97. };
  98. public static GSXRPluginAndroid Create()
  99. {
  100. if(Application.isEditor)
  101. {
  102. Debug.LogError("GSXRPlugin not supported in unity editor!");
  103. throw new InvalidOperationException();
  104. }
  105. return new GSXRPluginAndroid();
  106. }
  107. private GSXRPluginAndroid() {}
  108. private void IssueEvent(RenderEvent e)
  109. {
  110. // Queue a specific callback to be called on the render thread
  111. GL.IssuePluginEvent(GetRenderEventFunc(), (int)e);
  112. }
  113. public override bool IsInitialized() { return GSXRIsInitialized(); }
  114. public override bool IsRunning() { return GSXRIsRunning(); }
  115. public override IEnumerator Initialize()
  116. {
  117. //yield return new WaitUntil(() => GSXRIsInitialized() == false); // Wait for shutdown
  118. yield return base.Initialize();
  119. if(Application.platform == RuntimePlatform.Android) {
  120. AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
  121. AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
  122. GSXRInitializeEventData(activity.GetRawObject());
  123. }
  124. IssueEvent(RenderEvent.Initialize);
  125. yield return new WaitUntil (() => GSXRIsInitialized () == true);
  126. yield return null; // delay one frame - fix for re-init w multi-threaded rendering
  127. deviceInfo = GetDeviceInfo();
  128. }
  129. public override IEnumerator BeginVr(int cpuPerfLevel, int gpuPerfLevel)
  130. {
  131. //yield return new WaitUntil(() => GSXRIsRunning() == false); // Wait for EndVr
  132. yield return base.BeginVr(cpuPerfLevel, gpuPerfLevel);
  133. // float[6]: x, y, z, w, u, v
  134. float[] lowerLeft = { -1f, -1f, 0f, 1f, 0f, 0f };
  135. float[] upperLeft = { -1f, 1f, 0f, 1f, 0f, 1f };
  136. float[] upperRight = { 1f, 1f, 0f, 1f, 1f, 1f };
  137. float[] lowerRight = { 1f, -1f, 0f, 1f, 1f, 0f };
  138. GSXRSetupLayerCoords(-1, lowerLeft, lowerRight, upperLeft, upperRight); // Layers/All
  139. GSXRSetPerformanceLevelsEventData(cpuPerfLevel, gpuPerfLevel);
  140. ColorSpace space = QualitySettings.activeColorSpace;
  141. if(space == ColorSpace.Gamma)
  142. {
  143. // 0 == kColorSpaceLinear from slamApi.h
  144. GSXRSetColorSpace(0); //Unity will be supplying gamma space eye buffers into warp so we want
  145. //to setup a linear color space display surface so no further gamma
  146. //correction is performed
  147. }
  148. else
  149. {
  150. // 1 == kColorSpaceSRGB from slamApi.h
  151. GSXRSetColorSpace(1); //Unity will be supplying linear space eye buffers into warp so we want
  152. //to setup an sRGB color space display surface to properly convert
  153. //incoming linear values into sRGB
  154. }
  155. yield return new WaitUntil(() => GSXRCanBeginVR() == true);
  156. IssueEvent (RenderEvent.BeginVr);
  157. //yield return new WaitUntil(() => GSXRIsRunning() == true);
  158. }
  159. public override void EndVr()
  160. {
  161. base.EndVr();
  162. Debug.Log("EndVR...."+Time.frameCount);
  163. IssueEvent (RenderEvent.EndVr);
  164. }
  165. public override void BeginEye(int sideMask, float[] frameDelta)
  166. {
  167. GSXRSetFrameOffset(frameDelta); // Enabled for foveation head orientation delta
  168. GSXRSetEyeEventData(sideMask, 0);
  169. IssueEvent (RenderEvent.BeginEye);
  170. }
  171. public override void EndEye(int sideMask, int layerMask)
  172. {
  173. GSXRSetEyeEventData(sideMask, layerMask);
  174. IssueEvent(RenderEvent.EndEye);
  175. }
  176. public override void SetTrackingMode(int modeMask)
  177. {
  178. GSXRSetTrackingModeEventData(modeMask);
  179. IssueEvent (RenderEvent.SetTrackingMode);
  180. }
  181. public override void SetFoveationParameters(int textureId, int previousId, float focalPointX, float focalPointY, float foveationGainX, float foveationGainY, float foveationArea, float foveationMinimum)
  182. {
  183. GSXRSetFoveationParameters(textureId, previousId, focalPointX, focalPointY, foveationGainX, foveationGainY, foveationArea, foveationMinimum);
  184. }
  185. public override void ApplyFoveation()
  186. {
  187. IssueEvent(RenderEvent.Foveation);
  188. }
  189. public override int GetTrackingMode()
  190. {
  191. return GSXRGetTrackingMode();
  192. }
  193. public override void SetPerformanceLevels(int newCpuPerfLevel, int newGpuPerfLevel)
  194. {
  195. GSXRSetPerformanceLevelsEventData((int)newCpuPerfLevel, (int)newGpuPerfLevel);
  196. IssueEvent (RenderEvent.SetPerformanceLevels);
  197. }
  198. public override void SetFrameOption(FrameOption frameOption)
  199. {
  200. GSXRSetFrameOption((uint)frameOption);
  201. }
  202. public override void UnsetFrameOption(FrameOption frameOption)
  203. {
  204. GSXRUnsetFrameOption((uint)frameOption);
  205. }
  206. public override void SetVSyncCount(int vSyncCount)
  207. {
  208. GSXRSetVSyncCount(vSyncCount);
  209. }
  210. public override bool RecenterTracking()
  211. {
  212. //IssueEvent (RenderEvent.RecenterTracking);
  213. return GSXRRecenterTrackingPose();
  214. }
  215. public override int GetPredictedPose(ref Quaternion orientation, ref Vector3 position, int frameIndex)
  216. {
  217. orientation.z = -orientation.z;
  218. position.x = -position.x;
  219. position.y = -position.y;
  220. int rv = GSXRGetPredictedPose(ref orientation.x, ref orientation.y, ref orientation.z, ref orientation.w,
  221. ref position.x, ref position.y, ref position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  222. orientation.z = -orientation.z;
  223. position.x = -position.x;
  224. position.y = -position.y;
  225. return rv;
  226. }
  227. private float[] leftViewData = new float[16];
  228. private float[] rightViewData = new float[16];
  229. private float[] TData = new float[3];
  230. private float[] RData = new float[4];
  231. public override int GetHeadPose(ref HeadPose headPose, int frameIndex)
  232. {
  233. int rv = 0;
  234. if (!GSXR_Is_SupportOpticsCalibration()) {
  235. headPose.orientation.z = -headPose.orientation.z;
  236. headPose.position.x = -headPose.position.x;
  237. headPose.position.y = -headPose.position.y;
  238. rv = GSXRGetPredictedPose(ref headPose.orientation.x, ref headPose.orientation.y, ref headPose.orientation.z, ref headPose.orientation.w,
  239. ref headPose.position.x, ref headPose.position.y, ref headPose.position.z, frameIndex, SystemInfo.graphicsMultiThreaded);
  240. } else {
  241. rv = GSXR_Get_LatestEyeMatrices(leftViewData, rightViewData, TData, RData, frameIndex, SystemInfo.graphicsMultiThreaded);
  242. leftViewMatrix.SetColumn(0, new Vector4(leftViewData[0], leftViewData[1], leftViewData[2], leftViewData[3]));
  243. leftViewMatrix.SetColumn(1, new Vector4(leftViewData[4], leftViewData[5], leftViewData[6], leftViewData[7]));
  244. leftViewMatrix.SetColumn(2, new Vector4(leftViewData[8], leftViewData[9], leftViewData[10], leftViewData[11]));
  245. leftViewMatrix.SetColumn(3, new Vector4(leftViewData[12], leftViewData[13], leftViewData[14], leftViewData[15]));
  246. rightViewMatrix.SetColumn(0, new Vector4(rightViewData[0], rightViewData[1], rightViewData[2], rightViewData[3]));
  247. rightViewMatrix.SetColumn(1, new Vector4(rightViewData[4], rightViewData[5], rightViewData[6], rightViewData[7]));
  248. rightViewMatrix.SetColumn(2, new Vector4(rightViewData[8], rightViewData[9], rightViewData[10], rightViewData[11]));
  249. rightViewMatrix.SetColumn(3, new Vector4(rightViewData[12], rightViewData[13], rightViewData[14], rightViewData[15]));
  250. // Debug.Log($"leftViewMatrix [{leftViewMatrix.m00}, {leftViewMatrix.m01}, {leftViewMatrix.m02}, {leftViewMatrix.m03};" +
  251. //$"{leftViewMatrix.m10}, {leftViewMatrix.m11}, {leftViewMatrix.m12}, {leftViewMatrix.m13};" +
  252. //$"{leftViewMatrix.m20}, {leftViewMatrix.m21}, {leftViewMatrix.m22}, {leftViewMatrix.m23};" +
  253. //$"{leftViewMatrix.m30}, {leftViewMatrix.m31}, {leftViewMatrix.m32}, {leftViewMatrix.m33}]");
  254. // Debug.Log($"rightViewMatrix [{rightViewMatrix.m00}, {rightViewMatrix.m01}, {rightViewMatrix.m02}, {rightViewMatrix.m03};" +
  255. //$"{rightViewMatrix.m10}, {rightViewMatrix.m11}, {rightViewMatrix.m12}, {rightViewMatrix.m13};" +
  256. //$"{rightViewMatrix.m20}, {rightViewMatrix.m21}, {rightViewMatrix.m22}, {rightViewMatrix.m23};" +
  257. //$"{rightViewMatrix.m30}, {rightViewMatrix.m31}, {rightViewMatrix.m32}, {rightViewMatrix.m33}]");
  258. headPose.orientation.x = RData[0];
  259. headPose.orientation.y = RData[1];
  260. headPose.orientation.z = RData[2];
  261. headPose.orientation.w = RData[3];
  262. headPose.position.x = TData[0];
  263. headPose.position.y = TData[1];
  264. headPose.position.z = TData[2];
  265. }
  266. headPose.orientation.z = -headPose.orientation.z;
  267. headPose.position.x = -headPose.position.x;
  268. headPose.position.y = -headPose.position.y;
  269. return rv;
  270. }
  271. public override DeviceInfo GetDeviceInfo()
  272. {
  273. DeviceInfo info = new DeviceInfo();
  274. GSXRGetDeviceInfo (ref info.displayWidthPixels,
  275. ref info.displayHeightPixels,
  276. ref info.displayRefreshRateHz,
  277. ref info.targetEyeWidthPixels,
  278. ref info.targetEyeHeightPixels,
  279. ref info.targetFovXRad,
  280. ref info.targetFovYRad,
  281. ref info.targetFrustumLeft.left, ref info.targetFrustumLeft.right, ref info.targetFrustumLeft.bottom, ref info.targetFrustumLeft.top, ref info.targetFrustumLeft.near, ref info.targetFrustumLeft.far,
  282. ref info.targetFrustumRight.left, ref info.targetFrustumRight.right, ref info.targetFrustumRight.bottom, ref info.targetFrustumRight.top, ref info.targetFrustumRight.near, ref info.targetFrustumRight.far,
  283. ref info.targetFrustumConvergence, ref info.targetFrustumPitch);
  284. return info;
  285. }
  286. public override void SubmitFrame(int frameIndex, float fieldOfView, int frameType)
  287. {
  288. int i;
  289. int layerCount = 0;
  290. if (eyes != null) for (i = 0; i < eyes.Length; i++)
  291. {
  292. var eye = eyes[i];
  293. if (eyes[i].isActiveAndEnabled == false || eye.TextureId == 0 || eye.Side == 0) continue;
  294. if (eye.imageTransform != null && eye.imageTransform.gameObject.activeSelf == false) continue;
  295. GSXRSetupLayerData(layerCount, (int)eye.Side, eye.TextureId, eye.ImageType == GSXREye.eType.EglTexture ? 2 : 0, eye.layerDepth > 0 ? 0x0 : 0x2, eye.depthRTID);
  296. float[] lowerLeft = { eye.clipLowerLeft.x, eye.clipLowerLeft.y, eye.clipLowerLeft.z, eye.clipLowerLeft.w, eye.uvLowerLeft.x, eye.uvLowerLeft.y };
  297. float[] upperLeft = { eye.clipUpperLeft.x, eye.clipUpperLeft.y, eye.clipUpperLeft.z, eye.clipUpperLeft.w, eye.uvUpperLeft.x, eye.uvUpperLeft.y };
  298. float[] upperRight = { eye.clipUpperRight.x, eye.clipUpperRight.y, eye.clipUpperRight.z, eye.clipUpperRight.w, eye.uvUpperRight.x, eye.uvUpperRight.y };
  299. float[] lowerRight = { eye.clipLowerRight.x, eye.clipLowerRight.y, eye.clipLowerRight.z, eye.clipLowerRight.w, eye.uvLowerRight.x, eye.uvLowerRight.y };
  300. GSXRSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  301. layerCount++;
  302. }
  303. if (overlays != null) for (i = 0; i < overlays.Length; i++)
  304. {
  305. var overlay = overlays[i];
  306. if (overlay.isActiveAndEnabled == false || overlay.TextureId == 0 || overlay.Side == 0) continue;
  307. if (overlay.imageTransform != null && overlay.imageTransform.gameObject.activeSelf == false) continue;
  308. GSXRSetupLayerData(layerCount, (int)overlay.Side, overlay.TextureId, overlay.ImageType == GSXROverlay.eType.EglTexture ? 2 : 0, 0x1,0);
  309. float[] lowerLeft = { overlay.clipLowerLeft.x, overlay.clipLowerLeft.y, overlay.clipLowerLeft.z, overlay.clipLowerLeft.w, overlay.uvLowerLeft.x, overlay.uvLowerLeft.y };
  310. float[] upperLeft = { overlay.clipUpperLeft.x, overlay.clipUpperLeft.y, overlay.clipUpperLeft.z, overlay.clipUpperLeft.w, overlay.uvUpperLeft.x, overlay.uvUpperLeft.y };
  311. float[] upperRight = { overlay.clipUpperRight.x, overlay.clipUpperRight.y, overlay.clipUpperRight.z, overlay.clipUpperRight.w, overlay.uvUpperRight.x, overlay.uvUpperRight.y };
  312. float[] lowerRight = { overlay.clipLowerRight.x, overlay.clipLowerRight.y, overlay.clipLowerRight.z, overlay.clipLowerRight.w, overlay.uvLowerRight.x, overlay.uvLowerRight.y };
  313. GSXRSetupLayerCoords(layerCount, lowerLeft, lowerRight, upperLeft, upperRight);
  314. layerCount++;
  315. }
  316. for (i = layerCount; i < GSXRManager.RenderLayersMax; i++)
  317. {
  318. GSXRSetupLayerData(i, 0, 0, 0, 0,0);
  319. }
  320. GSXRSubmitFrameEventData(frameIndex, fieldOfView, frameType);
  321. Fps.tempALLWaitLockTime = Time.realtimeSinceStartup;
  322. IssueEvent (RenderEvent.SubmitFrame);
  323. Fps.ALLWaitLockTime = Mathf.Lerp(Fps.ALLWaitLockTime, 1000 * (Time.realtimeSinceStartup - Fps.tempALLWaitLockTime), Fps.lerp);
  324. }
  325. public override void Shutdown()
  326. {
  327. IssueEvent (RenderEvent.Shutdown);
  328. base.Shutdown();
  329. }
  330. public override bool PollEvent(ref GSXRManager.SlamEvent frameEvent)
  331. {
  332. uint[] dataBuffer = new uint[2];
  333. int dataCount = Marshal.SizeOf(frameEvent.eventData) / sizeof(uint);
  334. int eventType = 0;
  335. bool isEvent = GSXRPollEvent(ref eventType, ref frameEvent.deviceId, ref frameEvent.eventTimeStamp, dataCount, dataBuffer);
  336. frameEvent.eventType = (GSXRManager.slamEventType)(eventType);
  337. switch (frameEvent.eventType)
  338. {
  339. case GSXRManager.slamEventType.kEventThermal:
  340. //Debug.LogFormat("PollEvent: data {0} {1}", dataBuffer[0], dataBuffer[1]);
  341. frameEvent.eventData.thermal.zone = (GSXRManager.slamThermalZone)dataBuffer[0];
  342. frameEvent.eventData.thermal.level = (GSXRManager.slamThermalLevel)dataBuffer[1];
  343. break;
  344. }
  345. return isEvent;
  346. }
  347. //---------------------------------------------------------------------------------------------
  348. //Controller Apis
  349. //---------------------------------------------------------------------------------------------
  350. /// <summary>
  351. /// Controllers the start tracking.
  352. /// </summary>
  353. /// <returns>The start tracking.</returns>
  354. /// <param name="desc">Desc.</param>
  355. //---------------------------------------------------------------------------------------------
  356. public override int ControllerStartTracking(string desc)
  357. {
  358. return GSXRControllerStartTracking(desc);
  359. }
  360. /// <summary>
  361. /// Controllers the stop tracking.
  362. /// </summary>
  363. /// <param name="handle">Handle.</param>
  364. //---------------------------------------------------------------------------------------------
  365. public override void ControllerStopTracking(int handle)
  366. {
  367. GSXRControllerStopTracking(handle);
  368. }
  369. /// <summary>
  370. /// Dumps the state.
  371. /// </summary>
  372. /// <param name="state">State.</param>
  373. //---------------------------------------------------------------------------------------------
  374. private void dumpState(GSXRControllerState state)
  375. {
  376. String s = "{" + state.rotation + "}\n";
  377. s += "[" + state.position + "]\n";
  378. s += "<" + state.timestamp + ">\n";
  379. Debug.Log (s);
  380. }
  381. /// <summary>
  382. /// Controllers the state of the get.
  383. /// </summary>
  384. /// <returns>The get state.</returns>
  385. /// <param name="handle">Handle.</param>
  386. //---------------------------------------------------------------------------------------------
  387. public override GSXRControllerState ControllerGetState(int handle, int space)
  388. {
  389. GSXRControllerState state = new GSXRControllerState();
  390. GSXRControllerGetState (handle, space, ref state);
  391. //dumpState (state);
  392. return state;
  393. }
  394. /// <summary>
  395. /// Controllers the send event.
  396. /// </summary>
  397. /// <param name="handle">Handle.</param>
  398. /// <param name="what">What.</param>
  399. /// <param name="arg1">Arg1.</param>
  400. /// <param name="arg2">Arg2.</param>
  401. //---------------------------------------------------------------------------------------------
  402. public override void ControllerSendMessage(int handle, GSXRController.slamControllerMessageType what, int arg1, int arg2)
  403. {
  404. GSXRControllerSendMessage (handle, (int)what, arg1, arg2);
  405. }
  406. /// <summary>
  407. /// Controllers the query.
  408. /// </summary>
  409. /// <returns>The query.</returns>
  410. /// <param name="handle">Handle.</param>
  411. /// <param name="what">What.</param>
  412. /// <param name="mem">Mem.</param>
  413. /// <param name="size">Size.</param>
  414. //---------------------------------------------------------------------------------------------
  415. public override object ControllerQuery(int handle, GSXRController.slamControllerQueryType what)
  416. {
  417. int memorySize = 0;
  418. IntPtr memory = IntPtr.Zero;
  419. object result = null;
  420. System.Type typeOfObject = null;
  421. switch(what)
  422. {
  423. case GSXRController.slamControllerQueryType.kControllerBatteryRemaining:
  424. {
  425. typeOfObject = typeof(int);
  426. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  427. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  428. }
  429. break;
  430. case GSXRController.slamControllerQueryType.kControllerControllerCaps:
  431. {
  432. typeOfObject = typeof(SlamControllerCaps);
  433. memorySize = System.Runtime.InteropServices.Marshal.SizeOf (typeOfObject);
  434. memory = System.Runtime.InteropServices.Marshal.AllocHGlobal (memorySize);
  435. }
  436. break;
  437. }
  438. int writtenBytes = GSXRControllerQuery (handle, (int)what, memory, memorySize);
  439. if (memorySize == writtenBytes) {
  440. result = System.Runtime.InteropServices.Marshal.PtrToStructure (memory, typeOfObject);
  441. }
  442. if (memory != IntPtr.Zero) {
  443. Marshal.FreeHGlobal (memory);
  444. }
  445. return result;
  446. }
  447. [DllImport(DLLName)]
  448. public static extern void GSXR_setOnFloorOrOnHead(int type);
  449. public override void GSXR_Set_OnFloorOrOnHead(int type)
  450. {
  451. GSXR_setOnFloorOrOnHead(type);
  452. }
  453. #region Controller
  454. [DllImport(DLLName)]
  455. public static extern bool GSXRIsSupportController();
  456. [DllImport(DLLName)]
  457. public static extern int GSXRControllerMode();
  458. [DllImport(DLLName)]
  459. public static extern int GSXRGetControllerNum();
  460. [DllImport(DLLName)]
  461. public static extern void GSXRSetControllerKeyEventCallback(OnKeyEvent _event);
  462. [DllImport(DLLName)]
  463. public static extern void GSXR_Unity_SetControllerTouchPanelCallback(OnTouchPanelEvent _event);
  464. [DllImport(DLLName)]
  465. public static extern void GSXRSetControllerKeyTouchEventCallback(OnKeyTouchEvent _event);
  466. [DllImport(DLLName)]
  467. public static extern void GSXRSetControllerRockerCallback(OnTouchEvent _event);
  468. [DllImport(DLLName)]
  469. public static extern void GSXRSetControllerTriggerCallback(OnHallEvent _event);
  470. [DllImport(DLLName)]
  471. public static extern void GSXRSetControllerChargingEventCallback(OnChargingEvent _event);
  472. [DllImport(DLLName)]
  473. public static extern void GSXRSetControllerBatteryEventCallback(OnBatteryEvent _event);
  474. [DllImport(DLLName)]
  475. public static extern void GSXRSetControllerConnectEventCallback(OnConnectEvent _event);
  476. [DllImport(DLLName)]
  477. public static extern void GSXRSetControllerBondEventCallback(OnBondStateEvent _event);
  478. [DllImport(DLLName)]
  479. public static extern void GSXRSetControllerHallRangeCallback(OnHallRangeEvent _event);
  480. [DllImport(DLLName)]
  481. public static extern int GSXRGetControllerBattery(int lr);
  482. [DllImport(DLLName)]
  483. public static extern float GSXRGetControllerHSVersion(int lr);
  484. [DllImport(DLLName)]
  485. public static extern int GSXRGetControllerList();
  486. [DllImport(DLLName)]
  487. public static extern bool GSXRIsControllerConnect(int lr);
  488. [DllImport(DLLName)]
  489. public static extern void GSXRSetControllerLed(int enable);
  490. [DllImport(DLLName)]
  491. public static extern void GSXRSetControllerVibrate(int value);
  492. [DllImport(DLLName)]
  493. public static extern int GSXRGetControllerPosture(float[] outOrientationArray, int lr);
  494. [DllImport(DLLName)]
  495. public static extern int GSXRGetControllerVibrateState(int lr);
  496. [DllImport(DLLName)]
  497. public static extern int GSXRGetControllerAccelerometer(float[] orientationArray, int lr);
  498. [DllImport(DLLName)]
  499. public static extern int GSXRGetControllerGyroscope(float[] orientationArray, int lr);
  500. [DllImport(DLLName)]
  501. public static extern void GSXRBindController(int lr, int type);
  502. [DllImport(DLLName)]
  503. public static extern void GSXRUnbindController(int lr, int type);
  504. [DllImport(DLLName)]
  505. public static extern int GSXRGetControllerBondState();
  506. [DllImport(DLLName)]
  507. public static extern int GSXR_nativeGetControllerAngleVelocity(float[] orientationArray, int lr);
  508. [DllImport(DLLName)]
  509. public static extern int GSXR_nativeGetControllerLinearVelocity(float[] orientationArray, int lr);
  510. public override bool GSXR_Is_SupportController() { return true;/* GSXRIsSupportController();*/ }
  511. public override int GSXR_Get_ControllerMode() { return 2;/* GSXRControllerMode();*/ }
  512. public override int GSXR_Get_ControllerNum() { return 2;/*GSXRGetControllerNum();*/ }
  513. public override void GSXR_Set_ControllerKeyEventCallback(OnKeyEvent _event) { GSXRSetControllerKeyEventCallback(_event); }
  514. public override void GSXR_Set_ControllerTouchPanelCallback(OnTouchPanelEvent _event) { GSXR_Unity_SetControllerTouchPanelCallback(_event); }
  515. public override void GSXR_Set_ControllerKeyTouchEventCallback(OnKeyTouchEvent _event) { GSXRSetControllerKeyTouchEventCallback(_event); }
  516. public override void GSXR_Set_ControllerRockerCallback(OnTouchEvent _event) { GSXRSetControllerRockerCallback(_event); }
  517. public override void GSXR_Set_ControllerHallCallback(OnHallEvent _event) { GSXRSetControllerTriggerCallback(_event); }
  518. public override void GSXR_Set_ControllerChargingEventCallback(OnChargingEvent _event) { GSXRSetControllerChargingEventCallback(_event); }
  519. public override void GSXR_Set_ControllerBatteryEventCallback(OnBatteryEvent _event) { GSXRSetControllerBatteryEventCallback(_event); }
  520. public override void GSXR_Set_ControllerConnectEventCallback(OnConnectEvent _event) { GSXRSetControllerConnectEventCallback(_event); }
  521. public override void GSXR_Set_ControllerBondEventCallback(OnBondStateEvent _event) { GSXRSetControllerBondEventCallback(_event); }
  522. public override void GSXR_Set_ControllerHallRangeCallback(OnHallRangeEvent _event) { GSXRSetControllerHallRangeCallback(_event); }
  523. public override int GSXR_Get_ControllerBattery(int lr) { return 90;/* GSXRGetControllerBattery(lr); */
  524. }
  525. public override float GSXR_Get_ControllerHSVersion(int lr) { return 1f;/* GSXRGetControllerHSVersion(lr);*/ }
  526. public override int GSXR_Get_ControllerList() { return (int)SC.XR.Unity.Module_InputSystem.InputDeviceGC.KS.KSID.K102;/* GSXRGetControllerList(); */}
  527. public override bool GSXR_Is_ControllerConnect(int lr) { return true;/* GSXRIsControllerConnect(lr); */}
  528. public override void GSXR_Set_ControllerLed(int lr) { GSXRSetControllerLed(lr); }
  529. [Obsolete]
  530. public override void GSXR_Set_ControllerVibrate(int value) { GSXRSetControllerVibrate(value); }
  531. bool LeftVibrateisOn = false;
  532. bool RightVibrateisOn = false;
  533. int CurrenVibrateisOntState = 0;
  534. public override int GSXR_Set_ControllerVibrate(int lr,bool isOn) {
  535. CurrenVibrateisOntState = 0;
  536. if (lr == 0) {
  537. LeftVibrateisOn = isOn;
  538. CurrenVibrateisOntState = LeftVibrateisOn ? 1 : 0;
  539. CurrenVibrateisOntState = CurrenVibrateisOntState | (RightVibrateisOn ? 1 << 2 : 0 << 2);
  540. } else if (lr == 1) {
  541. RightVibrateisOn = isOn;
  542. CurrenVibrateisOntState = RightVibrateisOn ? 1 << 2 : 0 << 2;
  543. CurrenVibrateisOntState = CurrenVibrateisOntState | (LeftVibrateisOn ? 1 : 0);
  544. }
  545. //Debug.Log("GSXR_Set_ControllerVibrate:" + lr + " CurrenVibrateisOntState:" + CurrenVibrateisOntState);
  546. GSXRSetControllerVibrate(CurrenVibrateisOntState);
  547. return CurrenVibrateisOntState;
  548. }
  549. public override bool GSXR_Get_ControllerVibrateStatus(int lr)
  550. {
  551. if (lr == 0)
  552. {
  553. return LeftVibrateisOn;
  554. }
  555. else if (lr == 1)
  556. {
  557. return RightVibrateisOn;
  558. }
  559. return false;
  560. }
  561. public override int GSXR_Set_ControllerVibrate(int lr, bool isOn, int amplitude, int frequency, int time)
  562. {
  563. CurrenVibrateisOntState = 0;
  564. CurrenVibrateisOntState = lr;
  565. CurrenVibrateisOntState |= (isOn ? 1 : 0) << 2;
  566. CurrenVibrateisOntState |= (time << 3);
  567. CurrenVibrateisOntState |= (frequency << 8);
  568. CurrenVibrateisOntState |= (amplitude << 12);
  569. //Debug.Log("GSXR_Set_ControllerVibrate:" + lr + " CurrenVibrateisOntState:" + CurrenVibrateisOntState);
  570. GSXRSetControllerVibrate(CurrenVibrateisOntState);
  571. return CurrenVibrateisOntState;
  572. }
  573. public override int GSXR_Get_ControllerVibrateState(int lr)
  574. {
  575. return GSXRGetControllerVibrateState(lr);
  576. }
  577. public override int GSXR_Get_ControllerPosture(float[] outOrientationArray, int lr) {return GSXRGetControllerPosture(outOrientationArray, lr);}
  578. public override int GSXR_Get_ControllerAccelerometer(float[] orientationArray, int lr) { return GSXRGetControllerAccelerometer(orientationArray, lr); }
  579. public override int GSXR_Get_ControllerGyroscope(float[] orientationArray, int lr) { return GSXRGetControllerGyroscope(orientationArray, lr); }
  580. public override void GSXR_Bind_Controller(int lr, int type) { GSXRBindController(lr, type); }
  581. public override void GSXR_Unbind_Controller(int lr, int type) { GSXRUnbindController(lr, type); }
  582. public override int GSXR_Get_ControllerBondState() { return GSXRGetControllerBondState(); }
  583. public override int GSXR_Get_ControllerAngleVelocity(float[] orientationArray, int lr) { return GSXR_nativeGetControllerAngleVelocity(orientationArray,lr); }
  584. public override int GSXR_Get_ControllerLinearVelocity(float[] orientationArray, int lr) { return GSXR_nativeGetControllerLinearVelocity(orientationArray,lr); }
  585. #endregion Controller
  586. #region HandTracking
  587. [DllImport(DLLName)]
  588. private static extern bool GSXRIsSupportHandTracking();
  589. [DllImport(DLLName)]
  590. protected static extern void GSXRStartGesture(Action<int> func);
  591. [DllImport(DLLName)]
  592. public static extern void GSXRStopGesture();
  593. [DllImport(DLLName)]
  594. private static extern void GSXRSetGestureCallback(Action<int> func);
  595. [DllImport(DLLName)]
  596. private static extern int GSXRGetHandTrackingData(float[] model, float[] pose);
  597. //for test
  598. [DllImport(DLLName)]
  599. private static extern int GSXRSetGestureData(float[] model, float[] pose);
  600. [DllImport(DLLName)]
  601. private static extern int ScHANDTRACK_GetGestureWithIdx(ref ulong index, float[] model, float[] pose);
  602. [DllImport(DLLName)]
  603. private static extern int GSXRSetGestureModelCallback(Action func);
  604. [DllImport(DLLName)]
  605. protected static extern void GSXRSetHandTrackingLowPowerWarningCallback(Action<int> func);
  606. public override bool GSXR_Is_SupportHandTracking() {
  607. return false;/* GSXRIsSupportHandTracking();*/
  608. }
  609. public override void GSXR_StartHandTracking(Action<int> func) {
  610. GSXRStartGesture(func);
  611. }
  612. public override void GSXR_StopHandTracking() {
  613. GSXRStopGesture();
  614. }
  615. public override void GSXR_Get_HandTrackingData(float[] mode, float[] pose) {
  616. GSXRGetHandTrackingData(mode, pose);
  617. }
  618. public override int GSXR_Set_HandTrackingData(float[] mode, float[] pose) {
  619. return GSXRSetGestureData(mode, pose);
  620. }
  621. public override int GSXR_Get_HandTrackingGestureIdx(ref ulong index, float[] model, float[] pose) {
  622. return ScHANDTRACK_GetGestureWithIdx(ref index, model, pose);
  623. }
  624. public override void GSXR_Set_HandTrackingCallBack(Action<int> callback) {
  625. GSXRSetGestureCallback(callback);
  626. }
  627. public override int GSXR_Set_HandTrackingModelDataCallBack(Action callback) {
  628. return GSXRSetGestureModelCallback(callback);
  629. }
  630. public override void GSXR_Set_HandTrackingLowPowerWarningCallback(Action<int> func) {
  631. GSXRSetHandTrackingLowPowerWarningCallback(func);
  632. }
  633. #endregion HandTracking
  634. #region Deflection
  635. [DllImport(DLLName)]
  636. private static extern float GSXRFetchDeflection();
  637. [DllImport(DLLName)]
  638. private static extern void GSXRUpdateRelativeDeflection(int deflection);
  639. public override float GSXR_Get_Deflection() {
  640. return GSXRFetchDeflection();
  641. }
  642. public override void GSXR_Set_RelativeDeflection(int deflection) {
  643. GSXRUpdateRelativeDeflection(deflection);
  644. }
  645. #endregion Deflection
  646. #region PointCloud & Map
  647. [DllImport(DLLName)]
  648. private static extern bool GSXRSupportMap();
  649. [DllImport(DLLName)]
  650. private static extern bool GSXRSupportPanel();
  651. [DllImport(DLLName)]
  652. private static extern int GSXRGetPointCloudData(ref int dataNum,ref ulong dataTimestamp,float[] dataArray);
  653. [DllImport(DLLName)]
  654. private static extern int GSXRGetOfflineMapRelocState();
  655. /// <summary>
  656. /// 将地图信息保存在path里,是ply格式文件,存的是点云信息
  657. /// </summary>
  658. /// <param name="path"></param>
  659. /// <returns></returns>
  660. [DllImport(DLLName)]
  661. private static extern int GSXRResaveMap(string path);
  662. /// <summary>
  663. /// 保存地图
  664. /// </summary>
  665. [DllImport(DLLName)]
  666. private static extern void GSXRSaveMap();
  667. /// <summary>
  668. /// 发送通用命令
  669. /// </summary>
  670. /// <param name="command"></param>
  671. /// <param name="args"></param>
  672. /// <param name="arglen"></param>
  673. [DllImport(DLLName)]
  674. private static extern void GSXR_Unity_Command(int command, float[] args, int arglen);
  675. [DllImport(DLLName)]
  676. private static extern int ScGetGnss(ref double dt, float[] gnss);
  677. /// <summary>
  678. /// Get the identified plane
  679. /// </summary>
  680. /// <returns></returns>
  681. [DllImport(DLLName)]
  682. private static extern int GSXRGetPanelNum();
  683. /// <summary>
  684. /// Get the plane Infomations
  685. /// 1:id
  686. /// 2:nPoints
  687. /// 3: 20个点的坐标 :如果没20个点,后面为空
  688. /// 4: createPoint 平面顶点
  689. /// 5: normal 法向量
  690. /// </summary>
  691. /// <param name="info"></param>
  692. /// <returns></returns>
  693. [DllImport(DLLName)]
  694. private static extern int GSXRGetPlaneData(float[] info);
  695. public override void GSXR_Send_Unity_Command(int command, float[] args, int arglen)
  696. {
  697. GSXR_Unity_Command(command, args, arglen);
  698. }
  699. public override bool GSXR_Support_Map() {
  700. return false;/* GSXRSupportMap();*/
  701. }
  702. public override bool GSXR_Support_Panel() {
  703. return false;/*GSXRSupportPanel();*/
  704. }
  705. public override int GSXR_Get_PointCloudData(ref int dataNum, ref ulong dataTimestamp, float[] dataArray) {
  706. return GSXRGetPointCloudData(ref dataNum, ref dataTimestamp, dataArray);
  707. }
  708. private float[] commandArgs = new float[32];
  709. public override int GSXR_Get_OfflineMapRelocState() {
  710. GSXR_Unity_Command(0, commandArgs, 32);
  711. return (int)commandArgs[4];
  712. }
  713. public override int GSXR_ResaveMap(string path) {
  714. return GSXRResaveMap(path);
  715. }
  716. public override void GSXR_SaveMap() {
  717. GSXRSaveMap();
  718. }
  719. public override int GSXR_Get_Gnss(ref double dt, float[] gnss) {
  720. return ScGetGnss(ref dt, gnss);
  721. }
  722. public override int GSXR_Get_PanelNum() {
  723. return GSXRGetPanelNum();
  724. }
  725. public override int GSXR_Get_PanelInfo(float[] info) {
  726. return GSXRGetPlaneData(info);
  727. }
  728. #endregion PointCloud & Map
  729. #region FishEye Data
  730. [DllImport(DLLName)]
  731. public static extern int GSXRGetFishEyeInfo(ref int num, ref int outWidth, ref int outHeight);
  732. [DllImport(DLLName)]
  733. public static extern void GSXRGetLatestCameraFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  734. byte[] outFrameData, float[] outTRDataArray);
  735. [DllImport(DLLName)]
  736. public static extern void GSXRGetLatestCameraFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  737. byte[] outFrameData, float[] outTRDataArray);
  738. [DllImport(DLLName)]
  739. private static extern int GSXRGetLatestCameraBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,
  740. byte[] outLeftFrameData, byte[] outRightFrameData);
  741. //public override int GSXR_Get_FishEyeInfo(ref int num, ref int outWidth, ref int outHeight) {
  742. // return GSXRGetFishEyeInfo(ref num, ref outWidth, ref outHeight);
  743. //}
  744. public override void GSXR_Get_LatestFishEyeFrameData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outFrameData, float[] outTRDataArray) {
  745. GSXRGetLatestCameraFrameData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  746. if (outBUdate == false) {
  747. Debug.LogError("Error: Please Check Slamconfig prop: gUseXXXCamera = true");
  748. }
  749. }
  750. public override void GSXR_Get_LatestFishEyeFrameDataNoTransform(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano,byte[] outFrameData, float[] outTRDataArray) {
  751. GSXRGetLatestCameraFrameDataNoTransform(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outFrameData, outTRDataArray);
  752. if (outBUdate == false) {
  753. Debug.LogError("Error: Please Check Slamconfig prop: gUseXXXCamera = true");
  754. }
  755. }
  756. public override int GSXR_Get_LatestFishEyeBinocularData(ref bool outBUdate, ref uint outCurrFrameIndex, ref ulong outFrameExposureNano, byte[] outLeftFrameData, byte[] outRightFrameData) {
  757. return GSXRGetLatestCameraBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  758. }
  759. #endregion FishEye Data
  760. #region Optics Calibration
  761. [DllImport(DLLName)]
  762. private static extern bool GSXRIsSupportOpticsCalibration();
  763. public override bool GSXR_Is_SupportOpticsCalibration() {
  764. return false;/* GSXRIsSupportOpticsCalibration();*/
  765. }
  766. [DllImport(DLLName)]
  767. private static extern int GSXRGetTransformMatrix(ref bool outBLoaded, float[] outTransformArray);
  768. public override int GSXR_Get_TransformMatrix(ref bool outBLoaded, float[] outTransformArray) {
  769. return GSXRGetTransformMatrix(ref outBLoaded, outTransformArray);
  770. }
  771. [DllImport(DLLName)]
  772. private static extern int GSXRGetLatestEyeMatrices(float[] outLeftEyeMatrix,
  773. float[] outRightEyeMatrix,
  774. float[] outT,
  775. float[] outR,
  776. int frameIndex,
  777. bool isMultiThreadedRender);
  778. public override int GSXR_Get_LatestEyeMatrices(float[] outLeftEyeMatrix, float[] outRightEyeMatrix, float[] outT, float[] outR, int frameIndex, bool isMultiThreadedRender) {
  779. return GSXRGetLatestEyeMatrices(outLeftEyeMatrix, outRightEyeMatrix, outT, outR, frameIndex, isMultiThreadedRender);
  780. }
  781. #endregion Optics Calibration
  782. #region EyeTracking
  783. [DllImport(DLLName)]
  784. private static extern bool GSXRSupportEyeTracking();
  785. public override bool GSXR_Support_EyeTracking() { return false;/* GSXRSupportEyeTracking();*/ }
  786. [DllImport(DLLName)]
  787. private static extern int GSXRStartEyeTracking();
  788. public override int GSXR_Start_EyeTracking() { return GSXRStartEyeTracking(); }
  789. [DllImport(DLLName)]
  790. private static extern int GSXRStopEyeTracking();
  791. public override int GSXR_Stop_EyeTracking() { return GSXRStopEyeTracking(); }
  792. [DllImport(DLLName)]
  793. private static extern int GSXRGetEyeFocus(ref float outFocusX, ref float outFocusY);
  794. public override int GSXR_Get_EyeFocus(ref float outFocusX, ref float outFocusY) { return GSXRGetEyeFocus(ref outFocusX,ref outFocusY); }
  795. [DllImport(DLLName)]
  796. private static extern int GSXRGetEyePose(ref int leftStatus,
  797. ref int rightStatus,
  798. ref int combinedStatus,
  799. ref float leftOpenness,
  800. ref float rightOpenness,
  801. ref float leftDirectionX,
  802. ref float leftDirectionY,
  803. ref float leftDirectionZ,
  804. ref float leftPositionX,
  805. ref float leftPositionY,
  806. ref float leftPositionZ,
  807. ref float rightDirectionX,
  808. ref float rightDirectionY,
  809. ref float rightDirectionZ,
  810. ref float rightPositionX,
  811. ref float rightPositionY,
  812. ref float rightPositionZ,
  813. ref float combinedDirectionX,
  814. ref float combinedDirectionY,
  815. ref float combinedDirectionZ,
  816. ref float combinedPositionX,
  817. ref float combinedPositionY,
  818. ref float combinedPositionZ,
  819. int frameIndex);
  820. public override int GSXR_Get_EyePose(ref EyePose eyePose, int frameIndex = -1) {
  821. int rv = GSXRGetEyePose(
  822. ref eyePose.leftStatus, ref eyePose.rightStatus, ref eyePose.combinedStatus,
  823. ref eyePose.leftOpenness, ref eyePose.rightOpenness,
  824. ref eyePose.leftDirection.x, ref eyePose.leftDirection.y, ref eyePose.leftDirection.z,
  825. ref eyePose.leftPosition.x, ref eyePose.leftPosition.y, ref eyePose.leftPosition.z,
  826. ref eyePose.rightDirection.x, ref eyePose.rightDirection.y, ref eyePose.rightDirection.z,
  827. ref eyePose.rightPosition.x, ref eyePose.rightPosition.y, ref eyePose.rightPosition.z,
  828. ref eyePose.combinedDirection.x, ref eyePose.combinedDirection.y, ref eyePose.combinedDirection.z,
  829. ref eyePose.combinedPosition.x, ref eyePose.combinedPosition.y, ref eyePose.combinedPosition.z,
  830. frameIndex);
  831. return rv;
  832. }
  833. #endregion EyeTracking
  834. #region USBDisconnect
  835. [DllImport(DLLName)]
  836. public static extern void GSXRSetAppExit(Action callBack);
  837. public override void GSXR_Set_GlassDisconnectedCallBack(Action callBack) {
  838. GSXRSetAppExit(callBack);
  839. }
  840. #endregion
  841. #region luncher
  842. [DllImport(DLLName)]
  843. public static extern int ScInitLayer();
  844. [DllImport(DLLName)]
  845. public static extern int ScStartLayerRendering();
  846. [DllImport(DLLName)]
  847. public static extern int ScGetAllLayersData(ref GSXRManager.SCAllLayers outAllLayers);
  848. [DllImport(DLLName)]
  849. public static extern int ScEndLayerRendering(ref GSXRManager.SCAllLayers allLayers);
  850. [DllImport(DLLName)]
  851. public static extern int ScDestroyLayer();
  852. [DllImport(DLLName)]
  853. public static extern int ScUpdateModelMatrix(UInt32 layerId, float[] modelMatrixArray);
  854. [DllImport(DLLName)]
  855. public static extern int ScSendActionBarCMD(UInt32 layerId, int cmd);
  856. [DllImport(DLLName)]
  857. public static extern int ScSetForcedDisplaySize(int width, int height);
  858. [DllImport(DLLName)]
  859. public static extern int ScInjectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y);
  860. public override int initLayer() {
  861. return ScInitLayer();
  862. }
  863. public override int startLayerRendering() {
  864. IssueEvent(RenderEvent.StartLayerRendering);
  865. return ScStartLayerRendering();
  866. }
  867. public override int getAllLayersData(ref GSXRManager.SCAllLayers outAllLayers) {
  868. return ScGetAllLayersData(ref outAllLayers);
  869. }
  870. public override int endLayerRendering(ref GSXRManager.SCAllLayers allLayers) {
  871. return ScEndLayerRendering(ref allLayers);
  872. }
  873. public override int destroyLayer() {
  874. return ScDestroyLayer();
  875. }
  876. public override int updateModelMatrix(UInt32 layerId, float[] modelMatrixArray) {
  877. return ScUpdateModelMatrix(layerId, modelMatrixArray);
  878. }
  879. public override int sendActionBarCMD(UInt32 layerId, int cmd) {
  880. return ScSendActionBarCMD(layerId, cmd);
  881. }
  882. public override int setForcedDisplaySize(int width, int height)
  883. {
  884. return ScSetForcedDisplaySize(width, height);
  885. }
  886. public override int injectMotionEvent(UInt32 layerId, int displayID, int action, float x, float y) {
  887. return ScInjectMotionEvent(layerId, displayID, action, x, y);
  888. }
  889. #endregion
  890. #region Device
  891. [DllImport(DLLName)]
  892. private static extern int GSXR_Unity_SetMeshOffset(int type, float value);
  893. [DllImport(DLLName)]
  894. private static extern float GSXR_Unity_GetMeshOffset(int type);
  895. public override int GSXR_SetMeshOffset(int type, float value) {
  896. return GSXR_Unity_SetMeshOffset(type, value);
  897. }
  898. public override float GSXR_GetMeshOffset(int type) {
  899. return GSXR_Unity_GetMeshOffset(type);
  900. }
  901. [DllImport(DLLName)]
  902. private static extern int GSXR_Unity_Set6Dof(bool is6dof);
  903. public override int GSXR_Set_6Dof(bool is6dof) {
  904. return 0;// GSXR_Unity_Set6Dof(is6dof);
  905. }
  906. [DllImport(DLLName)]
  907. private static extern ulong GSXRGetSupportSlamMode();
  908. public override ulong GSXR_Get_SupportSlamMode() {
  909. return GSXRGetSupportSlamMode();
  910. }
  911. [DllImport(DLLName)]
  912. public static extern float GSXRGetControllerVersion();
  913. public override float GSXR_Get_ControllerVersion() { return 1;/* GSXRGetControllerVersion();*/
  914. }
  915. [DllImport(DLLName)]
  916. private static extern void GSXR_Unity_GetVersion(ref string version);
  917. public override string GSXR_Get_Version()
  918. {
  919. //string version = "0.0.0";///must init for malloc memory
  920. //GSXR_Unity_GetVersion(ref version);
  921. return "0.0.0";
  922. }
  923. [DllImport(DLLName)]
  924. private static extern int GSXRGetXRType();
  925. [DllImport(DLLName)]
  926. private static extern void GSXR_Unity_GetDeviceName(ref string deviceName);
  927. //public override string GSXR_Get_DeviceName() {
  928. // string devicename = "0000000000000000000000000000";//must init for malloc memory
  929. // //GSXR_Unity_GetDeviceName(ref devicename);
  930. // return devicename;
  931. //}
  932. public override XRType GSXR_Get_XRType() { return XRType.VR;/* (XRType)GSXRGetXRType();*/ }
  933. #endregion
  934. #region Seethrough
  935. private Action<bool> onSeeThrough;
  936. [DllImport(DLLName)]
  937. public static extern void GSXR_Unity_StartSeeThrough();
  938. [DllImport(DLLName)]
  939. public static extern void GSXR_Unity_StopSeeThrough();
  940. public override void GSXR_StartSeeThrough()
  941. {
  942. this.onSeeThrough?.Invoke(true);
  943. GSXR_Unity_StartSeeThrough();
  944. }
  945. public override void GSXR_StopSeeThrough()
  946. {
  947. GSXR_Unity_StopSeeThrough();
  948. this.onSeeThrough?.Invoke(false);
  949. }
  950. public override void GSXR_Regist_SeeThrough_Callback(Action<bool> callback) { this.onSeeThrough += callback; }
  951. public override void GSXR_UnRegist_SeeThrough_Callbak(Action<bool> callback) { this.onSeeThrough -= callback; }
  952. #endregion
  953. }