Branchwell

Become a Pro / website · becomeapro.ai / src / components / home / particle-shaders.ts

Raw 3.8 KB on 5069836c1e4c4e369ebe86b13dc7d299c5bed8c0
1export const VERTEX_SHADER = /* glsl */ `
2 uniform float uTime, uMix, uPx, uVel, uDim, uHA, uHB, uBrain, uGal, uZoomA, uSpinB;
3 uniform vec2 uMouse; uniform vec3 uHintA, uHintB, uOffA, uOffB; uniform mat3 uRot;
4 attribute vec3 aFrom, aTo; attribute vec4 aRand; attribute float aAmb; attribute vec2 aShade;
5 varying vec3 vColor; varying float vAlpha; varying float vRot;
6 vec3 spinY(vec3 p, float a) { float c = cos(a), s = sin(a); return vec3(p.x * c + p.z * s, p.y, -p.x * s + p.z * c); }
7 void main() {
8 float m = uMix, body = 1.0 - aAmb;
9 vec3 p = aFrom;
10 if (aAmb < 0.5) {
11 // shapes are built around their own centre: rotate in place, then move to the section's spot
12 vec3 local = mix(aFrom * uZoomA, spinY(aTo, uSpinB), m);
13 p = uRot * local + mix(uOffA, uOffB, m);
14 }
15 // between sections the particles burst out toward the viewer, swirl, then settle into the next shape
16 float burst = pow(sin(m * 3.14159), 1.25) * body;
17 vec3 dir = normalize(vec3(aRand.x - 0.5, aRand.y - 0.5, aRand.z - 0.5) + 0.0001);
18 float sw = burst * (aRand.y - 0.5) * 2.4, cs = cos(sw), sn = sin(sw);
19 vec2 fl = vec2(dir.x * cs - dir.y * sn, dir.x * sn + dir.y * cs);
20 p += vec3(fl * (1.7 + aRand.w * 4.2), 0.55 + aRand.w * 3.6) * burst;
21 float t = uTime * (0.25 + aRand.y * 0.5);
22 p += vec3(sin(t + aRand.z * 20.0), cos(t * 1.3 + aRand.x * 20.0), sin(t * 0.7 + aRand.w * 20.0)) * (0.018 + aAmb * 0.4 + uVel * 0.18);
23 vec4 wp = modelMatrix * vec4(p, 1.0);
24 vec2 d = wp.xy - uMouse; float L = length(d);
25 if (L < 1.4) wp.xy += normalize(d + 0.0001) * pow(1.0 - L / 1.4, 2.0) * (1.0 - aAmb * 0.6);
26 vec4 mv = viewMatrix * wp;
27 gl_Position = projectionMatrix * mv;
28 float sz = (0.55 + aRand.w * 0.95) * uPx * (12.0 / -mv.z) * (1.0 + burst * 0.3);
29 sz *= mix(1.0, 0.78, uBrain * body) * mix(1.0, 0.55 + aShade.y * 0.6, uGal * body);
30 gl_PointSize = min(sz, uPx * 3.6);
31 // blue-to-white blend: deep blue, brand blue, ice, white
32 float g = clamp(0.48 + p.y * 0.09 + p.z * 0.06 + (aRand.y - 0.5) * 0.85, 0.0, 1.0);
33 g = mix(g, clamp(0.02 + aShade.x * 0.86 + p.y * 0.04 + (aRand.y - 0.5) * 0.22, 0.0, 1.0), uBrain * body);
34 g = mix(g, clamp(0.04 + aShade.y * 0.96 + (aRand.y - 0.5) * 0.14, 0.0, 1.0), uGal * body);
35 vec3 deep = vec3(0.07, 0.24, 0.62), brand = vec3(0.275, 0.667, 0.914), ice = vec3(0.75, 0.89, 0.98);
36 vec3 col = g < 0.4 ? mix(deep, brand, g / 0.4) : (g < 0.78 ? mix(brand, ice, (g - 0.4) / 0.38) : mix(ice, vec3(1.0), (g - 0.78) / 0.22));
37 float hint = mix(uHA, uHB, m) * step(0.72, aRand.z) * body;
38 col = mix(col, mix(uHintA, uHintB, m), hint);
39 if (aAmb > 0.5 && aRand.x > 0.93) col = aRand.w > 0.5 ? vec3(1.0) : vec3(0.75, 0.89, 0.98);
40 vColor = col;
41 float tw = 0.62 + 0.38 * sin(uTime * (0.7 + aRand.y * 2.2) + aRand.x * 40.0);
42 vAlpha = tw * mix(0.6, 0.42, aAmb) * uDim * mix(1.0, 0.5, burst);
43 // the far side of the brain fades so the folds facing you read clearly
44 vAlpha *= mix(1.0, mix(1.0, 0.14, clamp((-mv.z - 11.6) / 2.4, 0.0, 1.0)), uBrain * body);
45 vAlpha *= mix(1.0, 0.3 + aShade.y * 0.7, uGal * body);
46 vRot = aRand.x * 6.2832 + uTime * (aRand.z - 0.5) * 1.4;
47 }`;
48
49// Each point is drawn as a rotating outlined triangle
50export const FRAGMENT_SHADER = /* glsl */ `
51 varying vec3 vColor; varying float vAlpha; varying float vRot;
52 float sdTri(vec2 p, float r) {
53 const float k = 1.7320508; p.x = abs(p.x) - r; p.y = p.y + r / k;
54 if (p.x + k * p.y > 0.0) p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
55 p.x -= clamp(p.x, -2.0 * r, 0.0); return -length(p) * sign(p.y);
56 }
57 void main() {
58 vec2 p = gl_PointCoord * 2.0 - 1.0; p.y = -p.y;
59 float c = cos(vRot), s = sin(vRot); p = mat2(c, -s, s, c) * p;
60 float d = sdTri(p, 0.62);
61 float a = 1.0 - smoothstep(0.05, 0.19, abs(d));
62 if (a < 0.02) discard;
63 gl_FragColor = vec4(vColor, a * vAlpha);
64 }`;