`
uniform vec4 uLook; // size, glow, shutter (s), arcane share
uniform vec4 uShape; // master fade, shell curvature 0..1, overlap dimming, share leaving a flake
uniform vec4 uTarget;
uniform vec3 uArcane; // luminance-1 magenta
${KELVIN_GLSL}
// Stefan-Boltzmann, with the Draper point: nothing visible below ~800 K
float emberI(float T) { float r = T / 1500.0; r *= r; return r * r * smoothstep(760.0, 980.0, T); }
float emberFade(float st, float age) { return uShape.x * (st < 1.5 ? smoothstep(0.0, 0.5, age) : 1.0); }
// slow breathing of the burn (the fast tumbling is per pixel along the streak)
float emberBreath(vec4 e) { return 0.78 + 0.22 * sin(uTime * mix(2.0, 7.0, e.z) + e.w * 40.0); }
vec3 swarmLight(int idx, vec4 h, vec3 p, vec3 v, float st, float age) {
float T = emberKelvin(idx, v, st, age);
return blackbody(T) * emberI(T) * emberFade(st, age) * uLook.y;
}
`
;
// a long-exposure streak: the ember's real path over the shutter (bent by its orbit), with its
// temperature on the head and its tumbling drawn along the length as dashes. x: -1 head cap,
// 0..1 along the path, 2 tail cap; y: -1..1 across.
export
const
STREAK_VERT
=
(
W
)
=>
/* glsl */
`
precision highp float;
precision highp int;
precision highp sampler2D;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;
in vec3 position;
${COMMON_GLSL}${NOISE_GLSL}${SWARM_STATE_GLSL(W)}${EMBER_LOOK}
out vec3 vUvC; // s along, y across, cap -1..1
out vec3 vCol;
out vec3 vCore;
out vec3 vTumble; // phase at the head, radians over the streak, depth
void main() {
int idx = gl_InstanceID;
ivec2 px = swarmTexel(idx);
vec4 s0 = texelFetch(uS0, px, 0), s1 = texelFetch(uS1, px, 0);
gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
vUvC = vec3(0.0); vCol = vCore = vTumble = vec3(0.0);
float st = s1.w;
if (st < 0.5 || st > 4.5) return;
float T = emberKelvin(idx, s1.xyz, st, s0.w);
float I = emberI(T) * emberFade(st, s0.w);
if (I < 1e-3) return;
vec4 e = emberHash(idx);
vec3 p = s0.xyz, v = (st > 1.5 && st < 2.5) || st > 3.5 ? vec3(0.0) : s1.xyz;
float tau = uLook.z * mix(0.6, 1.4, e.y);
// the path backwards over the shutter: x(-t) = p - v t + a t^2 / 2, a = the orbit's centripetal
vec3 a = vec3(0.0);
float sp2 = dot(v, v);
if (st < 1.5 && sp2 > 0.01) {
vec3 rr = p - uTarget.xyz;
vec3 q = rr - v * (dot(rr, v) / sp2);
float q2 = dot(q, q);
a = -q * (min(sp2 / max(q2, 0.09), 60.0)) * uShape.y * (1.0 - smoothstep(4.0, 9.0, dot(rr, rr)));
}
// overlap: thousands of lines in one place must keep their structure, so the dense parts run dimmer
float dens = st < 1.5 ? uShape.z * mix(1.0, 0.14, uShape.y * (1.0 - smoothstep(1.15, 1.7, length(p - uTarget.xyz) / uTarget.w))) : mix(0.35, 1.0, smoothstep(0.0, 0.5, s0.w));
float s = clamp(position.x, 0.0, 1.0), cap = position.x < 0.0 ? -1.0 : position.x > 1.0 ? 1.0 : 0.0;
float ts = tau * s;
vec3 P = p - v * ts + a * (0.5 * ts * ts), dP = (a * ts - v) * tau;
vec4 Pv = viewMatrix * vec4(P, 1.0);
Pv.z += 0.02;
vec2 tv = (mat3(viewMatrix) * dP).xy;
float len = length(tv);
vec2 dir = len > 1e-5 ? tv / len : vec2(1.0, 0.0), perp = vec2(-dir.y, dir.x);
float viewZ = max(-Pv.z, 0.05);
float pxWorld = 2.0 * viewZ / (projectionMatrix[1][1] * uResolution.y);
float breath = emberBreath(e);
// sizes run a power law, as in wildfire ember showers: mostly grains, a few fat burning flakes
float big = e.z * e.z * e.z;
float size = uLook.x * 0.0052 * mix(0.55, 2.2, big) * (0.7 + 0.3 * breath); // mean and mean-square width at or under a uniform spread: no extra fill
float w = max(size, 1.05 * pxWorld), aa = size / w;
// it never flies a ruled line: a small wander grows down the tail
Pv.xy += perp * (0.035 * len * s * sin(s * (3.0 + 9.0 * e.x) + e.y * 40.0 + uTime * 3.0));
Pv.xy += dir * cap * w + perp * position.y * w;
gl_Position = projectionMatrix * Pv;
// the same energy spread down the streak, a little held back so long streaks still read
// (a sub-pixel ember keeps more than its area: seen from across the arena it is a point of light
// that blooms, not a dim smear)
float k = pow(2.0 * w / (len + 2.0 * w), 0.55) * pow(aa, 0.4) * smoothstep(0.25, 0.8, viewZ) * breath * uLook.y * I * dens;
// the hottest cores whiten; for the arcane touch one ember in eight (once it burns properly) runs
// magenta, so the swarm is shot through with it instead of tinted pink (the hot ones carry most
// of the light: they stay blackbody)
// the glow is the ember's own blackbody colour; the core reads a step hotter (yellow, as a camera
// sees a glowing ember), never white
float hot = smoothstep(uHeat.x * 1.1, uHeat.x * 1.4, T);
float arc = uLook.w * 1.5 * step(0.875, e.w) * smoothstep(1050.0, 1350.0, T);
vCol = mix(blackbody(T), uArcane, min(arc, 0.9)) * k;
vCore = mix(blackbody(T * 1.3), uArcane, min(arc, 0.7)) * k * (0.4 + 0.7 * hot);
float om = mix(60.0, 220.0, e.z);
vUvC = vec3(s, position.y, cap);
vTumble = vec3(om * uTime + e.w * 80.0, -om * tau, e.w > 0.55 ? 0.72 : 0.18);
}`