Spaces:
Running
Running
Upload 9 files
Browse files- js/scene.js +75 -41
js/scene.js
CHANGED
|
@@ -287,11 +287,18 @@ function auroraMaterial(cfg, layer) {
|
|
| 287 |
|
| 288 |
function boundaryMaterial(color, strength, tail) {
|
| 289 |
return new THREE.ShaderMaterial({
|
| 290 |
-
uniforms: { uTime: { value: 0 }, uColor: { value: new THREE.Color(...color) }, uStrength: { value: strength }, uTail: { value: tail }
|
|
|
|
| 291 |
vertexShader: /* glsl */`
|
|
|
|
| 292 |
varying vec3 vN; varying vec3 vV; varying vec3 vP;
|
|
|
|
| 293 |
void main(){
|
| 294 |
vec4 wp = modelMatrix * vec4(position, 1.0);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 295 |
vP = wp.xyz; vN = normalize(mat3(modelMatrix) * normal); vV = cameraPosition - wp.xyz;
|
| 296 |
gl_Position = projectionMatrix * viewMatrix * wp;
|
| 297 |
}`,
|
|
@@ -312,7 +319,7 @@ function boundaryMaterial(color, strength, tail) {
|
|
| 312 |
// Coronal mass ejection: a glowing, turbulent cap of plasma that sweeps from the Sun past the planet.
|
| 313 |
function cmeMaterial(R, cosCap) {
|
| 314 |
return new THREE.ShaderMaterial({
|
| 315 |
-
uniforms: { uTime: { value: 0 }, uFade: { value: 1 }, uR: { value: R }, uCap: { value: cosCap } },
|
| 316 |
vertexShader: /* glsl */`
|
| 317 |
uniform float uR;
|
| 318 |
varying float vC; varying vec3 vN; varying vec3 vV; varying vec3 vL;
|
|
@@ -323,7 +330,7 @@ function cmeMaterial(R, cosCap) {
|
|
| 323 |
gl_Position = projectionMatrix * viewMatrix * wp;
|
| 324 |
}`,
|
| 325 |
fragmentShader: /* glsl */`
|
| 326 |
-
uniform float uTime, uFade, uCap;
|
| 327 |
varying float vC; varying vec3 vN; varying vec3 vV; varying vec3 vL;
|
| 328 |
${NOISE_GLSL}
|
| 329 |
void main(){
|
|
@@ -333,7 +340,9 @@ function cmeMaterial(R, cosCap) {
|
|
| 333 |
float f = pow(1.0 - abs(dot(normalize(vN), normalize(vV))), 1.4);
|
| 334 |
vec3 col = mix(vec3(1.0, 0.35, 0.12), vec3(1.0, 0.85, 0.5), n);
|
| 335 |
float a = edge * (0.25 + 0.75 * n + fil * 1.2) * (0.2 + 1.2 * f) * uFade;
|
| 336 |
-
|
|
|
|
|
|
|
| 337 |
}`,
|
| 338 |
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide
|
| 339 |
});
|
|
@@ -742,7 +751,7 @@ export function createScene(container, { t = k => k, onPlanetChange = () => {},
|
|
| 742 |
cme.renderOrder = 4; cme.visible = false; cme.frustumCulled = false;
|
| 743 |
cme.userData.R = cmeR;
|
| 744 |
root.add(cme);
|
| 745 |
-
state.cme = null;
|
| 746 |
|
| 747 |
scene.add(root);
|
| 748 |
state.root = root;
|
|
@@ -806,52 +815,73 @@ export function createScene(container, { t = k => k, onPlanetChange = () => {},
|
|
| 806 |
function updateWind(dt) {
|
| 807 |
if (!state.layers.wind) return;
|
| 808 |
const { wpos, wcol, wvel, respawn, endX } = state.objs;
|
| 809 |
-
const
|
| 810 |
-
const
|
| 811 |
-
|
| 812 |
-
const speed = state.cfg.mp * 0.55 * dt * Math.max(0.25, Math.min(state.speed, 3)) * (0.55 + 0.45 * k);
|
| 813 |
-
const n = Math.round(wvel.length * THREE.MathUtils.clamp(0.2 + 0.45 * k, 0.15, 1));
|
| 814 |
-
state.objs.wind.geometry.setDrawRange(0, n);
|
| 815 |
-
const b = THREE.MathUtils.clamp(0.75 + 0.25 * k, 0.6, 1.6);
|
| 816 |
-
// colour shifts from pale blue (calm) through yellow-white to hot orange-red (storm)
|
| 817 |
-
const u = THREE.MathUtils.clamp((k - 0.3) / 0.7, 0, 1), v = THREE.MathUtils.clamp((k - 1) / 2.5, 0, 1);
|
| 818 |
-
const cr = b, cg = b * (0.85 + 0.05 * u - 0.45 * v), cb = b * ((1.0 - 0.4 * u) - 0.3 * v);
|
| 819 |
-
const sr = b, sg = b * (0.55 - 0.25 * v), sb = b * (0.25 + 0.1 * v);
|
| 820 |
for (let i = 0; i < n; i++) {
|
| 821 |
const ix = i * 3;
|
| 822 |
let x = wpos[ix], y = wpos[ix + 1], z = wpos[ix + 2];
|
|
|
|
|
|
|
|
|
|
| 823 |
let r = Math.hypot(x, y, z);
|
| 824 |
let c = x / r;
|
| 825 |
const rbs = shue(c, mp * BS_SCALE, BS_ALPHA);
|
| 826 |
const inSheath = r < rbs;
|
| 827 |
-
x -=
|
| 828 |
r = Math.hypot(x, y, z); c = x / r;
|
| 829 |
const rmp = shue(c, mp * 1.03, MP_ALPHA);
|
| 830 |
if (r < rmp) {
|
| 831 |
-
|
| 832 |
-
|
|
|
|
|
|
|
| 833 |
if (Math.abs(y) + Math.abs(z) < 0.05 * mp) { y += (Math.random() - 0.5) * 0.2; z += (Math.random() - 0.5) * 0.2; }
|
| 834 |
}
|
| 835 |
wpos[ix] = x; wpos[ix + 1] = y; wpos[ix + 2] = z;
|
| 836 |
if (x < endX) respawn(i, false);
|
| 837 |
-
|
| 838 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 839 |
}
|
| 840 |
state.objs.wind.geometry.attributes.position.needsUpdate = true;
|
| 841 |
state.objs.wind.geometry.attributes.color.needsUpdate = true;
|
| 842 |
}
|
| 843 |
|
| 844 |
// Stand-off distance scales roughly as (dynamic pressure)^(-1/6); exaggerated a little so the change is easy to see.
|
| 845 |
-
function
|
| 846 |
-
function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 847 |
function applyWind() {
|
| 848 |
-
const o = state.objs, cfg = state.cfg, k =
|
| 849 |
o.boundaries.scale.setScalar(s);
|
| 850 |
-
const glow = THREE.MathUtils.clamp(0.65 + 0.35 * k
|
| 851 |
-
|
| 852 |
-
o.
|
| 853 |
-
|
| 854 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 855 |
// a stronger wind pushes the auroral oval towards the equator and widens it
|
| 856 |
const a = cfg.aurora;
|
| 857 |
const colat = a.colat * THREE.MathUtils.clamp(1 + 0.13 * (k - 1), 0.88, 1.42);
|
|
@@ -861,22 +891,27 @@ export function createScene(container, { t = k => k, onPlanetChange = () => {},
|
|
| 861 |
|
| 862 |
function launchCME(x0) {
|
| 863 |
const o = state.objs; if (!o.cme) return;
|
| 864 |
-
|
|
|
|
|
|
|
| 865 |
o.cme.visible = true;
|
| 866 |
}
|
| 867 |
function updateCME(dt, tsec) {
|
| 868 |
const o = state.objs, c = state.cme;
|
| 869 |
-
if (state.kick > 0) state.kick = Math.max(0, state.kick - dt * 0.11); // storm fades over ~9 s
|
| 870 |
if (!c) return;
|
| 871 |
const mp = state.cfg.mp;
|
| 872 |
c.x -= mp * 0.9 * dt;
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 880 |
}
|
| 881 |
|
| 882 |
function applyLayers() {
|
|
@@ -1003,8 +1038,7 @@ export function createScene(container, { t = k => k, onPlanetChange = () => {},
|
|
| 1003 |
state.time += dtHours;
|
| 1004 |
o.spin.rotation.y = (2 * Math.PI / cfg.period) * state.time;
|
| 1005 |
updateCME(dt, clock.elapsedTime);
|
| 1006 |
-
|
| 1007 |
-
state.windK += (windTarget - state.windK) * Math.min(1, dt * (state.kick > 0 ? 4 : 2.5));
|
| 1008 |
applyWind();
|
| 1009 |
const tsec = clock.elapsedTime;
|
| 1010 |
o.field.material.uniforms.uTime.value = tsec;
|
|
@@ -1063,7 +1097,7 @@ export function createScene(container, { t = k => k, onPlanetChange = () => {},
|
|
| 1063 |
mo.fx?.snapTail?.();
|
| 1064 |
},
|
| 1065 |
launchCME,
|
| 1066 |
-
windInfo() { return { k
|
| 1067 |
setWind(k, instant = false) { state.wind = THREE.MathUtils.clamp(k, 0.2, 5); if (instant) state.windK = state.wind; },
|
| 1068 |
view,
|
| 1069 |
refreshLabels,
|
|
|
|
| 287 |
|
| 288 |
function boundaryMaterial(color, strength, tail) {
|
| 289 |
return new THREE.ShaderMaterial({
|
| 290 |
+
uniforms: { uTime: { value: 0 }, uColor: { value: new THREE.Color(...color) }, uStrength: { value: strength }, uTail: { value: tail },
|
| 291 |
+
uFrontX: { value: 1e6 }, uCR: { value: 1 }, uCP: { value: 0 }, uKb: { value: 1 }, uCW: { value: 1 } },
|
| 292 |
vertexShader: /* glsl */`
|
| 293 |
+
uniform float uFrontX, uCR, uCP, uKb, uCW;
|
| 294 |
varying vec3 vN; varying vec3 vV; varying vec3 vP;
|
| 295 |
+
float sc(float k){ return clamp(pow(k, -0.22), 0.7, 1.35); }
|
| 296 |
void main(){
|
| 297 |
vec4 wp = modelMatrix * vec4(position, 1.0);
|
| 298 |
+
// CME front is a paraboloid x = front + rho^2 / 2R; everything sunward of it feels the extra pressure
|
| 299 |
+
float rho = length(wp.yz);
|
| 300 |
+
float amt = smoothstep(-uCW, uCW, wp.x - (uFrontX + rho * rho / (2.0 * uCR)));
|
| 301 |
+
wp.xyz *= sc(uKb + uCP * amt) / sc(uKb);
|
| 302 |
vP = wp.xyz; vN = normalize(mat3(modelMatrix) * normal); vV = cameraPosition - wp.xyz;
|
| 303 |
gl_Position = projectionMatrix * viewMatrix * wp;
|
| 304 |
}`,
|
|
|
|
| 319 |
// Coronal mass ejection: a glowing, turbulent cap of plasma that sweeps from the Sun past the planet.
|
| 320 |
function cmeMaterial(R, cosCap) {
|
| 321 |
return new THREE.ShaderMaterial({
|
| 322 |
+
uniforms: { uTime: { value: 0 }, uFade: { value: 1 }, uR: { value: R }, uCap: { value: cosCap }, uNear: { value: R / 3.2 } },
|
| 323 |
vertexShader: /* glsl */`
|
| 324 |
uniform float uR;
|
| 325 |
varying float vC; varying vec3 vN; varying vec3 vV; varying vec3 vL;
|
|
|
|
| 330 |
gl_Position = projectionMatrix * viewMatrix * wp;
|
| 331 |
}`,
|
| 332 |
fragmentShader: /* glsl */`
|
| 333 |
+
uniform float uTime, uFade, uCap, uNear;
|
| 334 |
varying float vC; varying vec3 vN; varying vec3 vV; varying vec3 vL;
|
| 335 |
${NOISE_GLSL}
|
| 336 |
void main(){
|
|
|
|
| 340 |
float f = pow(1.0 - abs(dot(normalize(vN), normalize(vV))), 1.4);
|
| 341 |
vec3 col = mix(vec3(1.0, 0.35, 0.12), vec3(1.0, 0.85, 0.5), n);
|
| 342 |
float a = edge * (0.25 + 0.75 * n + fil * 1.2) * (0.2 + 1.2 * f) * uFade;
|
| 343 |
+
// fade out as the front sweeps past the camera so it never floods the screen
|
| 344 |
+
a *= smoothstep(uNear * 0.4, uNear * 1.6, length(vV));
|
| 345 |
+
gl_FragColor = vec4(col * a * 0.32, 1.0);
|
| 346 |
}`,
|
| 347 |
transparent: true, depthWrite: false, blending: THREE.AdditiveBlending, side: THREE.DoubleSide
|
| 348 |
});
|
|
|
|
| 751 |
cme.renderOrder = 4; cme.visible = false; cme.frustumCulled = false;
|
| 752 |
cme.userData.R = cmeR;
|
| 753 |
root.add(cme);
|
| 754 |
+
state.cme = null;
|
| 755 |
|
| 756 |
scene.add(root);
|
| 757 |
state.root = root;
|
|
|
|
| 815 |
function updateWind(dt) {
|
| 816 |
if (!state.layers.wind) return;
|
| 817 |
const { wpos, wcol, wvel, respawn, endX } = state.objs;
|
| 818 |
+
const kb = state.windK, base = state.cfg.mp;
|
| 819 |
+
const step = base * 0.55 * dt * Math.max(0.25, Math.min(state.speed, 3));
|
| 820 |
+
const n = wvel.length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 821 |
for (let i = 0; i < n; i++) {
|
| 822 |
const ix = i * 3;
|
| 823 |
let x = wpos[ix], y = wpos[ix + 1], z = wpos[ix + 2];
|
| 824 |
+
// local solar wind strength: the slider value, plus the CME's pressure if the cloud has reached this point
|
| 825 |
+
const k = kb + cmeAmt(x, Math.hypot(y, z)) * (state.cme ? state.cme.P : 0);
|
| 826 |
+
const mp = base * scaleOf(k);
|
| 827 |
let r = Math.hypot(x, y, z);
|
| 828 |
let c = x / r;
|
| 829 |
const rbs = shue(c, mp * BS_SCALE, BS_ALPHA);
|
| 830 |
const inSheath = r < rbs;
|
| 831 |
+
x -= step * (0.55 + 0.45 * k) * wvel[i] * (inSheath ? 0.5 : 1);
|
| 832 |
r = Math.hypot(x, y, z); c = x / r;
|
| 833 |
const rmp = shue(c, mp * 1.03, MP_ALPHA);
|
| 834 |
if (r < rmp) {
|
| 835 |
+
// small penetrations snap to the boundary; big ones (boundary expanding after a storm) ease out
|
| 836 |
+
const kk = rmp * 1.01 / r, f = kk < 1.05 ? 1 : Math.min(1, dt * 6);
|
| 837 |
+
const m = 1 + (kk - 1) * f;
|
| 838 |
+
x *= m; y *= m; z *= m;
|
| 839 |
if (Math.abs(y) + Math.abs(z) < 0.05 * mp) { y += (Math.random() - 0.5) * 0.2; z += (Math.random() - 0.5) * 0.2; }
|
| 840 |
}
|
| 841 |
wpos[ix] = x; wpos[ix + 1] = y; wpos[ix + 2] = z;
|
| 842 |
if (x < endX) respawn(i, false);
|
| 843 |
+
// denser, brighter, redder where the wind is stronger; each particle fades around its own threshold
|
| 844 |
+
const dens = Math.min(1, Math.max(0.15, 0.2 + 0.45 * k));
|
| 845 |
+
const thr = (i * 0.6180339887) % 1;
|
| 846 |
+
const w = Math.min(1, Math.max(0, (dens - thr) / 0.12 + 0.5));
|
| 847 |
+
const b = Math.min(1.6, Math.max(0.6, 0.75 + 0.25 * k)) * w;
|
| 848 |
+
const u = Math.min(1, Math.max(0, (k - 0.3) / 0.7)), v = Math.min(1, Math.max(0, (k - 1) / 2.5));
|
| 849 |
+
if (inSheath) { wcol[ix] = b; wcol[ix + 1] = b * (0.55 - 0.25 * v); wcol[ix + 2] = b * (0.25 + 0.1 * v); }
|
| 850 |
+
else { wcol[ix] = b; wcol[ix + 1] = b * (0.85 + 0.05 * u - 0.45 * v); wcol[ix + 2] = b * (1.0 - 0.4 * u - 0.3 * v); }
|
| 851 |
}
|
| 852 |
state.objs.wind.geometry.attributes.position.needsUpdate = true;
|
| 853 |
state.objs.wind.geometry.attributes.color.needsUpdate = true;
|
| 854 |
}
|
| 855 |
|
| 856 |
// Stand-off distance scales roughly as (dynamic pressure)^(-1/6); exaggerated a little so the change is easy to see.
|
| 857 |
+
function scaleOf(k) { return THREE.MathUtils.clamp(Math.pow(k, -0.22), 0.7, 1.35); }
|
| 858 |
+
function windScale() { return scaleOf(state.windK); }
|
| 859 |
+
// how far the CME front has swept past a point (0 = not reached, 1 = inside the cloud); smooth over ~1 mp
|
| 860 |
+
function cmeAmt(x, rho) {
|
| 861 |
+
const c = state.cme; if (!c) return 0;
|
| 862 |
+
const d = x - (c.x + rho * rho / (2 * c.R));
|
| 863 |
+
const w = c.W, t = Math.min(1, Math.max(0, (d + w) / (2 * w)));
|
| 864 |
+
return t * t * (3 - 2 * t);
|
| 865 |
+
}
|
| 866 |
+
// effective strength felt at the magnetopause nose: drives field lines, aurora and the meters
|
| 867 |
+
function noseK() {
|
| 868 |
+
const kb = state.windK; if (!state.cme) return kb;
|
| 869 |
+
return kb + cmeAmt(state.cfg.mp * windScale(), 0) * state.cme.P;
|
| 870 |
+
}
|
| 871 |
+
function windBoost() { return THREE.MathUtils.clamp(0.45 + 0.55 * noseK(), 0.5, 2.6); }
|
| 872 |
function applyWind() {
|
| 873 |
+
const o = state.objs, cfg = state.cfg, s = windScale(), k = noseK(), sk = scaleOf(k);
|
| 874 |
o.boundaries.scale.setScalar(s);
|
| 875 |
+
const glow = THREE.MathUtils.clamp(0.65 + 0.35 * k, 0.6, 3);
|
| 876 |
+
const c = state.cme;
|
| 877 |
+
[o.mpMesh, o.bsMesh].forEach(m => {
|
| 878 |
+
const u = m.material.uniforms;
|
| 879 |
+
u.uStrength.value = m.userData.base * glow;
|
| 880 |
+
u.uKb.value = state.windK;
|
| 881 |
+
u.uFrontX.value = c ? c.x : 1e6; u.uCR.value = c ? c.R : 1; u.uCP.value = c ? c.P : 0; u.uCW.value = c ? c.W : 1;
|
| 882 |
+
});
|
| 883 |
+
o.field.material.uniforms.uMp.value = cfg.mp * sk;
|
| 884 |
+
o.moons.forEach(m => { if (m.tube) m.tube.material.uniforms.uMp.value = cfg.mp * sk; });
|
| 885 |
// a stronger wind pushes the auroral oval towards the equator and widens it
|
| 886 |
const a = cfg.aurora;
|
| 887 |
const colat = a.colat * THREE.MathUtils.clamp(1 + 0.13 * (k - 1), 0.88, 1.42);
|
|
|
|
| 891 |
|
| 892 |
function launchCME(x0) {
|
| 893 |
const o = state.objs; if (!o.cme) return;
|
| 894 |
+
const mp = state.cfg.mp;
|
| 895 |
+
// P = extra pressure carried by the cloud (in units of normal solar wind); W = thickness of the front
|
| 896 |
+
state.cme = { x: x0 ? x0 * mp : o.spawnX * 1.25, R: o.cme.userData.R, P: 3, W: mp * 0.45, passed: 0 };
|
| 897 |
o.cme.visible = true;
|
| 898 |
}
|
| 899 |
function updateCME(dt, tsec) {
|
| 900 |
const o = state.objs, c = state.cme;
|
|
|
|
| 901 |
if (!c) return;
|
| 902 |
const mp = state.cfg.mp;
|
| 903 |
c.x -= mp * 0.9 * dt;
|
| 904 |
+
// once the front is well past the planet, the cloud thins out and the magnetosphere recovers
|
| 905 |
+
if (c.x < -mp * 1.2) { c.passed += dt; c.P = 3 * Math.exp(-c.passed / 3.5); }
|
| 906 |
+
if (o.cme.visible) {
|
| 907 |
+
o.cme.position.set(c.x + o.cme.userData.R, 0, 0);
|
| 908 |
+
const u = o.cme.material.uniforms;
|
| 909 |
+
u.uTime.value = tsec;
|
| 910 |
+
// fade in after launch, fade out once it has swept past the planet
|
| 911 |
+
u.uFade.value = THREE.MathUtils.clamp((o.spawnX * 1.25 - c.x) / (mp * 0.6), 0, 1) * THREE.MathUtils.clamp((c.x - o.endX) / (mp * 2.5), 0, 1);
|
| 912 |
+
if (c.x < o.endX) o.cme.visible = false;
|
| 913 |
+
}
|
| 914 |
+
if (c.P < 0.01) { state.cme = null; o.cme.visible = false; }
|
| 915 |
}
|
| 916 |
|
| 917 |
function applyLayers() {
|
|
|
|
| 1038 |
state.time += dtHours;
|
| 1039 |
o.spin.rotation.y = (2 * Math.PI / cfg.period) * state.time;
|
| 1040 |
updateCME(dt, clock.elapsedTime);
|
| 1041 |
+
state.windK += (state.wind - state.windK) * Math.min(1, dt * 2.5);
|
|
|
|
| 1042 |
applyWind();
|
| 1043 |
const tsec = clock.elapsedTime;
|
| 1044 |
o.field.material.uniforms.uTime.value = tsec;
|
|
|
|
| 1097 |
mo.fx?.snapTail?.();
|
| 1098 |
},
|
| 1099 |
launchCME,
|
| 1100 |
+
windInfo() { const k = noseK(); return { k, boost: windBoost(), scale: scaleOf(k), cme: !!state.cme, kick: Math.max(0, (k - state.windK) / 3) }; },
|
| 1101 |
setWind(k, instant = false) { state.wind = THREE.MathUtils.clamp(k, 0.2, 5); if (instant) state.windK = state.wind; },
|
| 1102 |
view,
|
| 1103 |
refreshLabels,
|