NxrTrackedDevice.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. using NibiruAxis;
  2. using NibiruTask;
  3. using UnityEngine;
  4. namespace Nxr.Internal
  5. {
  6. public class NxrTrackedDevice : MonoBehaviour
  7. {
  8. public enum ButtonID
  9. {
  10. Trigger = 0,
  11. Grip = 1,
  12. Menu = 21,
  13. System = -1,
  14. TouchPad = 20,
  15. DPadUp = 5,
  16. DPadDown = 4,
  17. DPadLeft = 2,
  18. DPadRight = 3,
  19. DPadCenter = 6,
  20. TrackpadTouch = 7,
  21. InternalTrigger = 8,
  22. }
  23. public NxrInstantNativeApi.NibiruDeviceType deviceType;
  24. NxrInstantNativeApi.Nibiru_ControllerStates _prevStates;
  25. NxrInstantNativeApi.Nibiru_ControllerStates _currentStates;
  26. NxrControllerModel controllerModel;
  27. NibiruControllerPower controllerPower;
  28. NxrLaserPointer laserPointer;
  29. public bool isGamePad;
  30. public bool DebugInEditor;
  31. public NxrLaserPointer GetLaserPointer()
  32. {
  33. return laserPointer;
  34. }
  35. public void ReloadLaserPointer(NxrLaserPointer laserPointerIn)
  36. {
  37. this.laserPointer = laserPointerIn;
  38. if (laserPointer != null)
  39. {
  40. laserPointer.PointerIn += PointerInEventHandler;
  41. laserPointer.PointerOut += PointerOutEventHandler;
  42. }
  43. }
  44. private void Start()
  45. {
  46. isGamePad = gameObject.name.Contains("Gamepad");
  47. laserPointer = GetComponent<NxrLaserPointer>();
  48. if (laserPointer != null)
  49. {
  50. laserPointer.PointerIn += PointerInEventHandler;
  51. laserPointer.PointerOut += PointerOutEventHandler;
  52. }
  53. if (!isGamePad)
  54. {
  55. controllerModel = GetComponentInChildren<NxrControllerModel>();
  56. controllerPower = GetComponentInChildren<NibiruControllerPower>();
  57. if (controllerModel != null) controllerModel.gameObject.SetActive(false);
  58. if (controllerPower != null) controllerPower.gameObject.SetActive(false);
  59. }
  60. #if UNITY_ANDROID
  61. NibiruTaskApi.deviceConnectState += OnDeviceConnectState;
  62. _currentStates = new NxrInstantNativeApi.Nibiru_ControllerStates();
  63. _prevStates = new NxrInstantNativeApi.Nibiru_ControllerStates();
  64. _currentStates.connectStatus = 0;
  65. _prevStates.connectStatus = 0;
  66. _currentStates.buttons = 0;
  67. _currentStates.touches = 0;
  68. _prevStates.buttons = 0;
  69. _prevStates.touches = 0;
  70. #endif
  71. }
  72. private void OnDestroy()
  73. {
  74. if (laserPointer != null)
  75. {
  76. laserPointer.PointerIn -= PointerInEventHandler;
  77. laserPointer.PointerOut -= PointerOutEventHandler;
  78. }
  79. #if UNITY_ANDROID
  80. NibiruTaskApi.deviceConnectState -= OnDeviceConnectState;
  81. #endif
  82. }
  83. private int GetNoloType()
  84. {
  85. int noloType = (int) CDevice.NOLO_TYPE.NONE;
  86. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  87. {
  88. noloType = (int) CDevice.NOLO_TYPE.LEFT;
  89. }
  90. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  91. {
  92. noloType = (int) CDevice.NOLO_TYPE.RIGHT;
  93. }
  94. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.Hmd)
  95. {
  96. noloType = (int) CDevice.NOLO_TYPE.HEAD;
  97. }
  98. return noloType;
  99. }
  100. public void OnDeviceConnectState(int state, CDevice device)
  101. {
  102. if (device.getType() != CDevice.DEVICE_NOLO_SIXDOF) return;
  103. //0=connect,1=disconnect
  104. Debug.Log("NxrTrackedDevice.onDeviceConnectState:" + state + "," + device.getType() + "," + device.getName() + "," + device.getMode() + "," +
  105. device.getisQuat());
  106. if(state == 0)
  107. {
  108. _currentStates.connectStatus = 1;
  109. NxrViewer.Instance.SwitchControllerMode(true);
  110. }
  111. else
  112. {
  113. _currentStates.connectStatus = 0;
  114. }
  115. }
  116. void PointerInEventHandler(object sender, PointerEventArgs e)
  117. {
  118. NxrControllerHelper.ControllerRaycastObject = e.target.gameObject;
  119. //Debug.Log("PointerInEventHandler---------" + e.target.gameObject.name);
  120. }
  121. void PointerOutEventHandler(object sender, PointerEventArgs e)
  122. {
  123. NxrControllerHelper.ControllerRaycastObject = null;
  124. //Debug.Log("PointerOutEventHandler---------" + e.target.gameObject.name);
  125. }
  126. // Update is called once per frame
  127. void Update()
  128. {
  129. #if UNITY_ANDROID //&& !UNITY_EDITOR
  130. if (!NxrViewer.Instance.IsWinPlatform && !isGamePad)
  131. {
  132. // Android/NOLO
  133. int noloType = GetNoloType();
  134. int handType = noloType == (int)CDevice.NOLO_TYPE.LEFT ? (int)InteractionManager.NACTION_HAND_TYPE.HAND_LEFT : (int)InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT;
  135. bool isConnected = false;
  136. if (InteractionManager.IsInteractionSDKEnabled())
  137. {
  138. isConnected = InteractionManager.IsSixDofControllerConnected(handType);
  139. }
  140. else
  141. {
  142. isConnected = ControllerAndroid.isDeviceConn(noloType);
  143. }
  144. if (DebugInEditor)
  145. {
  146. isConnected = true;
  147. }
  148. if (_currentStates.connectStatus == 0 && isConnected)
  149. {
  150. NxrViewer.Instance.SwitchControllerMode(true);
  151. _currentStates.connectStatus = 1;
  152. NxrPlayerCtrl mNxrPlayerCtrl = NxrPlayerCtrl.Instance;
  153. if (mNxrPlayerCtrl != null)
  154. {
  155. // 关闭3dof手柄显示
  156. mNxrPlayerCtrl.GamepadEnabled = false;
  157. // 关闭白点显示
  158. }
  159. }
  160. else if (_currentStates.connectStatus == 1 && !isConnected)
  161. {
  162. _currentStates.connectStatus = 0;
  163. }
  164. //Debug.Log("status="+_currentStates.connectStatus + ", " + deviceType.ToString() + ","
  165. // + ControllerAndroid.isDeviceConn((int) CDevice.NOLO_TYPE.LEFT) + ", " + ControllerAndroid.isDeviceConn((int)CDevice.NOLO_TYPE.RIGHT));
  166. if (!IsConneted() && controllerModel != null && controllerModel.gameObject.activeSelf)
  167. {
  168. if (controllerPower != null) controllerPower.gameObject.SetActive(false);
  169. controllerModel.gameObject.SetActive(false);
  170. laserPointer.holder.SetActive(false);
  171. NxrControllerHelper.ControllerRaycastObject = null;
  172. Debug.Log("controllerModel Dismiss " + deviceType + "," + controllerModel.gameObject.activeSelf);
  173. }
  174. else if (IsConneted() && controllerModel != null && !controllerModel.gameObject.activeSelf)
  175. {
  176. if (controllerPower != null) controllerPower.gameObject.SetActive(true);
  177. controllerModel.gameObject.SetActive(true);
  178. laserPointer.holder.SetActive(true);
  179. Debug.Log("controllerModel Show " + deviceType);
  180. }
  181. if (GetButtonUp(ButtonID.InternalTrigger))
  182. {
  183. NxrSDKApi.Instance.SixDofControllerPrimaryDeviceType = deviceType;
  184. Debug.Log("IsPrimaryControllerHand " + deviceType.ToString());
  185. }
  186. if (laserPointer != null && controllerModel != null)
  187. {
  188. if (NxrSDKApi.Instance.SixDofControllerPrimaryDeviceType != deviceType)
  189. {
  190. // 非主手柄,隐藏射线
  191. laserPointer.holder.SetActive(false);
  192. }
  193. else
  194. {
  195. laserPointer.holder.SetActive(controllerModel.gameObject.activeSelf);
  196. }
  197. }
  198. if (IsConneted())
  199. {
  200. processControllerKeyEvent(noloType);
  201. float[] poseData = new float[8];
  202. if (InteractionManager.IsSixDofControllerConnected(handType))
  203. {
  204. float[] pose = InteractionManager.GetControllerPose((InteractionManager.NACTION_HAND_TYPE) handType);
  205. poseData[1] = pose[4];
  206. poseData[2] = pose[5];
  207. poseData[3] = pose[6];
  208. for (int i = 0; i < 4; i++)
  209. {
  210. poseData[4 + i] = pose[i];
  211. }
  212. }
  213. else
  214. {
  215. poseData = ControllerAndroid.getCPoseEvent(noloType, 1);
  216. }
  217. if(NxrViewer.Instance.SixDofMode == SixDofMode.Head_3Dof_Ctrl_6Dof)
  218. {
  219. Vector3 HeadPosition = NxrSDKApi.Instance.HeadPosition;
  220. // Debug.LogError(noloType + ".NOLO Controller Y: " + poseData[2] + "/HeadY=" + HeadPosition.y);
  221. poseData[1] = poseData[1] - HeadPosition.x;
  222. poseData[2] = poseData[2] - HeadPosition.y;
  223. poseData[3] = poseData[3] - HeadPosition.z;
  224. }
  225. else if (NxrViewer.Instance.SixDofMode == SixDofMode.Head_3Dof_Ctrl_3Dof)
  226. {
  227. var factor = handType == (int) InteractionManager.NACTION_HAND_TYPE.HAND_LEFT ? -1 : 1;
  228. var headPos = NxrViewer.Instance.GetHead().transform.localPosition;
  229. var pos = new Vector3(headPos.x + 0.25f * factor, headPos.y - 0.6f, headPos.z + 0.5f);
  230. poseData[1] = pos.x;
  231. poseData[2] = pos.y;
  232. poseData[3] = pos.z;
  233. }
  234. transform.localPosition = new Vector3(poseData[1], poseData[2], poseData[3]);
  235. transform.localRotation = new Quaternion(poseData[4], poseData[5], poseData[6], poseData[7]);
  236. }
  237. }
  238. #endif
  239. #if UNITY_STANDALONE_WIN
  240. if (NxrInstantNativeApi.Inited)
  241. {
  242. _prevStates = _currentStates;
  243. _currentStates = NxrInstantNativeApi.GetControllerStates(deviceType);
  244. if (isGamePad)
  245. {
  246. // 3dof手柄左右手模式切换
  247. if (!IsConneted())
  248. {
  249. _currentStates = NxrInstantNativeApi.GetControllerStates(NxrInstantNativeApi.NibiruDeviceType.LeftController);
  250. if (!IsConneted())
  251. {
  252. deviceType = NxrInstantNativeApi.NibiruDeviceType.RightController;
  253. }
  254. else
  255. {
  256. NxrControllerHelper.HandMode3DOF = NxrControllerHelper.LEFT_HAND_MODE;
  257. deviceType = NxrInstantNativeApi.NibiruDeviceType.LeftController;
  258. Debug.Log("Current 3dof HandMode is Left !!!");
  259. }
  260. }
  261. }
  262. if (!IsConneted() && controllerModel != null && controllerModel.gameObject.activeSelf)
  263. {
  264. controllerModel.gameObject.SetActive(false);
  265. laserPointer.holder.SetActive(false);
  266. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  267. {
  268. NxrControllerHelper.IsLeftNoloControllerConnected = false;
  269. }
  270. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  271. {
  272. NxrControllerHelper.IsRightNoloControllerConnected = false;
  273. }
  274. Debug.Log("controllerModel Dismiss " + deviceType + "," + controllerModel.gameObject.activeSelf);
  275. }
  276. else if (IsConneted() && controllerModel != null && !controllerModel.gameObject.activeSelf)
  277. {
  278. controllerModel.gameObject.SetActive(true);
  279. laserPointer.holder.SetActive(true);
  280. if (NxrControllerHelper.ControllerType == (int)NxrInstantNativeApi.NibiruControllerId.NOLO)
  281. {
  282. if (deviceType == NxrInstantNativeApi.NibiruDeviceType.LeftController)
  283. {
  284. NxrControllerHelper.IsLeftNoloControllerConnected = true;
  285. }
  286. else if (deviceType == NxrInstantNativeApi.NibiruDeviceType.RightController)
  287. {
  288. NxrControllerHelper.IsRightNoloControllerConnected = true;
  289. }
  290. }
  291. Debug.Log("controllerModel Show " + deviceType);
  292. }
  293. else if (!IsConneted() && isGamePad && NxrControllerHelper.Is3DofControllerConnected)
  294. {
  295. NxrControllerHelper.Is3DofControllerConnected = false;
  296. Debug.Log("Controller 3dof Dismiss.");
  297. }
  298. else if (IsConneted() && isGamePad && !NxrControllerHelper.Is3DofControllerConnected)
  299. {
  300. if (NxrControllerHelper.ControllerType == (int)NxrInstantNativeApi.NibiruControllerId.NORMAL_3DOF)
  301. {
  302. NxrControllerHelper.Is3DofControllerConnected = true;
  303. }
  304. Debug.Log("Controller 3dof Show." + NxrControllerHelper.ControllerType);
  305. }
  306. if (!isGamePad)
  307. {
  308. NxrInstantNativeApi.Nibiru_Pose pose = NxrInstantNativeApi.GetPoseByDeviceType(deviceType);
  309. // NOLO 非3DOF
  310. transform.localPosition = pose.position;
  311. transform.localRotation = new Quaternion(pose.rotation.x, pose.rotation.y, pose.rotation.z, pose.rotation.w);
  312. }
  313. }
  314. #endif
  315. }
  316. private int[] lastState;
  317. private int[] curState;
  318. private void initState()
  319. {
  320. if (lastState == null)
  321. {
  322. lastState = new int[256];
  323. curState = new int[256];
  324. for (int i = 0; i < 256; i++)
  325. {
  326. curState[i] = -1;
  327. lastState[i] = -1;
  328. }
  329. }
  330. }
  331. private void processControllerKeyEvent(int noloType)
  332. {
  333. int handType = noloType == (int)CDevice.NOLO_TYPE.LEFT ? (int)InteractionManager.NACTION_HAND_TYPE.HAND_LEFT : (int)InteractionManager.NACTION_HAND_TYPE.HAND_RIGHT;
  334. initState();
  335. _prevStates = _currentStates;
  336. lastState = curState;
  337. float[] touchInfo = new float[] {0, CKeyEvent.ACTION_UP, 0, 0}; // type-action-x-y
  338. if (InteractionManager.IsInteractionSDKEnabled())
  339. {
  340. curState = InteractionManager.GetKeyAction(handType);
  341. touchInfo[1] = curState[CKeyEvent.KEYCODE_CONTROLLER_TOUCHPAD_TOUCH];
  342. if (noloType == (int) CDevice.NOLO_TYPE.LEFT)
  343. {
  344. Vector3 pos = InteractionManager.TouchPadPositionLeft;
  345. touchInfo[2] = pos.x;
  346. touchInfo[3] = pos.y;
  347. }
  348. else
  349. {
  350. Vector3 pos = InteractionManager.TouchPadPositionRight;
  351. touchInfo[2] = pos.x;
  352. touchInfo[3] = pos.y;
  353. }
  354. }
  355. else
  356. {
  357. touchInfo = ControllerAndroid.getTouchEvent(noloType, 1);
  358. curState = ControllerAndroid.getKeyState(noloType, 1);
  359. }
  360. // N
  361. int btnNibiru = curState[CKeyEvent.KEYCODE_BUTTON_NIBIRU];
  362. int btnStart = curState[CKeyEvent.KEYCODE_BUTTON_START];
  363. // Side A/B
  364. int btnSelect = curState[CKeyEvent.KEYCODE_BUTTON_SELECT];
  365. // Menu
  366. int btnApp = curState[CKeyEvent.KEYCODE_BUTTON_APP];
  367. // TouchPad
  368. int btnCenter = curState[CKeyEvent.KEYCODE_DPAD_CENTER];
  369. // Trigger
  370. int btnR1 = curState[CKeyEvent.KEYCODE_BUTTON_R1];
  371. int btnR2 = curState[CKeyEvent.KEYCODE_BUTTON_R2];
  372. int btnInternalTrigger = curState[CKeyEvent.KEYCODE_CONTROLLER_TRIGGER_INTERNAL];
  373. // Nolo TouchPad = Center
  374. // Nolo Menu = App
  375. // Nolo Trigger = R1
  376. // Nolo Side = Select
  377. // Debug.LogError("=======>_currentStates.buttons=" + _currentStates.buttons);
  378. if (touchInfo[1] == CKeyEvent.ACTION_MOVE)
  379. {
  380. _currentStates.touches |= 1 << (int) ButtonID.TrackpadTouch;
  381. _currentStates.touchpadAxis = new Vector2(touchInfo[2], touchInfo[3]);
  382. }
  383. else if (touchInfo[1] == CKeyEvent.ACTION_UP &&
  384. ((_currentStates.touches & (1 << (int) ButtonID.TrackpadTouch)) != 0))
  385. {
  386. _currentStates.touches = 0;
  387. _currentStates.touchpadAxis = new Vector2(0, 0);
  388. }
  389. if (btnCenter == 0)
  390. {
  391. Debug.Log("NewSdk====>ButtonID.TouchPad,Down");
  392. // down
  393. _currentStates.buttons |= 1 << (int) ButtonID.TouchPad;
  394. }
  395. else if (lastState[CKeyEvent.KEYCODE_DPAD_CENTER] == 0)
  396. {
  397. Debug.Log("NewSdk====>ButtonID.TouchPad,Up");
  398. // up
  399. _currentStates.buttons -= 1 << (int) ButtonID.TouchPad;
  400. }
  401. if (btnApp == 0)
  402. {
  403. Debug.Log("NewSdk====>ButtonID.Menu,Down");
  404. _currentStates.buttons |= 1 << (int) ButtonID.Menu;
  405. }
  406. else if (lastState[CKeyEvent.KEYCODE_BUTTON_APP] == 0)
  407. {
  408. Debug.Log("NewSdk====>ButtonID.Menu,up");
  409. _currentStates.buttons -= 1 << (int) ButtonID.Menu;
  410. }
  411. if (btnR1 == 0 || btnR2 == 0)
  412. {
  413. Debug.Log("NewSdk====>ButtonID.Trigger,Down");
  414. _currentStates.buttons |= 1 << (int) ButtonID.Trigger;
  415. }
  416. else if (lastState[CKeyEvent.KEYCODE_BUTTON_R1] == 0 || lastState[CKeyEvent.KEYCODE_BUTTON_R2] == 0)
  417. {
  418. Debug.Log("NewSdk====>ButtonID.Trigger,Up");
  419. _currentStates.buttons -= 1 << (int) ButtonID.Trigger;
  420. }
  421. if (btnInternalTrigger == 0)
  422. {
  423. Debug.Log("NewSdk====>ButtonID.InternalTrigger,Down");
  424. _currentStates.buttons |= 1 << (int) ButtonID.InternalTrigger;
  425. }
  426. else if (lastState[CKeyEvent.KEYCODE_CONTROLLER_TRIGGER_INTERNAL] == 0)
  427. {
  428. Debug.Log("NewSdk====>ButtonID.InternalTrigger,Up");
  429. _currentStates.buttons -= 1 << (int) ButtonID.InternalTrigger;
  430. }
  431. if (btnSelect == 0)
  432. {
  433. Debug.Log("NewSdk====>ButtonID.Grip,Down");
  434. _currentStates.buttons |= 1 << (int) ButtonID.Grip;
  435. }
  436. else if (lastState[CKeyEvent.KEYCODE_BUTTON_SELECT] == 0)
  437. {
  438. Debug.Log("NewSdk====>ButtonID.Grip,Up");
  439. _currentStates.buttons -= 1 << (int) ButtonID.Grip;
  440. }
  441. //Debug.LogError("=====>" + _currentStates.buttons + "->Start=" + btnStart +
  442. // "->Nibiru=" + btnNibiru +
  443. // "->Select=" + btnSelect +
  444. // "->App=" + btnApp +
  445. // "->Center=" + btnCenter +
  446. // "->R1=" + btnR1);
  447. }
  448. public bool IsConneted()
  449. {
  450. return _currentStates.connectStatus == 1;
  451. }
  452. public bool GetButtonDown(ButtonID btn)
  453. {
  454. return (_currentStates.buttons & (1 << (int) btn)) != 0 && (_prevStates.buttons & (1 << (int) btn)) == 0;
  455. }
  456. public bool GetButtonUp(ButtonID btn)
  457. {
  458. return (_currentStates.buttons & (1 << (int) btn)) == 0 && (_prevStates.buttons & (1 << (int) btn)) != 0;
  459. }
  460. public bool GetButtonPressed(ButtonID btn)
  461. {
  462. return (_currentStates.buttons & (1 << (int) btn)) != 0;
  463. }
  464. public bool GetTouchPressed(ButtonID btn)
  465. {
  466. return (_currentStates.touches & (1 << (int) btn)) != 0;
  467. }
  468. public bool GetTouchDown(ButtonID btn)
  469. {
  470. return (_currentStates.touches & (1 << (int) btn)) != 0 && (_prevStates.touches & (1 << (int) btn)) == 0;
  471. }
  472. public bool GetTouchUp(ButtonID btn)
  473. {
  474. return (_currentStates.touches & (1 << (int) btn)) == 0 && (_prevStates.touches & (1 << (int) btn)) != 0;
  475. }
  476. public Vector2 GetTouchPosition(ButtonID axisIndex = ButtonID.TrackpadTouch)
  477. {
  478. if ((_currentStates.touches & (1 << (int) axisIndex)) != 0)
  479. {
  480. return new Vector2(_currentStates.touchpadAxis.x, _currentStates.touchpadAxis.y);
  481. }
  482. return Vector2.zero;
  483. }
  484. }
  485. }