SoftwareTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using SC.XR.Unity;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using TMPro;
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using static API_GSXR_Slam;
  10. public class SoftwareTest : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private TextMeshProUGUI state_36Dof;
  14. [SerializeField]
  15. private TextMeshProUGUI state_isSlamRunning;
  16. [SerializeField]
  17. private TextMeshProUGUI state_isSlamSlamInitialized;
  18. [SerializeField]
  19. private TextMeshProUGUI action_InitializedCallBack;
  20. [SerializeField]
  21. private TextMeshProUGUI state_isSlamDataLost;
  22. [SerializeField]
  23. private TextMeshProUGUI state_SlamPosition;
  24. [SerializeField]
  25. private TextMeshProUGUI state_SlamRotation;
  26. [SerializeField]
  27. private RawImage renderTexture0;
  28. [SerializeField]
  29. private RawImage renderTexture1;
  30. [SerializeField]
  31. private RawImage FishEyeCamera0;
  32. [SerializeField]
  33. private RawImage FishEyeCamera1;
  34. [SerializeField]
  35. private int frameRate;
  36. private TrackMode trackMode = TrackMode.Mode_6Dof;
  37. List<RenderTexture> renderTextures;
  38. private bool render_Running = false;
  39. private bool isRenderCreated = false;
  40. private int onFloorOrOnHead = 0;
  41. Texture2D textureTemp;
  42. bool isPreview = false;
  43. int imageWidth;
  44. int imageHeight;
  45. bool outBUdate = true;
  46. uint outCurrFrameIndex = 0;
  47. ulong outFrameExposureNano = 0;
  48. byte[] outLeftFrameData;
  49. byte[] outRightFrameData;
  50. TextureFormat textureFormat = TextureFormat.Alpha8;
  51. private const string project_name = "SoftwareTest:";
  52. /// <summary>
  53. /// 设置渲染帧率
  54. /// 添加Slam初始化完成时的回调
  55. /// 添加按键按下回调
  56. /// </summary>
  57. private void Start()
  58. {
  59. GSXR_Set_RenderFrame(frameRate);
  60. GSXR_Add_InitializedCallBack(Action_InitializedCallBack);
  61. GSXR_Add_InitializedCallBack(Init);
  62. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateRegister(KeyDown);
  63. }
  64. /// <summary>
  65. /// 按下左手柄按钮触发事件
  66. /// X键启动Slam
  67. /// Y键停止Slam
  68. /// LFunction重置Slam
  69. /// </summary>
  70. /// <param name="keyCode"></param>
  71. /// <param name="part"></param>
  72. private void KeyDown(InputKeyCode keyCode, InputDevicePartBase part)
  73. {
  74. if (keyCode == InputKeyCode.LFunction && part.PartType == InputDevicePartType.KSLeft)
  75. {
  76. Slam_Reset();
  77. }
  78. }
  79. /// <summary>
  80. /// 3/6Dof模式
  81. /// Slam系统是否在运行
  82. /// Slam系统是否初始化完成
  83. /// 获取左右眼瞳距
  84. /// Slam数据是否丢失
  85. /// Slam头部数据
  86. /// </summary>
  87. private void Update()
  88. {
  89. //if (SlamManager == null || !GSXR_Is_SlamRunning())
  90. //{
  91. // return;
  92. //}
  93. state_36Dof.text = "TrackMode:" + trackMode;
  94. state_isSlamRunning.text = "SlamIsRunning:" + GSXR_Is_SlamRunning();
  95. state_isSlamSlamInitialized.text = "SlamIsSlamInitialized:" + GSXR_Is_SlamInitialized();
  96. state_isSlamDataLost.text = "IsSlamDataLost:" + GSXR_Is_SlamDataLost;
  97. state_SlamPosition.text =Camera.main.transform.position.ToString("F3");
  98. state_SlamRotation.text =Camera.main.transform.rotation.ToString("F3");
  99. }
  100. private void LateUpdate()
  101. {
  102. if (isPreview)
  103. {
  104. ShowCamera();
  105. }
  106. else
  107. {
  108. FishEyeCamera0.texture = null;
  109. FishEyeCamera1.texture = null;
  110. }
  111. }
  112. /// <summary>
  113. /// 取消Slam初始化完成时的回调
  114. /// 取消按键按下回调
  115. /// 释放render
  116. /// </summary>
  117. private void OnDestroy()
  118. {
  119. GSXR_Remove_InitializedCallBack(Action_InitializedCallBack);
  120. API_GSXR_Module_InputSystem.GSXR_KeyDownDelegateUnRegister(KeyDown);
  121. if (isRenderCreated)
  122. {
  123. renderTextures[0].Release();
  124. renderTextures[1].Release();
  125. }
  126. GSXR_Remove_InitializedCallBack(Init);
  127. textureTemp = null;
  128. outCurrFrameIndex = 0;
  129. outFrameExposureNano = 0;
  130. outLeftFrameData = null;
  131. outRightFrameData = null;
  132. }
  133. /// <summary>
  134. /// 设置Slam初始化完成时的回调
  135. /// </summary>
  136. private void Action_InitializedCallBack()
  137. {
  138. action_InitializedCallBack.text = "GSXR_Add_InitializedCallBack succeed.";
  139. Debug.Log(project_name + "GSXR_Add_InitializedCallBack succeed.");
  140. }
  141. /// <summary>
  142. /// 切换6Dof/3Dof
  143. /// </summary>
  144. public void Set_3DofOR6Dof()
  145. {
  146. trackMode = trackMode == TrackMode.Mode_6Dof ? TrackMode.Mode_3Dof : TrackMode.Mode_6Dof;
  147. GSXR_Set_TrackMode(trackMode);
  148. Debug.Log(project_name + "Set trackMode to" + trackMode + ".");
  149. }
  150. /// <summary>
  151. /// 获取左右眼渲染画面
  152. /// </summary>
  153. public void Get_RenderTexure()
  154. {
  155. if (!isRenderCreated)
  156. {
  157. renderTextures = GSXR_Get_RenderTexure();
  158. isRenderCreated = true;
  159. Debug.Log(project_name + "Get RenderTexure.");
  160. }
  161. if (render_Running)
  162. {
  163. renderTexture0.texture = null;
  164. renderTexture1.texture = null;
  165. Debug.Log(project_name + "Clear render0 and render1.");
  166. }
  167. else
  168. {
  169. renderTexture0.texture = renderTextures[0];
  170. renderTexture1.texture = renderTextures[1];
  171. Debug.Log(project_name + "Show render0 and render1.");
  172. }
  173. render_Running = !render_Running;
  174. }
  175. /// <summary>
  176. /// 重定位
  177. /// </summary>
  178. public void RecenterTracking()
  179. {
  180. GSXR_RecenterTracking();
  181. Debug.Log(project_name + "GSXR_RecenterTracking.");
  182. }
  183. /// <summary>
  184. /// 重置SLAM
  185. /// </summary>
  186. public void Slam_Reset()
  187. {
  188. GSXR_Reset_Slam();
  189. Debug.Log(project_name + "GSXR_Reset_Slam.");
  190. }
  191. /// <summary>
  192. /// 以Head方式设置原点
  193. /// </summary>
  194. public void Set_HeadOrigin()
  195. {
  196. GSXR_Set_HeadOrigin(new Vector3(1, 0, 0));
  197. Debug.Log(project_name + "Set HeadOrigin by (1,0,0).");
  198. }
  199. /// <summary>
  200. /// 以World方式设置原点
  201. /// </summary>
  202. public void Set_WorldOrigin()
  203. {
  204. GSXR_Set_WorldOrigin(new Vector3(1, 0, 0));
  205. Debug.Log(project_name + "Set WorldOrigin by (1,0,0).");
  206. }
  207. public void Set_OnFloorOrOnHead()
  208. {
  209. if(onFloorOrOnHead==0)
  210. {
  211. GSXR_Set_OnFloorOrOnHead(0);
  212. Debug.Log(project_name + "GSXR_Set_OnFloorOrOnHead(0).");
  213. onFloorOrOnHead++;
  214. }
  215. else
  216. {
  217. GSXR_Set_OnFloorOrOnHead(1);
  218. Debug.Log(project_name + "GSXR_Set_OnFloorOrOnHead(1).");
  219. onFloorOrOnHead--;
  220. }
  221. }
  222. public void PreBtn()
  223. {
  224. isPreview = !isPreview;
  225. }
  226. void Init()
  227. {
  228. imageWidth = (int)API_Module_Device.Current.FishEyeResolution.x;
  229. imageHeight = (int)API_Module_Device.Current.FishEyeResolution.y;
  230. outBUdate = true;
  231. outCurrFrameIndex = 0;
  232. outFrameExposureNano = 0;
  233. outLeftFrameData = new byte[imageWidth * imageHeight];
  234. outRightFrameData = new byte[imageWidth * imageHeight];
  235. textureFormat = TextureFormat.Alpha8;
  236. textureTemp = new Texture2D(imageWidth, imageHeight, textureFormat, false);
  237. }
  238. public void ShowCamera()
  239. {
  240. if (Application.platform == RuntimePlatform.Android)
  241. {
  242. GSXR_Get_LatestFishEyeBinocularData(ref outBUdate, ref outCurrFrameIndex, ref outFrameExposureNano, outLeftFrameData, outRightFrameData);
  243. if (outBUdate)
  244. {
  245. FishEyeCamera0.texture = GetTexture(outLeftFrameData);
  246. FishEyeCamera0.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  247. FishEyeCamera1.texture = GetTexture(outRightFrameData);
  248. FishEyeCamera1.rectTransform.sizeDelta = new Vector2(imageWidth, imageHeight);
  249. }
  250. else
  251. {
  252. Debug.Log("Error: Please Check Slamconfig prop: gUseXXXCamera = true");
  253. }
  254. }
  255. }
  256. public Texture2D GetTexture(byte[] outFrameData)
  257. {
  258. textureTemp.LoadRawTextureData(outFrameData);
  259. textureTemp.Apply();
  260. return textureTemp;
  261. }
  262. }