FrustumBoundingBox.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. export default class FrustumBoundingBox {
  2. constructor() {
  3. this.min = {
  4. x: 0,
  5. y: 0,
  6. z: 0
  7. };
  8. this.max = {
  9. x: 0,
  10. y: 0,
  11. z: 0
  12. };
  13. }
  14. fromFrustum( frustum ) {
  15. const vertices = [];
  16. for ( let i = 0; i < 4; i ++ ) {
  17. vertices.push( frustum.vertices.near[ i ] );
  18. vertices.push( frustum.vertices.far[ i ] );
  19. }
  20. this.min = {
  21. x: vertices[ 0 ].x,
  22. y: vertices[ 0 ].y,
  23. z: vertices[ 0 ].z
  24. };
  25. this.max = {
  26. x: vertices[ 0 ].x,
  27. y: vertices[ 0 ].y,
  28. z: vertices[ 0 ].z
  29. };
  30. for ( let i = 1; i < 8; i ++ ) {
  31. this.min.x = Math.min( this.min.x, vertices[ i ].x );
  32. this.min.y = Math.min( this.min.y, vertices[ i ].y );
  33. this.min.z = Math.min( this.min.z, vertices[ i ].z );
  34. this.max.x = Math.max( this.max.x, vertices[ i ].x );
  35. this.max.y = Math.max( this.max.y, vertices[ i ].y );
  36. this.max.z = Math.max( this.max.z, vertices[ i ].z );
  37. }
  38. return this;
  39. }
  40. getSize() {
  41. this.size = {
  42. x: this.max.x - this.min.x,
  43. y: this.max.y - this.min.y,
  44. z: this.max.z - this.min.z
  45. };
  46. return this.size;
  47. }
  48. getCenter( margin ) {
  49. this.center = {
  50. x: ( this.max.x + this.min.x ) / 2,
  51. y: ( this.max.y + this.min.y ) / 2,
  52. z: this.max.z + margin
  53. };
  54. return this.center;
  55. }
  56. }