Frustum.js 7.0 KB

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