Frustum.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { Vector3 } from './Vector3.js';
  2. import { Sphere } from './Sphere.js';
  3. import { Plane } from './Plane.js';
  4. /**
  5. * @author mrdoob / http://mrdoob.com/
  6. * @author alteredq / http://alteredqualia.com/
  7. * @author bhouston / http://clara.io
  8. */
  9. const _sphere = new Sphere();
  10. const _vector = new Vector3();
  11. function Frustum( p0, p1, p2, p3, p4, p5 ) {
  12. this.planes = [
  13. ( p0 !== undefined ) ? p0 : new Plane(),
  14. ( p1 !== undefined ) ? p1 : new Plane(),
  15. ( p2 !== undefined ) ? p2 : new Plane(),
  16. ( p3 !== undefined ) ? p3 : new Plane(),
  17. ( p4 !== undefined ) ? p4 : new Plane(),
  18. ( p5 !== undefined ) ? p5 : new Plane()
  19. ];
  20. }
  21. Object.assign( Frustum.prototype, {
  22. set: function ( p0, p1, p2, p3, p4, p5 ) {
  23. const planes = this.planes;
  24. planes[ 0 ].copy( p0 );
  25. planes[ 1 ].copy( p1 );
  26. planes[ 2 ].copy( p2 );
  27. planes[ 3 ].copy( p3 );
  28. planes[ 4 ].copy( p4 );
  29. planes[ 5 ].copy( p5 );
  30. return this;
  31. },
  32. clone: function () {
  33. return new this.constructor().copy( this );
  34. },
  35. copy: function ( frustum ) {
  36. const planes = this.planes;
  37. for ( let i = 0; i < 6; i ++ ) {
  38. planes[ i ].copy( frustum.planes[ i ] );
  39. }
  40. return this;
  41. },
  42. setFromProjectionMatrix: function ( m ) {
  43. const planes = this.planes;
  44. const me = m.elements;
  45. const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  46. const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  47. const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  48. const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  49. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  50. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  51. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  52. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  53. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  54. planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
  55. return this;
  56. },
  57. intersectsObject: function ( object ) {
  58. const geometry = object.geometry;
  59. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  60. _sphere.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
  61. return this.intersectsSphere( _sphere );
  62. },
  63. intersectsSprite: function ( sprite ) {
  64. _sphere.center.set( 0, 0, 0 );
  65. _sphere.radius = 0.7071067811865476;
  66. _sphere.applyMatrix4( sprite.matrixWorld );
  67. return this.intersectsSphere( _sphere );
  68. },
  69. intersectsSphere: function ( sphere ) {
  70. const planes = this.planes;
  71. const center = sphere.center;
  72. const negRadius = - sphere.radius;
  73. for ( let i = 0; i < 6; i ++ ) {
  74. const distance = planes[ i ].distanceToPoint( center );
  75. if ( distance < negRadius ) {
  76. return false;
  77. }
  78. }
  79. return true;
  80. },
  81. intersectsBox: function ( box ) {
  82. const planes = this.planes;
  83. for ( let i = 0; i < 6; i ++ ) {
  84. const plane = planes[ i ];
  85. // corner at max distance
  86. _vector.x = plane.normal.x > 0 ? box.max.x : box.min.x;
  87. _vector.y = plane.normal.y > 0 ? box.max.y : box.min.y;
  88. _vector.z = plane.normal.z > 0 ? box.max.z : box.min.z;
  89. if ( plane.distanceToPoint( _vector ) < 0 ) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. },
  95. containsPoint: function ( point ) {
  96. const planes = this.planes;
  97. for ( let i = 0; i < 6; i ++ ) {
  98. if ( planes[ i ].distanceToPoint( point ) < 0 ) {
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. } );
  105. export { Frustum };