FrustumBoundingBox.js 1.3 KB

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