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