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.

120 lines
4.9 KiB

  1. // SDK Transform Modify|Utilities|90150
  2. namespace VRTK
  3. {
  4. using UnityEngine;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. [Serializable]
  9. public class VRTK_SDKTransformModifiers
  10. {
  11. [Header("SDK settings")]
  12. [Tooltip("An optional SDK Setup to use to determine when to modify the transform.")]
  13. public VRTK_SDKSetup loadedSDKSetup = null;
  14. [Tooltip("An optional SDK controller type to use to determine when to modify the transform.")]
  15. public SDK_BaseController.ControllerType controllerType = SDK_BaseController.ControllerType.Undefined;
  16. [Header("Transform Override Settings")]
  17. [Tooltip("The new local position to change the transform to.")]
  18. public Vector3 position = Vector3.zero;
  19. [Tooltip("The new local rotation in eular angles to change the transform to.")]
  20. public Vector3 rotation = Vector3.zero;
  21. [Tooltip("The new local scale to change the transform to.")]
  22. public Vector3 scale = Vector3.one;
  23. }
  24. /// <summary>
  25. /// The SDK Transform Modify can be used to change a transform orientation at runtime based on the currently used SDK or SDK controller.
  26. /// </summary>
  27. [AddComponentMenu("VRTK/Scripts/Utilities/VRTK_SDKTransformModify")]
  28. public class VRTK_SDKTransformModify : VRTK_SDKControllerReady
  29. {
  30. [Tooltip("The target Transform to modify on enable. If this is left blank then the Transform the script is attached to will be used.")]
  31. public Transform target;
  32. [Tooltip("If this is checked then the target Transform will be reset to the original orientation when this script is disabled.")]
  33. public bool resetOnDisable = true;
  34. [Tooltip("A collection of SDK Transform overrides to change the given target Transform for each specified SDK.")]
  35. public List<VRTK_SDKTransformModifiers> sdkOverrides = new List<VRTK_SDKTransformModifiers>();
  36. protected Vector3 originalPosition;
  37. protected Quaternion originalRotation;
  38. protected Vector3 originalScale;
  39. /// <summary>
  40. /// The UpdateTransform method updates the Transform data on the current GameObject for the specified settings.
  41. /// </summary>
  42. /// <param name="controllerReference">An optional reference to the controller to update the transform with.</param>
  43. public virtual void UpdateTransform(VRTK_ControllerReference controllerReference = null)
  44. {
  45. if (target == null)
  46. {
  47. return;
  48. }
  49. VRTK_SDKTransformModifiers selectedModifier = GetSelectedModifier(controllerReference);
  50. //If a modifier is found then change the transform
  51. if (selectedModifier != null)
  52. {
  53. target.localPosition = selectedModifier.position;
  54. target.localEulerAngles = selectedModifier.rotation;
  55. target.localScale = selectedModifier.scale;
  56. }
  57. }
  58. /// <summary>
  59. /// The SetOrigins method sets the original position, rotation, scale of the target Transform.
  60. /// </summary>
  61. public virtual void SetOrigins()
  62. {
  63. if (target != null)
  64. {
  65. originalPosition = target.position;
  66. originalRotation = target.rotation;
  67. originalScale = target.localScale;
  68. }
  69. }
  70. protected override void OnEnable()
  71. {
  72. target = (target != null ? target : transform);
  73. SetOrigins();
  74. base.OnEnable();
  75. }
  76. protected override void OnDisable()
  77. {
  78. base.OnDisable();
  79. if (resetOnDisable)
  80. {
  81. target.position = originalPosition;
  82. target.rotation = originalRotation;
  83. target.localScale = originalScale;
  84. }
  85. }
  86. protected override void ControllerReady(VRTK_ControllerReference controllerReference)
  87. {
  88. if (VRTK_SDKManager.GetLoadedSDKSetup() != null && gameObject.activeInHierarchy)
  89. {
  90. UpdateTransform(controllerReference);
  91. }
  92. }
  93. protected virtual VRTK_SDKTransformModifiers GetSelectedModifier(VRTK_ControllerReference controllerReference)
  94. {
  95. //attempt to find by the overall SDK set up to start with
  96. VRTK_SDKTransformModifiers selectedModifier = sdkOverrides.FirstOrDefault(item => item.loadedSDKSetup == VRTK_SDKManager.GetLoadedSDKSetup());
  97. //If no sdk set up is found or it is null then try and find by the SDK controller
  98. if (selectedModifier == null)
  99. {
  100. SDK_BaseController.ControllerType currentControllerType = VRTK_DeviceFinder.GetCurrentControllerType(controllerReference);
  101. selectedModifier = sdkOverrides.FirstOrDefault(item => item.controllerType == currentControllerType);
  102. }
  103. return selectedModifier;
  104. }
  105. }
  106. }