-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
362 lines (312 loc) · 14.2 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>simple-viewer</title>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/three@<version>/examples/jsm/"
}
}
</script>
</head>
<body>
<canvas id="canvas" width = '1920' height = '1080' ></canvas>
<script src="three.min.js"></script>
<script src="OrbitControls.js"></script>
<script src="stats.min.js"></script>
<script src="three-geo.min.js"></script>
<script type = "module" src = "GLTFExporter.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
<div id="tooltip" style="display: none; position: absolute; pointer-events: none; padding: 10px; background: rgba(0, 0, 0, 0.5); color: white; border-radius: 5px;"></div>
<script type="module">
(async () => {
// this array will hold all the "dots" , which represent the locations of the earthquakes
const dots = [];
// create a gui
const gui = new dat.GUI();
// just initial values for the parameters
const parameters = {
latitude: 64,
longitude: -21,
radius: 2
};
// flags to prevent the user from refreshing the terrain before all the parameters are set
let isLatitudeChanged = false;
let isLongitudeChanged = false;
let isRadiusChanged = false;
/************************************************************
GUI CODE
The following code is for the GUI. It allows the user to change the latitude, longitude, and radius of the terrain.
*************************************************************/
// thise code sets up some basic geographic presets that had intensive earthquakes
const presets = [
{
name: 'Vanuatu, Coral Sea',
parameters: {
latitude: -19.5,
longitude: 169.5,
radius: 5
}
},
{
name: 'Peru, near Juanjui',
parameters: {
latitude: -7.169,
longitude: -76.728,
radius: 5
}
},
{
name: 'Dominican Republic, near Higuey',
parameters: {
latitude: 18.616,
longitude: -68.707,
radius: 5
}
},
{
name: 'Chile, Near Valdivia',
parameters: {
latitude: -39.8,
longitude: -73.2,
radius: 5
}
},
{
name: 'Alaska, Prince William Sound',
parameters: {
latitude: 61.0,
longitude: -147.5,
radius: 5
}
},
{
name: 'Indonesia, Northern Sumatra',
parameters: {
latitude: 3.3,
longitude: 95.8,
radius: 5
}
},
];
// basic structure of these code blocks is that for each parameter,
// it creates an anonymous function that is called when the user clicks the button
// if it isnt changed yet, then the user can change it.
// added this to prevent user from loading in more terrain before parameters are set
// or adding more JSON data before the terrain is loaded so on so forth,
// essentially it makes it such that everything must be done in order
presets.forEach(preset => {
const folder = gui.addFolder(preset.name);
folder.add({
setPreset: function() {
parameters.latitude = preset.parameters.latitude;
parameters.longitude = preset.parameters.longitude;
parameters.radius = preset.parameters.radius;
refreshTerrain();
alert("Please refresh the page before setting another preset or using other controls. loadData is fine to use however.");
}
}, 'setPreset');
});
gui.add({changeLatitude: function() {
if (!isLatitudeChanged) {
let newValue = parseFloat(prompt("Enter new latitude (-90 to 90):"));
if (newValue >= -90 && newValue <= 90) {
parameters.latitude = newValue;
isLatitudeChanged = true;
} else {
alert("Invalid input. Please enter a number between -90 and 90.");
}
}
}}, 'changeLatitude');
gui.add({changeLongitude: function() {
if (!isLongitudeChanged) {
let newValue = parseFloat(prompt("Enter new longitude (-180 to 180):"));
if (newValue >= -180 && newValue <= 180) {
parameters.longitude = newValue;
isLongitudeChanged = true;
} else {
alert("Invalid input. Please enter a number between -180 and 180.");
}
}
}}, 'changeLongitude');
gui.add({changeRadius: function() {
if (!isRadiusChanged) {
let newValue = parseFloat(prompt("Enter new radius (0 to 5):"));
if (newValue >= 0 && newValue <= 125) {
parameters.radius = newValue;
isRadiusChanged = true;
} else {
alert("Invalid input. Please enter a number between 0 and 5.");
}
}
}}, 'changeRadius');
gui.add({refreshTerrain: function() {
if (isLatitudeChanged && isLongitudeChanged && isRadiusChanged) {
refreshTerrain();
} else {
alert("Please set all parameters before refreshing the terrain.");
}
}}, 'refreshTerrain');
// this anon function loads in the JSON data from the user
gui.add({loadData: function() {
const json = prompt("Enter JSON data:");
const data = JSON.parse(json);
// if theres some data to work with and it has some features
if (data && data.features) {
// go through each feature of the features (the format of the USGS API note: this can always be tweaked to fit whatever JSON format you are using if you don't want to use USGS)
data.features.forEach(feature => {
// grab coords
const coordinates = feature.geometry.coordinates;
// grab lat and long and magnitude
const magnitude = feature.properties.mag;
const longitudeDot = coordinates[0];
const latitudeDot = coordinates[1];
// convert from geographic coords to WebGL coords
const {proj, unitsPerMeter } = tgeo.getProjection([parameters.latitude, parameters.longitude], parameters.radius);
const [x, y] = proj([latitudeDot, longitudeDot]), z = 20;
// create a marker
const geom = new THREE.BufferGeometry();
const vertices = new Float32Array([x, y , z * unitsPerMeter]);
geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
// this code creates a "heatmap" of sorts based on the magnitude of the earthquake
// green < 3 , red <= 4, yellow <= 5, purple > 5
let color;
if (magnitude < 3) {
console.log(magnitude);
color = 0x00cc00;
} else if (magnitude <= 4) {
color = 0xffff00;
} else if (magnitude <= 5) {
color = 0xff0000;
} else {
color = 0x800080;
}
// creates a dot with the magnitude color and the title of the earthquake and adds it to the scene
const dot = new THREE.Points(geom, new THREE.PointsMaterial({
size: 8,
sizeAttenuation: false,
color: color,
}));
dot.userData.title = feature.properties.title;
scene.add(dot);
dots.push(dot);
// for debugging purposes
console.log(`Longitude: ${longitudeDot}, Latitude: ${latitudeDot} Magnitude: ${magnitude}`);
});
} else {
alert("Invalid JSON data.");
}
}}, 'loadData');
// END GUI SETUP
/*******************************************************************
* CANVAS SET UP
*
* Here we set up the canvas, tooltip, raycaster, orbitcontrols, "walls",
* axes, camera, renderer, stats, instance of ThreeGeo and scene.
*
*
*
* ****************************************************************/
THREE.Object3D.DefaultUp = new THREE.Vector3(0, 0, 1);
// create canvas
const canvas = document.getElementById("canvas");
const tooltip = document.getElementById('tooltip');
// resize the canvas to fill browser window dynamically
window.addEventListener("resize", function() {
canvas.width = window.innerWidth
canvas.height = window.innerHeight
})
// create camera and setpos
const camera = new THREE.PerspectiveCamera(75, canvas.width/canvas.height, 0.1, 1000);
camera.position.set(0, 0, 1.5);
// create renderer and orbit controls
const renderer = new THREE.WebGLRenderer({ canvas });
const controls = new THREE.OrbitControls(camera, renderer.domElement);
// create scene and the 3d "walls" and axes
// note: this code was taken from the three-geo.js examples
// i thought it was a nice feature to orient the map
const scene = new THREE.Scene();
const walls = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxBufferGeometry(1, 1, 1)),
new THREE.LineBasicMaterial({color: 0xcccccc}));
walls.position.set(0, 0, 0);
scene.add(walls);
scene.add(new THREE.AxesHelper(1));
// create raycaster and mouse
// this is used to detect when the mouse is hovering over a dot, and display the dot's title
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
// add event listener for mouse movement
canvas.addEventListener('mousemove', (event) => {
console.log(event.clientX, event.clientY);
mouse.x = (event.clientX / canvas.clientWidth) * 2 - 1;
mouse.y = -(event.clientY / canvas.clientHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
// checks if the mouse is hovering over a dot SPECIFICALLY....
const intersects = raycaster.intersectObjects(dots);
if (intersects.length > 0) {
// debug tool
//console.log('Showing tooltip');
// tooltip style setup
tooltip.style.display = 'block';
tooltip.textContent = intersects[0].object.userData.title;
tooltip.style.left = event.clientX + 'px';
tooltip.style.top = event.clientY + 'px';
// some chicanery I had to do to get from pixel coords to WebGL coords
const position = intersects[0].point.clone();
position.project(camera);
const x = (position.x * 0.5 + 0.5) * canvas.clientWidth;
const y = (position.y * -0.5 + 0.5) * canvas.clientHeight;
tooltip.style.left = x + 'px';
tooltip.style.top = y + 'px';
} else {
tooltip.style.display = 'none';
}
}, false);
// render the stats and append
const stats = new Stats();
stats.showPanel(1); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
const render = () => {
stats.update();
renderer.render(scene, camera);
};
controls.addEventListener('change', render);
render(); // first time
// API token, get your own at https://www.mapbox.com/
// left mine in just for implementation purposes
const ioToken = 'sk.eyJ1IjoiYWhvdW5haW4iLCJhIjoiY2xwcjd0bDVrMDdmbzJqbzBydnZ0a3l4eCJ9.LkPI09omn9o4dYfEharg9Q';
// create a instance of ThreeGeo
const tgeo = new ThreeGeo({
tokenMapbox: ioToken, //originally planned on a system to read in token from a file but that didn't work
});
// END SCENE SETUP
/*******************************************************************
* refreshTerrain()
*
* This function is called whenever the user interacts with the refreshTerrain button.
*
*
* ****************************************************************/
let terrain;
async function refreshTerrain() {
// these were all set up up top in the GUI section
const origin = [parameters.latitude, parameters.longitude];
const radius = parameters.radius;
terrain = await tgeo.getTerrainRgb(
origin, // [lat, lng]
radius, // radius of bounding circle (km) (limited to 5 so my api token doesnt get wasted)
// users are free to modify above and below at their own expense lol
11); // zoom resolution (too high will cause large requests)
scene.add(terrain);
render();
}
// this is the main render loop
render();
})();
</script>
</body>
</html>