/
main.js
212 lines (166 loc) · 4.69 KB
/
main.js
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
/**
* SKETCH: "Where are we going" + Contact info
* Three.js + Threex + Tween.js - Thanks Mr.doob and J. Etienne!
*
* Still deeply in WIP!
*/
var aboutBox = document.getElementById('about');
// Show error msg and about if WebGL is missing
if(!Detector.webgl) {
Detector.addGetWebGLMessage();
window.onload = function() {
aboutBox.className = 'active';
};
}
// Variables setup
var w = window.innerWidth
, h = oldH = window.innerHeight
, scene = new THREE.Scene()
, clock = new THREE.Clock()
, camera = new THREE.PerspectiveCamera(90, w / h, 1, 10000)
, renderer = new THREE.WebGLRenderer({antialias: true});
var tilt = 0.41,
radius = 45,
stopped = false;
// Fix FOV on window resize
var tanFOV = Math.tan( ( ( Math.PI / 180 ) * camera.fov / 2 ) );
scene.add(camera);
// Attach event handler
THREE.Object3D._threexDomEvent.camera(camera);
camera.position.z = 100;
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);
var planetTexture = THREE.ImageUtils.loadTexture('img/texture/earth_night_2048.jpg')
, cloudsTexture = THREE.ImageUtils.loadTexture('img/texture/earth_clouds_1024.png');
// Planet
var planet = new THREE.Mesh(
new THREE.SphereGeometry(radius, 100, 50),
new THREE.MeshLambertMaterial({color: 0xFFFFFF, map: planetTexture})
);
planet.rotation.z = tilt;
scene.add(planet);
// Hover animations
planet.on('mouseover', function() {
if(window.zoomOutTween) window.zoomOutTween.stop();
stopped = true;
clock.stop();
// TODO: Tween with THREE.Vector3 ?
var tween = window.zoomInTween = new TWEEN.Tween({
px: planet.rotation.x,
py: planet.rotation.y,
pz: planet.rotation.z,
cx: clouds.rotation.x,
cy: clouds.rotation.y,
cz: clouds.rotation.z,
co: cloudMaterial.opacity
}).to({
px: 1.035,
py: (planet.rotation.y - (planet.rotation.y % 6.28)) + 4.28,
pz: 0,
cx: 1.035,
cy: (clouds.rotation.y - (clouds.rotation.y % 9.42)) + 4.28,
cz: 0,
co: .3
}, 2500)
.easing(TWEEN.Easing.Cubic.Out)
.onUpdate(function() {
planet.rotation.x = this.px;
planet.rotation.y = this.py;
planet.rotation.z = this.pz;
clouds.rotation.x = this.cx;
clouds.rotation.y = this.cy;
clouds.rotation.z = this.cz;
cloudMaterial.opacity = this.co;
}).start();
aboutBox.className = 'active';
});
planet.on('mouseout', function() {
zoomInTween.stop();
stopped = false;
clock.start();
var tween = window.zoomOutTween = new TWEEN.Tween({
px: planet.rotation.x,
pz: planet.rotation.z,
cx: clouds.rotation.x,
cz: clouds.rotation.z,
co: cloudMaterial.opacity
}).to({
px: 0.7,
pz: .21,
cx: 0.7,
cz: .21,
co: .8
}, 1000)
.easing(TWEEN.Easing.Cubic.Out)
.onUpdate(function() {
planet.rotation.x = this.px;
planet.rotation.z = this.pz;
clouds.rotation.x = this.cx;
clouds.rotation.z = this.cz;
cloudMaterial.opacity = this.co;
}).onComplete(function() {
aboutBox.className = '';
}).start();
});
// Lighting
var pointLight = new THREE.PointLight(0xFFFFFF);
pointLight.position.x = 10;
pointLight.position.y = 50;
pointLight.position.z = 130;
scene.add(pointLight);
// Clouds
var cloudMaterial = new THREE.MeshLambertMaterial({
color: 0xffffff,
map: cloudsTexture,
transparent: true
});
var clouds = new THREE.Mesh(
new THREE.SphereGeometry(radius, 100, 50),
cloudMaterial
);
clouds.rotation.z = tilt;
clouds.scale.set(1.005, 1.005, 1.005);
scene.add(clouds);
// Animate the scene
function animate() {
requestAnimationFrame(animate);
if(!stopped) {
var delta = clock.getDelta();
planet.rotation.y += .1 * delta;
clouds.rotation.y += .15 * delta;
}
renderer.render(scene, camera);
TWEEN.update();
}
animate();
// Reflow on resize
window.addEventListener('resize', function() {
w = window.innerWidth;
h = window.innerHeight;
camera.fov = ( 360 / Math.PI ) * Math.atan( tanFOV * ( h / oldH ) );
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
}, false);
// Stop the clock on page hide and viceversa
var hidden, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
}
document.addEventListener(visibilityChange, function() {
if(document[hidden]) {
clock.stop();
} else {
clock.start();
}
}, false);