-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathearthWebXR.html
792 lines (625 loc) · 35.5 KB
/
earthWebXR.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
<!DOCTYPE html>
<html lang="en">
<head>
<title>Shaking Earth</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="container"></div>
<div id="info">Loading Data</div>
<div id="infoSeismic" style="visibility: hidden;"></div>
<script type="x-shader/x-vertex" id="vertexshaderParticle">
attribute float size;
attribute vec2 coords;
attribute vec3 epicenter;
attribute vec3 positionIntroText;
uniform float time;
uniform float timeSinceMove;
uniform vec3 mouse3D;
uniform int modeVisu;
uniform float magnitude;
varying vec3 vColor;
varying vec2 vCoords;
varying vec3 vPosition;
varying float crossProduct;
varying float vDeltaPos;
varying float vDisplacementRadar;
const float M_PI = 3.1415926535897932384626433832795;
void main() {
vColor = color;
vCoords = coords;
vPosition = position;
vDeltaPos = 1.;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_PointSize = 6.; // max(size, 4.) * ( 300.0 / -mvPosition.z );
gl_Position = projectionMatrix * mvPosition;
crossProduct = min( 0.4 + 1. - acos( dot(normalize(position), normalize(cameraPosition))), 1.);
if(modeVisu > 0){
crossProduct = min( 0.4 + 1. - acos( dot((normalize(modelMatrix * vec4(position, 1.))).xyz, normalize(cameraPosition))), 1.);
}
// Test wave
float PI = 3.14159;
float distFromMouse = distance(position, mouse3D) / 10.;
// Seismic place rendering
if(modeVisu > 0){
gl_PointSize = 12.;
float distToEpicentre = distance(position, epicenter) * 10000000.;
vDeltaPos = 100. * sin(distToEpicentre / 2. - time * size / 3.); // size is magnitude (dirty reuse of ) // 10. * sin(distToEpicentre / 4. - time * 2.);
vec3 nPosition = position * (1. + vDeltaPos / (distToEpicentre * 100.) );
gl_Position = projectionMatrix * modelViewMatrix * vec4( nPosition, 1.0 );
}
// Rotating earth local elevation / radar effect
float alpha = atan(position.z, position.x) + M_PI;
float angularTime = mod(time / 8., 2. * M_PI);
float modulationSize = 0.02;
float distToModulation = abs(alpha - angularTime);
if(distToModulation < modulationSize){
vDisplacementRadar = modulationSize - distToModulation; // abs(sin(( angularTime - alpha ) * time ) ) / 50.;
vec3 nPos = position * ( 1. + vDisplacementRadar / 2.);
gl_Position = projectionMatrix * modelViewMatrix * vec4( nPos, 1.0 );
}
// When mouse over earthquake we shake earth locally
if(distFromMouse < .01){
vec3 newPosition = position * (1. + 0.1 * sin(distFromMouse * 4. - time * 2.) / (distFromMouse * 1000.));
gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
}
/*
// When mouse over earthquake we shake earth locally
if(distFromMouse < 8.){
vec3 newPosition = position * (1. + 10. * timeSinceMove * sin(distFromMouse * 4. - time * 2.) / (distFromMouse * 100.));
gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 );
}
*/
// Pos as text intro
if(modeVisu < 1 && (time - 10. ) < 1. ){
// vec3 interpolatedPos = mix(position, positionIntroText, max(1. - (time - 8.), 0.));
vec3 interpolatedPos = mix(position, positionIntroText, max(1. - (time - 10. * coords.x ), 0.));
gl_Position = projectionMatrix * modelViewMatrix * vec4( interpolatedPos, 1.0 );
}
}
</script>
<script type="x-shader/x-fragment" id="fragmentshaderParticle">
uniform sampler2D pointTexture;
uniform sampler2D earthTexture;
uniform sampler2D earthNightTexture;
uniform int modeVisu;
uniform float time;
uniform float magnitude;
varying vec2 vCoords;
varying vec3 vColor;
varying vec3 vPosition;
varying float crossProduct;
varying float vDeltaPos;
varying float vDisplacementRadar;
float luminance(float r, float g, float b){
return (r * 0.3) + (g * 0.59) + (b * 0.11);
}
void main() {
//gl_FragColor = vec4( vColor, 1.0 );
//gl_FragColor = 1.5 * texture2D( pointTexture, gl_PointCoord ); // + texture2D( earthNightTexture, gl_PointCoord );
//if(modeVisu > 0){ gl_FragColor = gl_FragColor * texture2D( earthTexture, vCoords );}
vec4 pointSpriteTexture = texture2D( pointTexture, gl_PointCoord );
vec4 earthMap = texture2D( earthNightTexture, vCoords ) * 4.; // + texture2D( earthTexture, vCoords ) * 0.4;
gl_FragColor = crossProduct * earthMap; // pointSpriteTexture;
gl_FragColor.a = crossProduct * pointSpriteTexture.a * 8.; // * 0.8;
if(modeVisu > 0){
float normalizedColor = vDeltaPos / 50.;
gl_FragColor = vec4(vColor + vec3( normalizedColor ), 0.5); // //vec4(1.,0.,0., max(crossProduct, 0.01) );
gl_FragColor.a = crossProduct * pointSpriteTexture.a * 8. * max(normalizedColor, 0.2);
}
gl_FragColor.xyz = mix(gl_FragColor.xyz, vec3(1.,0.,0.5), vDisplacementRadar * 25.);
// We do horizon culling
//float maxDist = sqrt(length(vPosition) * length(vPosition) + length(cameraPosition) * length(cameraPosition));
}
</script>
<script type="x-shader/x-vertex" id="vertexshaderEarth">
attribute float size;
uniform float time;
uniform vec3 mouse3D;
varying vec3 vColor;
varying vec2 vUv;
varying vec3 vPosition;
varying float distFromMouse;
void main() {
vColor = color;
vUv = uv;
vPosition = position;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
//gl_PointSize = size * ( 300.0 / -mvPosition.z );
//gl_Position = projectionMatrix * mvPosition;
// Test wave
float PI = 3.14159;
distFromMouse = distance(position, mouse3D) / 10.; // length(position);
if(distFromMouse < 8.){
vPosition = position * (1. + 10. * sin(distFromMouse * 4. - time * 2.) / (distFromMouse * 100.));
}
gl_Position = projectionMatrix * modelViewMatrix * vec4( vPosition, 1.0 );
}
</script>
<script type="x-shader/x-fragment" id="fragmentshaderEarth">
uniform sampler2D pointTexture;
uniform sampler2D earthTexture;
uniform sampler2D earthNightTexture;
uniform sampler2D earthSpecularTexture;
uniform sampler2D earthBumpTexture;
uniform int modeVisu;
varying vec2 vUv;
varying vec3 vColor;
varying vec3 vPosition;
varying float distFromMouse;
void main() {
gl_FragColor = texture2D( earthNightTexture, vUv); //(1. + (length(vPosition) - 199.) / 10. ) * texture2D( earthBumpTexture, vUv);
if(modeVisu == 1) {
gl_FragColor.a = 1. - distFromMouse / 8.;
}
vec4 col = texture2D( earthSpecularTexture, vUv);
if(col.b > 0.9)
gl_FragColor = vec4(0.,0.,0.,0.); // gl_FragColor.a = 0.;
}
</script>
<script type="module">
import * as THREE from './three.module.js';
import { ARButton } from './ARButton.js';
import { OrbitControls } from './controls/OrbitControls.js';
var renderer, scene, camera, controls;
var particleSystemEarth, uniforms, geometry;
var particles = 100000;
var cloudsSphere;
var earthSphere;
var earthSphereBackSide;
var wireframe;
var time = 0;
var timeSinceMove = 1;
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var mouse3D = new THREE.Vector3();
var textMesh;
var seismicParticleSystem;
var allEarthquakeCenterObjects = [];
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var radius = 0.14;
// XR Part
var controller;
init();
/*
var light = new THREE.DirectionalLight(0xffffff,0.5);
light.position.set(-1,0,1);
scene.add(light);
var smokeTexture = new THREE.TextureLoader().load('textures/Smoke-Element.png');
var smokeMaterial = new THREE.MeshLambertMaterial({color: 0xeeeeee, map: smokeTexture, transparent: true});
var smokeGeo = new THREE.PlaneGeometry(300,300);
var smokeParticles = new THREE.Object3D();
for (var p = 0; p < 150; p++) {
var particle = new THREE.Mesh(smokeGeo,smokeMaterial);
particle.position.set(Math.random()*500-250,Math.random()*500-250,Math.random()*1000-100);
particle.rotation.z = Math.random() * 360;
smokeParticles.add(particle);
}
function evolveSmoke() {
smokeParticles.rotation.copy(camera.rotation);
var sp = smokeParticles.children.length;
while(sp--) {
smokeParticles.children[sp].rotation.z = (time * 0.3);
}
}
scene.add(smokeParticles);
*/
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.02, 50 );
//camera.position.y = 400;
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({antialias:true, alpha:true});
renderer.xr.enabled = true;
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( function () {
renderer.setAnimationLoop( render );
} );
var container = document.getElementById( 'container' );
container.appendChild( renderer.domElement );
// XR Part
document.body.appendChild( ARButton.createButton( renderer ) );
var geometryC = new THREE.BoxGeometry( 0.1,0.1,0.1 );
var materialC = new THREE.MeshBasicMaterial( {color: 0x00ff00, side: THREE.DoubleSide} );
var cube = new THREE.Mesh( geometryC, materialC );
// cube.position.copy(0,0.1,0.5);
// scene.add( cube ); // scene.add( cube );
/*
controls = new OrbitControls( camera, renderer.domElement );
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.05;
controls.screenSpacePanning = false;
controls.minDistance = 100;
controls.maxDistance = 2000;
*/
uniforms = {
time: {value: time},
timeSinceMove: {value: timeSinceMove},
mouse3D: {value: mouse3D},
modeVisu: {value: 0},
magnitude: {value:0},
pointTexture: { value: new THREE.TextureLoader().load( "textures/spark1.png" ) },
earthTexture: { value: new THREE.TextureLoader().load( "textures/BlackMarble_2016_3km.jpg"/*"textures/8k_earth_daymap.jpg" */) },
earthNightTexture: { value: new THREE.TextureLoader().load( "textures/BlackMarble_2016_3km.jpg" ) },
earthSpecularTexture: { value: new THREE.TextureLoader().load( "textures/2k_earth_specular_map.jpg" ) },
earthBumpTexture: { value: new THREE.TextureLoader().load( "textures/elev_bump_2k.jpg" ) }
};
var shaderMaterialParticles = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexshaderParticle' ).textContent,
fragmentShader: document.getElementById( 'fragmentshaderParticle' ).textContent,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true,
vertexColors: true
} );
/*
// Test creating text geometry to get vertices to particles
var loader = new THREE.FontLoader();
loader.load( 'SuperYeastyRegular.json', function ( font ) { // https://threejs.org/examples/fonts/helvetiker_regular.typeface.json
var geometry = new THREE.TextGeometry( 'Earth Beats', {
font: font,
size: 80,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 10,
bevelSize: 8,
bevelOffset: 0,
bevelSegments: 5
} );
textMesh = new THREE.Mesh(geometry, new THREE.MeshBasicMaterial({wireframe: true}));
// scene.add(textMesh);
// Now we transform the text into particles
console.log("text: ",textMesh);
createEarth();
} );
*/
function createEarth(){
geometry = new THREE.BufferGeometry();
var positions = [];
var epicenters = [];
var positionsIntroText = [];
var colors = [];
var sizes = [];
var coords = [];
var color = new THREE.Color();
var nbPerlongitude = 700;
var nbPerLatitude = nbPerlongitude / 2;
var particleRadius = radius * 1;
var textInc = 0;
//var textPosArray = textMesh.geometry.vertices;
for ( var i = 0; i < nbPerlongitude; ++i){
var phi = i / nbPerlongitude * 2 * Math.PI;
for ( var j = 0; j < nbPerLatitude; ++j){
var theta = j / nbPerLatitude * Math.PI;
var x = - particleRadius * Math.cos(phi) * Math.sin(theta);
var y = particleRadius * Math.cos(theta);
var z = particleRadius * Math.sin(phi) * Math.sin(theta);
coords.push( i / nbPerlongitude, 1 - j / nbPerLatitude);
/* var posText = textPosArray[textInc];
var nexPosText = posText.clone();
if(textInc < textPosArray.length -1)
nexPosText = textPosArray[textInc + 1];
var avPos = (posText.add(nexPosText)).divideScalar(2.); //.addScalar(Math.random());
textInc++;
if(textInc >= textPosArray.length) textInc = 0;
*/
var p_2 = particleRadius /2;
// positionsIntroText.push(avPos.x - 180, avPos.y + Math.random(), avPos.z+ 80 );
positionsIntroText.push(Math.random() * particleRadius - p_2, Math.random() * particleRadius - p_2,Math.random() * particleRadius - p_2);
positions.push(x, y, z);
epicenters.push(x, y, z);
colors.push(0.5, 0., 1.);
sizes.push(6);
}
}
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
geometry.setAttribute( 'positionIntroText', new THREE.Float32BufferAttribute( positionsIntroText, 3 ) );
geometry.setAttribute( 'epicenter', new THREE.Float32BufferAttribute( epicenters, 3 ) );
geometry.setAttribute( 'coords', new THREE.Float32BufferAttribute( coords, 2 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
particleSystemEarth = new THREE.Points( geometry, shaderMaterialParticles );
scene.add( particleSystemEarth ); console.log("particleSystemEarth", particleSystemEarth);
animate();
}
createEarth();
// controls.maxPolarAngle = Math.PI / 2;
window.addEventListener( 'resize', onWindowResize, false );
/*
// clouds
var geometryClouds = new THREE.SphereGeometry( radius + 3, 32, 32 );
var material = new THREE.MeshBasicMaterial( {
transparent:true,
opacity: 0.2,
map: new THREE.TextureLoader().load( "textures/4k_earth_clouds.jpg" ),
blending: THREE.AdditiveBlending,
});
cloudsSphere = new THREE.Mesh( geometryClouds, material );
*/
//scene.add( cloudsSphere );
// Version Mesh of the earth
var uniformsEarth = {
time: {value: time},
mouse3D: {value: mouse3D},
modeVisu: {value: 0},
pointTexture: { value: new THREE.TextureLoader().load( "textures/spark1.png" ) },
earthTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_daymap.jpg" ) },
earthNightTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_nightmap.jpg" ) },
earthSpecularTexture: { value: new THREE.TextureLoader().load( "textures/2k_earth_specular_map.jpg" ) },
earthBumpTexture: { value: new THREE.TextureLoader().load( "textures/elev_bump_2k.jpg" ) }
};
var shaderMaterialEarth = new THREE.ShaderMaterial( {
uniforms: uniformsEarth,
vertexShader: document.getElementById( 'vertexshaderEarth' ).textContent,
fragmentShader: document.getElementById( 'fragmentshaderEarth' ).textContent,
//blending: THREE.AdditiveBlending,
//wireframe: true,
side:THREE.FrontSide,
depthTest: true,
transparent: true,
vertexColors: true
} );
var shaderMaterialEarthBackSide = new THREE.ShaderMaterial( {
uniforms: uniformsEarth,
vertexShader: document.getElementById( 'vertexshaderEarth' ).textContent,
fragmentShader: document.getElementById( 'fragmentshaderEarth' ).textContent,
//blending: THREE.AdditiveBlending,
//wireframe: true,
side:THREE.BackSide,
depthTest: true,
transparent: false,
vertexColors: true
} );
var geometryEarth = new THREE.SphereBufferGeometry( radius , 128, 128);
earthSphere = new THREE.Mesh( geometryEarth, shaderMaterialEarth);
var geometryEarthBackSide = new THREE.SphereBufferGeometry( radius , 128, 128);
earthSphereBackSide = new THREE.Mesh( geometryEarthBackSide, shaderMaterialEarthBackSide);
// Version Lines of the earth
var uniformsLines= {
time: {value: time},
mouse3D: {value: mouse3D},
modeVisu: {value: 1},
pointTexture: { value: new THREE.TextureLoader().load( "textures/spark1.png" ) },
earthTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_daymap.jpg" ) },
earthNightTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_nightmap.jpg" ) },
earthSpecularTexture: { value: new THREE.TextureLoader().load( "textures/2k_earth_specular_map.jpg" ) },
earthBumpTexture: { value: new THREE.TextureLoader().load( "textures/elev_bump_2k.jpg" ) }
};
var shaderMaterialLines = new THREE.ShaderMaterial( {
uniforms: uniformsLines,
vertexShader: document.getElementById( 'vertexshaderEarth' ).textContent,
fragmentShader: document.getElementById( 'fragmentshaderEarth' ).textContent,
// blending: THREE.AdditiveBlending,
// wireframe: true,
depthTest: true,
transparent: true,
vertexColors: true
} );
const wireframeGeometry = new THREE.WireframeGeometry( geometryEarth );
const wireframeMaterial = new THREE.LineBasicMaterial( { /*color: 0xff0000*/ } );
wireframe = new THREE.LineSegments( wireframeGeometry, shaderMaterialLines );
// earthSphere.add( wireframe );
// scene.add( earthSphere );
//scene.add(earthSphereBackSide);
getSeismicData();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
// controls.update();
requestAnimationFrame( animate );
render();
}
function render() {
time += 0.001;
if(time > 1) time = 0;
//console.log(time);
if(timeSinceMove > 0 ) timeSinceMove -= 0.02;
//cloudsSphere.rotation.y = time / 10.;
particleSystemEarth.material.uniforms.time.value = time * 40.;
particleSystemEarth.material.uniforms.timeSinceMove.value = timeSinceMove;
mouse3D = new THREE.Vector3(radius * Math.sin(time), 0,0);
particleSystemEarth.material.uniforms.mouse3D.value = mouse3D;
// earthSphere.material.uniforms.mouse3D.value = mouse3D;
//earthSphereBackSide.material.uniforms.mouse3D.value = mouse3D;
//wireframe.material.uniforms.mouse3D.value = mouse3D;
geometry.attributes.size.needsUpdate = true;
if(seismicParticleSystem)
seismicParticleSystem.material.uniforms.time.value = time * 40.;
// evolveSmoke();
// scene.rotation.y += 0.001;
//console.log(camera.position);
renderer.render( scene, camera );
}
function onMouseMove( event ) {
if(event.buttons == 0 ){
// timeSinceMove = 1;
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
// update the picking ray with the camera and mouse position
raycaster.setFromCamera( mouse, camera );
// calculate objects intersecting the picking ray
//var intersects = raycaster.intersectObjects( [cloudsSphere] );
var intersects = raycaster.intersectObjects( allEarthquakeCenterObjects );
var div = document.getElementById('infoSeismic');
div.style.visibility = "hidden";
for ( var i = 0; i < intersects.length; i++ ) {
var inter = intersects[ i ];
mouse3D = inter.point;
if(inter.object.evt){
timeSinceMove = 1;
var left = event.pageX - 140 + "px";
var top = event.clientY + "px";
div.style.left = left;
div.style.top = top;
div.innerText = inter.object.evt.eventLocationName +
'\nMagnitude: ' + inter.object.evt.Magnitude +
'\n ' + inter.object.evt.time;
div.style.visibility = "visible";
}
}
particleSystemEarth.material.uniforms.mouse3D.value = mouse3D;
earthSphere.material.uniforms.mouse3D.value = mouse3D;
earthSphereBackSide.material.uniforms.mouse3D.value = mouse3D;
wireframe.material.uniforms.mouse3D.value = mouse3D;
}
}
window.addEventListener( 'mousemove', onMouseMove, false );
/********************************** Part regarding the seismic data ***************************/
// Url to access data: https://service.iris.edu/fdsnws/event/1/query?starttime=2019-12-18T00:00:00&endtime=2019-12-19T23:00:00&includeallmagnitudes=true&orderby=time&format=text&nodata=404
// We get the data using text cause it's much faster to access and lighter
// Seismic event is 12 elements
var seismicArray = [];
function getSeismicData(){
var yesterday = new Date(new Date().getTime() - (24 * 3600 * 1000)).toISOString();
fetch('https://service.iris.edu/fdsnws/event/1/query?starttime=' + yesterday + '&includeallmagnitudes=true&orderby=time&format=text&nodata=404').then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
response.text().then(function(data) {
//console.log(data);
var st = data.replace(/[\/]/g, "");
var seismicDataArray = st.split(/[\r\n]+/gm); // split(new RegExp("\n")); // split(/\r?\n/) //split(/[\r\n]+/gm); // replace( /[\r\n]+/gm, "" ).split('|');
//console.log(seismicDataArray);
for(var i = 1; i < seismicDataArray.length; ++i){
var seismicEvent = seismicDataArray[i].split("|");
//console.log(seismicEvent);
seismicArray.push({ eventID : seismicEvent[0],
time : seismicEvent[1],
latitude : seismicEvent[2],
longitude : seismicEvent[3],
depth : seismicEvent[4],
author : seismicEvent[5],
catalog : seismicEvent[6],
contributor : seismicEvent[7],
contributorID : seismicEvent[8],
MagType : seismicEvent[9],
Magnitude : seismicEvent[10],
MagAuthor : seismicEvent[11],
eventLocationName : seismicEvent[12]
});
}
//console.log(seismicArray);
// Then we can launch the visualization of that data
displaySeismicData();
});
}
).catch(function(err) {
console.log('Fetch Error :-S', err);
});
}
// We have all the seismic data (seismicArray), we can now make a nice visualization for it
// ex eventID: "11159981"
/*
time: "2019-12-19T12:07:55"
latitude: "32.525"
longitude: "87.0848"
depth: "10.0"
author: "us"
catalog: "NEIC PDE"
contributor: "us"
contributorID: "us70006nf9"
MagType: "mb"
Magnitude: "4.9"
MagAuthor: "us"
eventLocationName: "XIZANG"
*/
function displaySeismicData(){
var uniforms = {
time: {value: time},
timeSinceMove: {value: timeSinceMove},
mouse3D: {value: mouse3D},
magnitude: {value:0},
modeVisu: {value: 1},
epicenter: {value: new THREE.Vector3()},
pointTexture: { value: new THREE.TextureLoader().load( "textures/spark1.png" ) },
earthTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_daymap.jpg" ) },
earthNightTexture: { value: new THREE.TextureLoader().load( "textures/4k_earth_nightmap.jpg" ) },
earthSpecularTexture: { value: new THREE.TextureLoader().load( "textures/2k_earth_specular_map.jpg" ) },
earthBumpTexture: { value: new THREE.TextureLoader().load( "textures/elev_bump_2k.jpg" ) }
};
var shaderMaterialParticles = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexshaderParticle' ).textContent,
fragmentShader: document.getElementById( 'fragmentshaderParticle' ).textContent,
blending: THREE.AdditiveBlending,
depthTest: false,
transparent: true,
vertexColors: true
} );
var radiusS = radius + 0.01;
var geometry = new THREE.BufferGeometry();
var positions = [];
var epicenters = [];
var colors = [];
var sizes = [];
var coords = [];
var color = new THREE.Color();
var nbParticlesPerSeism = 4000;
var noise = 0.06;// 0.10;
var maxDistanceToEpicenter = 0.01// 15;
var geometryCone = new THREE.SphereBufferGeometry(0.005, 6, 6); // new THREE.SphereBufferGeometry(7,4,4);
var materialCone = new THREE.MeshBasicMaterial( {color: 0xffff00, transparent: true, opacity:0.8} );
var divInfo = document.getElementById('info');
divInfo.innerText = "Showing " + seismicArray.length + " earthquakes for the last 24 hours";
// allEarthquakeCenterObjects.push(earthSphere); // We add the earth for picking reason
for(var i in seismicArray){
if(seismicArray[i].eventID != ''){
var evt = seismicArray[i];
var lon = Math.PI + parseFloat(evt.longitude) / 180 * Math.PI;
var lat = Math.PI / 2 - parseFloat(evt.latitude) / 180 * Math.PI;
var magnitude = parseFloat(evt.Magnitude) * 0.01;
var x = - radiusS * Math.cos(lon) * Math.sin(lat);
var y = radiusS * Math.cos(lat);
var z = radiusS * Math.sin(lon) * Math.sin(lat);
var cone = new THREE.Mesh( geometryCone, materialCone );
cone.evt = evt;
//console.log(cone.evt);
cone.position.set(x,y,z);
cone.up.copy(new THREE.Vector3(x,y,z));
allEarthquakeCenterObjects.push(cone);
scene.add( cone );
for(var j = 0; j < nbParticlesPerSeism; ++j){
// Ugly way with lon lat noisy
var noiseLon = lon + (Math.random() - 0.5) * noise * 4.;//* magnitude;
var noiseLat = lat + (Math.random() - 0.5) * noise * 4.;// * magnitude;
var x2 = - radiusS * Math.cos(noiseLon) * Math.sin(noiseLat);
var y2 = radiusS * Math.cos(noiseLat);
var z2 = radiusS * Math.sin(noiseLon) * Math.sin(noiseLat);
var pos = new THREE.Vector3(x2, y2, z2);
// Version using noise distance to epicenter
// 2D circle var nx =
var d = new THREE.Vector3(x,y,z).distanceTo(pos);
if(d < maxDistanceToEpicenter / 4 + 1 * magnitude / 5){
positions.push(pos.x, pos.y, pos.z);
epicenters.push(x, y, z);
coords.push(lon, lat);
colors.push(d / maxDistanceToEpicenter / 4 + 0.6 * magnitude ,0.,1.);
sizes.push(magnitude); // We use it for magnitude
}
}
}
}
console.log("displayed particles for earthquakes: ", sizes.length);
geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
geometry.setAttribute( 'epicenter', new THREE.Float32BufferAttribute( epicenters, 3 ) );
geometry.setAttribute( 'coords', new THREE.Float32BufferAttribute( coords, 2 ) );
geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ).setUsage( THREE.DynamicDrawUsage ) );
seismicParticleSystem = new THREE.Points( geometry, shaderMaterialParticles );
scene.add(seismicParticleSystem);
// seismicParticleSystem.material.opacity = 0.;
}
</script>
</body>
</html>