/
index.html
187 lines (148 loc) · 4.28 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
<!DOCTYPE html>
<html>
<head>
<title>Threejs Camera Dolly</title>
<link rel="stylesheet" type="text/css" href="css/app.css">
</head>
<body>
<header id="header">
<h1>Threejs Camera Dolly</h1>
<p>When dealing with camera animation in threejs it's difficult to control both camera and lookat positions with precision.</p>
<p>This helper was created to solve this problem by using one path for the camera and another for it's lookat.</p>
<p>Path positions can be exported to JSON and loaded back in.</p>
<h3>Controls</h3>
<p>
<ol>
<li>Drag the cameraPosition slider to move the camera</li>
<li>Drag the lookatPositon slider to move the lookat point</li>
</ol>
</p>
<p>This helper was created for <a href="http://void.hi-res.net" target="_blank">VOID</a>, an interactive webgl experience by <a href="http://hi-res.net" target="_blank">Hi-ReS!</a></p>
</header>
<a id="dpr" href="http://ivxvixviii.io"></a>
<script type="text/javascript" src="vendor/dat.gui.js"></script>
<script type="text/javascript" src="vendor/three.min.js"></script>
<script type="text/javascript" src="vendor/TrackballControls.js"></script>
<script type="text/javascript" src="dolly.js"></script>
<script type="text/javascript">
var WIDTH = window.innerWidth;
var HEIGHT = window.innerHeight;
var canvas = document.getElementById('canvas');
var renderer = new THREE.WebGLRenderer();
var camera = new THREE.PerspectiveCamera( 65, WIDTH / HEIGHT, 0.1, 1000 );
var cameraDolly = new THREE.PerspectiveCamera( 65, WIDTH / HEIGHT, 0.1, 1000 );
var scene = new THREE.Scene();
document.body.appendChild(renderer.domElement);
onWindowResize();
// Helpers
scene.add( new THREE.GridHelper(50, 10));
scene.add( new THREE.AxisHelper(10));
scene.add( new THREE.CameraHelper( cameraDolly ) );
// Controls
var controls = new THREE.TrackballControls( camera, renderer.domElement );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.5;
// Listeners
window.addEventListener('resize', onWindowResize, false);
// Start
var zoom = 80;
camera.position.set(1 * zoom, 0.75 * zoom, 1 * zoom);
camera.lookAt(new THREE.Vector3());
// Dolly
var points = {
"camera": [
{
"x": -35,
"y": 0,
"z": 0
},
{
"x": 0,
"y": 10,
"z": 20
},
{
"x": 40,
"y": 0,
"z": 0
}
],
"lookat": [
{
"x": -40,
"y": 10,
"z": -40
},
{
"x": 0,
"y": -10,
"z": -40
},
{
"x": 40,
"y": 10,
"z": -40
}
]
};
// Controller
var controller = {
animate: true
};
// GUI
var gui = new dat.GUI;
gui.width = 350;
gui.open();
var dolly = new THREE.CameraDolly(cameraDolly, scene, points, gui);
gui.add(dolly, 'cameraPosition', 0, 1).onChange(function(){
dolly.update();
});
gui.add(dolly, 'lookatPosition', 0, 1).onChange(function(){
dolly.update();
});
gui.add(dolly, 'exportPositions');
gui.add(controller, 'animate');
// Clock
var clock = new THREE.Clock( true );
function update(){
requestAnimationFrame(update);
render( camera, 0, 0, 1, 1 );
render( cameraDolly, 0.75, 0, 0.25, 0.25 );
var delta = clock.getElapsedTime() * 0.5;
var position = THREE.Math.mapLinear(Math.sin(delta), -1, 1, 0, 1);
if(controller.animate){
dolly.cameraPosition = position;
dolly.lookatPosition = position;
dolly.update();
for (var i in gui.__controllers) {
gui.__controllers[i].updateDisplay();
}
}
controls.update();
}
function render( camera, left, bottom, width, height ){
left *= WIDTH;
bottom *= HEIGHT;
width *= WIDTH;
height *= HEIGHT;
renderer.setViewport( left, bottom, width, height );
renderer.setScissor( left, bottom, width, height );
renderer.enableScissorTest( true );
renderer.render( scene, camera );
}
function onWindowResize(){
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;
camera.aspect = WIDTH / HEIGHT;
camera.updateProjectionMatrix();
renderer.setSize( WIDTH, HEIGHT );
}
update();
</script>
</body>
</html>