nova-shell/modules/hex_wave.frag

93 lines
2.5 KiB
GLSL

#version 440
layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;
layout(std140, binding = 0) uniform buf {
mat4 qt_Matrix;
float qt_Opacity;
float uSize;
float uWavePhase;
vec4 uResolution;
vec4 uC0;
vec4 uC1;
vec4 uC2;
};
float sdHexagon(vec2 p, float r) {
const vec3 k = vec3(-0.866025404, 0.5, 0.577350269);
p = abs(p);
p -= 2.0 * min(dot(k.xy, p), 0.0) * k.xy;
p -= vec2(clamp(p.x, -k.z * r, k.z * r), r);
return length(p) * sign(p.y);
}
// Rainbow from angle — color picker style
vec3 hsv2rgb(float h) {
return clamp(abs(mod(h * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
}
void main() {
vec2 res = uResolution.xy;
vec2 frag = qt_TexCoord0 * res;
float dx = uSize * 1.5;
float dy = uSize * 1.7320508;
float col = round(frag.x / dx);
float yoff = mod(col, 2.0) != 0.0 ? dy * 0.5 : 0.0;
float row = round((frag.y - yoff) / dy);
vec2 center = vec2(col * dx, row * dy + yoff);
// Wave factor
float dist = center.x - uWavePhase;
float wf = exp(-dist * dist / 9000.0);
float baseR = uSize * 0.48;
float inradius = baseR * (1.0 + 0.35 * wf);
vec2 p = frag - center;
float d = sdHexagon(p.yx, inradius); // swap for flat-top
if (d > 0.0) {
fragColor = vec4(0.0);
return;
}
// Gradient color
float fx = clamp(center.x / res.x, 0.0, 1.0);
vec3 rgb = fx < 0.5
? mix(uC0.rgb, uC1.rgb, fx * 2.0)
: mix(uC1.rgb, uC2.rgb, (fx - 0.5) * 2.0);
// Alpha from distance to center
float fy = clamp(center.y / res.y, 0.0, 1.0);
float dc = length(vec2(fx - 0.5, fy - 0.5));
float a = 0.03 + dc * 0.06;
// Wave brighten
rgb = min(rgb + vec3(0.3 * wf), vec3(1.0));
a += 0.12 * wf;
// Rainbow shimmer on hex edges when wave passes
float edgeWidth = 2.0;
float edgeFactor = smoothstep(-edgeWidth, 0.0, d); // 0 at interior, 1 at edge
if (wf > 0.01 && edgeFactor > 0.0) {
// Angle around hex center → hue
float angle = atan(p.y, p.x);
float hue = (angle + 3.14159) / 6.28318;
// Shift hue by position so neighboring hexes have different phase
hue = fract(hue + center.x * 0.003 + center.y * 0.005);
vec3 rainbow = hsv2rgb(hue);
float shimmer = edgeFactor * wf;
rgb = mix(rgb, rainbow, shimmer * 0.8);
a = mix(a, 0.5, shimmer * 0.6);
}
// Anti-alias outer edge
float aa = 1.0 - smoothstep(-1.0, 0.0, d);
a *= aa;
fragColor = vec4(rgb * a, a) * qt_Opacity;
}