Assignment for RMIT Mixed Reality in 2020
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.

791 lines
39 KiB

  1. // Avatar Hands|Prefabs|0150
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System;
  6. using System.Collections;
  7. [Serializable]
  8. public sealed class AxisOverrides
  9. {
  10. /// <summary>
  11. /// Determine when to apply the override.
  12. /// </summary>
  13. public enum ApplyOverrideType
  14. {
  15. /// <summary>
  16. /// Never apply the override.
  17. /// </summary>
  18. Never,
  19. /// <summary>
  20. /// Always apply the override.
  21. /// </summary>
  22. Always,
  23. /// <summary>
  24. /// Only apply the override when the state is set to digital.
  25. /// </summary>
  26. DigitalState,
  27. /// <summary>
  28. /// Only apply the override when the state is set to axis.
  29. /// </summary>
  30. AxisState,
  31. /// <summary>
  32. /// Only apply the override when the state is set to sense axis.
  33. /// </summary>
  34. SenseAxisState,
  35. /// <summary>
  36. /// Only apply the override when the state is set to axis or sense axis.
  37. /// </summary>
  38. AxisAndSenseAxisState
  39. }
  40. [Header("Global Override Settings")]
  41. [Tooltip("Determines whether to ignore all of the given overrides on an Interaction event.")]
  42. public bool ignoreAllOverrides = true;
  43. [Tooltip("Sets the Animation parameter for the interaction type and can be used to change the Idle pose based on interaction type.")]
  44. public float stateValue = -1f;
  45. [Header("Thumb Override Settings")]
  46. [Tooltip("Determines when to apply the given thumb override.")]
  47. public ApplyOverrideType applyThumbOverride = ApplyOverrideType.Always;
  48. [Tooltip("The axis override for the thumb on an Interact Touch event. Will only be applicable if the thumb button state is not touched.")]
  49. [Range(0f, 1f)]
  50. public float thumbOverride;
  51. [Header("Index Finger Override Settings")]
  52. [Tooltip("Determines when to apply the given index finger override.")]
  53. public ApplyOverrideType applyIndexOverride = ApplyOverrideType.Always;
  54. [Tooltip("The axis override for the index finger on an Interact Touch event. Will only be applicable if the index finger button state is not touched.")]
  55. [Range(0f, 1f)]
  56. public float indexOverride;
  57. [Header("Middle Finger Override Settings")]
  58. [Tooltip("Determines when to apply the given middle finger override.")]
  59. public ApplyOverrideType applyMiddleOverride = ApplyOverrideType.Always;
  60. [Tooltip("The axis override for the middle finger on an Interact Touch event. Will only be applicable if the middle finger button state is not touched.")]
  61. [Range(0f, 1f)]
  62. public float middleOverride;
  63. [Header("Ring Finger Override Settings")]
  64. [Tooltip("Determines when to apply the given ring finger override.")]
  65. public ApplyOverrideType applyRingOverride = ApplyOverrideType.Always;
  66. [Tooltip("The axis override for the ring finger on an Interact Touch event. Will only be applicable if the ring finger button state is not touched.")]
  67. [Range(0f, 1f)]
  68. public float ringOverride;
  69. [Header("Pinky Finger Override Settings")]
  70. [Tooltip("Determines when to apply the given pinky finger override.")]
  71. public ApplyOverrideType applyPinkyOverride = ApplyOverrideType.Always;
  72. [Tooltip("The axis override for the pinky finger on an Interact Touch event. Will only be applicable if the pinky finger button state is not touched.")]
  73. [Range(0f, 1f)]
  74. public float pinkyOverride;
  75. }
  76. /// <summary>
  77. /// Provides a custom controller hand model with psuedo finger functionality.
  78. /// </summary>
  79. /// <remarks>
  80. /// **Prefab Usage:**
  81. /// * Place the `VRTK/Prefabs/AvatarHands/BasicHands/VRTK_BasicHand` prefab as a child of either the left or right script alias.
  82. /// * If the prefab is being used in the left hand then check the `Mirror Model` parameter.
  83. /// * By default, the avatar hand controller will detect which controller is connected and represent it accordingly.
  84. /// * Optionally, use SDKTransformModify scripts to adjust the hand orientation based on different controller types.
  85. /// </remarks>
  86. /// <example>
  87. /// `032_Controller_CustomControllerModel` uses the `VRTK_BasicHand` prefab to display custom avatar hands for the left and right controller.
  88. /// </example>
  89. public class VRTK_AvatarHandController : MonoBehaviour
  90. {
  91. [Header("Hand Settings")]
  92. [Tooltip("The controller type to use for default finger settings.")]
  93. public SDK_BaseController.ControllerType controllerType;
  94. [Tooltip("Determines whether the Finger and State settings are auto set based on the connected controller type.")]
  95. public bool setFingersForControllerType = true;
  96. [Tooltip("If this is checked then the model will be mirrored, tick this if the avatar hand is for the left hand controller.")]
  97. public bool mirrorModel = false;
  98. [Tooltip("The speed in which a finger will transition to it's destination position if the finger state is `Digital`.")]
  99. public float animationSnapSpeed = 0.1f;
  100. [Header("Digital Finger Settings")]
  101. [Tooltip("The button alias to control the thumb if the thumb state is `Digital`.")]
  102. public VRTK_ControllerEvents.ButtonAlias thumbButton = VRTK_ControllerEvents.ButtonAlias.TouchpadTouch;
  103. [Tooltip("The button alias to control the index finger if the index finger state is `Digital`.")]
  104. public VRTK_ControllerEvents.ButtonAlias indexButton = VRTK_ControllerEvents.ButtonAlias.TriggerPress;
  105. [Tooltip("The button alias to control the middle finger if the middle finger state is `Digital`.")]
  106. public VRTK_ControllerEvents.ButtonAlias middleButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  107. [Tooltip("The button alias to control the ring finger if the ring finger state is `Digital`.")]
  108. public VRTK_ControllerEvents.ButtonAlias ringButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  109. [Tooltip("The button alias to control the pinky finger if the pinky finger state is `Digital`.")]
  110. public VRTK_ControllerEvents.ButtonAlias pinkyButton = VRTK_ControllerEvents.ButtonAlias.Undefined;
  111. [Tooltip("The button alias to control the middle, ring and pinky finger if the three finger state is `Digital`.")]
  112. public VRTK_ControllerEvents.ButtonAlias threeFingerButton = VRTK_ControllerEvents.ButtonAlias.GripPress;
  113. [Header("Axis Finger Settings")]
  114. [Tooltip("The button type to listen for axis changes to control the thumb.")]
  115. public SDK_BaseController.ButtonTypes thumbAxisButton = SDK_BaseController.ButtonTypes.Touchpad;
  116. [Tooltip("The button type to listen for axis changes to control the index finger.")]
  117. public SDK_BaseController.ButtonTypes indexAxisButton = SDK_BaseController.ButtonTypes.Trigger;
  118. [Tooltip("The button type to listen for axis changes to control the middle finger.")]
  119. public SDK_BaseController.ButtonTypes middleAxisButton = SDK_BaseController.ButtonTypes.MiddleFinger;
  120. [Tooltip("The button type to listen for axis changes to control the ring finger.")]
  121. public SDK_BaseController.ButtonTypes ringAxisButton = SDK_BaseController.ButtonTypes.RingFinger;
  122. [Tooltip("The button type to listen for axis changes to control the pinky finger.")]
  123. public SDK_BaseController.ButtonTypes pinkyAxisButton = SDK_BaseController.ButtonTypes.PinkyFinger;
  124. [Tooltip("The button type to listen for axis changes to control the middle, ring and pinky finger.")]
  125. public SDK_BaseController.ButtonTypes threeFingerAxisButton = SDK_BaseController.ButtonTypes.Grip;
  126. [Header("Finger State Settings")]
  127. [Tooltip("The Axis Type to utilise when dealing with the thumb state. Not all controllers support all axis types on all of the available buttons.")]
  128. public VRTK_ControllerEvents.AxisType thumbState = VRTK_ControllerEvents.AxisType.Digital;
  129. public VRTK_ControllerEvents.AxisType indexState = VRTK_ControllerEvents.AxisType.Digital;
  130. public VRTK_ControllerEvents.AxisType middleState = VRTK_ControllerEvents.AxisType.Digital;
  131. public VRTK_ControllerEvents.AxisType ringState = VRTK_ControllerEvents.AxisType.Digital;
  132. public VRTK_ControllerEvents.AxisType pinkyState = VRTK_ControllerEvents.AxisType.Digital;
  133. public VRTK_ControllerEvents.AxisType threeFingerState = VRTK_ControllerEvents.AxisType.Digital;
  134. [Header("Finger Axis Overrides")]
  135. [Tooltip("Finger axis overrides on an Interact NearTouch event.")]
  136. public AxisOverrides nearTouchOverrides;
  137. [Tooltip("Finger axis overrides on an Interact Touch event.")]
  138. public AxisOverrides touchOverrides;
  139. [Tooltip("Finger axis overrides on an Interact Grab event.")]
  140. public AxisOverrides grabOverrides;
  141. [Tooltip("Finger axis overrides on an Interact Use event.")]
  142. public AxisOverrides useOverrides;
  143. [Header("Custom Settings")]
  144. [Tooltip("The Transform that contains the avatar hand model. If this is left blank then a child GameObject named `Model` will be searched for to use as the Transform.")]
  145. public Transform handModel;
  146. [Tooltip("The controller to listen for the events on. If this is left blank as it will be auto populated by finding the Controller Events script on the parent GameObject.")]
  147. public VRTK_ControllerEvents controllerEvents;
  148. [Tooltip("An optional Interact NearTouch to listen for near touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact NearTouch script on the parent GameObject.")]
  149. public VRTK_InteractNearTouch interactNearTouch;
  150. [Tooltip("An optional Interact Touch to listen for touch events on. If this is left blank as it will attempt to be auto populated by finding the Interact Touch script on the parent GameObject.")]
  151. public VRTK_InteractTouch interactTouch;
  152. [Tooltip("An optional Interact Grab to listen for grab events on. If this is left blank as it will attempt to be auto populated by finding the Interact Grab script on the parent GameObject.")]
  153. public VRTK_InteractGrab interactGrab;
  154. [Tooltip("An optional Interact Use to listen for use events on. If this is left blank as it will attempt to be auto populated by finding the Interact Use script on the parent GameObject.")]
  155. public VRTK_InteractUse interactUse;
  156. #region Protected class variables
  157. protected Animator animator;
  158. protected enum OverrideState
  159. {
  160. NoOverride,
  161. IsOverriding,
  162. WasOverring,
  163. KeepOverring
  164. }
  165. // Index Finger Mapping: 0 = thumb, 1 = index, 2 = middle, 3 = ring, 4 = pinky
  166. protected bool[] fingerStates = new bool[5];
  167. protected bool[] fingerChangeStates = new bool[5];
  168. protected float[] fingerAxis = new float[5];
  169. protected float[] fingerRawAxis = new float[5];
  170. protected float[] fingerUntouchedAxis = new float[5];
  171. protected float[] fingerSaveAxis = new float[5];
  172. protected float[] fingerForceAxis = new float[5];
  173. protected OverrideState[] overrideAxisValues = new OverrideState[5];
  174. protected VRTK_ControllerEvents.AxisType[] axisTypes = new VRTK_ControllerEvents.AxisType[5];
  175. protected Coroutine[] fingerAnimationRoutine = new Coroutine[5];
  176. protected VRTK_ControllerEvents.ButtonAlias savedThumbButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  177. protected VRTK_ControllerEvents.ButtonAlias savedIndexButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  178. protected VRTK_ControllerEvents.ButtonAlias savedMiddleButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  179. protected VRTK_ControllerEvents.ButtonAlias savedRingButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  180. protected VRTK_ControllerEvents.ButtonAlias savedPinkyButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  181. protected VRTK_ControllerEvents.ButtonAlias savedThreeFingerButtonState = VRTK_ControllerEvents.ButtonAlias.Undefined;
  182. protected SDK_BaseController.ButtonTypes savedThumbAxisButtonState;
  183. protected SDK_BaseController.ButtonTypes savedIndexAxisButtonState;
  184. protected SDK_BaseController.ButtonTypes savedMiddleAxisButtonState;
  185. protected SDK_BaseController.ButtonTypes savedRingAxisButtonState;
  186. protected SDK_BaseController.ButtonTypes savedPinkyAxisButtonState;
  187. protected SDK_BaseController.ButtonTypes savedThreeFingerAxisButtonState;
  188. protected VRTK_ControllerReference controllerReference;
  189. #endregion Protected class variables
  190. #region MonoBehaviour methods
  191. protected virtual void OnEnable()
  192. {
  193. animator = GetComponent<Animator>();
  194. controllerEvents = (controllerEvents != null ? controllerEvents : GetComponentInParent<VRTK_ControllerEvents>());
  195. interactNearTouch = (interactNearTouch != null ? interactNearTouch : GetComponentInParent<VRTK_InteractNearTouch>());
  196. interactTouch = (interactTouch != null ? interactTouch : GetComponentInParent<VRTK_InteractTouch>());
  197. interactGrab = (interactGrab != null ? interactGrab : GetComponentInParent<VRTK_InteractGrab>());
  198. interactUse = (interactUse != null ? interactUse : GetComponentInParent<VRTK_InteractUse>());
  199. controllerReference = VRTK_ControllerReference.GetControllerReference(controllerEvents.gameObject);
  200. }
  201. protected virtual void OnDisable()
  202. {
  203. UnsubscribeEvents();
  204. controllerType = SDK_BaseController.ControllerType.Undefined;
  205. for (int i = 0; i < fingerAnimationRoutine.Length; i++)
  206. {
  207. if (fingerAnimationRoutine[i] != null)
  208. {
  209. fingerAnimationRoutine[i] = null;
  210. }
  211. }
  212. }
  213. protected virtual void Update()
  214. {
  215. if (controllerType == SDK_BaseController.ControllerType.Undefined)
  216. {
  217. DetectController();
  218. }
  219. if (animator != null)
  220. {
  221. ProcessFinger(thumbState, 0);
  222. ProcessFinger(indexState, 1);
  223. ProcessFinger(middleState, 2);
  224. ProcessFinger(ringState, 3);
  225. ProcessFinger(pinkyState, 4);
  226. }
  227. }
  228. #endregion MonoBehaviour methods
  229. protected virtual void SubscribeButtonEvent(VRTK_ControllerEvents.ButtonAlias buttonType, ref VRTK_ControllerEvents.ButtonAlias saveType, ControllerInteractionEventHandler eventHandler)
  230. {
  231. if (buttonType != VRTK_ControllerEvents.ButtonAlias.Undefined)
  232. {
  233. saveType = buttonType;
  234. controllerEvents.SubscribeToButtonAliasEvent(buttonType, true, eventHandler);
  235. controllerEvents.SubscribeToButtonAliasEvent(buttonType, false, eventHandler);
  236. }
  237. }
  238. protected virtual void UnsubscribeButtonEvent(VRTK_ControllerEvents.ButtonAlias buttonType, ControllerInteractionEventHandler eventHandler)
  239. {
  240. if (buttonType != VRTK_ControllerEvents.ButtonAlias.Undefined)
  241. {
  242. controllerEvents.UnsubscribeToButtonAliasEvent(buttonType, true, eventHandler);
  243. controllerEvents.UnsubscribeToButtonAliasEvent(buttonType, false, eventHandler);
  244. }
  245. }
  246. protected virtual void SubscribeButtonAxisEvent(SDK_BaseController.ButtonTypes buttonType, ref SDK_BaseController.ButtonTypes saveType, VRTK_ControllerEvents.AxisType axisType, ControllerInteractionEventHandler eventHandler)
  247. {
  248. saveType = buttonType;
  249. controllerEvents.SubscribeToAxisAliasEvent(buttonType, axisType, eventHandler);
  250. }
  251. protected virtual void UnsubscribeButtonAxisEvent(SDK_BaseController.ButtonTypes buttonType, VRTK_ControllerEvents.AxisType axisType, ControllerInteractionEventHandler eventHandler)
  252. {
  253. controllerEvents.UnsubscribeToAxisAliasEvent(buttonType, axisType, eventHandler);
  254. }
  255. #region Subscription Managers
  256. protected virtual void SubscribeEvents()
  257. {
  258. if (controllerEvents != null)
  259. {
  260. SubscribeButtonEvent(thumbButton, ref savedThumbButtonState, DoThumbEvent);
  261. SubscribeButtonEvent(indexButton, ref savedIndexButtonState, DoIndexEvent);
  262. SubscribeButtonEvent(middleButton, ref savedMiddleButtonState, DoMiddleEvent);
  263. SubscribeButtonEvent(ringButton, ref savedRingButtonState, DoRingEvent);
  264. SubscribeButtonEvent(pinkyButton, ref savedPinkyButtonState, DoPinkyEvent);
  265. SubscribeButtonEvent(threeFingerButton, ref savedThreeFingerButtonState, DoThreeFingerEvent);
  266. SubscribeButtonAxisEvent(thumbAxisButton, ref savedThumbAxisButtonState, thumbState, DoThumbAxisEvent);
  267. SubscribeButtonAxisEvent(indexAxisButton, ref savedIndexAxisButtonState, indexState, DoIndexAxisEvent);
  268. SubscribeButtonAxisEvent(middleAxisButton, ref savedMiddleAxisButtonState, middleState, DoMiddleAxisEvent);
  269. SubscribeButtonAxisEvent(ringAxisButton, ref savedRingAxisButtonState, ringState, DoRingAxisEvent);
  270. SubscribeButtonAxisEvent(pinkyAxisButton, ref savedPinkyAxisButtonState, pinkyState, DoPinkyAxisEvent);
  271. SubscribeButtonAxisEvent(threeFingerAxisButton, ref savedThreeFingerAxisButtonState, threeFingerState, DoThreeFingerAxisEvent);
  272. }
  273. if (interactNearTouch != null)
  274. {
  275. interactNearTouch.ControllerNearTouchInteractableObject += DoControllerNearTouch;
  276. interactNearTouch.ControllerNearUntouchInteractableObject += DoControllerNearUntouch;
  277. }
  278. if (interactTouch != null)
  279. {
  280. interactTouch.ControllerTouchInteractableObject += DoControllerTouch;
  281. interactTouch.ControllerUntouchInteractableObject += DoControllerUntouch;
  282. }
  283. if (interactGrab != null)
  284. {
  285. interactGrab.ControllerGrabInteractableObject += DoControllerGrab;
  286. interactGrab.ControllerUngrabInteractableObject += DoControllerUngrab;
  287. }
  288. if (interactUse != null)
  289. {
  290. interactUse.ControllerUseInteractableObject += DoControllerUse;
  291. interactUse.ControllerUnuseInteractableObject += DoControllerUnuse;
  292. }
  293. }
  294. protected virtual void UnsubscribeEvents()
  295. {
  296. if (controllerEvents != null)
  297. {
  298. UnsubscribeButtonEvent(savedThumbButtonState, DoThumbEvent);
  299. UnsubscribeButtonEvent(savedIndexButtonState, DoIndexEvent);
  300. UnsubscribeButtonEvent(savedMiddleButtonState, DoMiddleEvent);
  301. UnsubscribeButtonEvent(savedRingButtonState, DoRingEvent);
  302. UnsubscribeButtonEvent(savedPinkyButtonState, DoPinkyEvent);
  303. UnsubscribeButtonEvent(savedThreeFingerButtonState, DoThreeFingerEvent);
  304. UnsubscribeButtonAxisEvent(savedThumbAxisButtonState, thumbState, DoThumbAxisEvent);
  305. UnsubscribeButtonAxisEvent(savedIndexAxisButtonState, indexState, DoIndexAxisEvent);
  306. UnsubscribeButtonAxisEvent(savedMiddleAxisButtonState, middleState, DoMiddleAxisEvent);
  307. UnsubscribeButtonAxisEvent(savedRingAxisButtonState, ringState, DoRingAxisEvent);
  308. UnsubscribeButtonAxisEvent(savedPinkyAxisButtonState, pinkyState, DoPinkyAxisEvent);
  309. UnsubscribeButtonAxisEvent(savedThreeFingerAxisButtonState, threeFingerState, DoThreeFingerAxisEvent);
  310. }
  311. if (interactNearTouch != null)
  312. {
  313. interactNearTouch.ControllerNearTouchInteractableObject -= DoControllerNearTouch;
  314. interactNearTouch.ControllerNearUntouchInteractableObject -= DoControllerNearUntouch;
  315. }
  316. if (interactTouch != null)
  317. {
  318. interactTouch.ControllerTouchInteractableObject -= DoControllerTouch;
  319. interactTouch.ControllerUntouchInteractableObject -= DoControllerUntouch;
  320. }
  321. if (interactGrab != null)
  322. {
  323. interactGrab.ControllerGrabInteractableObject -= DoControllerGrab;
  324. interactGrab.ControllerUngrabInteractableObject -= DoControllerUngrab;
  325. }
  326. if (interactUse != null)
  327. {
  328. interactUse.ControllerUseInteractableObject -= DoControllerUse;
  329. interactUse.ControllerUnuseInteractableObject -= DoControllerUnuse;
  330. }
  331. }
  332. #endregion Subscription Managers
  333. #region Event methods
  334. protected virtual void SetFingerEvent(int fingerIndex, ControllerInteractionEventArgs e)
  335. {
  336. if (overrideAxisValues[fingerIndex] == OverrideState.NoOverride)
  337. {
  338. fingerChangeStates[fingerIndex] = true;
  339. fingerStates[fingerIndex] = (e.buttonPressure == 0f ? false : true);
  340. }
  341. }
  342. protected virtual void SetFingerAxisEvent(int fingerIndex, ControllerInteractionEventArgs e)
  343. {
  344. fingerRawAxis[fingerIndex] = e.buttonPressure;
  345. if (overrideAxisValues[fingerIndex] == OverrideState.NoOverride)
  346. {
  347. fingerAxis[fingerIndex] = e.buttonPressure;
  348. }
  349. }
  350. protected virtual void DoThumbEvent(object sender, ControllerInteractionEventArgs e)
  351. {
  352. SetFingerEvent(0, e);
  353. }
  354. protected virtual void DoIndexEvent(object sender, ControllerInteractionEventArgs e)
  355. {
  356. SetFingerEvent(1, e);
  357. }
  358. protected virtual void DoMiddleEvent(object sender, ControllerInteractionEventArgs e)
  359. {
  360. SetFingerEvent(2, e);
  361. }
  362. protected virtual void DoRingEvent(object sender, ControllerInteractionEventArgs e)
  363. {
  364. SetFingerEvent(3, e);
  365. }
  366. protected virtual void DoPinkyEvent(object sender, ControllerInteractionEventArgs e)
  367. {
  368. SetFingerEvent(4, e);
  369. }
  370. protected virtual void DoThreeFingerEvent(object sender, ControllerInteractionEventArgs e)
  371. {
  372. SetFingerEvent(2, e);
  373. SetFingerEvent(3, e);
  374. SetFingerEvent(4, e);
  375. }
  376. protected virtual void DoThumbAxisEvent(object sender, ControllerInteractionEventArgs e)
  377. {
  378. SetFingerAxisEvent(0, e);
  379. }
  380. protected virtual void DoIndexAxisEvent(object sender, ControllerInteractionEventArgs e)
  381. {
  382. SetFingerAxisEvent(1, e);
  383. }
  384. protected virtual void DoMiddleAxisEvent(object sender, ControllerInteractionEventArgs e)
  385. {
  386. SetFingerAxisEvent(2, e);
  387. }
  388. protected virtual void DoRingAxisEvent(object sender, ControllerInteractionEventArgs e)
  389. {
  390. SetFingerAxisEvent(3, e);
  391. }
  392. protected virtual void DoPinkyAxisEvent(object sender, ControllerInteractionEventArgs e)
  393. {
  394. SetFingerAxisEvent(4, e);
  395. }
  396. protected virtual void DoThreeFingerAxisEvent(object sender, ControllerInteractionEventArgs e)
  397. {
  398. SetFingerAxisEvent(2, e);
  399. SetFingerAxisEvent(3, e);
  400. SetFingerAxisEvent(4, e);
  401. }
  402. protected virtual bool IsButtonPressed(int arrayIndex)
  403. {
  404. float minValue = (axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.SenseAxis && controllerEvents != null ? controllerEvents.senseAxisPressThreshold : 0f);
  405. return (fingerStates[arrayIndex] || fingerRawAxis[arrayIndex] > minValue);
  406. }
  407. protected virtual void SaveFingerAxis(int arrayIndex, float updateAxis)
  408. {
  409. fingerSaveAxis[arrayIndex] = (fingerSaveAxis[arrayIndex] != fingerForceAxis[arrayIndex] ? updateAxis : fingerSaveAxis[arrayIndex]);
  410. }
  411. protected virtual void HandleOverrideOn(bool ignoreAllOverrides, float[] givenFingerAxis, bool[] overridePermissions, float[] overrideValues)
  412. {
  413. if (!ignoreAllOverrides)
  414. {
  415. for (int i = 0; i < overrideAxisValues.Length; i++)
  416. {
  417. if (overridePermissions[i] && !IsButtonPressed(i) && overrideAxisValues[i] != OverrideState.WasOverring)
  418. {
  419. SetOverrideValue(i, ref overrideAxisValues, OverrideState.IsOverriding);
  420. if (overrideAxisValues[i] == OverrideState.NoOverride)
  421. {
  422. fingerUntouchedAxis[i] = givenFingerAxis[i];
  423. }
  424. SaveFingerAxis(i, givenFingerAxis[i]);
  425. fingerForceAxis[i] = overrideValues[i];
  426. }
  427. }
  428. }
  429. }
  430. protected virtual void HandleOverrideOff(bool ignoreAllOverrides, bool[] overridePermissions, bool keepOverriding)
  431. {
  432. if (!ignoreAllOverrides)
  433. {
  434. for (int i = 0; i < fingerAxis.Length; i++)
  435. {
  436. if (overridePermissions[i] && !IsButtonPressed(i) && overrideAxisValues[i] == OverrideState.IsOverriding)
  437. {
  438. SetOverrideValue(i, ref overrideAxisValues, (keepOverriding ? OverrideState.KeepOverring : OverrideState.WasOverring));
  439. fingerAxis[i] = fingerForceAxis[i];
  440. fingerForceAxis[i] = fingerSaveAxis[i];
  441. }
  442. }
  443. }
  444. }
  445. protected virtual float CorrectOverrideValue(float givenOverride)
  446. {
  447. return (givenOverride == 0f ? 0.0001f : givenOverride);
  448. }
  449. protected virtual bool ApplyFingerOverrides(AxisOverrides.ApplyOverrideType overrideType, int arrayIndex)
  450. {
  451. if (overrideType == AxisOverrides.ApplyOverrideType.Always ||
  452. (overrideType == AxisOverrides.ApplyOverrideType.DigitalState && axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.Digital) ||
  453. (overrideType == AxisOverrides.ApplyOverrideType.AxisState && axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.Axis) ||
  454. (overrideType == AxisOverrides.ApplyOverrideType.SenseAxisState && axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.SenseAxis) ||
  455. (overrideType == AxisOverrides.ApplyOverrideType.AxisAndSenseAxisState && (axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.Axis || axisTypes[arrayIndex] == VRTK_ControllerEvents.AxisType.SenseAxis)))
  456. {
  457. return true;
  458. }
  459. return false;
  460. }
  461. protected virtual bool[] GetOverridePermissions(AxisOverrides overrideType)
  462. {
  463. bool[] overrides = new bool[]
  464. {
  465. ApplyFingerOverrides(overrideType.applyThumbOverride, 0),
  466. ApplyFingerOverrides(overrideType.applyIndexOverride, 1),
  467. ApplyFingerOverrides(overrideType.applyMiddleOverride, 2),
  468. ApplyFingerOverrides(overrideType.applyRingOverride, 3),
  469. ApplyFingerOverrides(overrideType.applyPinkyOverride, 4)
  470. };
  471. return overrides;
  472. }
  473. protected virtual float[] GetOverrideValues(AxisOverrides overrideType)
  474. {
  475. float[] overrides = new float[]
  476. {
  477. CorrectOverrideValue(overrideType.thumbOverride),
  478. CorrectOverrideValue(overrideType.indexOverride),
  479. CorrectOverrideValue(overrideType.middleOverride),
  480. CorrectOverrideValue(overrideType.ringOverride),
  481. CorrectOverrideValue(overrideType.pinkyOverride)
  482. };
  483. return overrides;
  484. }
  485. protected virtual void SetAnimatorStateOn(string state, AxisOverrides overrides)
  486. {
  487. animator.SetFloat(state, (overrides.ignoreAllOverrides ? -1f : overrides.stateValue));
  488. }
  489. protected virtual void SetAnimatorStateOff(string state)
  490. {
  491. animator.SetFloat(state, -1f);
  492. }
  493. protected virtual void DoControllerNearTouch(object sender, ObjectInteractEventArgs e)
  494. {
  495. if (interactTouch != null && interactTouch.GetTouchedObject() == null)
  496. {
  497. SetAnimatorStateOn("NearTouchState", nearTouchOverrides);
  498. HandleOverrideOn(nearTouchOverrides.ignoreAllOverrides, fingerAxis, GetOverridePermissions(nearTouchOverrides), GetOverrideValues(nearTouchOverrides));
  499. }
  500. }
  501. protected virtual void DoControllerNearUntouch(object sender, ObjectInteractEventArgs e)
  502. {
  503. if (interactNearTouch.GetNearTouchedObjects().Count == 0 && (interactTouch == null || interactTouch.GetTouchedObject() == null))
  504. {
  505. for (int i = 0; i < fingerUntouchedAxis.Length; i++)
  506. {
  507. if (!IsButtonPressed(i))
  508. {
  509. SetOverrideValue(i, ref overrideAxisValues, OverrideState.WasOverring);
  510. fingerForceAxis[i] = fingerUntouchedAxis[i];
  511. }
  512. }
  513. SetAnimatorStateOff("NearTouchState");
  514. HandleOverrideOff(nearTouchOverrides.ignoreAllOverrides, GetOverridePermissions(nearTouchOverrides), false);
  515. }
  516. }
  517. protected virtual void DoControllerTouch(object sender, ObjectInteractEventArgs e)
  518. {
  519. SetAnimatorStateOn("TouchState", touchOverrides);
  520. HandleOverrideOn(touchOverrides.ignoreAllOverrides, fingerAxis, GetOverridePermissions(touchOverrides), GetOverrideValues(touchOverrides));
  521. }
  522. protected virtual void DoControllerUntouch(object sender, ObjectInteractEventArgs e)
  523. {
  524. if (interactNearTouch == null || nearTouchOverrides.ignoreAllOverrides)
  525. {
  526. for (int i = 0; i < fingerUntouchedAxis.Length; i++)
  527. {
  528. if (!IsButtonPressed(i))
  529. {
  530. SetOverrideValue(i, ref overrideAxisValues, OverrideState.WasOverring);
  531. fingerForceAxis[i] = fingerUntouchedAxis[i];
  532. }
  533. }
  534. }
  535. SetAnimatorStateOff("TouchState");
  536. HandleOverrideOff(touchOverrides.ignoreAllOverrides, GetOverridePermissions(touchOverrides), false);
  537. }
  538. protected virtual void DoControllerGrab(object sender, ObjectInteractEventArgs e)
  539. {
  540. bool isUsing = (interactUse != null && interactUse.GetUsingObject() != null);
  541. float[] overrideValues = (GetOverrideValues((isUsing ? useOverrides : grabOverrides)));
  542. float[] overrideFingerAxis = (isUsing ? GetOverrideValues(grabOverrides) : fingerAxis);
  543. SetAnimatorStateOn("GrabState", grabOverrides);
  544. HandleOverrideOn(grabOverrides.ignoreAllOverrides, overrideFingerAxis, GetOverridePermissions(grabOverrides), overrideValues);
  545. }
  546. protected virtual void DoControllerUngrab(object sender, ObjectInteractEventArgs e)
  547. {
  548. SetAnimatorStateOff("GrabState");
  549. HandleOverrideOff(grabOverrides.ignoreAllOverrides, GetOverridePermissions(touchOverrides), false);
  550. }
  551. protected virtual void DoControllerUse(object sender, ObjectInteractEventArgs e)
  552. {
  553. bool isGrabbing = (interactGrab != null && interactGrab.GetGrabbedObject() != null);
  554. float[] overrideFingerAxis = (isGrabbing ? GetOverrideValues(grabOverrides) : fingerAxis);
  555. SetAnimatorStateOn("UseState", useOverrides);
  556. HandleOverrideOn(useOverrides.ignoreAllOverrides, overrideFingerAxis, GetOverridePermissions(useOverrides), GetOverrideValues(useOverrides));
  557. }
  558. protected virtual void DoControllerUnuse(object sender, ObjectInteractEventArgs e)
  559. {
  560. SetAnimatorStateOff("UseState");
  561. HandleOverrideOff(useOverrides.ignoreAllOverrides, GetOverridePermissions(useOverrides), true);
  562. }
  563. #endregion Event methods
  564. protected virtual void DetectController()
  565. {
  566. controllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  567. if (controllerType != SDK_BaseController.ControllerType.Undefined)
  568. {
  569. if (setFingersForControllerType)
  570. {
  571. switch (controllerType)
  572. {
  573. case SDK_BaseController.ControllerType.SteamVR_ViveWand:
  574. case SDK_BaseController.ControllerType.SteamVR_WindowsMRController:
  575. case SDK_BaseController.ControllerType.WindowsMR_MotionController:
  576. thumbState = VRTK_ControllerEvents.AxisType.Digital;
  577. indexState = VRTK_ControllerEvents.AxisType.Axis;
  578. middleState = VRTK_ControllerEvents.AxisType.Digital;
  579. ringState = VRTK_ControllerEvents.AxisType.Digital;
  580. pinkyState = VRTK_ControllerEvents.AxisType.Digital;
  581. threeFingerState = VRTK_ControllerEvents.AxisType.Digital;
  582. break;
  583. case SDK_BaseController.ControllerType.Oculus_OculusTouch:
  584. case SDK_BaseController.ControllerType.SteamVR_OculusTouch:
  585. thumbState = VRTK_ControllerEvents.AxisType.Digital;
  586. indexState = VRTK_ControllerEvents.AxisType.Axis;
  587. middleState = VRTK_ControllerEvents.AxisType.Digital;
  588. ringState = VRTK_ControllerEvents.AxisType.Digital;
  589. pinkyState = VRTK_ControllerEvents.AxisType.Digital;
  590. threeFingerState = VRTK_ControllerEvents.AxisType.Axis;
  591. break;
  592. case SDK_BaseController.ControllerType.SteamVR_ValveKnuckles:
  593. thumbState = VRTK_ControllerEvents.AxisType.Digital;
  594. indexState = VRTK_ControllerEvents.AxisType.SenseAxis;
  595. middleState = VRTK_ControllerEvents.AxisType.SenseAxis;
  596. ringState = VRTK_ControllerEvents.AxisType.SenseAxis;
  597. pinkyState = VRTK_ControllerEvents.AxisType.SenseAxis;
  598. threeFingerState = VRTK_ControllerEvents.AxisType.SenseAxis;
  599. threeFingerAxisButton = SDK_BaseController.ButtonTypes.StartMenu;
  600. break;
  601. default:
  602. thumbState = VRTK_ControllerEvents.AxisType.Digital;
  603. indexState = VRTK_ControllerEvents.AxisType.Digital;
  604. middleState = VRTK_ControllerEvents.AxisType.Digital;
  605. ringState = VRTK_ControllerEvents.AxisType.Digital;
  606. pinkyState = VRTK_ControllerEvents.AxisType.Digital;
  607. threeFingerState = VRTK_ControllerEvents.AxisType.Digital;
  608. break;
  609. }
  610. }
  611. UnsubscribeEvents();
  612. SubscribeEvents();
  613. if (mirrorModel)
  614. {
  615. mirrorModel = false;
  616. MirrorHand();
  617. }
  618. }
  619. }
  620. protected virtual void MirrorHand()
  621. {
  622. Transform modelTransform = (handModel != null ? handModel : transform.Find("Model"));
  623. if (modelTransform != null)
  624. {
  625. modelTransform.localScale = new Vector3(modelTransform.localScale.x * -1f, modelTransform.localScale.y, modelTransform.localScale.z);
  626. }
  627. }
  628. protected virtual void SetOverrideValue(int stateIndex, ref OverrideState[] overrideState, OverrideState stateValue)
  629. {
  630. overrideState[stateIndex] = stateValue;
  631. }
  632. protected virtual void ProcessFinger(VRTK_ControllerEvents.AxisType state, int arrayIndex)
  633. {
  634. axisTypes[arrayIndex] = state;
  635. if (overrideAxisValues[arrayIndex] != OverrideState.NoOverride)
  636. {
  637. if (fingerAxis[arrayIndex] != fingerForceAxis[arrayIndex])
  638. {
  639. LerpChangePosition(arrayIndex, fingerAxis[arrayIndex], fingerForceAxis[arrayIndex], animationSnapSpeed);
  640. }
  641. else if (overrideAxisValues[arrayIndex] == OverrideState.WasOverring)
  642. {
  643. SetOverrideValue(arrayIndex, ref overrideAxisValues, OverrideState.NoOverride);
  644. }
  645. }
  646. else
  647. {
  648. if (state == VRTK_ControllerEvents.AxisType.Digital)
  649. {
  650. if (fingerChangeStates[arrayIndex])
  651. {
  652. fingerChangeStates[arrayIndex] = false;
  653. float startAxis = (fingerStates[arrayIndex] ? 0f : 1f);
  654. float targetAxis = (fingerStates[arrayIndex] ? 1f : 0f);
  655. LerpChangePosition(arrayIndex, startAxis, targetAxis, animationSnapSpeed);
  656. }
  657. }
  658. else
  659. {
  660. SetFingerPosition(arrayIndex, fingerAxis[arrayIndex]);
  661. }
  662. }
  663. //Final sanity check, if you're not touching anything but the override is still set, then clear the override.
  664. if (((interactTouch == null && interactNearTouch == null) || (interactNearTouch == null && interactTouch.GetTouchedObject() == null) || (interactNearTouch != null && interactNearTouch.GetNearTouchedObjects().Count == 0)) && overrideAxisValues[arrayIndex] != OverrideState.NoOverride)
  665. {
  666. SetOverrideValue(arrayIndex, ref overrideAxisValues, OverrideState.NoOverride);
  667. }
  668. }
  669. protected virtual void LerpChangePosition(int arrayIndex, float startPosition, float targetPosition, float speed)
  670. {
  671. fingerAnimationRoutine[arrayIndex] = StartCoroutine(ChangePosition(arrayIndex, startPosition, targetPosition, speed));
  672. }
  673. protected virtual IEnumerator ChangePosition(int arrayIndex, float startAxis, float targetAxis, float time)
  674. {
  675. float elapsedTime = 0f;
  676. while (elapsedTime < time)
  677. {
  678. elapsedTime += Time.deltaTime;
  679. float currentAxis = Mathf.Lerp(startAxis, targetAxis, (elapsedTime / time));
  680. SetFingerPosition(arrayIndex, currentAxis);
  681. yield return null;
  682. }
  683. SetFingerPosition(arrayIndex, targetAxis);
  684. fingerAnimationRoutine[arrayIndex] = null;
  685. }
  686. protected virtual void SetFingerPosition(int arrayIndex, float axis)
  687. {
  688. int animationLayer = arrayIndex + 1;
  689. animator.SetLayerWeight(animationLayer, axis);
  690. fingerAxis[arrayIndex] = axis;
  691. if (overrideAxisValues[arrayIndex] == OverrideState.WasOverring)
  692. {
  693. SetOverrideValue(arrayIndex, ref overrideAxisValues, OverrideState.NoOverride);
  694. }
  695. }
  696. }
  697. }