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