Frustum.tests.js 11 KB

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