-
Notifications
You must be signed in to change notification settings - Fork 27
/
globe.js
549 lines (446 loc) · 13.6 KB
/
globe.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Realtime WebGL globe
// Copyright (c) 2015 Mike van Rossum
/**
* Realtime Globe is a WebGL based earth globe that
* makes it super simple to add shapes in realtime
* on specific lat/lon positions on earth.
*
* @class Globe
* @param {HTMLElement} container
* @param {Object} urls - URLs of earth images
* @param {String} urls.earth
* @param {String|undefined} urls.bump (optional)
* @param {String|undefined} urls.specular (optional)
*/
var Globe = function Globe(container, urls) {
var PI = Math.PI;
var PI_HALF = PI / 2;
// Three.js objects
var camera;
var scene;
var light;
var renderer;
var earthGeometry;
var earthPosition;
// camera's distance from center (and thus the globe)
var distanceTarget = 900;
var distance = distanceTarget;
// camera's position
var rotation = { x: 2, y: 1 };
var target = { x: 2, y: 1 };
// holds currently levitating blocks
var levitatingBlocks = [];
// holds all block references
var blocks = [];
// What gets exposed by calling:
//
// var globe = [new] Globe(div, urls);
//
// attach public functions to this object
var api = {};
/**
* Initializes the globe
*
*/
api.init = function() {
setSize();
// Camera
camera = new THREE.PerspectiveCamera(30, w / h, 1, 1000);
camera.position.z = distance;
// Scene
scene = new THREE.Scene();
// Earth geom, used for earth & atmosphere
earthGeometry = new THREE.SphereGeometry(200, 64, 64);
// Light, reposition close to camera
light = createMesh.directionalLight();
// we use this to correctly position camera and blocks
var earth = createMesh.earth(urls);
earthPosition = earth.position;
// Add meshes to scene
scene.add(earth);
scene.add(createMesh.atmosphere());
// Add lights to scene
scene.add(new THREE.AmbientLight(0x656565));
scene.add(light);
// Renderer
renderer = new THREE.WebGLRenderer({antialias: true});
renderer.setSize(w, h);
// Add scene to DOM
renderer.domElement.style.position = 'absolute';
container.appendChild(renderer.domElement);
// DOM event handlers
container.addEventListener('mousedown', handle.drag.start, false);
window.addEventListener('resize', handle.resize, false);
// Scroll for Chrome
window.addEventListener('mousewheel', handle.scroll, false);
// Scroll for Firefox
window.addEventListener('DOMMouseScroll', handle.scroll, false);
// Bootstrap render
animate();
return this;
}
var setSize = function() {
w = container.offsetWidth || window.innerWidth;
h = container.offsetHeight || window.innerHeight;
}
var createMesh = {
// @param urls Object URLs of images
//
// {
// earth: String URL
// bump: Sting URL [optional]
// specular: String URL [optional]
// }
//
// See
// @link http://learningthreejs.com/blog/2013/09/16/how-to-make-the-earth-in-webgl/
// @link http://learningthreejs.com/data/2013-09-16-how-to-make-the-earth-in-webgl/demo/index.html
earth: function(urls) {
if(!urls.earth)
throw 'No image URL provided for an earth image';
var material = new THREE.MeshPhongMaterial();
material.map = THREE.ImageUtils.loadTexture(urls.earth);
if(urls.bump) {
material.bump = THREE.ImageUtils.loadTexture(urls.bump);
material.bumpScale = 0.02;
}
if(urls.specular) {
material.specularMap = THREE.ImageUtils.loadTexture(urls.specular);
material.specular = new THREE.Color('grey');
}
return new THREE.Mesh(earthGeometry, material);
},
// See
// @link https://github.com/dataarts/webgl-globe/blob/master/globe/globe.js#L52
// @link http://bkcore.com/blog/3d/webgl-three-js-animated-selective-glow.html
//
// Currently has some issues, especially when zooming out (distance > 900)
atmosphere: function() {
var material = new THREE.ShaderMaterial({
vertexShader: [
'varying vec3 vNormal;',
'void main() {',
'vNormal = normalize( normalMatrix * normal );',
'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );',
'}'
].join('\n'),
fragmentShader: [
'varying vec3 vNormal;',
'void main() {',
'float intensity = pow( 0.8 - dot( vNormal, vec3( 0, 0, 1.0 ) ), 7.0 );',
'gl_FragColor = vec4( 0.7, 1.0, 0.7, 1.0 ) * intensity;',
'}'
].join('\n'),
side: THREE.BackSide,
blending: THREE.AdditiveBlending,
transparent: false
});
var mesh = new THREE.Mesh(earthGeometry, material);
mesh.scale.set(1.1, 1.1, 1.1);
return mesh;
},
directionalLight: function() {
return new THREE.DirectionalLight(0xcccccc, 0.5);
},
block: function(color) {
return new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshLambertMaterial({color: color})
);
}
}
// Keep track of mouse positions
var mouse = { x: 0, y: 0 };
var mouseOnDown = { x: 0, y: 0 };
var targetOnDown = { x: 0, y: 0 };
// DOM event handlers
var handle = {
scroll: function(e) {
e.preventDefault();
// See
// @link http://www.h3xed.com/programming/javascript-mouse-scroll-wheel-events-in-firefox-and-chrome
if(e.wheelDelta) {
// chrome
var delta = e.wheelDelta * 0.5;
} else {
// firefox
var delta = -e.detail * 15;
}
api.zoomRelative(delta);
return false;
},
resize: function(e) {
setSize();
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(w, h);
},
// See
// @link https://github.com/dataarts/webgl-globe/blob/master/globe/globe.js#L273-L334
drag: {
start: function(e) {
e.preventDefault();
container.addEventListener('mousemove', handle.drag.move, false);
container.addEventListener('mouseup', handle.drag.end, false);
container.addEventListener('mouseout', handle.drag.end, false);
mouseOnDown.x = -e.clientX;
mouseOnDown.y = e.clientY;
targetOnDown.x = target.x;
targetOnDown.y = target.y;
container.style.cursor = 'move';
},
move: function(e) {
mouse.x = -e.clientX;
mouse.y = e.clientY;
var zoomDamp = distance / 1000;
target.x = targetOnDown.x + (mouse.x - mouseOnDown.x) * 0.005 * zoomDamp;
target.y = targetOnDown.y + (mouse.y - mouseOnDown.y) * 0.005 * zoomDamp;
target.y = target.y > PI_HALF ? PI_HALF : target.y;
target.y = target.y < - PI_HALF ? - PI_HALF : target.y;
},
end: function(e) {
container.removeEventListener('mousemove', handle.drag.move, false);
container.removeEventListener('mouseup', handle.drag.end, false);
container.removeEventListener('mouseout', handle.drag.end, false);
container.style.cursor = 'auto';
}
}
}
var checkAltituteBoundries = function() {
// max zoom
if(distanceTarget < 300)
distanceTarget = 300;
// min zoom
else if(distanceTarget > 900)
distanceTarget = 900;
}
var animate = function() {
requestAnimationFrame(animate);
render();
}
var render = function() {
levitateBlocks();
// Rotate towards the target
rotation.x += (target.x - rotation.x) * 0.1;
rotation.y += (target.y - rotation.y) * 0.1;
distance += (distanceTarget - distance) * 0.3;
// determine camera position
set3dPosition(camera, {
x: rotation.x,
y: rotation.y,
altitude: distance
});
// Determine light position based
set3dPosition(light, {
x: rotation.x - 150,
y: rotation.y - 150,
altitude: distance
});
camera.lookAt(earthPosition);
renderer.render(scene, camera);
}
// @param Object position (2d lat/lon coordinates)
// @return Object coords (x/y coordinates)
//
// Calculates x, y coordinates based on
// lat/lon coordinates.
var calculate2dPosition = function(coords) {
var phi = (90 + coords.lon) * PI / 180;
var theta = (180 - coords.lat) * PI / 180;
return {
x: phi,
y: PI - theta
}
}
// @param Mesh object
// @param Object coords (x/y coordinates in 2d space + altitute)
//
// Calculates 3d position and sets it on mesh
var set3dPosition = function(mesh, coords) {
if(!coords)
coords = mesh.userData;
var x = coords.x;
var y = coords.y;
var altitude = coords.altitude;
mesh.position.set(
altitude * Math.sin(x) * Math.cos(y),
altitude * Math.sin(y),
altitude * Math.cos(x) * Math.cos(y)
);
}
// Create a block mesh and set its position in 3d
// space just below the earths surface
var createLevitatingBlock = function(properties) {
// create mesh
var block = createMesh.block(properties.color);
// calculate 2d position
var pos2d = calculate2dPosition(properties);
block.userData = {
// set 2d position on earth so we can more
// easily recalculate the 3d position
x: pos2d.x,
y: pos2d.y,
altitude: 200 - properties.size / 1.5,
// speed at which block levitates outside
// earth's core
levitation: .1,
size: properties.size
}
// calculate 3d position
set3dPosition(block);
// rotate towards earth
block.lookAt(earthPosition);
block.scale.z = properties.size;
block.scale.x = properties.size;
block.scale.y = properties.size;
block.updateMatrix();
return block;
}
// Create a block mesh and set its position in 3d
// space just below the earths surface
var createBlock = function(properties) {
// create mesh
var block = createMesh.block(properties.color);
// calculate 2d position
var pos2d = calculate2dPosition(properties);
// add altitute
pos2d.altitude = 200 + properties.size / 2;
// calculate 3d position
set3dPosition(block, pos2d);
// rotate towards earth
block.lookAt(earthPosition);
block.scale.z = properties.size;
block.scale.x = properties.size;
block.scale.y = properties.size;
block.updateMatrix();
return block;
}
// internal function to levitate all levitating
// blocks each tick. Called on render.
var levitateBlocks = function() {
levitatingBlocks.forEach(function(block, i) {
var userData = block.userData;
// if entirely outide of earth, stop levitating
if(userData.altitude > 200 + userData.size / 2) {
levitatingBlocks.splice(i, 1);
return;
}
userData.altitude += userData.levitation;
set3dPosition(block);
block.updateMatrix();
});
}
// Public functions
/**
* Zoom the earth relatively to its current zoom
* (passing a positive number will zoom towards
* the earth, while a negative number will zoom
* away from earth).
*
* @param {Integer} delta
* @return {this}
*/
api.zoomRelative = function(delta) {
distanceTarget -= delta;
checkAltituteBoundries();
return this;
}
/**
* Transition the altitute of the camera to a
* specific distance from the earth's core.
*
* @param {Integer} altitute
* @return {this}
*/
api.zoomTo = function(altitute) {
distanceTarget = altitute;
checkAltituteBoundries();
return this;
}
/**
* Set the altitute of the camera to a specific
* distance from the earth's core.
*
* @param {Integer} altitude
* @return {this}
*/
api.zoomImmediatelyTo = function(altitute) {
distanceTarget = distance = altitute;
checkAltituteBoundries();
return this;
}
/**
* Transition the globe from its current position
* to the new coordinates.
*
* @param {Object} pos - the position
* @param {Float} pos.lat - latitute position
* @param {Float} pos.lon - longtitute position
* @return {this}
*/
api.center = function(pos) {
target = calculate2dPosition(pos);
return this;
}
/**
* Center the globe on the new coordinates.
*
* @param {Object} pos - the position
* @param {Float} pos.lat - latitute position
* @param {Float} pos.lon - longtitute position
* @return {this}
*/
api.centerImmediate = function(pos) {
target = rotation = calculate2dPosition(pos);
return this;
}
/**
* Adds a block to the globe. The globe will spawn
* just below the earth's surface and `levitate`
* out of the surface until it is fully `out` of the
* earth.
*
* @param {Object} data
* @param {Float} data.lat - latitute position
* @param {Float} data.lon - longtitute position
* @param {Float} data.size - size of the block
* @param {String} data.color - color of the block
* @return {this}
*/
api.addLevitatingBlock = function(data) {
var block = createLevitatingBlock(data);
scene.add(block);
levitatingBlocks.push(block);
blocks.push(block);
return this;
}
/**
* Adds a block to the globe.
*
* @param {Object} data
* @param {Float} data.lat - latitute position
* @param {Float} data.lon - longtitute position
* @param {Float} data.size - size of the block
* @param {String} data.color - color of the block
* @return {this}
*/
api.addBlock = function(data) {
var block = createBlock(data);
scene.add(block);
blocks.push(block);
return this;
}
/**
* Remove all blocks from the globe.
*
* @return {this}
*/
api.removeAllBlocks = function() {
blocks.forEach(function(block) {
scene.remove(block);
});
blocks = [];
return this;
}
return api;
}