SwingArmModel.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Modified by Unity from originals located
  15. // https://github.com/googlevr/daydream-elements/blob/master/Assets/DaydreamElements/Elements/ArmModels/Scripts/ArmModels/SwingArmModel.cs
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using UnityEngine;
  19. #if ENABLE_VR || ENABLE_AR
  20. using UnityEngine.Experimental.XR.Interaction;
  21. namespace UnityEngine.XR.LegacyInputHelpers
  22. {
  23. public class SwingArmModel : ArmModel
  24. {
  25. [Tooltip("Portion of controller rotation applied to the shoulder joint.")]
  26. [SerializeField]
  27. [Range(0.0f, 1.0f)]
  28. float m_ShoulderRotationRatio = 0.5f;
  29. /// <summary>
  30. /// Portion of controller rotation applied to the shoulder joint.
  31. /// </summary>
  32. public float shoulderRotationRatio
  33. {
  34. get { return m_ShoulderRotationRatio; }
  35. set { m_ShoulderRotationRatio = value; }
  36. }
  37. [Tooltip("Portion of controller rotation applied to the elbow joint.")]
  38. [Range(0.0f, 1.0f)]
  39. [SerializeField]
  40. float m_ElbowRotationRatio = 0.3f;
  41. /// <summary>
  42. /// Portion of controller rotation applied to the elbow joint.
  43. /// </summary>
  44. public float elbowRotationRatio
  45. {
  46. get { return m_ElbowRotationRatio; }
  47. set { m_ElbowRotationRatio = value; }
  48. }
  49. [Tooltip("Portion of controller rotation applied to the wrist joint.")]
  50. [Range(0.0f, 1.0f)]
  51. [SerializeField]
  52. float m_WristRotationRatio = 0.2f;
  53. /// <summary>
  54. /// Portion of controller rotation applied to the wrist joint.
  55. /// </summary>
  56. public float wristRotationRatio
  57. {
  58. get { return m_WristRotationRatio; }
  59. set { m_WristRotationRatio = value; }
  60. }
  61. [SerializeField]
  62. Vector2 m_JointShiftAngle = new Vector2(160.0f, 180.0f);
  63. /// <summary>
  64. /// Min angle of the controller before starting to lerp towards the shifted joint ratios.
  65. /// </summary>
  66. public float minJointShiftAngle
  67. {
  68. get { return m_JointShiftAngle.x; }
  69. set { m_JointShiftAngle.x = value; }
  70. }
  71. /// <summary>
  72. /// Max angle of the controller before starting to lerp towards the shifted joint ratios.
  73. /// </summary>
  74. public float maxJointShiftAngle
  75. {
  76. get { return m_JointShiftAngle.y; }
  77. set { m_JointShiftAngle.y = value; }
  78. }
  79. [Tooltip("Exponent applied to the joint shift ratio to control the curve of the shift.")]
  80. [Range(1.0f, 20.0f)]
  81. [SerializeField]
  82. float m_JointShiftExponent = 6.0f;
  83. /// <summary>
  84. /// Exponent applied to the joint shift ratio to control the curve of the shift.
  85. /// </summary>
  86. public float jointShiftExponent
  87. {
  88. get { return m_JointShiftExponent; }
  89. set { m_JointShiftExponent = value; }
  90. }
  91. [Tooltip("Portion of controller rotation applied to the shoulder joint when the controller is backwards.")]
  92. [Range(0.0f, 1.0f)]
  93. [SerializeField]
  94. float m_ShiftedShoulderRotationRatio = 0.1f;
  95. /// <summary>
  96. /// Portion of controller rotation applied to the shoulder joint when the controller is backwards.
  97. /// </summary>
  98. public float shiftedShoulderRotationRatio
  99. {
  100. get { return m_ShiftedShoulderRotationRatio; }
  101. set { m_ShiftedShoulderRotationRatio = value; }
  102. }
  103. [Tooltip("Portion of controller rotation applied to the elbow joint when the controller is backwards.")]
  104. [Range(0.0f, 1.0f)]
  105. [SerializeField]
  106. float m_ShiftedElbowRotationRatio = 0.4f;
  107. /// <summary>
  108. /// Portion of controller rotation applied to the elbow joint when the controller is backwards.
  109. /// </summary>
  110. public float shiftedElbowRotationRatio
  111. {
  112. get { return m_ShiftedElbowRotationRatio; }
  113. set { m_ShiftedElbowRotationRatio = value; }
  114. }
  115. [Tooltip("Portion of controller rotation applied to the wrist joint when the controller is backwards.")]
  116. [Range(0.0f, 1.0f)]
  117. [SerializeField]
  118. float m_ShiftedWristRotationRatio = 0.5f;
  119. /// <summary>
  120. /// Portion of controller rotation applied to the wrist joint when the controller is backwards.
  121. /// </summary>
  122. public float shiftedWristRotationRatio
  123. {
  124. get { return m_ShiftedWristRotationRatio; }
  125. set { m_ShiftedWristRotationRatio = value; }
  126. }
  127. protected override void CalculateFinalJointRotations(Quaternion controllerOrientation, Quaternion xyRotation, Quaternion lerpRotation)
  128. {
  129. // As the controller angle increases the ratio of the rotation applied to each joint shifts.
  130. float totalAngle = Quaternion.Angle(xyRotation, Quaternion.identity);
  131. float jointShiftAngleRange = maxJointShiftAngle - minJointShiftAngle;
  132. float angleRatio = Mathf.Clamp01((totalAngle - minJointShiftAngle) / jointShiftAngleRange);
  133. float jointShiftRatio = Mathf.Pow(angleRatio, m_JointShiftExponent);
  134. // Calculate what portion of the rotation is applied to each joint.
  135. float finalShoulderRatio = Mathf.Lerp(m_ShoulderRotationRatio, m_ShiftedShoulderRotationRatio, jointShiftRatio);
  136. float finalElbowRatio = Mathf.Lerp(m_ElbowRotationRatio, m_ShiftedElbowRotationRatio, jointShiftRatio);
  137. float finalWristRatio = Mathf.Lerp(m_WristRotationRatio, m_ShiftedWristRotationRatio, jointShiftRatio);
  138. // Calculate relative rotations for each joint.
  139. Quaternion swingShoulderRot = Quaternion.Lerp(Quaternion.identity, xyRotation, finalShoulderRatio);
  140. Quaternion swingElbowRot = Quaternion.Lerp(Quaternion.identity, xyRotation, finalElbowRatio);
  141. Quaternion swingWristRot = Quaternion.Lerp(Quaternion.identity, xyRotation, finalWristRatio);
  142. // Calculate final rotations.
  143. Quaternion shoulderRotation = m_TorsoRotation * swingShoulderRot;
  144. m_ElbowRotation = shoulderRotation * swingElbowRot;
  145. m_WristRotation = elbowRotation * swingWristRot;
  146. m_ControllerRotation = m_TorsoRotation * controllerOrientation;
  147. m_TorsoRotation = shoulderRotation;
  148. }
  149. }
  150. }
  151. #endif