math.frustum.ts 5.1 KB

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