NxrControllerManager.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using UnityEngine;
  2. using NibiruTask;
  3. using System.Collections.Generic;
  4. using NibiruAxis;
  5. using UnityEngine.UI;
  6. namespace Nxr.Internal
  7. {
  8. public class NxrControllerManager : MonoBehaviour
  9. {
  10. public bool IsDebugInEditor;
  11. Dictionary<InteractionManager.NACTION_HAND_TYPE, NxrTrackedDevice> DualCtrlDeviceDict =
  12. new Dictionary<InteractionManager.NACTION_HAND_TYPE, NxrTrackedDevice>();
  13. bool[] DualCtrlConnected = new bool[5] {false, false, false, false, false};
  14. Transform transformCache;
  15. // Use this for initialization
  16. void Start()
  17. {
  18. transformCache = transform;
  19. DualCtrlDeviceDict.Clear();
  20. NxrSDKApi.Instance.ControllerStatusChangeEvent += OnControllerStatusChangeEvent;
  21. }
  22. private void Update()
  23. {
  24. if (IsDebugInEditor)
  25. {
  26. IsDebugInEditor = false;
  27. NxrSDKApi.Instance.ExecuteControllerStatusChangeEvent(InteractionManager.NACTION_HAND_TYPE.HAND_LEFT,
  28. true, true);
  29. NxrSDKApi.Instance.ExecuteControllerStatusChangeEvent(InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT,
  30. true, true);
  31. }
  32. if (InteractionManager.IsInteractionSDKEnabled() && InteractionManager.IsSixDofController &&
  33. DualCtrlDeviceDict.Count != 2)
  34. {
  35. if (InteractionManager.IsSixDofControllerConnected((int) InteractionManager.NACTION_HAND_TYPE.HAND_LEFT)
  36. && !DualCtrlDeviceDict.ContainsKey(InteractionManager.NACTION_HAND_TYPE.HAND_LEFT))
  37. {
  38. OnControllerStatusChangeEvent(InteractionManager.NACTION_HAND_TYPE.HAND_LEFT, true,
  39. InteractionManager.IsSixDofController);
  40. }
  41. if (InteractionManager.IsSixDofControllerConnected(
  42. (int) InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT)
  43. && !DualCtrlDeviceDict.ContainsKey(InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT))
  44. {
  45. OnControllerStatusChangeEvent(InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT, true,
  46. InteractionManager.IsSixDofController);
  47. }
  48. }
  49. if (leftObj || rightObj)
  50. {
  51. InteractionManager.ControllerConfig controllerConfig = InteractionManager.GetControllerConfig();
  52. if (NxrSDKApi.Instance.Last6DofModelPath == null)
  53. {
  54. NxrSDKApi.Instance.Last6DofModelPath = controllerConfig.modelPath;
  55. }
  56. else
  57. {
  58. var currentModelPath = controllerConfig.modelPath;
  59. if (currentModelPath != NxrSDKApi.Instance.Last6DofModelPath)
  60. {
  61. isLeftTipsGoGreated = false;
  62. isRightTipsGoGreated = false;
  63. NxrSDKApi.Instance.Is6DofSpriteFirstLoad = false;
  64. isLoadSprites = false;
  65. isLeftTipsCreated = false;
  66. isRightTipsCreated = false;
  67. }
  68. }
  69. if (leftObj && !isLeftTipsGoGreated)
  70. {
  71. isLeftTipsGoGreated = true;
  72. CreateControllerTips(leftObj);
  73. }
  74. if (rightObj && !isRightTipsGoGreated)
  75. {
  76. isRightTipsGoGreated = true;
  77. CreateControllerTips(rightObj);
  78. }
  79. if (leftTipsGo && !isLeftTipsCreated) CreateTipImgs(leftTipsGo);
  80. if (rightTipsGo && !isRightTipsCreated) CreateTipImgs(rightTipsGo);
  81. if (leftTipsGo && isLeftTipsCreated) ChangeTipAlpha(leftTipsGo);
  82. if (rightTipsGo && isRightTipsCreated) ChangeTipAlpha(rightTipsGo);
  83. if (leftModelGo && leftTipsGo) leftTipsGo.SetActive(leftModelGo.activeSelf);
  84. if (rightModelGo && rightTipsGo) rightTipsGo.SetActive(rightModelGo.activeSelf);
  85. }
  86. }
  87. void OnControllerStatusChangeEvent(InteractionManager.NACTION_HAND_TYPE handType, bool isConnected,
  88. bool isSixDofController)
  89. {
  90. if (!isSixDofController)
  91. {
  92. Debug.Log("---OnControllerStatusChangeEvent---isSixDofController=false-");
  93. return;
  94. }
  95. DualCtrlConnected[(int) handType] = isConnected;
  96. if (handType == InteractionManager.NACTION_HAND_TYPE.HAND_LEFT && isConnected)
  97. {
  98. Debug.Log("---CreateControllerModel---Left-");
  99. LoadControllerModel(NxrInstantNativeApi.NibiruDeviceType.LeftController, "Controller6DofLeft");
  100. }
  101. if (handType == InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT && isConnected)
  102. {
  103. Debug.Log("---CreateControllerModel---Right-");
  104. LoadControllerModel(NxrInstantNativeApi.NibiruDeviceType.RightController, "Controller6DofRight");
  105. }
  106. }
  107. private void LoadControllerModel(NxrInstantNativeApi.NibiruDeviceType nibiruDeviceType, string objName)
  108. {
  109. if (InteractionManager.IsSupportControllerModel())
  110. {
  111. if (NxrPlayerCtrl.Instance.isNeedCustomModel)
  112. {
  113. if (string.IsNullOrEmpty(NxrPlayerCtrl.Instance.customModelPrefabName))
  114. {
  115. Debug.LogError("The path of the custom handle model prefab is wrong");
  116. }
  117. else
  118. {
  119. LoadDefaultCtrl(NxrPlayerCtrl.Instance.customModelPrefabName);
  120. return;
  121. }
  122. }
  123. InteractionManager.ControllerConfig mControllerConfig = InteractionManager.GetControllerConfig();
  124. CreateControllerModel(nibiruDeviceType, objName, mControllerConfig);
  125. }
  126. else
  127. {
  128. LoadDefaultCtrl("CustomModel");
  129. }
  130. }
  131. private void LoadDefaultCtrl(string prefabName)
  132. {
  133. GameObject obj = GameObject.Find(prefabName);
  134. if (!obj)
  135. {
  136. GameObject objPrefab = (GameObject) Resources.Load(string.Concat("CustomModelPrefabs/", prefabName));
  137. if (!objPrefab) Debug.LogError("The prefab was not created successfully");
  138. GameObject mGameObject =
  139. (GameObject) Instantiate(objPrefab, Vector3.zero, Quaternion.identity);
  140. var leftObj = mGameObject.transform.GetChild(0).gameObject;
  141. leftObj.name = "LeftModel";
  142. var rightObj = Instantiate(leftObj, mGameObject.transform) as GameObject;
  143. rightObj.name = "RightModel";
  144. NxrTrackedDevice trackedDeviceLeft = leftObj.GetComponent<NxrTrackedDevice>();
  145. trackedDeviceLeft.deviceType = NxrInstantNativeApi.NibiruDeviceType.LeftController;
  146. NxrTrackedDevice trackedDeviceRight = rightObj.GetComponent<NxrTrackedDevice>();
  147. trackedDeviceLeft.deviceType = NxrInstantNativeApi.NibiruDeviceType.RightController;
  148. if (!DualCtrlDeviceDict.ContainsKey(InteractionManager.NACTION_HAND_TYPE.HAND_LEFT))
  149. {
  150. DualCtrlDeviceDict.Add(InteractionManager.NACTION_HAND_TYPE.HAND_LEFT, trackedDeviceLeft);
  151. }
  152. else
  153. {
  154. DualCtrlDeviceDict[InteractionManager.NACTION_HAND_TYPE.HAND_LEFT] = trackedDeviceLeft;
  155. }
  156. if (!DualCtrlDeviceDict.ContainsKey(InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT))
  157. {
  158. DualCtrlDeviceDict.Add(InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT, trackedDeviceRight);
  159. }
  160. else
  161. {
  162. DualCtrlDeviceDict[InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT] = trackedDeviceRight;
  163. }
  164. NxrViewer.Instance.SwitchControllerMode(true);
  165. }
  166. }
  167. private void CreateControllerModel(NxrInstantNativeApi.NibiruDeviceType deviceType, string objName,
  168. InteractionManager.ControllerConfig mControllerConfig)
  169. {
  170. string objPath = mControllerConfig.objPath;
  171. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  172. {
  173. objPath = mControllerConfig.leftCtrlObjPath;
  174. }
  175. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  176. {
  177. objPath = mControllerConfig.rightCtrlObjPath;
  178. }
  179. if (objPath == null)
  180. {
  181. Debug.LogError("CreateControllerModel failed, objPath is null......" + objName);
  182. return;
  183. }
  184. GameObject go = new GameObject(objName);
  185. NxrLaserPointer mNxrLaserPointer = go.AddComponent<NxrLaserPointer>();
  186. mNxrLaserPointer.deviceType = deviceType;
  187. go.transform.SetParent(transformCache);
  188. go.transform.localScale = Vector3.one;
  189. go.transform.localPosition = new Vector3(0, 0, 0);
  190. go.transform.localRotation = new Quaternion(0, 0, 0, 1);
  191. GameObject modelGOParent = new GameObject("model_P");
  192. modelGOParent.transform.localScale = new Vector3(-1, 1, 1);
  193. modelGOParent.transform.localPosition = new Vector3(0, 0, 0);
  194. modelGOParent.transform.localRotation = new Quaternion(0, 0, 0, 1);
  195. modelGOParent.transform.SetParent(go.transform);
  196. GameObject modelGO = new GameObject("model");
  197. modelGO.transform.SetParent(modelGOParent.transform);
  198. modelGO.transform.localScale = new Vector3(mControllerConfig.modelScale[0]
  199. , mControllerConfig.modelScale[1], mControllerConfig.modelScale[2]);
  200. modelGO.transform.localRotation = Quaternion.Euler(mControllerConfig.modelRotation[0],
  201. mControllerConfig.modelRotation[1], mControllerConfig.modelRotation[2]);
  202. modelGO.transform.localPosition = new Vector3(mControllerConfig.modelPosition[0]
  203. , mControllerConfig.modelPosition[1], mControllerConfig.modelPosition[2]);
  204. modelGO.AddComponent<NxrControllerModel>();
  205. // string objPath = "/system/etc/Objs/housing_bott.obj";
  206. Debug.Log("objPath=" + objPath);
  207. ObjModelLoader mObjModelLoader = go.GetComponent<ObjModelLoader>();
  208. if (mObjModelLoader == null)
  209. {
  210. go.AddComponent<ObjMaterial>();
  211. mObjModelLoader = go.AddComponent<ObjModelLoader>();
  212. }
  213. mObjModelLoader.LoadObjFile(objPath, modelGO.transform);
  214. GameObject powerGO = new GameObject("Power");
  215. powerGO.transform.SetParent(go.transform);
  216. MeshRenderer powerMeshRenderer = powerGO.AddComponent<MeshRenderer>();
  217. Mesh quadMesh = new Mesh();
  218. quadMesh.name = "QUAD";
  219. float quadSize = 0.5f;
  220. quadMesh.vertices = new Vector3[]
  221. {
  222. new Vector3(-1 * quadSize, -1 * quadSize, 0),
  223. new Vector3(-1 * quadSize, 1 * quadSize, 0),
  224. new Vector3(1 * quadSize, 1 * quadSize, 0),
  225. new Vector3(1 * quadSize, -1 * quadSize, 0)
  226. };
  227. quadMesh.uv = new Vector2[]
  228. {
  229. new Vector2(0, 0),
  230. new Vector2(0, 1),
  231. new Vector2(1, 1),
  232. new Vector2(1, 0)
  233. };
  234. int[] triangles = {0, 1, 2, 0, 2, 3};
  235. quadMesh.triangles = triangles;
  236. powerGO.AddComponent<MeshFilter>().mesh = quadMesh;
  237. powerGO.AddComponent<MeshCollider>();
  238. powerGO.AddComponent<NibiruControllerPower>();
  239. powerGO.transform.localPosition = new Vector3(mControllerConfig.batteryPosition[0],
  240. mControllerConfig.batteryPosition[1]
  241. , mControllerConfig.batteryPosition[2]);
  242. powerGO.transform.localRotation = Quaternion.Euler(mControllerConfig.batteryRotation[0],
  243. mControllerConfig.batteryRotation[1]
  244. , mControllerConfig.batteryRotation[2]);
  245. powerGO.transform.localScale = new Vector3(mControllerConfig.batteryScale[0],
  246. mControllerConfig.batteryScale[1]
  247. , mControllerConfig.batteryScale[2]);
  248. // 射线起点
  249. mNxrLaserPointer.SetHolderLocalPosition(new Vector3(mControllerConfig.rayStartPosition[0],
  250. mControllerConfig.rayStartPosition[1],
  251. mControllerConfig.rayStartPosition[2]));
  252. NxrTrackedDevice trackedDevice = go.AddComponent<NxrTrackedDevice>();
  253. if (trackedDevice != null)
  254. {
  255. trackedDevice.ReloadLaserPointer(mNxrLaserPointer);
  256. trackedDevice.deviceType = deviceType;
  257. InteractionManager.NACTION_HAND_TYPE mHandType =
  258. deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController
  259. ? InteractionManager.NACTION_HAND_TYPE.HAND_LEFT
  260. : InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT;
  261. if (!DualCtrlDeviceDict.ContainsKey(mHandType))
  262. {
  263. DualCtrlDeviceDict.Add(mHandType, trackedDevice);
  264. }
  265. else
  266. {
  267. DualCtrlDeviceDict[mHandType] = trackedDevice;
  268. }
  269. }
  270. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  271. {
  272. leftObj = go;
  273. leftModelGo = modelGO;
  274. }
  275. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  276. {
  277. rightObj = go;
  278. rightModelGo = modelGO;
  279. }
  280. //close
  281. NxrViewer.Instance.SwitchControllerMode(true);
  282. Debug.Log("HideGaze.ForceUseReticle3");
  283. }
  284. private GameObject leftObj, rightObj, leftModelGo, rightModelGo, leftTipsGo, rightTipsGo;
  285. private bool isLoadSprites, isLeftTipsGoGreated, isRightTipsGoGreated, isLeftTipsCreated, isRightTipsCreated;
  286. /// <summary>
  287. /// Create parent object of handle prompt UI.
  288. /// </summary>
  289. public void CreateControllerTips(GameObject obj)
  290. {
  291. GameObject tipsGo;
  292. #if UNITY_EDITOR
  293. tipsGo =
  294. Instantiate(Resources.Load<GameObject>("Controller/Objs/108/Canvas"), obj.transform) as GameObject;
  295. tipsGo.transform.localPosition = Vector3.zero;
  296. if (leftObj && leftObj == obj) leftTipsGo = tipsGo;
  297. else if (rightObj && rightObj == obj) rightTipsGo = tipsGo;
  298. #elif UNITY_ANDROID
  299. if (!NxrSDKApi.Instance.IsSptControllerTipUI()) return;
  300. tipsGo = new GameObject("Canvas");
  301. tipsGo.transform.SetParent(obj.transform);
  302. tipsGo.transform.localPosition = Vector3.zero;
  303. tipsGo.transform.localRotation = Quaternion.Euler(75, 0, 0);
  304. tipsGo.transform.localScale = Vector3.one * 0.001f;
  305. var canvas = tipsGo.AddComponent<Canvas>();
  306. canvas.renderMode = RenderMode.WorldSpace;
  307. tipsGo.AddComponent<CanvasScaler>();
  308. tipsGo.AddComponent<GraphicRaycaster>();
  309. tipsGo.AddComponent<CanvasGroup>();
  310. if (leftObj && leftObj == obj) leftTipsGo = tipsGo;
  311. else if (rightObj && rightObj == obj) rightTipsGo = tipsGo;
  312. if (!isLoadSprites)
  313. {
  314. isLoadSprites = true;
  315. RefreshSprites();
  316. }
  317. #endif
  318. }
  319. /// <summary>
  320. /// Cach handle prompt UI.
  321. /// </summary>
  322. private void RefreshSprites()
  323. {
  324. var controllerKeyInfoList = InteractionManager.GetControllerConfig().KeyInfoList;
  325. if (controllerKeyInfoList == null)
  326. {
  327. Debug.LogError("----------KeyInfoList is null----------");
  328. return;
  329. }
  330. var imgsPath = new string[controllerKeyInfoList.Count];
  331. if (!NxrSDKApi.Instance.Is6DofSpriteFirstLoad)
  332. {
  333. NxrSDKApi.Instance.ClearCachSpriteDict();
  334. for (var i = 0; i < controllerKeyInfoList.Count; i++)
  335. {
  336. var tipList = controllerKeyInfoList[i].tipList;
  337. imgsPath[i] = tipList[(int) NxrSDKApi.Instance.GetTipLanguage()].picPath;
  338. }
  339. var spriteLoader = gameObject.AddComponent<SpriteLoader>();
  340. spriteLoader.LoadSpriteFile(imgsPath);
  341. }
  342. }
  343. /// <summary>
  344. /// Create handle prompt UI.
  345. /// </summary>
  346. public void CreateTipImgs(GameObject tipsGo)
  347. {
  348. if (!NxrSDKApi.Instance.Is6DofSpriteFirstLoad) return;
  349. var controllerKeyInfoList = InteractionManager.GetControllerConfig().KeyInfoList;
  350. for (var i = 0; i < controllerKeyInfoList.Count; i++)
  351. {
  352. if (controllerKeyInfoList[i].picName == "left.png" && rightTipsGo && rightTipsGo == tipsGo ||
  353. controllerKeyInfoList[i].picName == "right.png" && leftTipsGo && leftTipsGo == tipsGo) continue;
  354. var tipGo = new GameObject(controllerKeyInfoList[i].desc);
  355. tipGo.transform.SetParent(tipsGo.transform);
  356. var img = tipGo.AddComponent<Image>();
  357. var tipList = controllerKeyInfoList[i].tipList;
  358. var currentControllerTip = tipList[(int) NxrSDKApi.Instance.GetTipLanguage()];
  359. var pos = currentControllerTip.position;
  360. var rotation = currentControllerTip.rotation;
  361. var scale = currentControllerTip.size;
  362. img.sprite = NxrSDKApi.Instance.GetSprite(currentControllerTip.picPath);
  363. img.SetNativeSize();
  364. img.transform.localPosition = new Vector3(pos[0], pos[1], pos[2]);
  365. img.transform.localRotation = new Quaternion(rotation[0], rotation[1], rotation[2], 1);
  366. img.transform.localScale = new Vector2(scale[0], scale[1]);
  367. }
  368. if (leftTipsGo && leftTipsGo == tipsGo) isLeftTipsCreated = true;
  369. if (rightTipsGo && rightTipsGo == tipsGo) isRightTipsCreated = true;
  370. }
  371. /// <summary>
  372. /// Change the Alpha of handle prompt UI by judging the angle of handle.
  373. /// </summary>
  374. public void ChangeTipAlpha(GameObject tipsGo)
  375. {
  376. var parentRotationX = tipsGo.transform.parent.localEulerAngles.x;
  377. var tipsAlpha = (360.0f - parentRotationX) / 90.0f;
  378. if (parentRotationX >= 270.0f && parentRotationX <= 360.0f)
  379. {
  380. tipsAlpha = Mathf.Clamp(tipsAlpha, 0.0f, 1.0f);
  381. }
  382. else
  383. {
  384. tipsAlpha = 0.0f;
  385. }
  386. tipsGo.GetComponent<CanvasGroup>().alpha = tipsAlpha;
  387. }
  388. }
  389. }