2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
11 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. #if UNITY_5_5_OR_NEWER && !UNITY_2017_1_OR_NEWER
  3. using HTC.UnityPlugin.Utility;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6. using UnityEngine.VR;
  7. #endif
  8. namespace HTC.UnityPlugin.VRModuleManagement
  9. {
  10. public sealed partial class UnityEngineVRModule : VRModule.ModuleBase
  11. {
  12. #if UNITY_5_5_OR_NEWER && !UNITY_2017_1_OR_NEWER
  13. private static readonly Regex m_viveRgx = new Regex("^.*(htc|vive|openvr).*$", RegexOptions.IgnoreCase);
  14. private static readonly Regex m_oculusRgx = new Regex("^.*(oculus).*$", RegexOptions.IgnoreCase);
  15. private static readonly Regex m_leftRgx = new Regex("^.*left.*$", RegexOptions.IgnoreCase);
  16. private static readonly Regex m_rightRgx = new Regex("^.*right.*$", RegexOptions.IgnoreCase);
  17. private const uint HEAD_INDEX = 0u;
  18. private const uint LEFT_INDEX = 1u;
  19. private const uint RIGHT_INDEX = 2u;
  20. private uint m_leftIndex = INVALID_DEVICE_INDEX;
  21. private uint m_rightIndex = INVALID_DEVICE_INDEX;
  22. private string m_leftJoystickName = string.Empty;
  23. private string m_rightJoystickName = string.Empty;
  24. private int m_leftJoystickNameIndex = -1;
  25. private int m_rightJoystickNameIndex = -1;
  26. #if UNITY_5_6_OR_NEWER
  27. private TrackingSpaceType m_prevTrackingSpace;
  28. public override void OnActivated()
  29. {
  30. m_prevTrackingSpace = VRDevice.GetTrackingSpaceType();
  31. UpdateTrackingSpaceType();
  32. EnsureDeviceStateLength(3);
  33. }
  34. public override void OnDeactivated()
  35. {
  36. VRDevice.SetTrackingSpaceType(m_prevTrackingSpace);
  37. }
  38. public override void UpdateTrackingSpaceType()
  39. {
  40. switch (VRModule.trackingSpaceType)
  41. {
  42. case VRModuleTrackingSpaceType.Stationary:
  43. VRDevice.SetTrackingSpaceType(TrackingSpaceType.Stationary);
  44. break;
  45. case VRModuleTrackingSpaceType.RoomScale:
  46. VRDevice.SetTrackingSpaceType(TrackingSpaceType.RoomScale);
  47. break;
  48. }
  49. }
  50. #endif
  51. public override uint GetLeftControllerDeviceIndex() { return m_leftIndex; }
  52. public override uint GetRightControllerDeviceIndex() { return m_rightIndex; }
  53. public override void BeforeRenderUpdate()
  54. {
  55. var joystickNames = default(string[]);
  56. FlushDeviceState();
  57. // head
  58. IVRModuleDeviceState headPrevState;
  59. IVRModuleDeviceStateRW headCurrState;
  60. EnsureValidDeviceState(HEAD_INDEX, out headPrevState, out headCurrState);
  61. headCurrState.isConnected = VRDevice.isPresent;
  62. if (headCurrState.isConnected)
  63. {
  64. if (!headPrevState.isConnected)
  65. {
  66. headCurrState.deviceClass = VRModuleDeviceClass.HMD;
  67. headCurrState.serialNumber = VRDevice.model + " HMD";
  68. headCurrState.modelNumber = VRDevice.model + " HMD";
  69. if (m_viveRgx.IsMatch(VRDevice.model))
  70. {
  71. headCurrState.deviceModel = VRModuleDeviceModel.ViveHMD;
  72. }
  73. else if (m_oculusRgx.IsMatch(VRDevice.model))
  74. {
  75. headCurrState.deviceModel = VRModuleDeviceModel.OculusHMD;
  76. }
  77. else
  78. {
  79. headCurrState.deviceModel = VRModuleDeviceModel.Unknown;
  80. }
  81. headCurrState.renderModelName = VRDevice.model + " " + headCurrState.deviceModel.ToString();
  82. }
  83. headCurrState.position = InputTracking.GetLocalPosition(VRNode.Head);
  84. headCurrState.rotation = InputTracking.GetLocalRotation(VRNode.Head);
  85. headCurrState.isPoseValid = headCurrState.pose != RigidPose.identity && headCurrState.pose != headPrevState.pose;
  86. }
  87. else
  88. {
  89. if (headPrevState.isConnected)
  90. {
  91. headCurrState.Reset();
  92. }
  93. }
  94. // right
  95. IVRModuleDeviceState rightPrevState;
  96. IVRModuleDeviceStateRW rightCurrState;
  97. EnsureValidDeviceState(RIGHT_INDEX, out rightPrevState, out rightCurrState);
  98. rightCurrState.position = InputTracking.GetLocalPosition(VRNode.RightHand);
  99. rightCurrState.rotation = InputTracking.GetLocalRotation(VRNode.RightHand);
  100. rightCurrState.isPoseValid = rightCurrState.pose != RigidPose.identity && rightCurrState.pose != rightPrevState.pose;
  101. // right connected state
  102. if (rightCurrState.isPoseValid)
  103. {
  104. if (!rightPrevState.isConnected)
  105. {
  106. if (joystickNames == null) { joystickNames = Input.GetJoystickNames(); }
  107. for (int i = joystickNames.Length - 1; i >= 0; --i)
  108. {
  109. if (!string.IsNullOrEmpty(joystickNames[i]) && m_rightRgx.IsMatch(joystickNames[i]))
  110. {
  111. rightCurrState.isConnected = true;
  112. m_rightJoystickName = joystickNames[i];
  113. m_rightJoystickNameIndex = i;
  114. m_rightIndex = RIGHT_INDEX;
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if (rightPrevState.isConnected)
  123. {
  124. if (joystickNames == null) { joystickNames = Input.GetJoystickNames(); }
  125. if (string.IsNullOrEmpty(joystickNames[m_rightJoystickNameIndex]))
  126. {
  127. rightCurrState.isConnected = false;
  128. m_rightJoystickName = string.Empty;
  129. m_rightJoystickNameIndex = -1;
  130. m_rightIndex = INVALID_DEVICE_INDEX;
  131. }
  132. }
  133. }
  134. // right input state
  135. if (rightCurrState.isConnected)
  136. {
  137. if (!rightPrevState.isConnected)
  138. {
  139. rightCurrState.deviceClass = VRModuleDeviceClass.Controller;
  140. rightCurrState.serialNumber = m_rightJoystickName;
  141. rightCurrState.modelNumber = VRDevice.model + " Controller";
  142. if (m_viveRgx.IsMatch(VRDevice.model))
  143. {
  144. rightCurrState.deviceModel = VRModuleDeviceModel.ViveController;
  145. rightCurrState.input2DType = VRModuleInput2DType.TouchpadOnly;
  146. }
  147. else if (m_oculusRgx.IsMatch(VRDevice.model))
  148. {
  149. rightCurrState.deviceModel = VRModuleDeviceModel.OculusTouchRight;
  150. rightCurrState.input2DType = VRModuleInput2DType.JoystickOnly;
  151. }
  152. else
  153. {
  154. rightCurrState.deviceModel = VRModuleDeviceModel.Unknown;
  155. rightCurrState.input2DType = VRModuleInput2DType.Unknown;
  156. }
  157. rightCurrState.renderModelName = VRDevice.model + " " + rightCurrState.deviceModel.ToString();
  158. }
  159. UpdateRightControllerInput(rightPrevState, rightCurrState);
  160. }
  161. else
  162. {
  163. if (rightPrevState.isConnected)
  164. {
  165. rightCurrState.Reset();
  166. }
  167. }
  168. // left
  169. IVRModuleDeviceState leftPrevState;
  170. IVRModuleDeviceStateRW leftCurrState;
  171. EnsureValidDeviceState(LEFT_INDEX, out leftPrevState, out leftCurrState);
  172. leftCurrState.position = InputTracking.GetLocalPosition(VRNode.LeftHand);
  173. leftCurrState.rotation = InputTracking.GetLocalRotation(VRNode.LeftHand);
  174. leftCurrState.isPoseValid = leftCurrState.pose != RigidPose.identity && leftCurrState.pose != leftPrevState.pose;
  175. // left connected state
  176. if (leftCurrState.isPoseValid)
  177. {
  178. if (!leftPrevState.isConnected)
  179. {
  180. if (joystickNames == null) { joystickNames = Input.GetJoystickNames(); }
  181. for (int i = joystickNames.Length - 1; i >= 0; --i)
  182. {
  183. if (!string.IsNullOrEmpty(joystickNames[i]) && m_leftRgx.IsMatch(joystickNames[i]))
  184. {
  185. leftCurrState.isConnected = true;
  186. m_leftJoystickName = joystickNames[i];
  187. m_leftJoystickNameIndex = i;
  188. m_leftIndex = LEFT_INDEX;
  189. break;
  190. }
  191. }
  192. }
  193. }
  194. else
  195. {
  196. if (leftPrevState.isConnected)
  197. {
  198. if (joystickNames == null) { joystickNames = Input.GetJoystickNames(); }
  199. if (string.IsNullOrEmpty(joystickNames[m_leftJoystickNameIndex]))
  200. {
  201. leftCurrState.isConnected = false;
  202. m_leftJoystickName = string.Empty;
  203. m_leftJoystickNameIndex = -1;
  204. m_leftIndex = INVALID_DEVICE_INDEX;
  205. }
  206. }
  207. }
  208. // left input state
  209. if (leftCurrState.isConnected)
  210. {
  211. if (!leftPrevState.isConnected)
  212. {
  213. leftCurrState.deviceClass = VRModuleDeviceClass.Controller;
  214. leftCurrState.serialNumber = m_leftJoystickName;
  215. leftCurrState.modelNumber = VRDevice.model + " Controller";
  216. if (m_viveRgx.IsMatch(VRDevice.model))
  217. {
  218. leftCurrState.deviceModel = VRModuleDeviceModel.ViveController;
  219. leftCurrState.input2DType = VRModuleInput2DType.TouchpadOnly;
  220. }
  221. else if (m_oculusRgx.IsMatch(VRDevice.model))
  222. {
  223. leftCurrState.deviceModel = VRModuleDeviceModel.OculusTouchLeft;
  224. leftCurrState.input2DType = VRModuleInput2DType.JoystickOnly;
  225. }
  226. else
  227. {
  228. leftCurrState.deviceModel = VRModuleDeviceModel.Unknown;
  229. leftCurrState.input2DType = VRModuleInput2DType.Unknown;
  230. }
  231. leftCurrState.renderModelName = VRDevice.model + " " + leftCurrState.deviceModel.ToString();
  232. }
  233. UpdateLeftControllerInput(leftPrevState, leftCurrState);
  234. }
  235. else
  236. {
  237. if (leftPrevState.isConnected)
  238. {
  239. leftCurrState.Reset();
  240. }
  241. }
  242. ProcessConnectedDeviceChanged();
  243. ProcessDevicePoseChanged();
  244. ProcessDeviceInputChanged();
  245. }
  246. #endif
  247. }
  248. }