BoxProximityField.cs 970 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. using UnityEngine.Assertions;
  3. namespace Rokid.UXR.Interaction
  4. {
  5. public class BoxProximityField : MonoBehaviour, IProximityField
  6. {
  7. [SerializeField]
  8. private Transform _boxTransform;
  9. protected virtual void Start()
  10. {
  11. Assert.IsNotNull(_boxTransform);
  12. }
  13. // Closest point in box is computed by transforming the point to OBB space,
  14. // clamping to a 1-1-1 box, and transforming the point back to world space
  15. public Vector3 ComputeClosestPoint(Vector3 point)
  16. {
  17. Vector3 localPoint = _boxTransform.InverseTransformPoint(point);
  18. localPoint.x = Mathf.Clamp(localPoint.x, -0.5f, 0.5f);
  19. localPoint.y = Mathf.Clamp(localPoint.y, -0.5f, 0.5f);
  20. localPoint.z = Mathf.Clamp(localPoint.z, -0.5f, 0.5f);
  21. Vector3 worldPoint = _boxTransform.TransformPoint(localPoint);
  22. return worldPoint;
  23. }
  24. }
  25. }