Frustum.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. });
  38. test( "copy", function() {
  39. var p0 = new THREE.Plane( unit3, -1 );
  40. var p1 = new THREE.Plane( unit3, 1 );
  41. var p2 = new THREE.Plane( unit3, 2 );
  42. var p3 = new THREE.Plane( unit3, 3 );
  43. var p4 = new THREE.Plane( unit3, 4 );
  44. var p5 = new THREE.Plane( unit3, 5 );
  45. var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
  46. var a = new THREE.Frustum().copy( b );
  47. ok( a.planes[0].equals( p0 ), "Passed!" );
  48. ok( a.planes[1].equals( p1 ), "Passed!" );
  49. ok( a.planes[2].equals( p2 ), "Passed!" );
  50. ok( a.planes[3].equals( p3 ), "Passed!" );
  51. ok( a.planes[4].equals( p4 ), "Passed!" );
  52. ok( a.planes[5].equals( p5 ), "Passed!" );
  53. // ensure it is a true copy by modifying source
  54. b.planes[0] = p1;
  55. ok( a.planes[0].equals( p0 ), "Passed!" );
  56. });
  57. test( "setFromMatrix/makeOrthographic/containsPoint", function() {
  58. var m = new THREE.Matrix4().makeOrthographic( -1, 1, -1, 1, 1, 100 )
  59. var a = new THREE.Frustum().setFromMatrix( m );
  60. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  61. ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
  62. ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
  63. ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
  64. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -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( 0, 0, -100 ) ), "Passed!" );
  68. ok( a.containsPoint( new THREE.Vector3( -1, -1, -100 ) ), "Passed!" );
  69. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -100.1 ) ), "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( 0, 0, -101 ) ), "Passed!" );
  73. });
  74. test( "setFromMatrix/makeFrustum/containsPoint", function() {
  75. var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
  76. var a = new THREE.Frustum().setFromMatrix( m );
  77. ok( ! a.containsPoint( new THREE.Vector3( 0, 0, 0 ) ), "Passed!" );
  78. ok( a.containsPoint( new THREE.Vector3( 0, 0, -50 ) ), "Passed!" );
  79. ok( a.containsPoint( new THREE.Vector3( 0, 0, -1.001 ) ), "Passed!" );
  80. ok( a.containsPoint( new THREE.Vector3( -1, -1, -1.001 ) ), "Passed!" );
  81. ok( ! a.containsPoint( new THREE.Vector3( -1.1, -1.1, -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( 0, 0, -99.999 ) ), "Passed!" );
  85. ok( a.containsPoint( new THREE.Vector3( -99.999, -99.999, -99.999 ) ), "Passed!" );
  86. ok( ! a.containsPoint( new THREE.Vector3( -100.1, -100.1, -100.1 ) ), "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( 0, 0, -101 ) ), "Passed!" );
  90. });
  91. test( "setFromMatrix/makeFrustum/intersectsSphere", function() {
  92. var m = new THREE.Matrix4().makeFrustum( -1, 1, -1, 1, 1, 100 )
  93. var a = new THREE.Frustum().setFromMatrix( m );
  94. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0 ) ), "Passed!" );
  95. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 0.9 ) ), "Passed!" );
  96. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, 0 ), 1.1 ) ), "Passed!" );
  97. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -50 ), 0 ) ), "Passed!" );
  98. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -1.001 ), 0 ) ), "Passed!" );
  99. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1, -1, -1.001 ), 0 ) ), "Passed!" );
  100. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0 ) ), "Passed!" );
  101. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -1.1, -1.1, -1.001 ), 0.5 ) ), "Passed!" );
  102. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1, 1, -1.001 ), 0 ) ), "Passed!" );
  103. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0 ) ), "Passed!" );
  104. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 1.1, 1.1, -1.001 ), 0.5 ) ), "Passed!" );
  105. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -99.999 ), 0 ) ), "Passed!" );
  106. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -99.999, -99.999, -99.999 ), 0 ) ), "Passed!" );
  107. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0 ) ), "Passed!" );
  108. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( -100.1, -100.1, -100.1 ), 0.5 ) ), "Passed!" );
  109. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 99.999, 99.999, -99.999 ), 0 ) ), "Passed!" );
  110. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0 ) ), "Passed!" );
  111. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 100.1, 100.1, -100.1 ), 0.2 ) ), "Passed!" );
  112. ok( ! a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 0 ) ), "Passed!" );
  113. ok( a.intersectsSphere( new THREE.Sphere( new THREE.Vector3( 0, 0, -101 ), 1.1 ) ), "Passed!" );
  114. });
  115. test( "clone", function() {
  116. var p0 = new THREE.Plane( unit3, -1 );
  117. var p1 = new THREE.Plane( unit3, 1 );
  118. var p2 = new THREE.Plane( unit3, 2 );
  119. var p3 = new THREE.Plane( unit3, 3 );
  120. var p4 = new THREE.Plane( unit3, 4 );
  121. var p5 = new THREE.Plane( unit3, 5 );
  122. var b = new THREE.Frustum( p0, p1, p2, p3, p4, p5 );
  123. var a = b.clone();
  124. ok( a.planes[0].equals( p0 ), "Passed!" );
  125. ok( a.planes[1].equals( p1 ), "Passed!" );
  126. ok( a.planes[2].equals( p2 ), "Passed!" );
  127. ok( a.planes[3].equals( p3 ), "Passed!" );
  128. ok( a.planes[4].equals( p4 ), "Passed!" );
  129. ok( a.planes[5].equals( p5 ), "Passed!" );
  130. // ensure it is a true copy by modifying source
  131. a.planes[0].copy( p1 );
  132. ok( b.planes[0].equals( p0 ), "Passed!" );
  133. });