-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
651 lines (596 loc) · 32.1 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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
body {
margin: 0;
color: white;
}
#three {
position: relative;
z-index: 0;
}
#background {
position: relative;
z-index: 0;
}
#three {
position: absolute;
cursor: none;
}
#overlay {
width: 100%;
position: absolute;
}
</style>
</head>
<body>
<div id="background">
<div id="three"></div>
<div id="overlay">
<div class = "container-xl">
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">
Fleet Command
</a>
<div class="btn-group px-2">
<button type="button" class="btn btn-light dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-map-fill" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M16 .5a.5.5 0 0 0-.598-.49L10.5.99 5.598.01a.5.5 0 0 0-.196 0l-5 1A.5.5 0 0 0 0 1.5v14a.5.5 0 0 0 .598.49l4.902-.98 4.902.98a.502.502 0 0 0 .196 0l5-1A.5.5 0 0 0 16 14.5V.5zM5 14.09V1.11l.5-.1.5.1v12.98l-.402-.08a.498.498 0 0 0-.196 0L5 14.09zm5 .8V1.91l.402.08a.5.5 0 0 0 .196 0L11 1.91v12.98l-.5.1-.5-.1z"/>
</svg>
Map
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#" id = "displayElevationColor">Surface Elevation</a>
<a class="dropdown-item" href="#" id = "displayClimate">Climate Type</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" id = "displayPolitical">Political Boundaries</a>
<a class="dropdown-item" href="#" id = "displayContinent">Continent</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#" id = "displayPopulation">Population Density</a>
</div>
</div>
<div class="btn-group ">
<button type="button" class="btn btn-light dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-expanded="false">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-rulers" viewBox="0 0 16 16">
<path d="M1 0a1 1 0 0 0-1 1v14a1 1 0 0 0 1 1h5v-1H2v-1h4v-1H4v-1h2v-1H2v-1h4V9H4V8h2V7H2V6h4V2h1v4h1V4h1v2h1V2h1v4h1V4h1v2h1V2h1v4h1V1a1 1 0 0 0-1-1H1z"/>
</svg>
Elevation
<span class="sr-only">Toggle Dropdown</span>
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#" id = "displayElevationOn">On</a>
<a class="dropdown-item" href="#" id = "displayElevationOff">Off</a>
</div>
</div>
</nav>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
<script>
const blackMaterial = new THREE.MeshStandardMaterial( {
color: 0x020202,
opacity: 1
});
const whiteMaterial = new THREE.MeshStandardMaterial( {
color: 0xffffff,
opacity: .1,
depthWrite: false
} );
//For calculating xyz from lat and long:
function LatLonXYZ(coords,radius){
let lon = coords[0];
let lat = coords[1];
let phi = (90-lat)*(Math.PI/180);
let theta = (lon+180)*(Math.PI/180);
let x = -((radius) * Math.sin(phi)*Math.cos(theta));
let z = ((radius) * Math.sin(phi)*Math.sin(theta));
let y = ((radius) * Math.cos(phi));
return [x,y,z];
}
//Fetch the world point data from github:
fetch("https://raw.githubusercontent.com/LiamOsler/minijam/master/data/points.geojson")
.then(response => {
return response.json();
})
.then(pointData =>{
fetch("https://raw.githubusercontent.com/LiamOsler/minijam/master/data/polys.geojson")
.then(response => {
return response.json();
})
.then(polyData =>{
//console.log(polyData);
//Basic scene and render setup, resize handler:
//Append render to document:
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.getElementById("three").appendChild( renderer.domElement );
let pointDataRaw = pointData;
console.log(pointDataRaw);
const scene = new THREE.Scene();
scene.background = new THREE.Color( 0x111111 );
scene.fog = new THREE.Fog(0x111111, 200, 500);
const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.enablePan = false;
controls.maxDistance = 400;
controls.minDistance = 120;
camera.position.set( 0, 0, 150 );
controls.update();
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
//Create a closed wavey loop
const curve = new THREE.CatmullRomCurve3( [
new THREE.Vector3( -150, 0, 100 ),
new THREE.Vector3( -150, 5, 150 ),
new THREE.Vector3( 0, 0, 0 ),
new THREE.Vector3( 5, -5, 5 ),
new THREE.Vector3( 10, 0, 10 )
] );
const points = curve.getPoints( 50 );
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const material = new THREE.LineBasicMaterial( { color: 0xff0000 } );
// Create the final object to add to the scene
const curveObject = new THREE.Line( geometry, material );
scene.add(curveObject);
const borderMaterial = new THREE.LineBasicMaterial({
color: 0xffffff,
linewidth: 1,
linecap: 'round', //ignored by WebGLRenderer
linejoin: 'round' //ignored by WebGLRenderer
});
function displayBorders(){
for(let polygon of polyData.features){
//console.log(polygon);
if(polygon.geometry.type == "MultiPolygon"){
for(let feature of polygon.geometry.coordinates){
//console.log(subPoly)
for(let subPoly of feature){
const linePoints = [];
for(let point of subPoly){
const pointCoords = LatLonXYZ(point, 100);
const pointVec = new THREE.Vector3(pointCoords[0], pointCoords[1], pointCoords[2]);
linePoints.push(pointVec);
}
const lineGeometry = new THREE.BufferGeometry().setFromPoints( linePoints );
const line = new THREE.Line( lineGeometry, borderMaterial );
scene.add(line);
}
}
}
else{
for(let feature of polygon.geometry.coordinates){
const linePoints = [];
for(let point of feature){
const pointCoords = LatLonXYZ(point, 100);
const pointVec = new THREE.Vector3(pointCoords[0], pointCoords[1], pointCoords[2]);
linePoints.push(pointVec);
}
const lineGeometry = new THREE.BufferGeometry().setFromPoints( linePoints );
const line = new THREE.Line( lineGeometry, borderMaterial );
scene.add(line);
}
}
}
}
let pointArr = [];
const color = new THREE.Color();
const pointMaterial = new THREE.PointsMaterial( { size: 1, vertexColors: true } );
const pointGeometry = new THREE.BufferGeometry();
function setPointLocations(type){
pointArr = [];
if(type == "flat"){
for (let point of pointData.features){
const coordLatLon = point.geometry.coordinates;
const coordXZY = LatLonXYZ(coordLatLon, 100 + 1);
pointArr.push(coordXZY[0], coordXZY[1],coordXZY[2]);
}
pointGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( pointArr, 3 ) );
}
if(type == "elevation"){
for(let point of pointData.features){
const coordElevation = point.properties.elevation;
const coordLatLon = point.geometry.coordinates;
const coordXZY = LatLonXYZ(coordLatLon, 100 + coordElevation/1000);
pointArr.push(coordXZY[0], coordXZY[1],coordXZY[2]);
}
pointGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( pointArr, 3 ) );
}
}
//console.log(pointData.features);
function changePointColors(type){
const colors = [];
for (let point of pointData.features){
if(type == "political"){
switch(point.properties.mapcolor7){
case 1:
color.setRGB(256/256,0/256,256/256);
colors.push(color.r, color.g, color.b);
break;
case 2:
color.setRGB(0/256, 256/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case 3:
color.setRGB(256/256, 256/256, 0/256);
colors.push(color.r, color.g, color.b);
break;
case 4:
color.setRGB(0/256, 256/256, 0/256);
colors.push(color.r, color.g, color.b);
break;
case 5:
color.setRGB(64/256, 0/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case 6:
color.setRGB(256/256, 0/256, 0/256);
colors.push(color.r, color.g, color.b);
break;
case 7:
color.setRGB(256/256, 128/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
default:
color.setRGB(0/256, 0/256, 25/256);
colors.push(color.r, color.g, color.b);
}
}
else if(type == "continent"){
switch(point.properties.continent){
case "Europe":
color.setRGB(0/256, 256/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "Asia":
color.setRGB(256/256, 0/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "Africa":
color.setRGB(128/256, 128/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "North America":
color.setRGB(128/256, 128/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "South America":
color.setRGB(256/256, 0/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "Oceania":
color.setRGB(0/256, 256/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case "Antarctica":
color.setRGB(256/256, 256/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
default:
color.setRGB(0,0,0.1);
colors.push(color.r, color.g, color.b);
break;
}
}
else if(type == "climate"){
switch(point.properties.climate){
case 1:
color.setRGB(106/256, 119/256, 104/256);
colors.push(color.r, color.g, color.b);
break;
case 2:
color.setRGB(200/256, 200/256, 200/256);
colors.push(color.r, color.g, color.b);
break;
case 3:
color.setRGB(58/256, 150/256, 75/256);
colors.push(color.r, color.g, color.b);
break;
case 4:
color.setRGB(124/256, 127/256, 104/256);
colors.push(color.r, color.g, color.b);
break;
case 5:
color.setRGB(191/256, 169/256, 144/256);
colors.push(color.r, color.g, color.b);
break;
case 6:
color.setRGB(80/256, 106/256, 73/256);
colors.push(color.r, color.g, color.b);
break;
case 7:
color.setRGB(255/256, 255/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case 8:
color.setRGB(46/256, 76/256, 45/256);
colors.push(color.r, color.g, color.b);
break;
case 9:
color.setRGB(46/256, 120/256, 45/256);
colors.push(color.r, color.g, color.b);
break;
case 10:
color.setRGB(153/256, 145/256, 133/256);
colors.push(color.r, color.g, color.b);
break;
case 11:
color.setRGB(185/256, 174/256, 146/256);
colors.push(color.r, color.g, color.b);
break;
case 12:
color.setRGB(85/256, 111/256, 81/256);
colors.push(color.r, color.g, color.b);
break;
case 13:
color.setRGB(45/256, 90/256, 64/256);
colors.push(color.r, color.g, color.b);
break;
case 14:
color.setRGB(58/256, 153/256, 78/256);
colors.push(color.r, color.g, color.b);
break;
case 15:
color.setRGB(67/256, 76/256, 58/256);
colors.push(color.r, color.g, color.b);
break;
case 16:
color.setRGB(87/256, 107/256, 83/256);
colors.push(color.r, color.g, color.b);
break;
case 17:
color.setRGB(230/256, 218/256, 207/256);
colors.push(color.r, color.g, color.b);
break;
case 18:
color.setRGB(63/256, 166/256, 85/256);
colors.push(color.r, color.g, color.b);
break;
case 19:
color.setRGB(55/256, 85/256, 54/256);
colors.push(color.r, color.g, color.b);
break;
case 20:
color.setRGB(25/256, 60/256, 27/256);
colors.push(color.r, color.g, color.b);
break;
case 21:
color.setRGB(58/256, 82/256, 57/256);
colors.push(color.r, color.g, color.b);
break;
case 22:
color.setRGB(36/256, 65/256, 60/256);
colors.push(color.r, color.g, color.b);
break;
case 23:
color.setRGB(36/256, 67/256, 58/256);
colors.push(color.r, color.g, color.b);
break;
case 24:
color.setRGB(256/256, 256/256, 256/256);
colors.push(color.r, color.g, color.b);
break;
case 25:
color.setRGB(92/256, 108/256, 85/256);
colors.push(color.r, color.g, color.b);
break;
case 27:
color.setRGB(125/256, 141/256, 109/256);
colors.push(color.r, color.g, color.b);
break;
case 28:
color.setRGB(140/256, 140/256, 140/256);
colors.push(color.r, color.g, color.b);
break;
case 29:
color.setRGB(140/256, 140/256, 140/256);
colors.push(color.r, color.g, color.b);
break;
case 30:
color.setRGB(256/256, 0/256, 0/256);
colors.push(color.r, color.g, color.b);
break;
default:
color.setRGB(0,0,.1);
colors.push(color.r, color.g, color.b);
}
}
else if(type == "elevation"){
let elevation = point.properties.elevation /2000;
if(elevation > 0){
color.setRGB(elevation+0.01, Math.sqrt(elevation)+0.3, Math.sqrt(elevation/2)+0.01);
}
else{
color.setRGB(0, 1-Math.abs(elevation)/5, 1-Math.abs(elevation/10));
}
colors.push(color.r, color.g, color.b);
}
else if(type == "population"){
let population_density = point.properties.population_density;
if(population_density < 255){
color.setRGB(population_density/256, population_density/256, population_density/256);
}
else{
color.setRGB(0,0,0.1);
}
colors.push(color.r, color.g, color.b);
}
}
pointGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
}
let pointObjs;
function displayPoints(){
pointObjs = new THREE.Points(pointGeometry, pointMaterial);
scene.add(pointObjs);
}
function removePoints(){
scene.remove(pointObjs);
}
const sphereGeometryFlat = new THREE.SphereGeometry( 100, 24, 24 );
const backgroundSphereFlat = new THREE.Mesh( sphereGeometryFlat, blackMaterial );
scene.add( backgroundSphereFlat );
const sphereGeometryElevation = new THREE.SphereGeometry( 94, 24, 24 );
const backgroundSphereElevation = new THREE.Mesh( sphereGeometryElevation, blackMaterial );
scene.add( backgroundSphereElevation );
const cursorGeometry = new THREE.SphereGeometry( 1 , 24, 24 );
const cursorSphere = new THREE.Mesh( cursorGeometry, whiteMaterial );
scene.add( cursorSphere );
const light = new THREE.AmbientLight( 0xffffff ); // soft white light
scene.add( light );
function raycast(){
raycaster.setFromCamera( pointer, camera );
const intersections = raycaster.intersectObjects( scene.children );
for(let intersect of intersections){
// console.log(controls.getDistance());
if(intersect.distance <controls.getDistance()-50){
if(intersect.object.type == "Points"){
let index = intersect.index;
// console.log(intersect);
cursorSphere.position.x = pointArr[index*3];
cursorSphere.position.y = pointArr[index*3+1];
cursorSphere.position.z = pointArr[index*3+2];
}
}
}
renderer.render( scene, camera );
}
let requestedColor = "population";
let requestedElevation = "flat";
function redisplayElevationColor(){
requestedColor = "elevation";
}
function redisplayPoliticalColor(){
requestedColor = "political";
}
function redisplayClimateColor(){
requestedColor = "climate";
}
function redisplayContinentColor(){
requestedColor = "continent";
}
function redisplayPopulationColor(){
requestedColor = "population";
}
function redisplayElevationOn(){
requestedElevation = "elevation";
scene.remove( backgroundSphereElevation );
scene.remove( backgroundSphereFlat );
scene.add( backgroundSphereElevation );
}
function redisplayElevationOff(){
requestedElevation = "flat";
scene.remove( backgroundSphereElevation );
scene.remove( backgroundSphereFlat );
scene.add( backgroundSphereFlat );
}
const displayElevationColor = document.getElementById("displayElevationColor");
displayElevationColor.addEventListener("click", redisplayElevationColor);
const displayPoliticalColor = document.getElementById("displayPolitical");
displayPoliticalColor.addEventListener("click", redisplayPoliticalColor);
const displayClimateColor = document.getElementById("displayClimate");
displayClimateColor.addEventListener("click", redisplayClimateColor);
const displayContinentColor = document.getElementById("displayContinent");
displayContinentColor.addEventListener("click", redisplayContinentColor);
const displayPopulationColor = document.getElementById("displayPopulation");
displayPopulationColor.addEventListener("click", redisplayPopulationColor);
const displayElevationOn = document.getElementById("displayElevationOn");
displayElevationOn.addEventListener("click", redisplayElevationOn);
const displayElevationOff = document.getElementById("displayElevationOff");
displayElevationOff.addEventListener("click", redisplayElevationOff);
function redisplayPoints(){
removePoints();
setPointLocations(requestedElevation);
changePointColors(requestedColor);
displayPoints();
}
function animate() {
requestAnimationFrame( animate );
controls.update();
raycast();
redisplayPoints();
renderer.render( scene, camera );
}
let keys = {
"left" :false,
"right" : false,
"up" : false,
"down" : false,
"space" : false,
"w" : false,
"a" : false,
"s" : false,
"d" : false,
}
function keyDownHandler(event) {
if(event.keyCode == 39) {
keys.left = true;
}
else if(event.keyCode == 37) {
keys.right = true;
}
if(event.keyCode == 40) {
keys.down = true;
}
else if(event.keyCode == 38) {
keys.up = true;
}
if(event.keyCode == 32) {
keys.space = true;
}
}
function keyUpHandler(event) {
if(event.keyCode == 39) {
keys.left = false;
}
else if(event.keyCode == 37) {
keys.right = false;
}
if(event.keyCode == 40) {
keys.down = false;
}
else if(event.keyCode == 38) {
keys.up = false;
}
if(event.keyCode == 32) {
keys.space = false;
}
}
function init(){
window.addEventListener('resize', onWindowResize, false);
window.addEventListener( 'pointermove', onPointerMove );
window.addEventListener('keydown', keyDownHandler, false);
window.addEventListener('keyup', keyUpHandler, false);
removePoints();
setPointLocations("elevation");
changePointColors("elevation");
displayBorders();
displayPoints();
}
init();
animate();
//Window resize event handler:
function onWindowResize(event) {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
function onPointerMove( event ) {
pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
});
});
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>