NxrSDKApi.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using NibiruTask;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace Nxr.Internal
  7. {
  8. /// <summary>
  9. /// Nxr SDK Api Global Single Instance
  10. /// </summary>
  11. public class NxrSDKApi
  12. {
  13. private static object syncRoot = new object();
  14. private static NxrSDKApi _instance = null;
  15. public static NxrSDKApi Instance
  16. {
  17. get
  18. {
  19. if (_instance == null) //第一重判断,先判断实例是否存在,不存在再加锁处理
  20. {
  21. lock (syncRoot) //加锁,在某一时刻只允许一个线程访问
  22. {
  23. if (_instance == null) //第二重判断: 第一个线程进入Lock中执行创建代码,第二个线程处于排队等待状态,当第二个线程进入Lock后并不知道实例已创建,将会继续创建新的实例
  24. {
  25. _instance = new NxrSDKApi();
  26. }
  27. }
  28. }
  29. return _instance;
  30. }
  31. }
  32. private NxrSDKApi()
  33. {
  34. IsInXRMode = false;
  35. }
  36. /// <summary>
  37. /// Is in xr mode(AR/VR)
  38. /// </summary>
  39. public bool IsInXRMode { set; get; }
  40. // Controller connection status change : handType 0=left, 1=right
  41. public delegate void ControllerStatusChange(NibiruTask.InteractionManager.NACTION_HAND_TYPE handType,
  42. bool isConnected, bool isSixDofController);
  43. public event ControllerStatusChange ControllerStatusChangeEvent;
  44. public void ExecuteControllerStatusChangeEvent(NibiruTask.InteractionManager.NACTION_HAND_TYPE handType,
  45. bool isConnected, bool isSixDofController)
  46. {
  47. Debug.Log("handtype=" + handType + "," + "isConnected=" + isConnected + "," + "isSixDofController=" + isSixDofController);
  48. if (ControllerStatusChangeEvent != null)
  49. {
  50. ControllerStatusChangeEvent(handType, isConnected, isSixDofController);
  51. }
  52. if (isConnected)
  53. {
  54. var controllerTipState = NxrViewer.Instance.GetDevice().GetControllerTipState();
  55. Debug.Log("ControllerTipState:" + controllerTipState);
  56. if (controllerTipState == 0)
  57. {
  58. NibiruRemindBox.Instance.CalibrationDelay();
  59. NxrViewer.Instance.GetDevice().SetControllerTipState(1);
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// The Trigger key can switch the main controller, only the main controller can emit rays {global variable}.
  65. /// </summary>
  66. NxrInstantNativeApi.NibiruDeviceType sixDofControllerPrimaryDeviceType;
  67. /// <summary>
  68. /// Current sixDof controller primary device type.(LeftController or RightController)
  69. /// </summary>
  70. public NxrInstantNativeApi.NibiruDeviceType SixDofControllerPrimaryDeviceType
  71. {
  72. set
  73. {
  74. sixDofControllerPrimaryDeviceType = value;
  75. if (NxrViewer.Instance != null)
  76. {
  77. NxrViewer.Instance.GetDevice()
  78. .SetSixDofControllerPrimaryDeviceType(sixDofControllerPrimaryDeviceType);
  79. }
  80. }
  81. get { return sixDofControllerPrimaryDeviceType; }
  82. }
  83. /// <summary>
  84. /// The Position of Head.
  85. /// </summary>
  86. public Vector3 HeadPosition { set; get; }
  87. private Dictionary<string, ObjMesh> CacheMeshDict = new Dictionary<string, ObjMesh>();
  88. public void AddObjMesh(string name, ObjMesh mesh)
  89. {
  90. if (CacheMeshDict.ContainsKey(name))
  91. {
  92. CacheMeshDict[name] = mesh;
  93. }
  94. else
  95. {
  96. CacheMeshDict.Add(name, mesh);
  97. }
  98. }
  99. public ObjMesh GetObjMesh(string name)
  100. {
  101. if (CacheMeshDict.ContainsKey(name))
  102. {
  103. return CacheMeshDict[name];
  104. }
  105. else
  106. {
  107. return null;
  108. }
  109. }
  110. private Dictionary<string, Sprite> Cach3DofSpriteDict = new Dictionary<string, Sprite>();
  111. public Dictionary<string, Sprite> Cach6DofSpriteDict = new Dictionary<string, Sprite>();
  112. public bool Is3DofSpriteFirstLoad { set; get; }
  113. public bool Is6DofSpriteFirstLoad { set; get; }
  114. public string Last3DofModelPath { set; get; }
  115. public string Last6DofModelPath { set; get; }
  116. public void AddSprite(string name, Sprite sprite)
  117. {
  118. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_3DOF)
  119. {
  120. if (Cach3DofSpriteDict.ContainsKey(name))
  121. {
  122. Cach3DofSpriteDict[name] = sprite;
  123. }
  124. else
  125. {
  126. Cach3DofSpriteDict.Add(name, sprite);
  127. }
  128. }
  129. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_6DOF)
  130. {
  131. if (Cach6DofSpriteDict.ContainsKey(name))
  132. {
  133. Cach6DofSpriteDict[name] = sprite;
  134. }
  135. else
  136. {
  137. Cach6DofSpriteDict.Add(name, sprite);
  138. }
  139. }
  140. }
  141. public Sprite GetSprite(string name)
  142. {
  143. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_3DOF)
  144. {
  145. if (Cach3DofSpriteDict.ContainsKey(name)) return Cach3DofSpriteDict[name];
  146. }
  147. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_6DOF)
  148. {
  149. if (Cach6DofSpriteDict.ContainsKey(name)) return Cach6DofSpriteDict[name];
  150. }
  151. return null;
  152. }
  153. public void ClearCachSpriteDict()
  154. {
  155. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_3DOF)
  156. {
  157. Cach3DofSpriteDict.Clear();
  158. }
  159. if (InteractionManager.GetControllerModeType() == InteractionManager.NACTION_CONTROLLER_TYPE.CONTROL_6DOF)
  160. {
  161. Cach6DofSpriteDict.Clear();
  162. }
  163. }
  164. public void Destroy()
  165. {
  166. CacheMeshDict.Clear();
  167. }
  168. /// <summary>
  169. /// Is support multiThreaded rendering
  170. /// </summary>
  171. public bool IsSptMultiThreadedRendering { set; get; }
  172. /// <summary>
  173. /// Whether the current controller has a prompt UI.
  174. /// </summary>
  175. /// <returns></returns>
  176. public bool IsSptControllerTipUI()
  177. {
  178. return InteractionManager.IsSptControllerTipUI();
  179. }
  180. public List<InteractionManager.ControllerKeyInfo> GetControllerKeyInfoList()
  181. {
  182. return InteractionManager.GetControllerConfig().KeyInfoList;
  183. }
  184. /// <summary>
  185. /// The language of Controller Prompt UI.
  186. /// </summary>
  187. private InteractionManager.TipLanguage tipLanguage;
  188. public InteractionManager.TipLanguage GetTipLanguage()
  189. {
  190. if (Application.systemLanguage == SystemLanguage.Chinese ||
  191. Application.systemLanguage == SystemLanguage.ChineseSimplified ||
  192. Application.systemLanguage == SystemLanguage.ChineseTraditional)
  193. {
  194. tipLanguage = InteractionManager.TipLanguage.ZH;
  195. }
  196. else if (Application.systemLanguage == SystemLanguage.English)
  197. {
  198. tipLanguage = InteractionManager.TipLanguage.EN;
  199. }
  200. else
  201. {
  202. tipLanguage = InteractionManager.TipLanguage.DEFAULT;
  203. }
  204. return tipLanguage;
  205. }
  206. public string assetPath = "Assets/Samples/0.0.1/ShadowCreator/NXR/Resources/Config/";
  207. #if UNITY_EDITOR
  208. public SettingsAssetConfig GetSettingsAssetConfig()
  209. {
  210. var assetpath = assetPath + "SettingsAssetConfig.asset";
  211. SettingsAssetConfig asset;
  212. if (System.IO.File.Exists(assetpath))
  213. {
  214. asset = UnityEditor.AssetDatabase.LoadAssetAtPath<SettingsAssetConfig>(assetpath);
  215. }
  216. else
  217. {
  218. asset = new SettingsAssetConfig();
  219. asset.mSixDofMode = SixDofMode.Head_3Dof_Ctrl_6Dof;
  220. asset.mSleepTimeoutMode = SleepTimeoutMode.NEVER_SLEEP;
  221. asset.mHeadControl = HeadControl.GazeApplication;
  222. asset.mTextureQuality = TextureQuality.Best;
  223. asset.mTextureMSAA = TextureMSAA.MSAA_2X;
  224. UnityEditor.AssetDatabase.CreateAsset(asset, assetpath);
  225. }
  226. return asset;
  227. }
  228. #endif
  229. public bool IsSptEyeLocalRp { get; set; }
  230. public float[] LeftEyeLocalRotation = new float[9];
  231. public float[] LeftEyeLocalPosition = new float[3];
  232. public float[] RightEyeLocalRotation = new float[9];
  233. public float[] RightEyeLocalPosition = new float[3];
  234. }
  235. }