|
1 | 1 | <!doctype html>
|
2 | 2 | <html lang="en">
|
3 | 3 | <head>
|
4 |
| - <title>three.js webgl - camera</title> |
| 4 | + <title>three.js webgl - cameras</title> |
5 | 5 | <meta charset="utf-8">
|
6 | 6 | <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
|
7 | 7 | <style>
|
|
28 | 28 | color: #0080ff;
|
29 | 29 | }
|
30 | 30 |
|
| 31 | + b { color: lightgreen } |
| 32 | + |
31 | 33 | </style>
|
32 | 34 | </head>
|
33 | 35 | <body>
|
34 | 36 |
|
35 | 37 | <div id="container"></div>
|
36 |
| - <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - camera</div> |
| 38 | + <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - cameras<br/> |
| 39 | + <b>O</b> orthographic <b>P</b> perspective |
| 40 | + </div> |
37 | 41 |
|
38 | 42 | <script src="../build/Three.js"></script>
|
39 | 43 | <script src="js/Stats.js"></script>
|
|
45 | 49 |
|
46 | 50 | var container, stats;
|
47 | 51 | var camera, scene, renderer, mesh;
|
| 52 | + var cameraRig, activeCamera, cameraPerspective, cameraOrtho; |
48 | 53 |
|
49 | 54 | var r = 0;
|
50 | 55 |
|
|
62 | 67 | camera.position.z = 2500;
|
63 | 68 | scene.add( camera );
|
64 | 69 |
|
65 |
| - camera2 = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 150, 1000 ); |
66 |
| - scene.add( camera2 ); |
| 70 | + cameraPerspective = new THREE.PerspectiveCamera( 50, 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT, 150, 1000 ); |
| 71 | + |
| 72 | + var visibleCameraPerspective = new THREE.VisibleCamera( cameraPerspective ); |
| 73 | + cameraPerspective.add( visibleCameraPerspective ); |
| 74 | + |
| 75 | + cameraOrtho = new THREE.OrthographicCamera( 0.5 * SCREEN_WIDTH / - 2, 0.5 * SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, 150, 1000 ); |
| 76 | + |
| 77 | + var visibleCameraOrtho = new THREE.VisibleCamera( cameraOrtho ); |
| 78 | + cameraOrtho.add( visibleCameraOrtho ); |
| 79 | + |
| 80 | + activeCamera = cameraPerspective; |
| 81 | + |
| 82 | + |
| 83 | + // counteract different front orientation of cameras vs rig |
67 | 84 |
|
68 |
| - var visibleCamera = new THREE.VisibleCamera( camera2 ); |
69 |
| - camera2.add( visibleCamera ); |
| 85 | + cameraOrtho.rotation.y = Math.PI; |
| 86 | + cameraPerspective.rotation.y = Math.PI; |
| 87 | + |
| 88 | + cameraRig = new THREE.Object3D(); |
| 89 | + |
| 90 | + cameraRig.add( cameraPerspective ); |
| 91 | + cameraRig.add( cameraOrtho ); |
| 92 | + |
| 93 | + scene.add( cameraRig ); |
70 | 94 |
|
71 | 95 | //
|
72 | 96 |
|
|
78 | 102 | mesh.add( mesh2 );
|
79 | 103 |
|
80 | 104 | var mesh3 = new THREE.Mesh( new THREE.SphereGeometry( 5, 16, 8 ), new THREE.MeshBasicMaterial( { color: 0x0000ff, wireframe: true } ) );
|
81 |
| - mesh3.position.z = -150; |
82 |
| - camera2.add( mesh3 ); |
| 105 | + mesh3.position.z = 150; |
| 106 | + cameraRig.add( mesh3 ); |
83 | 107 |
|
84 | 108 | //
|
85 | 109 |
|
|
125 | 149 | //
|
126 | 150 |
|
127 | 151 | window.addEventListener( 'resize', onWindowResize, false );
|
| 152 | + document.addEventListener( 'keydown', onKeyDown, false ); |
128 | 153 |
|
129 | 154 | }
|
130 | 155 |
|
| 156 | + // |
| 157 | + |
| 158 | + function onKeyDown ( event ) { |
| 159 | + |
| 160 | + switch( event.keyCode ) { |
| 161 | + |
| 162 | + case 79: /*O*/ activeCamera = cameraOrtho; break; |
| 163 | + case 80: /*P*/ activeCamera = cameraPerspective; break; |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + }; |
| 168 | + |
| 169 | + // |
| 170 | + |
131 | 171 | function onWindowResize( event ) {
|
132 | 172 |
|
133 | 173 | SCREEN_WIDTH = window.innerWidth;
|
|
138 | 178 | camera.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT;
|
139 | 179 | camera.updateProjectionMatrix();
|
140 | 180 |
|
141 |
| - camera2.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT; |
142 |
| - camera2.updateProjectionMatrix(); |
| 181 | + cameraPerspective.aspect = 0.5 * SCREEN_WIDTH / SCREEN_HEIGHT; |
| 182 | + cameraPerspective.updateProjectionMatrix(); |
| 183 | + |
| 184 | + cameraOrtho.left = - 0.5 * SCREEN_WIDTH / 2; |
| 185 | + cameraOrtho.right = 0.5 * SCREEN_WIDTH / 2; |
| 186 | + cameraOrtho.top = SCREEN_HEIGHT / 2; |
| 187 | + cameraOrtho.bottom = - SCREEN_HEIGHT / 2; |
| 188 | + cameraOrtho.updateProjectionMatrix(); |
143 | 189 |
|
144 | 190 | }
|
145 | 191 |
|
|
164 | 210 | mesh.children[ 0 ].position.x = 70 * Math.cos( 2 * r );
|
165 | 211 | mesh.children[ 0 ].position.z = 70 * Math.sin( r );
|
166 | 212 |
|
167 |
| - camera2.fov = 35 + 30 * Math.sin( 0.5 * r ); |
168 |
| - camera2.far = mesh.position.length(); |
169 |
| - camera2.updateProjectionMatrix(); |
170 |
| - camera2.children[ 0 ].update( camera2 ); |
| 213 | + if ( activeCamera === cameraPerspective ) { |
| 214 | + |
| 215 | + cameraPerspective.fov = 35 + 30 * Math.sin( 0.5 * r ); |
| 216 | + cameraPerspective.far = mesh.position.length(); |
| 217 | + cameraPerspective.updateProjectionMatrix(); |
| 218 | + cameraPerspective.children[ 0 ].update( cameraPerspective ); |
| 219 | + |
| 220 | + cameraPerspective.children[ 0 ].lines.visible = true; |
| 221 | + cameraOrtho.children[ 0 ].lines.visible = false; |
| 222 | + |
| 223 | + } else { |
| 224 | + |
| 225 | + cameraOrtho.far = mesh.position.length(); |
| 226 | + cameraOrtho.updateProjectionMatrix(); |
| 227 | + cameraOrtho.children[ 0 ].update( cameraOrtho ); |
| 228 | + |
| 229 | + cameraPerspective.children[ 0 ].lines.visible = false; |
| 230 | + cameraOrtho.children[ 0 ].lines.visible = true; |
| 231 | + |
| 232 | + } |
171 | 233 |
|
172 |
| - camera2.lookAt( mesh.position ); |
| 234 | + cameraRig.lookAt( mesh.position ); |
173 | 235 |
|
174 | 236 | renderer.clear();
|
175 | 237 |
|
176 | 238 | renderer.setViewport( 0, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
|
177 |
| - renderer.render( scene, camera2 ); |
| 239 | + renderer.render( scene, activeCamera ); |
178 | 240 |
|
179 | 241 | renderer.setViewport( SCREEN_WIDTH/2, 0, SCREEN_WIDTH/2, SCREEN_HEIGHT );
|
180 | 242 | renderer.render( scene, camera );
|
|
0 commit comments