source: other-projects/playing-in-the-street/summer-2013/trunk/Playing-in-the-Street-WPF/Content/Web/mrdoob-three.js-4862f5f/src/extras/cameras/CombinedCamera.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: 5.0 KB
Line 
1/**
2 * @author zz85 / http://twitter.com/blurspline / http://www.lab4games.net/zz85/blog
3 *
4 * A general perpose camera, for setting FOV, Lens Focal Length,
5 * and switching between perspective and orthographic views easily.
6 * Use this only if you do not wish to manage
7 * both a Orthographic and Perspective Camera
8 *
9 */
10
11
12THREE.CombinedCamera = function ( width, height, fov, near, far, orthoNear, orthoFar ) {
13
14 THREE.Camera.call( this );
15
16 this.fov = fov;
17
18 this.left = -width / 2;
19 this.right = width / 2
20 this.top = height / 2;
21 this.bottom = -height / 2;
22
23 // We could also handle the projectionMatrix internally, but just wanted to test nested camera objects
24
25 this.cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, orthoNear, orthoFar );
26 this.cameraP = new THREE.PerspectiveCamera( fov, width / height, near, far );
27
28 this.zoom = 1;
29
30 this.toPerspective();
31
32 var aspect = width/height;
33
34};
35
36THREE.CombinedCamera.prototype = Object.create( THREE.Camera.prototype );
37
38THREE.CombinedCamera.prototype.toPerspective = function () {
39
40 // Switches to the Perspective Camera
41
42 this.near = this.cameraP.near;
43 this.far = this.cameraP.far;
44
45 this.cameraP.fov = this.fov / this.zoom ;
46
47 this.cameraP.updateProjectionMatrix();
48
49 this.projectionMatrix = this.cameraP.projectionMatrix;
50
51 this.inPerspectiveMode = true;
52 this.inOrthographicMode = false;
53
54};
55
56THREE.CombinedCamera.prototype.toOrthographic = function () {
57
58 // Switches to the Orthographic camera estimating viewport from Perspective
59
60 var fov = this.fov;
61 var aspect = this.cameraP.aspect;
62 var near = this.cameraP.near;
63 var far = this.cameraP.far;
64
65 // The size that we set is the mid plane of the viewing frustum
66
67 var hyperfocus = ( near + far ) / 2;
68
69 var halfHeight = Math.tan( fov / 2 ) * hyperfocus;
70 var planeHeight = 2 * halfHeight;
71 var planeWidth = planeHeight * aspect;
72 var halfWidth = planeWidth / 2;
73
74 halfHeight /= this.zoom;
75 halfWidth /= this.zoom;
76
77 this.cameraO.left = -halfWidth;
78 this.cameraO.right = halfWidth;
79 this.cameraO.top = halfHeight;
80 this.cameraO.bottom = -halfHeight;
81
82 // this.cameraO.left = -farHalfWidth;
83 // this.cameraO.right = farHalfWidth;
84 // this.cameraO.top = farHalfHeight;
85 // this.cameraO.bottom = -farHalfHeight;
86
87 // this.cameraO.left = this.left / this.zoom;
88 // this.cameraO.right = this.right / this.zoom;
89 // this.cameraO.top = this.top / this.zoom;
90 // this.cameraO.bottom = this.bottom / this.zoom;
91
92 this.cameraO.updateProjectionMatrix();
93
94 this.near = this.cameraO.near;
95 this.far = this.cameraO.far;
96 this.projectionMatrix = this.cameraO.projectionMatrix;
97
98 this.inPerspectiveMode = false;
99 this.inOrthographicMode = true;
100
101};
102
103
104THREE.CombinedCamera.prototype.setSize = function( width, height ) {
105
106 this.cameraP.aspect = width / height;
107 this.left = -width / 2;
108 this.right = width / 2
109 this.top = height / 2;
110 this.bottom = -height / 2;
111
112};
113
114
115THREE.CombinedCamera.prototype.setFov = function( fov ) {
116
117 this.fov = fov;
118
119 if ( this.inPerspectiveMode ) {
120
121 this.toPerspective();
122
123 } else {
124
125 this.toOrthographic();
126
127 }
128
129};
130
131// For mantaining similar API with PerspectiveCamera
132
133THREE.CombinedCamera.prototype.updateProjectionMatrix = function() {
134
135 if ( this.inPerspectiveMode ) {
136
137 this.toPerspective();
138
139 } else {
140
141 this.toPerspective();
142 this.toOrthographic();
143
144 }
145
146};
147
148/*
149* Uses Focal Length (in mm) to estimate and set FOV
150* 35mm (fullframe) camera is used if frame size is not specified;
151* Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
152*/
153THREE.CombinedCamera.prototype.setLens = function ( focalLength, frameHeight ) {
154
155 if ( frameHeight === undefined ) frameHeight = 24;
156
157 var fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) );
158
159 this.setFov( fov );
160
161 return fov;
162};
163
164
165THREE.CombinedCamera.prototype.setZoom = function( zoom ) {
166
167 this.zoom = zoom;
168
169 if ( this.inPerspectiveMode ) {
170
171 this.toPerspective();
172
173 } else {
174
175 this.toOrthographic();
176
177 }
178
179};
180
181THREE.CombinedCamera.prototype.toFrontView = function() {
182
183 this.rotation.x = 0;
184 this.rotation.y = 0;
185 this.rotation.z = 0;
186
187 // should we be modifing the matrix instead?
188
189 this.rotationAutoUpdate = false;
190
191};
192
193THREE.CombinedCamera.prototype.toBackView = function() {
194
195 this.rotation.x = 0;
196 this.rotation.y = Math.PI;
197 this.rotation.z = 0;
198 this.rotationAutoUpdate = false;
199
200};
201
202THREE.CombinedCamera.prototype.toLeftView = function() {
203
204 this.rotation.x = 0;
205 this.rotation.y = - Math.PI / 2;
206 this.rotation.z = 0;
207 this.rotationAutoUpdate = false;
208
209};
210
211THREE.CombinedCamera.prototype.toRightView = function() {
212
213 this.rotation.x = 0;
214 this.rotation.y = Math.PI / 2;
215 this.rotation.z = 0;
216 this.rotationAutoUpdate = false;
217
218};
219
220THREE.CombinedCamera.prototype.toTopView = function() {
221
222 this.rotation.x = - Math.PI / 2;
223 this.rotation.y = 0;
224 this.rotation.z = 0;
225 this.rotationAutoUpdate = false;
226
227};
228
229THREE.CombinedCamera.prototype.toBottomView = function() {
230
231 this.rotation.x = Math.PI / 2;
232 this.rotation.y = 0;
233 this.rotation.z = 0;
234 this.rotationAutoUpdate = false;
235
236};
Note: See TracBrowser for help on using the repository browser.