Frustum.tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* global QUnit */
  2. import { Frustum } from '../../../../src/math/Frustum.js';
  3. import { Sphere } from '../../../../src/math/Sphere.js';
  4. import { Plane } from '../../../../src/math/Plane.js';
  5. import { Sprite } from '../../../../src/objects/Sprite.js';
  6. import { Vector3 } from '../../../../src/math/Vector3.js';
  7. import { Matrix4 } from '../../../../src/math/Matrix4.js';
  8. import { Box3 } from '../../../../src/math/Box3.js';
  9. import { Mesh } from '../../../../src/objects/Mesh.js';
  10. import { BoxGeometry } from '../../../../src/geometries/BoxGeometry.js';
  11. import { zero3, one3, eps } from '../../utils/math-constants.js';
  12. const unit3 = new Vector3( 1, 0, 0 );
  13. export default QUnit.module( 'Maths', () => {
  14. QUnit.module( 'Frustum', () => {
  15. // INSTANCING
  16. QUnit.test( 'Instancing', ( assert ) => {
  17. let a = new Frustum();
  18. assert.ok( a.planes !== undefined, 'Passed!' );
  19. assert.ok( a.planes.length === 6, 'Passed!' );
  20. const pDefault = new Plane();
  21. for ( let i = 0; i < 6; i ++ ) {
  22. assert.ok( a.planes[ i ].equals( pDefault ), 'Passed!' );
  23. }
  24. const p0 = new Plane( unit3, - 1 );
  25. const p1 = new Plane( unit3, 1 );
  26. const p2 = new Plane( unit3, 2 );
  27. const p3 = new Plane( unit3, 3 );
  28. const p4 = new Plane( unit3, 4 );
  29. const p5 = new Plane( unit3, 5 );
  30. a = new Frustum( p0, p1, p2, p3, p4, p5 );
  31. assert.ok( a.planes[ 0 ].equals( p0 ), 'Passed!' );
  32. assert.ok( a.planes[ 1 ].equals( p1 ), 'Passed!' );
  33. assert.ok( a.planes[ 2 ].equals( p2 ), 'Passed!' );
  34. assert.ok( a.planes[ 3 ].equals( p3 ), 'Passed!' );
  35. assert.ok( a.planes[ 4 ].equals( p4 ), 'Passed!' );
  36. assert.ok( a.planes[ 5 ].equals( p5 ), 'Passed!' );
  37. } );
  38. // PUBLIC
  39. QUnit.test( 'set', ( assert ) => {
  40. const a = new Frustum();
  41. const p0 = new Plane( unit3, - 1 );
  42. const p1 = new Plane( unit3, 1 );
  43. const p2 = new Plane( unit3, 2 );
  44. const p3 = new Plane( unit3, 3 );
  45. const p4 = new Plane( unit3, 4 );
  46. const p5 = new Plane( unit3, 5 );
  47. a.set( p0, p1, p2, p3, p4, p5 );
  48. assert.ok( a.planes[ 0 ].equals( p0 ), 'Check plane #0' );
  49. assert.ok( a.planes[ 1 ].equals( p1 ), 'Check plane #1' );
  50. assert.ok( a.planes[ 2 ].equals( p2 ), 'Check plane #2' );
  51. assert.ok( a.planes[ 3 ].equals( p3 ), 'Check plane #3' );
  52. assert.ok( a.planes[ 4 ].equals( p4 ), 'Check plane #4' );
  53. assert.ok( a.planes[ 5 ].equals( p5 ), 'Check plane #5' );
  54. } );
  55. QUnit.test( 'clone', ( assert ) => {
  56. const p0 = new Plane( unit3, - 1 );
  57. const p1 = new Plane( unit3, 1 );
  58. const p2 = new Plane( unit3, 2 );
  59. const p3 = new Plane( unit3, 3 );
  60. const p4 = new Plane( unit3, 4 );
  61. const p5 = new Plane( unit3, 5 );
  62. const b = new Frustum( p0, p1, p2, p3, p4, p5 );
  63. const a = b.clone();
  64. assert.ok( a.planes[ 0 ].equals( p0 ), 'Passed!' );
  65. assert.ok( a.planes[ 1 ].equals( p1 ), 'Passed!' );
  66. assert.ok( a.planes[ 2 ].equals( p2 ), 'Passed!' );
  67. assert.ok( a.planes[ 3 ].equals( p3 ), 'Passed!' );
  68. assert.ok( a.planes[ 4 ].equals( p4 ), 'Passed!' );
  69. assert.ok( a.planes[ 5 ].equals( p5 ), 'Passed!' );
  70. // ensure it is a true copy by modifying source
  71. a.planes[ 0 ].copy( p1 );
  72. assert.ok( b.planes[ 0 ].equals( p0 ), 'Passed!' );
  73. } );
  74. QUnit.test( 'copy', ( assert ) => {
  75. const p0 = new Plane( unit3, - 1 );
  76. const p1 = new Plane( unit3, 1 );
  77. const p2 = new Plane( unit3, 2 );
  78. const p3 = new Plane( unit3, 3 );
  79. const p4 = new Plane( unit3, 4 );
  80. const p5 = new Plane( unit3, 5 );
  81. const b = new Frustum( p0, p1, p2, p3, p4, p5 );
  82. const a = new Frustum().copy( b );
  83. assert.ok( a.planes[ 0 ].equals( p0 ), 'Passed!' );
  84. assert.ok( a.planes[ 1 ].equals( p1 ), 'Passed!' );
  85. assert.ok( a.planes[ 2 ].equals( p2 ), 'Passed!' );
  86. assert.ok( a.planes[ 3 ].equals( p3 ), 'Passed!' );
  87. assert.ok( a.planes[ 4 ].equals( p4 ), 'Passed!' );
  88. assert.ok( a.planes[ 5 ].equals( p5 ), 'Passed!' );
  89. // ensure it is a true copy by modifying source
  90. b.planes[ 0 ] = p1;
  91. assert.ok( a.planes[ 0 ].equals( p0 ), 'Passed!' );
  92. } );
  93. QUnit.test( 'setFromProjectionMatrix/makeOrthographic/containsPoint', ( assert ) => {
  94. const m = new Matrix4().makeOrthographic( - 1, 1, - 1, 1, 1, 100 );
  95. const a = new Frustum().setFromProjectionMatrix( m );
  96. assert.ok( ! a.containsPoint( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  97. assert.ok( a.containsPoint( new Vector3( 0, 0, - 50 ) ), 'Passed!' );
  98. assert.ok( a.containsPoint( new Vector3( 0, 0, - 1.001 ) ), 'Passed!' );
  99. assert.ok( a.containsPoint( new Vector3( - 1, - 1, - 1.001 ) ), 'Passed!' );
  100. assert.ok( ! a.containsPoint( new Vector3( - 1.1, - 1.1, - 1.001 ) ), 'Passed!' );
  101. assert.ok( a.containsPoint( new Vector3( 1, 1, - 1.001 ) ), 'Passed!' );
  102. assert.ok( ! a.containsPoint( new Vector3( 1.1, 1.1, - 1.001 ) ), 'Passed!' );
  103. assert.ok( a.containsPoint( new Vector3( 0, 0, - 100 ) ), 'Passed!' );
  104. assert.ok( a.containsPoint( new Vector3( - 1, - 1, - 100 ) ), 'Passed!' );
  105. assert.ok( ! a.containsPoint( new Vector3( - 1.1, - 1.1, - 100.1 ) ), 'Passed!' );
  106. assert.ok( a.containsPoint( new Vector3( 1, 1, - 100 ) ), 'Passed!' );
  107. assert.ok( ! a.containsPoint( new Vector3( 1.1, 1.1, - 100.1 ) ), 'Passed!' );
  108. assert.ok( ! a.containsPoint( new Vector3( 0, 0, - 101 ) ), 'Passed!' );
  109. } );
  110. QUnit.test( 'setFromProjectionMatrix/makePerspective/containsPoint', ( assert ) => {
  111. const m = new Matrix4().makePerspective( - 1, 1, 1, - 1, 1, 100 );
  112. const a = new Frustum().setFromProjectionMatrix( m );
  113. assert.ok( ! a.containsPoint( new Vector3( 0, 0, 0 ) ), 'Passed!' );
  114. assert.ok( a.containsPoint( new Vector3( 0, 0, - 50 ) ), 'Passed!' );
  115. assert.ok( a.containsPoint( new Vector3( 0, 0, - 1.001 ) ), 'Passed!' );
  116. assert.ok( a.containsPoint( new Vector3( - 1, - 1, - 1.001 ) ), 'Passed!' );
  117. assert.ok( ! a.containsPoint( new Vector3( - 1.1, - 1.1, - 1.001 ) ), 'Passed!' );
  118. assert.ok( a.containsPoint( new Vector3( 1, 1, - 1.001 ) ), 'Passed!' );
  119. assert.ok( ! a.containsPoint( new Vector3( 1.1, 1.1, - 1.001 ) ), 'Passed!' );
  120. assert.ok( a.containsPoint( new Vector3( 0, 0, - 99.999 ) ), 'Passed!' );
  121. assert.ok( a.containsPoint( new Vector3( - 99.999, - 99.999, - 99.999 ) ), 'Passed!' );
  122. assert.ok( ! a.containsPoint( new Vector3( - 100.1, - 100.1, - 100.1 ) ), 'Passed!' );
  123. assert.ok( a.containsPoint( new Vector3( 99.999, 99.999, - 99.999 ) ), 'Passed!' );
  124. assert.ok( ! a.containsPoint( new Vector3( 100.1, 100.1, - 100.1 ) ), 'Passed!' );
  125. assert.ok( ! a.containsPoint( new Vector3( 0, 0, - 101 ) ), 'Passed!' );
  126. } );
  127. QUnit.test( 'setFromProjectionMatrix/makePerspective/intersectsSphere', ( assert ) => {
  128. const m = new Matrix4().makePerspective( - 1, 1, 1, - 1, 1, 100 );
  129. const a = new Frustum().setFromProjectionMatrix( m );
  130. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( 0, 0, 0 ), 0 ) ), 'Passed!' );
  131. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( 0, 0, 0 ), 0.9 ) ), 'Passed!' );
  132. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 0, 0, 0 ), 1.1 ) ), 'Passed!' );
  133. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 0, 0, - 50 ), 0 ) ), 'Passed!' );
  134. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 0, 0, - 1.001 ), 0 ) ), 'Passed!' );
  135. assert.ok( a.intersectsSphere( new Sphere( new Vector3( - 1, - 1, - 1.001 ), 0 ) ), 'Passed!' );
  136. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( - 1.1, - 1.1, - 1.001 ), 0 ) ), 'Passed!' );
  137. assert.ok( a.intersectsSphere( new Sphere( new Vector3( - 1.1, - 1.1, - 1.001 ), 0.5 ) ), 'Passed!' );
  138. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 1, 1, - 1.001 ), 0 ) ), 'Passed!' );
  139. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( 1.1, 1.1, - 1.001 ), 0 ) ), 'Passed!' );
  140. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 1.1, 1.1, - 1.001 ), 0.5 ) ), 'Passed!' );
  141. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 0, 0, - 99.999 ), 0 ) ), 'Passed!' );
  142. assert.ok( a.intersectsSphere( new Sphere( new Vector3( - 99.999, - 99.999, - 99.999 ), 0 ) ), 'Passed!' );
  143. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( - 100.1, - 100.1, - 100.1 ), 0 ) ), 'Passed!' );
  144. assert.ok( a.intersectsSphere( new Sphere( new Vector3( - 100.1, - 100.1, - 100.1 ), 0.5 ) ), 'Passed!' );
  145. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 99.999, 99.999, - 99.999 ), 0 ) ), 'Passed!' );
  146. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( 100.1, 100.1, - 100.1 ), 0 ) ), 'Passed!' );
  147. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 100.1, 100.1, - 100.1 ), 0.2 ) ), 'Passed!' );
  148. assert.ok( ! a.intersectsSphere( new Sphere( new Vector3( 0, 0, - 101 ), 0 ) ), 'Passed!' );
  149. assert.ok( a.intersectsSphere( new Sphere( new Vector3( 0, 0, - 101 ), 1.1 ) ), 'Passed!' );
  150. } );
  151. QUnit.test( 'intersectsObject', ( assert ) => {
  152. const m = new Matrix4().makePerspective( - 1, 1, 1, - 1, 1, 100 );
  153. const a = new Frustum().setFromProjectionMatrix( m );
  154. const object = new Mesh( new BoxGeometry( 1, 1, 1 ) );
  155. let intersects;
  156. intersects = a.intersectsObject( object );
  157. assert.notOk( intersects, 'No intersection' );
  158. object.position.set( - 1, - 1, - 1 );
  159. object.updateMatrixWorld();
  160. intersects = a.intersectsObject( object );
  161. assert.ok( intersects, 'Successful intersection' );
  162. object.position.set( 1, 1, 1 );
  163. object.updateMatrixWorld();
  164. intersects = a.intersectsObject( object );
  165. assert.notOk( intersects, 'No intersection' );
  166. } );
  167. QUnit.test( 'intersectsSprite', ( assert ) => {
  168. const m = new Matrix4().makePerspective( - 1, 1, 1, - 1, 1, 100 );
  169. const a = new Frustum().setFromProjectionMatrix( m );
  170. const sprite = new Sprite();
  171. let intersects;
  172. intersects = a.intersectsSprite( sprite );
  173. assert.notOk( intersects, 'No intersection' );
  174. sprite.position.set( - 1, - 1, - 1 );
  175. sprite.updateMatrixWorld();
  176. intersects = a.intersectsSprite( sprite );
  177. assert.ok( intersects, 'Successful intersection' );
  178. } );
  179. QUnit.todo( 'intersectsSphere', ( assert ) => {
  180. assert.ok( false, 'everything\'s gonna be alright' );
  181. } );
  182. QUnit.test( 'intersectsBox', ( assert ) => {
  183. const m = new Matrix4().makePerspective( - 1, 1, 1, - 1, 1, 100 );
  184. const a = new Frustum().setFromProjectionMatrix( m );
  185. const box = new Box3( zero3.clone(), one3.clone() );
  186. let intersects;
  187. intersects = a.intersectsBox( box );
  188. assert.notOk( intersects, 'No intersection' );
  189. // add eps so that we prevent box touching the frustum,
  190. // which might intersect depending on floating point numerics
  191. box.translate( new Vector3( - 1 - eps, - 1 - eps, - 1 - eps ) );
  192. intersects = a.intersectsBox( box );
  193. assert.ok( intersects, 'Successful intersection' );
  194. } );
  195. QUnit.todo( 'containsPoint', ( assert ) => {
  196. assert.ok( false, 'everything\'s gonna be alright' );
  197. } );
  198. } );
  199. } );