source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/extras/helpers/CameraHelper.js@ 28897

Last change on this file since 28897 was 28897, checked in by davidb, 10 years ago

GUI front-end to server base plus web page content

File size: 3.6 KB
Line 
1/**
2 * @author alteredq / http://alteredqualia.com/
3 *
4 * - shows frustum, line of sight and up of the camera
5 * - suitable for fast updates
6 * - based on frustum visualization in lightgl.js shadowmap example
7 * http://evanw.github.com/lightgl.js/tests/shadowmap.html
8 */
9
10THREE.CameraHelper = function ( camera ) {
11
12 var geometry = new THREE.Geometry();
13 var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
14
15 var pointMap = {};
16
17 // colors
18
19 var hexFrustum = 0xffaa00;
20 var hexCone = 0xff0000;
21 var hexUp = 0x00aaff;
22 var hexTarget = 0xffffff;
23 var hexCross = 0x333333;
24
25 // near
26
27 addLine( "n1", "n2", hexFrustum );
28 addLine( "n2", "n4", hexFrustum );
29 addLine( "n4", "n3", hexFrustum );
30 addLine( "n3", "n1", hexFrustum );
31
32 // far
33
34 addLine( "f1", "f2", hexFrustum );
35 addLine( "f2", "f4", hexFrustum );
36 addLine( "f4", "f3", hexFrustum );
37 addLine( "f3", "f1", hexFrustum );
38
39 // sides
40
41 addLine( "n1", "f1", hexFrustum );
42 addLine( "n2", "f2", hexFrustum );
43 addLine( "n3", "f3", hexFrustum );
44 addLine( "n4", "f4", hexFrustum );
45
46 // cone
47
48 addLine( "p", "n1", hexCone );
49 addLine( "p", "n2", hexCone );
50 addLine( "p", "n3", hexCone );
51 addLine( "p", "n4", hexCone );
52
53 // up
54
55 addLine( "u1", "u2", hexUp );
56 addLine( "u2", "u3", hexUp );
57 addLine( "u3", "u1", hexUp );
58
59 // target
60
61 addLine( "c", "t", hexTarget );
62 addLine( "p", "c", hexCross );
63
64 // cross
65
66 addLine( "cn1", "cn2", hexCross );
67 addLine( "cn3", "cn4", hexCross );
68
69 addLine( "cf1", "cf2", hexCross );
70 addLine( "cf3", "cf4", hexCross );
71
72 function addLine( a, b, hex ) {
73
74 addPoint( a, hex );
75 addPoint( b, hex );
76
77 }
78
79 function addPoint( id, hex ) {
80
81 geometry.vertices.push( new THREE.Vector3() );
82 geometry.colors.push( new THREE.Color( hex ) );
83
84 if ( pointMap[ id ] === undefined ) {
85
86 pointMap[ id ] = [];
87
88 }
89
90 pointMap[ id ].push( geometry.vertices.length - 1 );
91
92 }
93
94 THREE.Line.call( this, geometry, material, THREE.LinePieces );
95
96 this.camera = camera;
97 this.matrixWorld = camera.matrixWorld;
98 this.matrixAutoUpdate = false;
99
100 this.pointMap = pointMap;
101
102 this.update();
103
104};
105
106THREE.CameraHelper.prototype = Object.create( THREE.Line.prototype );
107
108THREE.CameraHelper.prototype.update = function () {
109
110 var vector = new THREE.Vector3();
111 var camera = new THREE.Camera();
112 var projector = new THREE.Projector();
113
114 return function () {
115
116 var scope = this;
117
118 var w = 1, h = 1;
119
120 // we need just camera projection matrix
121 // world matrix must be identity
122
123 camera.projectionMatrix.copy( this.camera.projectionMatrix );
124
125 // center / target
126
127 setPoint( "c", 0, 0, -1 );
128 setPoint( "t", 0, 0, 1 );
129
130 // near
131
132 setPoint( "n1", -w, -h, -1 );
133 setPoint( "n2", w, -h, -1 );
134 setPoint( "n3", -w, h, -1 );
135 setPoint( "n4", w, h, -1 );
136
137 // far
138
139 setPoint( "f1", -w, -h, 1 );
140 setPoint( "f2", w, -h, 1 );
141 setPoint( "f3", -w, h, 1 );
142 setPoint( "f4", w, h, 1 );
143
144 // up
145
146 setPoint( "u1", w * 0.7, h * 1.1, -1 );
147 setPoint( "u2", -w * 0.7, h * 1.1, -1 );
148 setPoint( "u3", 0, h * 2, -1 );
149
150 // cross
151
152 setPoint( "cf1", -w, 0, 1 );
153 setPoint( "cf2", w, 0, 1 );
154 setPoint( "cf3", 0, -h, 1 );
155 setPoint( "cf4", 0, h, 1 );
156
157 setPoint( "cn1", -w, 0, -1 );
158 setPoint( "cn2", w, 0, -1 );
159 setPoint( "cn3", 0, -h, -1 );
160 setPoint( "cn4", 0, h, -1 );
161
162 function setPoint( point, x, y, z ) {
163
164 vector.set( x, y, z );
165 projector.unprojectVector( vector, camera );
166
167 var points = scope.pointMap[ point ];
168
169 if ( points !== undefined ) {
170
171 for ( var i = 0, il = points.length; i < il; i ++ ) {
172
173 scope.geometry.vertices[ points[ i ] ].copy( vector );
174
175 }
176
177 }
178
179 }
180
181 this.geometry.verticesNeedUpdate = true;
182
183 };
184
185}();
Note: See TracBrowser for help on using the repository browser.