PokeInteractable.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. using System;
  4. namespace Rokid.UXR.Interaction
  5. {
  6. public class PokeInteractable : PointerInteractable<PokeInteractor, PokeInteractable>
  7. {
  8. [SerializeField, Interface(typeof(ISurface))]
  9. public MonoBehaviour _surface;
  10. public ISurface Surface;
  11. [SerializeField, Interface(typeof(ISurface))]
  12. private MonoBehaviour _colliderSurface;
  13. public ISurface ColliderSurface;
  14. [SerializeField, Interface(typeof(IProximityField))]
  15. private MonoBehaviour _proximityField;
  16. public IProximityField ProximityField;
  17. [SerializeField]
  18. public float _maxDistance = 0.1f;
  19. public float MaxDistance => _maxDistance;
  20. [SerializeField]
  21. private float _enterHoverDistance = 0f;
  22. [SerializeField]
  23. private float _releaseDistance = 0.25f;
  24. [HideInInspector, SerializeField, Optional]
  25. private int _tiebreakerScore = 0;
  26. [HideInInspector, SerializeField, Optional]
  27. private Collider _volumeMask = null;
  28. [HideInInspector, SerializeField, Optional]
  29. private Transform hitPoint;
  30. [HideInInspector, SerializeField, Optional]
  31. private Transform proximityFieldPoint;
  32. public Collider VolumeMask { get => _volumeMask; }
  33. /// <summary>
  34. /// 拖拽阈值的配置
  35. /// </summary>
  36. [HideInInspector, Serializable]
  37. public class DragThresholdingConfig
  38. {
  39. public bool Enabled;
  40. public float SurfaceThreshold;
  41. public float ZThreshold;
  42. public ProgressCurve DragEaseCurve;
  43. }
  44. [HideInInspector, SerializeField]
  45. private DragThresholdingConfig _dragThresholding =
  46. new DragThresholdingConfig()
  47. {
  48. Enabled = true,
  49. SurfaceThreshold = 0.01f,
  50. ZThreshold = 0.01f,
  51. DragEaseCurve = new ProgressCurve(AnimationCurve.EaseInOut(0, 0, 1, 1), 0.05f)
  52. };
  53. /// <summary>
  54. /// 位置锁定的配置
  55. /// </summary>
  56. [Serializable]
  57. public class PositionPinningConfig
  58. {
  59. public bool Enabled;
  60. public float MaxPinDistance;
  61. }
  62. /// <summary>
  63. /// 固定位置
  64. /// </summary>
  65. /// <returns></returns>
  66. [HideInInspector, SerializeField]
  67. private PositionPinningConfig _positionPinning =
  68. new PositionPinningConfig()
  69. {
  70. Enabled = false,
  71. MaxPinDistance = 0f
  72. };
  73. #region Properties
  74. /// <summary>
  75. /// 进入悬停的位置
  76. /// </summary>
  77. public float EnterHoverDistance => _enterHoverDistance;
  78. /// <summary>
  79. /// 释放距离
  80. /// </summary>
  81. public float ReleaseDistance => _releaseDistance;
  82. public int TiebreakerScore
  83. {
  84. get
  85. {
  86. return _tiebreakerScore;
  87. }
  88. set
  89. {
  90. _tiebreakerScore = value;
  91. }
  92. }
  93. public DragThresholdingConfig DragThresholding
  94. {
  95. get
  96. {
  97. return _dragThresholding;
  98. }
  99. set
  100. {
  101. _dragThresholding = value;
  102. }
  103. }
  104. public PositionPinningConfig PositionPinning
  105. {
  106. get
  107. {
  108. return _positionPinning;
  109. }
  110. set
  111. {
  112. _positionPinning = value;
  113. }
  114. }
  115. #endregion
  116. protected override void Awake()
  117. {
  118. base.Awake();
  119. ProximityField = _proximityField as IProximityField;
  120. Surface = _surface as ISurface;
  121. ColliderSurface = _colliderSurface as ISurface;
  122. }
  123. protected override void Start()
  124. {
  125. Assert.IsNotNull(ProximityField);
  126. Assert.IsNotNull(Surface);
  127. if (_enterHoverDistance > 0f)
  128. {
  129. _enterHoverDistance = Mathf.Min(_enterHoverDistance, _maxDistance);
  130. }
  131. }
  132. public Vector3 ComputeClosestPoint(Vector3 point)
  133. {
  134. //获取邻场近点
  135. Vector3 proximityFieldPoint = ProximityField.ComputeClosestPoint(point);
  136. if (this.proximityFieldPoint != null)
  137. {
  138. this.proximityFieldPoint.position = proximityFieldPoint;
  139. }
  140. //这里应该有点问题,这里是计算最接近平面的点
  141. Surface.ClosestSurfacePoint(proximityFieldPoint, out SurfaceHit hit);
  142. if (hitPoint != null && Input.GetMouseButton(0))
  143. {
  144. hitPoint.position = hit.Point;
  145. }
  146. return hit.Point;
  147. }
  148. public Vector3 ClosestSurfacePoint(Vector3 point)
  149. {
  150. Surface.ClosestSurfacePoint(point, out SurfaceHit hit);
  151. return hit.Point;
  152. }
  153. public Vector3 ClosestSurfaceNormal(Vector3 point)
  154. {
  155. Surface.ClosestSurfacePoint(point, out SurfaceHit hit);
  156. return hit.Normal;
  157. }
  158. protected override void OnDisable()
  159. {
  160. base.OnDisable();
  161. }
  162. }
  163. }