math.frustum.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { Plane } from "./math.plane.js";
  2. /**
  3. * Represents a camera frustum
  4. */
  5. export class Frustum {
  6. /**
  7. * Gets the planes representing the frustum
  8. * @param transform matrix to be applied to the returned planes
  9. * @returns a new array of 6 Frustum planes computed by the given transformation matrix.
  10. */
  11. static GetPlanes(transform) {
  12. const frustumPlanes = [];
  13. for (let index = 0; index < 6; index++) {
  14. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  15. }
  16. Frustum.GetPlanesToRef(transform, frustumPlanes);
  17. return frustumPlanes;
  18. }
  19. /**
  20. * Gets the near frustum plane transformed by the transform matrix
  21. * @param transform transformation matrix to be applied to the resulting frustum plane
  22. * @param frustumPlane the resulting frustum plane
  23. */
  24. static GetNearPlaneToRef(transform, frustumPlane) {
  25. const m = transform.m;
  26. frustumPlane.normal.x = m[3] + m[2];
  27. frustumPlane.normal.y = m[7] + m[6];
  28. frustumPlane.normal.z = m[11] + m[10];
  29. frustumPlane.d = m[15] + m[14];
  30. frustumPlane.normalize();
  31. }
  32. /**
  33. * Gets the far frustum plane transformed by the transform matrix
  34. * @param transform transformation matrix to be applied to the resulting frustum plane
  35. * @param frustumPlane the resulting frustum plane
  36. */
  37. static GetFarPlaneToRef(transform, frustumPlane) {
  38. const m = transform.m;
  39. frustumPlane.normal.x = m[3] - m[2];
  40. frustumPlane.normal.y = m[7] - m[6];
  41. frustumPlane.normal.z = m[11] - m[10];
  42. frustumPlane.d = m[15] - m[14];
  43. frustumPlane.normalize();
  44. }
  45. /**
  46. * Gets the left frustum plane transformed by the transform matrix
  47. * @param transform transformation matrix to be applied to the resulting frustum plane
  48. * @param frustumPlane the resulting frustum plane
  49. */
  50. static GetLeftPlaneToRef(transform, frustumPlane) {
  51. const m = transform.m;
  52. frustumPlane.normal.x = m[3] + m[0];
  53. frustumPlane.normal.y = m[7] + m[4];
  54. frustumPlane.normal.z = m[11] + m[8];
  55. frustumPlane.d = m[15] + m[12];
  56. frustumPlane.normalize();
  57. }
  58. /**
  59. * Gets the right frustum plane transformed by the transform matrix
  60. * @param transform transformation matrix to be applied to the resulting frustum plane
  61. * @param frustumPlane the resulting frustum plane
  62. */
  63. static GetRightPlaneToRef(transform, frustumPlane) {
  64. const m = transform.m;
  65. frustumPlane.normal.x = m[3] - m[0];
  66. frustumPlane.normal.y = m[7] - m[4];
  67. frustumPlane.normal.z = m[11] - m[8];
  68. frustumPlane.d = m[15] - m[12];
  69. frustumPlane.normalize();
  70. }
  71. /**
  72. * Gets the top frustum plane transformed by the transform matrix
  73. * @param transform transformation matrix to be applied to the resulting frustum plane
  74. * @param frustumPlane the resulting frustum plane
  75. */
  76. static GetTopPlaneToRef(transform, frustumPlane) {
  77. const m = transform.m;
  78. frustumPlane.normal.x = m[3] - m[1];
  79. frustumPlane.normal.y = m[7] - m[5];
  80. frustumPlane.normal.z = m[11] - m[9];
  81. frustumPlane.d = m[15] - m[13];
  82. frustumPlane.normalize();
  83. }
  84. /**
  85. * Gets the bottom frustum plane transformed by the transform matrix
  86. * @param transform transformation matrix to be applied to the resulting frustum plane
  87. * @param frustumPlane the resulting frustum plane
  88. */
  89. static GetBottomPlaneToRef(transform, frustumPlane) {
  90. const m = transform.m;
  91. frustumPlane.normal.x = m[3] + m[1];
  92. frustumPlane.normal.y = m[7] + m[5];
  93. frustumPlane.normal.z = m[11] + m[9];
  94. frustumPlane.d = m[15] + m[13];
  95. frustumPlane.normalize();
  96. }
  97. /**
  98. * Sets the given array "frustumPlanes" with the 6 Frustum planes computed by the given transformation matrix.
  99. * @param transform transformation matrix to be applied to the resulting frustum planes
  100. * @param frustumPlanes the resulting frustum planes
  101. */
  102. static GetPlanesToRef(transform, frustumPlanes) {
  103. // Near
  104. Frustum.GetNearPlaneToRef(transform, frustumPlanes[0]);
  105. // Far
  106. Frustum.GetFarPlaneToRef(transform, frustumPlanes[1]);
  107. // Left
  108. Frustum.GetLeftPlaneToRef(transform, frustumPlanes[2]);
  109. // Right
  110. Frustum.GetRightPlaneToRef(transform, frustumPlanes[3]);
  111. // Top
  112. Frustum.GetTopPlaneToRef(transform, frustumPlanes[4]);
  113. // Bottom
  114. Frustum.GetBottomPlaneToRef(transform, frustumPlanes[5]);
  115. }
  116. /**
  117. * Tests if a point is located between the frustum planes.
  118. * @param point defines the point to test
  119. * @param frustumPlanes defines the frustum planes to test
  120. * @returns true if the point is located between the frustum planes
  121. */
  122. static IsPointInFrustum(point, frustumPlanes) {
  123. for (let i = 0; i < 6; i++) {
  124. if (frustumPlanes[i].dotCoordinate(point) < 0) {
  125. return false;
  126. }
  127. }
  128. return true;
  129. }
  130. }
  131. //# sourceMappingURL=math.frustum.js.map