CursorInPercent.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using SC.XR.Unity.Module_InputSystem;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. public class CursorInPercent :MonoBehaviour,IPointerExitHandler,IPointerEnterHandler {
  7. [SerializeField]
  8. private BoxCollider boxCollider;
  9. // Half the size of the current bounds
  10. private Vector3 currentBoundsExtents;
  11. public Vector3 CurrentBoundsExtents {
  12. get {
  13. return currentBoundsExtents;
  14. }
  15. }
  16. // Half the size of the current bounds
  17. private Vector3 initBoundsExtents;
  18. public Vector3 InitBoundsExtents {
  19. get {
  20. return initBoundsExtents;
  21. }
  22. }
  23. // Half the size of the current bounds
  24. private Vector3 currentBoundsCenter;
  25. public Vector3 CurrentBoundsCenter {
  26. get {
  27. return currentBoundsCenter;
  28. }
  29. }
  30. public enum Face {
  31. XY,
  32. YZ,
  33. XZ,
  34. }
  35. public Face face = Face.XY;
  36. [Range(0, 1)]
  37. [SerializeField]
  38. private float SetPercent1Start0 = 0.3f;
  39. private float m_InPercentStart0;
  40. public float CurrentInPercentStart0 {
  41. get {
  42. return m_InPercentStart0;
  43. }
  44. private set {
  45. m_InPercentStart0 = value;
  46. }
  47. }
  48. protected Vector3 InitScale { get; private set; }
  49. protected Vector3 WorldCenter;
  50. public void Start() {
  51. if (boxCollider == null) {
  52. boxCollider = GetComponent<BoxCollider>();
  53. }
  54. if (boxCollider == null) {
  55. Debug.LogError("Error ! No BoxCollider , HandCursorEffect will no work !");
  56. return;
  57. }
  58. // Store current rotation then zero out the rotation so that the bounds
  59. // are computed when the object is in its 'axis aligned orientation'.
  60. Quaternion currentRotation = transform.rotation;
  61. transform.rotation = Quaternion.identity;
  62. Physics.SyncTransforms(); // Update collider bounds
  63. initBoundsExtents = boxCollider.bounds.extents;
  64. //currentBoundsCenter = boxCollider.bounds.center;
  65. currentBoundsCenter = boxCollider.center;
  66. WorldCenter = currentBoundsCenter;
  67. InitScale = boxCollider.transform.lossyScale;
  68. //Debug.Log(initBoundsExtents.x + " " + initBoundsExtents.y + " " + initBoundsExtents.z);
  69. // After bounds are computed, restore rotation...
  70. transform.rotation = currentRotation;
  71. Physics.SyncTransforms();
  72. }
  73. PointerEventData currentPointerEventData;
  74. Vector3 deltaV;
  75. float wPercent, hPercent;
  76. void LateUpdate() {
  77. if (boxCollider == null)
  78. return;
  79. if (currentPointerEventData == null)
  80. return;
  81. currentBoundsExtents.x = InitBoundsExtents.x * (boxCollider.transform.lossyScale.x / InitScale.x);
  82. currentBoundsExtents.y = InitBoundsExtents.y * (boxCollider.transform.lossyScale.y / InitScale.y);
  83. currentBoundsExtents.z = InitBoundsExtents.z * (boxCollider.transform.lossyScale.z / InitScale.z);
  84. SetPercent1Start0 = Mathf.Clamp(SetPercent1Start0, 0, 1);
  85. //WorldCenter = currentBoundsCenter;
  86. WorldCenter = transform.TransformPoint( currentBoundsCenter);
  87. //WorldCenter = boxCollider.bounds.center;
  88. //Debug.Log(WorldCenter);
  89. deltaV = currentPointerEventData.pointerCurrentRaycast.worldPosition - WorldCenter;
  90. if (face == Face.XY) {
  91. wPercent = Vector3.Dot(deltaV, transform.right) / CurrentBoundsExtents.x;
  92. hPercent = Vector3.Dot(deltaV, transform.up) / CurrentBoundsExtents.y;
  93. } else if (face == Face.YZ) {
  94. wPercent = Vector3.Dot(deltaV, transform.forward) / CurrentBoundsExtents.z;
  95. hPercent = Vector3.Dot(deltaV, transform.up) / CurrentBoundsExtents.y;
  96. } else if (face == Face.XZ) {
  97. wPercent = Vector3.Dot(deltaV, transform.forward) / CurrentBoundsExtents.z;
  98. hPercent = Vector3.Dot(deltaV, transform.right) / CurrentBoundsExtents.x;
  99. }
  100. wPercent = Mathf.Abs(wPercent);
  101. hPercent = Mathf.Abs(hPercent);
  102. wPercent = Mathf.Clamp01(wPercent);
  103. hPercent = Mathf.Clamp01(hPercent);
  104. float maxPercent = Mathf.Max(wPercent, hPercent);
  105. m_InPercentStart0 = (1 / SetPercent1Start0) * (1 - maxPercent);
  106. m_InPercentStart0 = Mathf.Clamp01(m_InPercentStart0);
  107. //Debug.Log(wPercent + " " + hPercent + " " + m_InPercentStart0+" "+ deltaV.x+" "+ deltaV.y+" "+ deltaV.z +" "+ currentBoundsExtents.x + " " + currentBoundsExtents.y + " " + currentBoundsExtents.z);
  108. }
  109. public void OnPointerEnter(PointerEventData eventData) {
  110. currentPointerEventData = eventData;
  111. }
  112. public void OnPointerExit(PointerEventData eventData) {
  113. currentPointerEventData = null;
  114. }
  115. }