import{COMMON_GLSL,NOISE_GLSL,WORLD_GLSL,LIGHTS_GLSL}from'../../vfx/common.js';import{REFRACT_GLSL}from'../../vfx/Refraction.js';// Frozen lightning lattice: one crystal (a hexagonal bipyramid) per bolt segment, built on the CPU// once per cast into world space. The vertex shader grows each crystal when the freeze front// reaches it (ease-out-back about its centre) and later throws it off the lattice (ballistic,// tumbling, shrinking, resting on the floor). The fragment shader is the ice family's look kept// cheap: flat facets, analytic-ish thickness, refraction of the opaque scene, Beer-Lambert tint,// Fresnel sky, rime, glints, plus the lightning trapped inside (a core line, pulses running along// the lattice, the freshly frozen front, and stress cracks before the shatter).exportconstCRYSTAL_VERT=COMMON_GLSL+/* glsl */`
in vec4 aChunk; // crystal centre xyz, freeze-front time (s)
in vec4 aAxis; // crystal axis xyz, radius m
in vec4 aInfo; // seed 0..1, half length m, kind (0 channel, 1 branch, 2 spur), -
in vec3 aVel; // shatter velocity m/s
uniform vec4 uFreeze; // front time now, grow time, shatter age (< 0: whole), shatter delay per front second
uniform vec4 uShatter; // spin rad/s, gravity, -, -
out vec3 vWorld;
out vec3 vRest; // undeformed, relative to the crystal centre (noise lives here)
out float vFront; // seconds since the front passed this crystal
out float vRadius;
out float vSeed;
flat out vec3 vCentre; // crystal centre and axis after the shatter motion (the trapped filament)
flat out vec3 vAxis;
flat out float vKind;
vec3 rotateAxis(vec3 p, vec3 k, float a) { float c = cos(a), s = sin(a); return p * c + cross(k, p) * s + k * dot(k, p) * (1.0 - c); }
void main() {
vec3 c = aChunk.xyz, p = position - c;
vRest = p; vRadius = aAxis.w; vSeed = aInfo.x; vKind = aInfo.z;
vec3 ax = aAxis.xyz;
vFront = uFreeze.x - aChunk.w;
float g = clamp(vFront / uFreeze.y, 0.0, 1.0), u = g - 1.0;
p *= max(1.0 + 2.70158 * u * u * u + 1.70158 * u * u, 0.0); // ease-out-back
float s = uFreeze.z - aChunk.w * uFreeze.w;
if (uFreeze.z >= 0.0 && s > 0.0) {
vec3 h = hash31(aInfo.x * 917.0 + 3.1);
vec3 k = normalize(h - 0.5 + vec3(1e-3));
float ang = uShatter.x * s * (0.4 + h.x);
p = rotateAxis(p, k, ang);
ax = rotateAxis(ax, k, ang);
p *= 1.0 - smoothstep(0.45, 1.25, s + 0.35 * h.y);
c += aVel * s;
c.y -= 0.5 * uShatter.y * s * s;
c.y = max(c.y, aAxis.w * 0.5);
}
vWorld = c + p;
vCentre = c; vAxis = ax;
gl_Position = projectionMatrix * viewMatrix * vec4(vWorld, 1.0);
}