-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.html
257 lines (198 loc) · 8.13 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<html>
<head>
<title>Basic Camera Movement : three.js</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
#info, #side, #top {
position: absolute;
bottom: 50%;
left: 0%;
padding-top: 10px;
padding-left: 10px;
padding-bottom: 10px;
width: 100%;
text-align: left;
display:block;
color: #ffffff;
font-family: sans-serif;
}
#side, #top {
top: 50%;
font-style: italic;
}
#top {
left: 50%;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<a href="https://github.com/nselikoff/basic-camera-movement-three-js"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/c6286ade715e9bea433b4705870de482a654f78a/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f77686974655f6666666666662e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_white_ffffff.png"></a>
<div id="info">
<strong>Basic Camera Movement in <a href="http://threejs.org" target="_blank">three.js</a></strong><br/>
<small>Switch between the basic camera movements with the dropdown above right.</small>
</div>
<div id="side">
Side View
</div>
<div id="top">
Top View
</div>
<div id="container"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="js/Detector.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var params = {
movement: 'tilt'
};
var gui = new dat.GUI();
gui.add( params, 'movement', {
'Tilt': 'tilt',
'Pan': 'pan',
'Zoom': 'zoom',
'Pedestal': 'pedestal',
'Dolly': 'dolly',
'Truck / Track': 'truck'
} );
gui.open();
var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;
var views = [
{
left: 0,
bottom: 0,
width: 0.5,
height: 0.5,
eye: [ 0, 100, 200 ],
direction: [ 0, 1, 0 ]
},
{
left: 0.5,
bottom: 0,
width: 0.5,
height: 0.5,
eye: [ 0, 0, 200 ],
direction: [ -1, 0, 0 ]
}
];
for ( var ii = 0; ii < views.length; ++ii ) {
var view = views[ii];
orthoCam = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, -500, 10000 );
view.orthoCam = orthoCam;
}
var scene = new THREE.Scene();
scene.fog = new THREE.Fog( 0x333333, 500, 3000 );
var light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 1, 1, 0.5 ).normalize();
scene.add( light );
var startFov = 30.0;
var startPosition = new THREE.Vector3( 0, 20, 500 );
var startRotation = new THREE.Vector3( 0, 0, 0 );
var camera = new THREE.PerspectiveCamera( startFov, window.innerWidth/window.innerHeight, 0.1, 10000 );
var cameraHelper = new THREE.CameraHelper( camera );
scene.add( cameraHelper );
var renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
var container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
var clock = new THREE.Clock();
//
var geometry = new THREE.BoxGeometry( 20, 20, 20 );
for ( var i = 0; i < 500; i ++ ) {
var color = new THREE.Color();
color.setHSL( 0.55, 0.5, Math.random() * 0.5 + 0.5 );
var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: color } ) );
object.scale.x = Math.random() + 0.5;
object.scale.y = (Math.random() + 0.5) * 6.0;
object.scale.z = Math.random() + 0.5;
object.position.x = Math.random() * 400 - 200;
object.position.x += object.position.x > 0 ? 40 : -40;
object.position.y = 10 * object.scale.y;
object.position.z = Math.random() * 3000 - 2600;
scene.add( object );
}
// GROUND
var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
var planeMaterial = new THREE.MeshBasicMaterial( { color: 0x222222 } );
planeMaterial.ambient = planeMaterial.color;
var ground = new THREE.Mesh( geometry, planeMaterial );
ground.position.set( 0, 0, 0 );
ground.rotation.x = - Math.PI / 2;
ground.scale.set( 100, 100, 100 );
ground.castShadow = false;
ground.receiveShadow = true;
scene.add( ground );
//
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize() {
windowWidth = window.innerWidth;
windowHeight = window.innerHeight;
renderer.setSize( windowWidth, windowHeight );
}
//
var render = function () {
var time = clock.getElapsedTime() * 0.5;
requestAnimationFrame( render );
//
camera.position.set( startPosition.x, startPosition.y, startPosition.z );
camera.rotation.set( startRotation.x, startRotation.y, startRotation.z );
camera.fov = startFov;
if ( params.movement == 'tilt' )
camera.rotation.x = startRotation.x + Math.sin( time * 2.0 ) * 0.1 + 0.1;
else if ( params.movement == 'pan' )
camera.rotation.y = startRotation.y + Math.sin( time ) * 0.2;
else if ( params.movement == 'zoom' )
camera.fov = startFov + Math.sin( time ) * 10.0;
else if ( params.movement == 'pedestal' )
camera.position.y = startPosition.y + Math.cos( time * 2.0 ) * 20.0 + 10.0;
else if ( params.movement == 'dolly' )
camera.position.z = startPosition.z + Math.cos( time ) * 100.0;
else if ( params.movement == 'truck' )
camera.position.x = startPosition.x + Math.sin( time ) * 50.0;
var left = Math.floor( windowWidth * 0.0 );
var bottom = Math.floor( windowHeight * 0.5 );
var width = Math.floor( windowWidth * 1.0 );
var height = Math.floor( windowHeight * 0.5 );
renderer.setViewport( left, bottom, width, height );
renderer.setScissor( left, bottom, width, height );
renderer.enableScissorTest ( true );
camera.aspect = width / height;
camera.updateProjectionMatrix();
cameraHelper.update();
cameraHelper.visible = false;
renderer.render(scene, camera);
//
cameraHelper.visible = true;
for ( var ii = 0; ii < views.length; ++ii ) {
view = views[ii];
orthoCam = view.orthoCam;
orthoCam.left = windowWidth / - 2;
orthoCam.right = windowWidth / 2;
orthoCam.top = windowHeight / 2;
orthoCam.bottom = windowHeight / - 2;
orthoCam.position.x = view.eye[ 0 ];
orthoCam.position.y = view.eye[ 1 ];
orthoCam.position.z = view.eye[ 2 ];
orthoCam.rotation.x = view.direction[ 0 ] * Math.PI * 0.5;
orthoCam.rotation.y = view.direction[ 1 ] * Math.PI * 0.5;
orthoCam.rotation.z = view.direction[ 2 ] * Math.PI * 0.5;
// orthoCam.lookAt( scene.position );
orthoCam.updateProjectionMatrix();
var left = Math.floor( windowWidth * view.left );
var bottom = Math.floor( windowHeight * view.bottom );
var width = Math.floor( windowWidth * view.width );
var height = Math.floor( windowHeight * view.height );
renderer.setViewport( left, bottom, width, height );
renderer.setScissor( left, bottom, width, height );
renderer.enableScissorTest ( true );
// renderer.setClearColor( view.background );
renderer.render( scene, orthoCam );
}
};
render();
</script>
</body>
</html>