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.

314 lines
11 KiB

5 years ago
  1. //========= Copyright 2016-2020, HTC Corporation. All rights reserved. ===========
  2. using HTC.UnityPlugin.Utility;
  3. using System;
  4. using UnityEngine;
  5. namespace HTC.UnityPlugin.VRModuleManagement
  6. {
  7. public enum VRModuleDeviceClass
  8. {
  9. Invalid,
  10. HMD,
  11. Controller,
  12. GenericTracker,
  13. TrackingReference,
  14. }
  15. public enum VRModuleDeviceModel
  16. {
  17. Unknown,
  18. ViveHMD,
  19. ViveController,
  20. ViveTracker,
  21. ViveBaseStation,
  22. OculusHMD,
  23. OculusTouchLeft,
  24. OculusTouchRight,
  25. OculusSensor,
  26. KnucklesLeft,
  27. KnucklesRight,
  28. DaydreamHMD,
  29. DaydreamController,
  30. ViveFocusHMD,
  31. ViveFocusFinch,
  32. ViveFocusChirp,
  33. OculusGoController,
  34. OculusGearVrController,
  35. WMRHMD,
  36. WMRControllerLeft,
  37. WMRControllerRight,
  38. ViveCosmosControllerLeft,
  39. ViveCosmosControllerRight,
  40. OculusQuestControllerLeft,
  41. OculusQuestControllerRight,
  42. OculusQuestOrRiftSControllerLeft = OculusQuestControllerLeft,
  43. OculusQuestOrRiftSControllerRight = OculusQuestControllerRight,
  44. IndexHMD,
  45. IndexControllerLeft,
  46. IndexControllerRight,
  47. MagicLeapHMD,
  48. MagicLeapController,
  49. }
  50. public enum VRModuleRawButton
  51. {
  52. System = 0,
  53. ApplicationMenu = 1,
  54. Grip = 2,
  55. DPadLeft = 3,
  56. DPadUp = 4,
  57. DPadRight = 5,
  58. DPadDown = 6,
  59. A = 7,
  60. ProximitySensor = 31,
  61. DashboardBack = 2, // Grip
  62. Touchpad = 32, // Axis0
  63. Trigger = 33, // Axis1
  64. CapSenseGrip = 34, // Axis2
  65. Bumper = 35, // Axis3
  66. // alias
  67. Axis0 = 32,
  68. Axis1 = 33,
  69. Axis2 = 34,
  70. Axis3 = 35,
  71. Axis4 = 36,
  72. }
  73. public enum VRModuleRawAxis
  74. {
  75. TouchpadX = Axis0X,
  76. TouchpadY = Axis0Y,
  77. Trigger = Axis1X,
  78. CapSenseGrip = Axis2X,
  79. IndexCurl = Axis3X,
  80. MiddleCurl = Axis3Y,
  81. RingCurl = Axis4X,
  82. PinkyCurl = Axis4Y,
  83. JoystickX = Axis2X,
  84. JoystickY = Axis2Y,
  85. // alias
  86. Axis0X = 0,
  87. Axis0Y,
  88. Axis1X,
  89. Axis1Y,
  90. Axis2X,
  91. Axis2Y,
  92. Axis3X,
  93. Axis3Y,
  94. Axis4X,
  95. Axis4Y,
  96. }
  97. public enum VRModuleInput2DType
  98. {
  99. None,
  100. Unknown,
  101. TouchpadOnly,
  102. ThumbstickOnly,
  103. Both,
  104. TrackpadOnly = TouchpadOnly,
  105. JoystickOnly = ThumbstickOnly,
  106. }
  107. public interface IVRModuleDeviceStateRW
  108. {
  109. uint deviceIndex { get; }
  110. string serialNumber { get; set; }
  111. string modelNumber { get; set; }
  112. string renderModelName { get; set; }
  113. VRModuleDeviceClass deviceClass { get; set; }
  114. VRModuleDeviceModel deviceModel { get; set; }
  115. VRModuleInput2DType input2DType { get; set; }
  116. bool isConnected { get; set; }
  117. bool isPoseValid { get; set; }
  118. bool isOutOfRange { get; set; }
  119. bool isCalibrating { get; set; }
  120. bool isUninitialized { get; set; }
  121. Vector3 velocity { get; set; }
  122. Vector3 angularVelocity { get; set; }
  123. Vector3 position { get; set; }
  124. Quaternion rotation { get; set; }
  125. RigidPose pose { get; set; }
  126. ulong buttonPressed { get; set; }
  127. ulong buttonTouched { get; set; }
  128. float[] axisValue { get; }
  129. bool GetButtonPress(VRModuleRawButton button);
  130. bool GetButtonTouch(VRModuleRawButton button);
  131. float GetAxisValue(VRModuleRawAxis axis);
  132. void SetButtonPress(VRModuleRawButton button, bool value);
  133. void SetButtonTouch(VRModuleRawButton button, bool value);
  134. void SetAxisValue(VRModuleRawAxis axis, float value);
  135. void ResetAxisValues();
  136. void Reset();
  137. }
  138. public interface IVRModuleDeviceState
  139. {
  140. uint deviceIndex { get; }
  141. string serialNumber { get; }
  142. string modelNumber { get; }
  143. string renderModelName { get; }
  144. VRModuleDeviceClass deviceClass { get; }
  145. VRModuleDeviceModel deviceModel { get; }
  146. VRModuleInput2DType input2DType { get; }
  147. bool isConnected { get; }
  148. bool isPoseValid { get; }
  149. bool isOutOfRange { get; }
  150. bool isCalibrating { get; }
  151. bool isUninitialized { get; }
  152. Vector3 velocity { get; }
  153. Vector3 angularVelocity { get; }
  154. Vector3 position { get; }
  155. Quaternion rotation { get; }
  156. RigidPose pose { get; }
  157. ulong buttonPressed { get; }
  158. ulong buttonTouched { get; }
  159. bool GetButtonPress(VRModuleRawButton button);
  160. bool GetButtonTouch(VRModuleRawButton button);
  161. float GetAxisValue(VRModuleRawAxis axis);
  162. }
  163. public partial class VRModule : SingletonBehaviour<VRModule>
  164. {
  165. [Serializable]
  166. private class DeviceState : IVRModuleDeviceState, IVRModuleDeviceStateRW
  167. {
  168. [SerializeField]
  169. private string m_serialNumber;
  170. [SerializeField]
  171. private string m_modelNumber;
  172. [SerializeField]
  173. private string m_renderModelName;
  174. [SerializeField]
  175. private VRModuleDeviceClass m_deviceClass;
  176. [SerializeField]
  177. private VRModuleDeviceModel m_deviceModel;
  178. [SerializeField]
  179. private VRModuleInput2DType m_input2DType;
  180. [SerializeField]
  181. private bool m_isPoseValid;
  182. [SerializeField]
  183. private bool m_isConnected;
  184. [SerializeField]
  185. private bool m_isOutOfRange;
  186. [SerializeField]
  187. private bool m_isCalibrating;
  188. [SerializeField]
  189. private bool m_isUninitialized;
  190. [SerializeField]
  191. private Vector3 m_velocity;
  192. [SerializeField]
  193. private Vector3 m_angularVelocity;
  194. [SerializeField]
  195. private Vector3 m_position;
  196. [SerializeField]
  197. private Quaternion m_rotation;
  198. // device property, changed only when connected or disconnected
  199. public uint deviceIndex { get; private set; }
  200. public string serialNumber { get { return m_serialNumber; } set { m_serialNumber = value; } }
  201. public string modelNumber { get { return m_modelNumber; } set { m_modelNumber = value; } }
  202. public string renderModelName { get { return m_renderModelName; } set { m_renderModelName = value; } }
  203. public VRModuleDeviceClass deviceClass { get { return m_deviceClass; } set { m_deviceClass = value; } }
  204. public VRModuleDeviceModel deviceModel { get { return m_deviceModel; } set { m_deviceModel = value; } }
  205. public VRModuleInput2DType input2DType { get { return m_input2DType; } set { m_input2DType = value; } }
  206. // device pose state
  207. public bool isPoseValid { get { return m_isPoseValid; } set { m_isPoseValid = value; } }
  208. public bool isConnected { get { return m_isConnected; } set { m_isConnected = value; } }
  209. public bool isOutOfRange { get { return m_isOutOfRange; } set { m_isOutOfRange = value; } }
  210. public bool isCalibrating { get { return m_isCalibrating; } set { m_isCalibrating = value; } }
  211. public bool isUninitialized { get { return m_isUninitialized; } set { m_isUninitialized = value; } }
  212. public Vector3 velocity { get { return m_velocity; } set { m_velocity = value; } }
  213. public Vector3 angularVelocity { get { return m_angularVelocity; } set { m_angularVelocity = value; } }
  214. public Vector3 position { get { return m_position; } set { m_position = value; } }
  215. public Quaternion rotation { get { return m_rotation; } set { m_rotation = value; } }
  216. public RigidPose pose { get { return new RigidPose(m_position, m_rotation); } set { m_position = value.pos; m_rotation = value.rot; } }
  217. // device input state
  218. [SerializeField]
  219. private ulong m_buttonPressed;
  220. [SerializeField]
  221. private ulong m_buttonTouched;
  222. [SerializeField]
  223. private float[] m_axisValue;
  224. public ulong buttonPressed { get { return m_buttonPressed; } set { m_buttonPressed = value; } }
  225. public ulong buttonTouched { get { return m_buttonTouched; } set { m_buttonTouched = value; } }
  226. public float[] axisValue { get { return m_axisValue; } }
  227. public bool GetButtonPress(VRModuleRawButton button) { return EnumUtils.GetFlag(m_buttonPressed, (int)button); }
  228. public bool GetButtonTouch(VRModuleRawButton button) { return EnumUtils.GetFlag(m_buttonTouched, (int)button); }
  229. public float GetAxisValue(VRModuleRawAxis axis) { return m_axisValue[(int)axis]; }
  230. public void SetButtonPress(VRModuleRawButton button, bool value) { m_buttonPressed = value ? EnumUtils.SetFlag(m_buttonPressed, (int)button) : EnumUtils.UnsetFlag(m_buttonPressed, (int)button); }
  231. public void SetButtonTouch(VRModuleRawButton button, bool value) { m_buttonTouched = value ? EnumUtils.SetFlag(m_buttonTouched, (int)button) : EnumUtils.UnsetFlag(m_buttonTouched, (int)button); }
  232. public void SetAxisValue(VRModuleRawAxis axis, float value) { m_axisValue[(int)axis] = value; }
  233. public void ResetAxisValues() { Array.Clear(m_axisValue, 0, m_axisValue.Length); }
  234. public DeviceState(uint deviceIndex)
  235. {
  236. this.deviceIndex = deviceIndex;
  237. this.m_axisValue = new float[EnumUtils.GetMaxValue(typeof(VRModuleRawAxis)) + 1];
  238. Reset();
  239. }
  240. public void CopyFrom(DeviceState state)
  241. {
  242. m_serialNumber = state.m_serialNumber;
  243. m_modelNumber = state.m_modelNumber;
  244. m_renderModelName = state.m_renderModelName;
  245. m_deviceClass = state.m_deviceClass;
  246. m_deviceModel = state.m_deviceModel;
  247. m_input2DType = state.m_input2DType;
  248. m_isPoseValid = state.m_isPoseValid;
  249. m_isConnected = state.m_isConnected;
  250. m_isOutOfRange = state.m_isOutOfRange;
  251. m_isCalibrating = state.m_isCalibrating;
  252. m_isUninitialized = state.m_isUninitialized;
  253. m_velocity = state.m_velocity;
  254. m_angularVelocity = state.m_angularVelocity;
  255. m_position = state.m_position;
  256. m_rotation = state.m_rotation;
  257. m_buttonPressed = state.m_buttonPressed;
  258. m_buttonTouched = state.m_buttonTouched;
  259. Array.Copy(state.m_axisValue, m_axisValue, m_axisValue.Length);
  260. }
  261. public void Reset()
  262. {
  263. deviceClass = VRModuleDeviceClass.Invalid;
  264. input2DType = VRModuleInput2DType.None;
  265. serialNumber = string.Empty;
  266. modelNumber = string.Empty;
  267. renderModelName = string.Empty;
  268. isConnected = false;
  269. isPoseValid = false;
  270. isOutOfRange = false;
  271. isCalibrating = false;
  272. isUninitialized = false;
  273. velocity = Vector3.zero;
  274. angularVelocity = Vector3.zero;
  275. m_position = Vector3.zero;
  276. m_rotation = Quaternion.identity;
  277. m_buttonPressed = 0ul;
  278. m_buttonTouched = 0ul;
  279. ResetAxisValues();
  280. }
  281. }
  282. }
  283. }