Ray3D.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. namespace O3DWB
  4. {
  5. public struct Ray3D
  6. {
  7. #region Private Variables
  8. private Vector3 _origin;
  9. private Vector3 _direction;
  10. private Vector3 _normalizedDirection;
  11. #endregion
  12. #region Public Properties
  13. public Vector3 Origin { get { return _origin; } set { _origin = value; } }
  14. public Vector3 Direction
  15. {
  16. get { return _direction; }
  17. set
  18. {
  19. _direction = value;
  20. _normalizedDirection = _direction;
  21. _normalizedDirection.Normalize();
  22. }
  23. }
  24. public Vector3 NormalizedDirection { get { return _normalizedDirection; } }
  25. #endregion
  26. #region Constructors
  27. public Ray3D(Vector3 origin, Vector3 direction)
  28. {
  29. _origin = origin;
  30. _direction = direction;
  31. _normalizedDirection = direction;
  32. _normalizedDirection.Normalize();
  33. }
  34. public Ray3D(Ray source)
  35. {
  36. _origin = source.origin;
  37. _direction = source.direction;
  38. _normalizedDirection = _direction;
  39. _normalizedDirection.Normalize();
  40. }
  41. #endregion
  42. #region Public Methods
  43. /// <summary>
  44. /// Returns a 'Ray' instance which describes the ray.
  45. /// </summary>
  46. /// <remarks>
  47. /// 'Ray' instances always have a normalized direction vector, so the returned
  48. /// ray will contain a normalized version of the direction vector.
  49. /// </remarks>
  50. public Ray ToRayWithNormalizedDirection()
  51. {
  52. return new Ray(_origin, _direction);
  53. }
  54. public void Transform(Matrix4x4 transformMatrix)
  55. {
  56. _origin = transformMatrix.MultiplyPoint(_origin);
  57. Direction = transformMatrix.MultiplyVector(_direction);
  58. }
  59. public void InverseTransform(Matrix4x4 transformMatrix)
  60. {
  61. Transform(transformMatrix.inverse);
  62. }
  63. public Vector3 GetPoint(float t)
  64. {
  65. return _origin + _direction * t;
  66. }
  67. /// <summary>
  68. /// Checks if the ray intersects the specified plane.
  69. /// </summary>
  70. /// <param name="t">
  71. /// At the end of the function call this will hold the intersection offset along the ray
  72. /// direction vector. If no intersection occurs, this will be set to 0.0f.
  73. /// </param>
  74. /// <returns>
  75. /// True if the ray intersects the plane and false otherwise. An intersection occurs only
  76. /// if the intersection offset is in the [0, 1] range (i.e. intersections along the reverse
  77. /// of the ray direction and past the direction length are not allowed).
  78. /// </returns>
  79. public bool IntersectsPlane(Plane plane, out float t)
  80. {
  81. t = 0.0f;
  82. // Calculate the distance from the ray origin to the plane and the project the the ray
  83. // direction vector on the plane normal.
  84. float originDistanceFromPlane = plane.GetDistanceToPoint(_origin);
  85. float directionProjectionOnPlaneNormal = Vector3.Dot(plane.normal, _direction);
  86. // If the projected ray direction is close to 0, it means it runs perpendicualr to the plane
  87. // normal and in that case it can not possible intersect the plane.
  88. if (Mathf.Abs(directionProjectionOnPlaneNormal) < 1e-5f) return false;
  89. // Calculate the intersection offset. This is the ratio between the ray origin distance
  90. // from the plane and the ray direction projected on the plane normal.
  91. t = -(originDistanceFromPlane / directionProjectionOnPlaneNormal);
  92. // Only accept values in the [0, 1] interval
  93. if (t < 0.0f || t > 1.0f) return false;
  94. // The plane was intersected
  95. return true;
  96. }
  97. #endregion
  98. }
  99. }
  100. #endif