Frustum.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @author bhouston / http://exocortex.com
  3. */
  4. module( "Frustum" );
  5. var unit3 = new THREE.Vector3( 1, 0, 0 );
  6. var planeEquals = function ( a, b, tolerance ) {
  7. tolerance = tolerance || 0.0001;
  8. if( a.normal.distanceTo( b.normal ) > tolerance ) {
  9. return false;
  10. }
  11. if( Math.abs( a.constant - b.constant ) > tolerance ) {
  12. return false;
  13. }
  14. return true;
  15. };
  16. test( "constructor", function() {
  17. var a = new THREE.Frustum();
  18. ok( a.planes !== undefined, "Passed!" );
  19. ok( a.planes.length === 6, "Passed!" );
  20. var pDefault = new THREE.Plane();
  21. for( var i = 0; i < 6; i ++ ) {
  22. ok( a.planes[i].equals( pDefault ), "Passed!" );
  23. }
  24. var p0 = new THREE.Plane( unit3, -1 );
  25. var p1 = new THREE.Plane( unit3, 1 );
  26. var p2 = new THREE.Plane( unit3, 2 );
  27. var p3 = new THREE.Plane( unit3, 3 );
  28. var p4 = new THREE.Plane( unit3, 4 );
  29. var p5 = new THREE.Plane( unit3, 5 );
  30. a = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
  31. ok( a.planes[0].equals( p0 ), "Passed!" );
  32. ok( a.planes[1].equals( p1 ), "Passed!" );
  33. ok( a.planes[2].equals( p2 ), "Passed!" );
  34. ok( a.planes[3].equals( p3 ), "Passed!" );
  35. ok( a.planes[4].equals( p4 ), "Passed!" );
  36. ok( a.planes[5].equals( p5 ), "Passed!" );
  37. p0.copy( p1 );
  38. ok( ! a.planes[0].equals( p0 ), "Passed!" );
  39. });
  40. test( "copy", function() {
  41. var p0 = new THREE.Plane( unit3, -1 );
  42. var p1 = new THREE.Plane( unit3, 1 );
  43. var p2 = new THREE.Plane( unit3, 2 );
  44. var p3 = new THREE.Plane( unit3, 3 );
  45. var p4 = new THREE.Plane( unit3, 4 );
  46. var p5 = new THREE.Plane( unit3, 5 );
  47. var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
  48. var a = new THREE.Frustum().copy( b );
  49. ok( a.planes[0].equals( p0 ), "Passed!" );
  50. ok( a.planes[1].equals( p1 ), "Passed!" );
  51. ok( a.planes[2].equals( p2 ), "Passed!" );
  52. ok( a.planes[3].equals( p3 ), "Passed!" );
  53. ok( a.planes[4].equals( p4 ), "Passed!" );
  54. ok( a.planes[5].equals( p5 ), "Passed!" );
  55. // ensure it is a true copy by modifying source
  56. b.planes[0] = p1;
  57. ok( a.planes[0].equals( p0 ), "Passed!" );
  58. });
  59. test( "setFromMatrix/makeOrthographic/containsPoint", function() {
  60. var m = new THREE.Matrix4().makeOrthographic( -1, 1, -1, 1, 1, 100 )
  61. var a = new THREE.Frustum().setFromMatrix( m );
  62. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  63. ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
  64. ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
  65. ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
  66. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -1.001 ) ), "Passed!" );
  67. ok( a.containsPoint( new THREE.Vector3( 1, 1, -1.001 ) ), "Passed!" );
  68. ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -1.001 ) ), "Passed!" );
  69. ok( a.containsPoint( new THREE.Vector3( 0, 0, -100 ) ), "Passed!" );
  70. ok( a.containsPoint( new THREE.Vector3( -1, -1, -100 ) ), "Passed!" );
  71. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -100.1 ) ), "Passed!" );
  72. ok( a.containsPoint( new THREE.Vector3( 1, 1, -100 ) ), "Passed!" );
  73. ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -100.1 ) ), "Passed!" );
  74. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
  75. });
  76. test( "setFromMatrix/makeFrustum/containsPoint", function() {
  77. var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
  78. var a = new THREE.Frustum().setFromMatrix( m );
  79. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  80. ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
  81. ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
  82. ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
  83. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -1.001 ) ), "Passed!" );
  84. ok( a.containsPoint( new THREE.Vector3( 1, 1, -1.001 ) ), "Passed!" );
  85. ok( ! a.containsPoint( new THREE.Vector3( 1.1, 1.1, -1.001 ) ), "Passed!" );
  86. ok( a.containsPoint( new THREE.Vector3( 0, 0, -99.999 ) ), "Passed!" );
  87. ok( a.containsPoint( new THREE.Vector3( -99.999, -99.999, -99.999 ) ), "Passed!" );
  88. ok( ! a.containsPoint( new THREE.Vector3( -100.1, -100.1, -100.1 ) ), "Passed!" );
  89. ok( a.containsPoint( new THREE.Vector3( 99.999, 99.999, -99.999 ) ), "Passed!" );
  90. ok( ! a.containsPoint( new THREE.Vector3( 100.1, 100.1, -100.1 ) ), "Passed!" );
  91. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, -101 ) ), "Passed!" );
  92. });
  93. test( "setFromMatrix/makeFrustum/intersectsSphere", function() {
  94. var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
  95. var a = new THREE.Frustum().setFromMatrix( m );
  96. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0 ) ), "Passed!" );
  97. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0.9 ) ), "Passed!" );
  98. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 1.1 ) ), "Passed!" );
  99. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -50 ), 0 ) ), "Passed!" );
  100. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -1.001 ), 0 ) ), "Passed!" );
  101. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1, -1, -1.001 ), 0 ) ), "Passed!" );
  102. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0 ) ), "Passed!" );
  103. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0.5 ) ), "Passed!" );
  104. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1, 1, -1.001 ), 0 ) ), "Passed!" );
  105. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0 ) ), "Passed!" );
  106. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0.5 ) ), "Passed!" );
  107. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -99.999 ), 0 ) ), "Passed!" );
  108. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -99.999, -99.999, -99.999 ), 0 ) ), "Passed!" );
  109. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0 ) ), "Passed!" );
  110. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0.5 ) ), "Passed!" );
  111. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 99.999, 99.999, -99.999 ), 0 ) ), "Passed!" );
  112. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0 ) ), "Passed!" );
  113. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0.2 ) ), "Passed!" );
  114. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 0 ) ), "Passed!" );
  115. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 1.1 ) ), "Passed!" );
  116. });
  117. test( "clone", function() {
  118. var p0 = new THREE.Plane( unit3, -1 );
  119. var p1 = new THREE.Plane( unit3, 1 );
  120. var p2 = new THREE.Plane( unit3, 2 );
  121. var p3 = new THREE.Plane( unit3, 3 );
  122. var p4 = new THREE.Plane( unit3, 4 );
  123. var p5 = new THREE.Plane( unit3, 5 );
  124. var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
  125. var a = b.clone();
  126. ok( a.planes[0].equals( p0 ), "Passed!" );
  127. ok( a.planes[1].equals( p1 ), "Passed!" );
  128. ok( a.planes[2].equals( p2 ), "Passed!" );
  129. ok( a.planes[3].equals( p3 ), "Passed!" );
  130. ok( a.planes[4].equals( p4 ), "Passed!" );
  131. ok( a.planes[5].equals( p5 ), "Passed!" );
  132. // ensure it is a true copy by modifying source
  133. b.planes[0].copy( p1 );
  134. ok( a.planes[0].equals( p0 ), "Passed!" );
  135. });