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.

373 lines
18 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. namespace HTC.UnityPlugin.VRModuleManagement
  8. {
  9. public partial class VRModule : SingletonBehaviour<VRModule>
  10. {
  11. public abstract class ModuleBase
  12. {
  13. protected enum DefaultModuleOrder
  14. {
  15. Simulator = 1,
  16. UnityNativeVR,
  17. UnityXR,
  18. SteamVR,
  19. OculusVR,
  20. DayDream,
  21. WaveVR,
  22. }
  23. [Obsolete("Module should set their own MAX_DEVICE_COUNT, use EnsureDeviceStateLength to set, VRModule.GetDeviceStateCount() to get")]
  24. protected const uint MAX_DEVICE_COUNT = VRModule.MAX_DEVICE_COUNT;
  25. protected const uint INVALID_DEVICE_INDEX = VRModule.INVALID_DEVICE_INDEX;
  26. private static readonly Regex s_viveRgx = new Regex("^.*(vive|htc).*$", RegexOptions.IgnoreCase);
  27. private static readonly Regex s_viveCosmosRgx = new Regex("^.*(cosmos).*$", RegexOptions.IgnoreCase);
  28. private static readonly Regex s_oculusRgx = new Regex("^.*(oculus).*$", RegexOptions.IgnoreCase);
  29. private static readonly Regex s_indexRgx = new Regex("^.*(index|knuckles).*$", RegexOptions.IgnoreCase);
  30. private static readonly Regex s_knucklesRgx = new Regex("^.*(knu_ev1).*$", RegexOptions.IgnoreCase);
  31. private static readonly Regex s_daydreamRgx = new Regex("^.*(daydream).*$", RegexOptions.IgnoreCase);
  32. private static readonly Regex s_wmrRgx = new Regex("(^.*(asus|acer|dell|lenovo|hp|samsung|windowsmr).*(mr|$))|spatial", RegexOptions.IgnoreCase);
  33. private static readonly Regex s_magicLeapRgx = new Regex("^.*(magicleap).*$", RegexOptions.IgnoreCase);
  34. private static readonly Regex s_waveVrRgx = new Regex("^.*(wvr).*$", RegexOptions.IgnoreCase);
  35. private static readonly Regex s_leftRgx = new Regex("^.*(left|_l).*$", RegexOptions.IgnoreCase);
  36. private static readonly Regex s_rightRgx = new Regex("^.*(right|_r).*$", RegexOptions.IgnoreCase);
  37. private struct WVRCtrlProfile
  38. {
  39. public VRModuleDeviceModel model;
  40. public VRModuleInput2DType input2D;
  41. }
  42. private static Dictionary<string, WVRCtrlProfile> m_wvrModels = new Dictionary<string, WVRCtrlProfile>
  43. {
  44. { "WVR_CONTROLLER_FINCH3DOF_2_0", new WVRCtrlProfile() { model = VRModuleDeviceModel.ViveFocusFinch, input2D = VRModuleInput2DType.TouchpadOnly } },
  45. { "WVR_CONTROLLER_ASPEN_MI6_1", new WVRCtrlProfile() { model = VRModuleDeviceModel.ViveFocusChirp, input2D = VRModuleInput2DType.TouchpadOnly } },
  46. };
  47. public bool isActivated { get; private set; }
  48. public virtual int moduleOrder { get { return moduleIndex; } }
  49. public abstract int moduleIndex { get; }
  50. public virtual bool ShouldActiveModule() { return false; }
  51. public void Activated()
  52. {
  53. isActivated = true;
  54. OnActivated();
  55. }
  56. public void Deactivated()
  57. {
  58. isActivated = false;
  59. OnDeactivated();
  60. }
  61. public virtual void OnActivated() { }
  62. public virtual void OnDeactivated() { }
  63. public virtual bool HasInputFocus() { return true; }
  64. public virtual uint GetLeftControllerDeviceIndex() { return INVALID_DEVICE_INDEX; }
  65. public virtual uint GetRightControllerDeviceIndex() { return INVALID_DEVICE_INDEX; }
  66. public virtual void UpdateTrackingSpaceType() { }
  67. public virtual void Update() { }
  68. public virtual void FixedUpdate() { }
  69. public virtual void LateUpdate() { }
  70. public virtual void BeforeRenderUpdate() { }
  71. [Obsolete]
  72. public virtual void UpdateDeviceState(IVRModuleDeviceState[] prevState, IVRModuleDeviceStateRW[] currState) { }
  73. public virtual void TriggerViveControllerHaptic(uint deviceIndex, ushort durationMicroSec = 500) { }
  74. public virtual void TriggerHapticVibration(uint deviceIndex, float durationSeconds = 0.01f, float frequency = 85f, float amplitude = 0.125f, float startSecondsFromNow = 0f) { }
  75. protected void InvokeInputFocusEvent(bool value)
  76. {
  77. VRModule.InvokeInputFocusEvent(value);
  78. }
  79. protected void InvokeControllerRoleChangedEvent()
  80. {
  81. VRModule.InvokeControllerRoleChangedEvent();
  82. }
  83. protected uint GetDeviceStateLength()
  84. {
  85. return Instance.GetDeviceStateLength();
  86. }
  87. protected void EnsureDeviceStateLength(uint capacity)
  88. {
  89. Instance.EnsureDeviceStateLength(capacity);
  90. }
  91. protected bool TryGetValidDeviceState(uint index, out IVRModuleDeviceState prevState, out IVRModuleDeviceStateRW currState)
  92. {
  93. return Instance.TryGetValidDeviceState(index, out prevState, out currState);
  94. }
  95. protected void EnsureValidDeviceState(uint index, out IVRModuleDeviceState prevState, out IVRModuleDeviceStateRW currState)
  96. {
  97. Instance.EnsureValidDeviceState(index, out prevState, out currState);
  98. }
  99. protected void FlushDeviceState()
  100. {
  101. Instance.ModuleFlushDeviceState();
  102. }
  103. protected void ProcessConnectedDeviceChanged()
  104. {
  105. Instance.ModuleConnectedDeviceChanged();
  106. }
  107. protected void ProcessDevicePoseChanged()
  108. {
  109. InvokeNewPosesEvent();
  110. }
  111. protected void ProcessDeviceInputChanged()
  112. {
  113. InvokeNewInputEvent();
  114. }
  115. protected static void SetupKnownDeviceModel(IVRModuleDeviceStateRW deviceState)
  116. {
  117. if (s_viveRgx.IsMatch(deviceState.modelNumber) || s_viveRgx.IsMatch(deviceState.renderModelName))
  118. {
  119. switch (deviceState.deviceClass)
  120. {
  121. case VRModuleDeviceClass.HMD:
  122. deviceState.deviceModel = VRModuleDeviceModel.ViveHMD;
  123. return;
  124. case VRModuleDeviceClass.Controller:
  125. if (s_viveCosmosRgx.IsMatch(deviceState.modelNumber))
  126. {
  127. if (s_leftRgx.IsMatch(deviceState.renderModelName))
  128. {
  129. deviceState.deviceModel = VRModuleDeviceModel.ViveCosmosControllerLeft;
  130. }
  131. else if (s_rightRgx.IsMatch(deviceState.renderModelName))
  132. {
  133. deviceState.deviceModel = VRModuleDeviceModel.ViveCosmosControllerRight;
  134. }
  135. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  136. }
  137. else
  138. {
  139. deviceState.deviceModel = VRModuleDeviceModel.ViveController;
  140. deviceState.input2DType = VRModuleInput2DType.TouchpadOnly;
  141. }
  142. return;
  143. case VRModuleDeviceClass.GenericTracker:
  144. deviceState.deviceModel = VRModuleDeviceModel.ViveTracker;
  145. return;
  146. case VRModuleDeviceClass.TrackingReference:
  147. deviceState.deviceModel = VRModuleDeviceModel.ViveBaseStation;
  148. return;
  149. }
  150. }
  151. else if (s_oculusRgx.IsMatch(deviceState.modelNumber))
  152. {
  153. switch (deviceState.deviceClass)
  154. {
  155. case VRModuleDeviceClass.HMD:
  156. deviceState.deviceModel = VRModuleDeviceModel.OculusHMD;
  157. return;
  158. case VRModuleDeviceClass.Controller:
  159. if (Application.platform == RuntimePlatform.Android)
  160. {
  161. if (deviceState.modelNumber.Contains("Go"))
  162. {
  163. deviceState.deviceModel = VRModuleDeviceModel.OculusGoController;
  164. deviceState.input2DType = VRModuleInput2DType.TouchpadOnly;
  165. return;
  166. }
  167. else if (s_leftRgx.IsMatch(deviceState.modelNumber))
  168. {
  169. deviceState.deviceModel = VRModuleDeviceModel.OculusQuestControllerLeft;
  170. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  171. return;
  172. }
  173. else if (s_rightRgx.IsMatch(deviceState.modelNumber))
  174. {
  175. deviceState.deviceModel = VRModuleDeviceModel.OculusQuestControllerRight;
  176. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  177. return;
  178. }
  179. }
  180. else
  181. {
  182. if (deviceState.modelNumber.Contains("Rift S"))
  183. {
  184. if (s_leftRgx.IsMatch(deviceState.modelNumber))
  185. {
  186. deviceState.deviceModel = VRModuleDeviceModel.OculusQuestControllerLeft;
  187. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  188. return;
  189. }
  190. else if (s_rightRgx.IsMatch(deviceState.modelNumber))
  191. {
  192. deviceState.deviceModel = VRModuleDeviceModel.OculusQuestControllerRight;
  193. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  194. return;
  195. }
  196. }
  197. else
  198. {
  199. if (s_leftRgx.IsMatch(deviceState.modelNumber))
  200. {
  201. deviceState.deviceModel = VRModuleDeviceModel.OculusTouchLeft;
  202. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  203. return;
  204. }
  205. else if (s_rightRgx.IsMatch(deviceState.modelNumber))
  206. {
  207. deviceState.deviceModel = VRModuleDeviceModel.OculusTouchRight;
  208. deviceState.input2DType = VRModuleInput2DType.JoystickOnly;
  209. return;
  210. }
  211. }
  212. }
  213. break;
  214. case VRModuleDeviceClass.TrackingReference:
  215. deviceState.deviceModel = VRModuleDeviceModel.OculusSensor;
  216. return;
  217. }
  218. }
  219. else if (s_wmrRgx.IsMatch(deviceState.modelNumber) || s_wmrRgx.IsMatch(deviceState.renderModelName))
  220. {
  221. switch (deviceState.deviceClass)
  222. {
  223. case VRModuleDeviceClass.HMD:
  224. deviceState.deviceModel = VRModuleDeviceModel.WMRHMD;
  225. return;
  226. case VRModuleDeviceClass.Controller:
  227. if (s_leftRgx.IsMatch(deviceState.modelNumber))
  228. {
  229. deviceState.deviceModel = VRModuleDeviceModel.WMRControllerLeft;
  230. deviceState.input2DType = VRModuleInput2DType.Both;
  231. return;
  232. }
  233. else if (s_rightRgx.IsMatch(deviceState.modelNumber))
  234. {
  235. deviceState.deviceModel = VRModuleDeviceModel.WMRControllerRight;
  236. deviceState.input2DType = VRModuleInput2DType.Both;
  237. return;
  238. }
  239. break;
  240. }
  241. }
  242. else if (s_indexRgx.IsMatch(deviceState.modelNumber) || s_indexRgx.IsMatch(deviceState.renderModelName))
  243. {
  244. switch (deviceState.deviceClass)
  245. {
  246. case VRModuleDeviceClass.HMD:
  247. deviceState.deviceModel = VRModuleDeviceModel.IndexHMD;
  248. return;
  249. case VRModuleDeviceClass.Controller:
  250. deviceState.input2DType = VRModuleInput2DType.TouchpadOnly;
  251. if (s_leftRgx.IsMatch(deviceState.renderModelName))
  252. {
  253. if (s_knucklesRgx.IsMatch(deviceState.renderModelName))
  254. {
  255. deviceState.deviceModel = VRModuleDeviceModel.KnucklesLeft;
  256. }
  257. else
  258. {
  259. deviceState.deviceModel = VRModuleDeviceModel.IndexControllerLeft;
  260. #if VIU_STEAMVR_2_0_0_OR_NEWER || (UNITY_2019_3_OR_NEWER && VIU_XR_GENERAL_SETTINGS)
  261. deviceState.input2DType = VRModuleInput2DType.Both;
  262. #endif
  263. }
  264. }
  265. else if (s_rightRgx.IsMatch(deviceState.renderModelName))
  266. {
  267. if (s_knucklesRgx.IsMatch(deviceState.renderModelName))
  268. {
  269. deviceState.deviceModel = VRModuleDeviceModel.KnucklesRight;
  270. }
  271. else
  272. {
  273. deviceState.deviceModel = VRModuleDeviceModel.IndexControllerRight;
  274. #if VIU_STEAMVR_2_0_0_OR_NEWER || (UNITY_2019_3_OR_NEWER && VIU_XR_GENERAL_SETTINGS)
  275. deviceState.input2DType = VRModuleInput2DType.Both;
  276. #endif
  277. }
  278. }
  279. return;
  280. case VRModuleDeviceClass.TrackingReference:
  281. deviceState.deviceModel = VRModuleDeviceModel.ViveBaseStation;
  282. return;
  283. }
  284. }
  285. else if (s_daydreamRgx.IsMatch(deviceState.modelNumber))
  286. {
  287. switch (deviceState.deviceClass)
  288. {
  289. case VRModuleDeviceClass.HMD:
  290. deviceState.deviceModel = VRModuleDeviceModel.DaydreamHMD;
  291. return;
  292. case VRModuleDeviceClass.Controller:
  293. deviceState.deviceModel = VRModuleDeviceModel.DaydreamController;
  294. deviceState.input2DType = VRModuleInput2DType.TrackpadOnly;
  295. return;
  296. }
  297. }
  298. else if (s_magicLeapRgx.IsMatch(deviceState.modelNumber))
  299. {
  300. switch (deviceState.deviceClass)
  301. {
  302. case VRModuleDeviceClass.HMD:
  303. deviceState.deviceModel = VRModuleDeviceModel.MagicLeapHMD;
  304. return;
  305. case VRModuleDeviceClass.Controller:
  306. deviceState.deviceModel = VRModuleDeviceModel.MagicLeapController;
  307. deviceState.input2DType = VRModuleInput2DType.TouchpadOnly;
  308. return;
  309. }
  310. }
  311. else if (s_waveVrRgx.IsMatch(deviceState.modelNumber))
  312. {
  313. switch (deviceState.deviceClass)
  314. {
  315. case VRModuleDeviceClass.HMD:
  316. deviceState.deviceModel = VRModuleDeviceModel.ViveFocusHMD;
  317. return;
  318. case VRModuleDeviceClass.Controller:
  319. {
  320. WVRCtrlProfile profile;
  321. if (m_wvrModels.TryGetValue(deviceState.modelNumber, out profile))
  322. {
  323. deviceState.deviceModel = profile.model;
  324. deviceState.input2DType = profile.input2D;
  325. return;
  326. }
  327. }
  328. break;
  329. }
  330. }
  331. deviceState.deviceModel = VRModuleDeviceModel.Unknown;
  332. }
  333. public static bool AxisToPress(bool previousPressedState, float currentAxisValue, float setThreshold, float unsetThreshold)
  334. {
  335. return previousPressedState ? currentAxisValue > unsetThreshold : currentAxisValue >= setThreshold;
  336. }
  337. }
  338. private sealed class DefaultModule : ModuleBase
  339. {
  340. public override int moduleIndex { get { return (int)VRModuleActiveEnum.None; } }
  341. }
  342. }
  343. }