// Eyes (their lives run on the CPU, in the site's shader_eyes.js):
// each opens from a line where it pleases, blinks and looks about, now
// and then at the pointer, then closes back to a line that fades.
// An almond of lids, a sclera, an iris with a darker ring and a pupil,
// and a glint. The shader only draws; openness, gaze and fade arrive
// per eye.
#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;
vec2 size; // pixels
// eye0-eye23: x, y, radius (half the eye's width, shorter side
// units, y down), openness 0-1
vec4 eye0;
vec4 eye1;
vec4 eye2;
vec4 eye3;
vec4 eye4;
vec4 eye5;
vec4 eye6;
vec4 eye7;
vec4 eye8;
vec4 eye9;
vec4 eye10;
vec4 eye11;
vec4 eye12;
vec4 eye13;
vec4 eye14;
vec4 eye15;
vec4 eye16;
vec4 eye17;
vec4 eye18;
vec4 eye19;
vec4 eye20;
vec4 eye21;
vec4 eye22;
vec4 eye23;
// look0-look23: gaze x, y (-1 to 1, the iris's full travel),
// shade 0-1, fade 0-1 (the whole eye, line included)
vec4 look0;
vec4 look1;
vec4 look2;
vec4 look3;
vec4 look4;
vec4 look5;
vec4 look6;
vec4 look7;
vec4 look8;
vec4 look9;
vec4 look10;
vec4 look11;
vec4 look12;
vec4 look13;
vec4 look14;
vec4 look15;
vec4 look16;
vec4 look17;
vec4 look18;
vec4 look19;
vec4 look20;
vec4 look21;
vec4 look22;
vec4 look23;
float count; // how many of eyes are in use
float breath; // near 1; the pupils widen a little as it falls
float grain; // 0-1, film grain
float dither; // 0-1, dither
float seed; // 0-1, changes each frame to move the grain
vec4 base; // the ground
vec4 deep; // the ground at the edges
vec4 ink;
vec4 tone;
};
const float UPPER = 0.55; // the open lids' heights, in radii
const float LOWER = 0.42;
float hash12(vec2 p)
{
vec3 p3 = fract(vec3(p.xyx) * 0.1031);
p3 += dot(p3, p3.yzx + 33.33);
return fract((p3.x + p3.y) * p3.z);
}
// Distance from q to a lid: the curve y = h (1 - x^2) for |x| <= 1,
// its corners beyond. Signed across the curve, > 0 below it.
float lid(vec2 q, float h)
{
float y = h * (1.0 - q.x * q.x);
float slope = -2.0 * h * q.x;
return (q.y - y) / sqrt(1.0 + slope * slope);
}
float stroke(vec2 q, float h, float width, float pe)
{
float d = abs(q.x) <= 1.0 ? abs(lid(q, h)) : length(q - vec2(sign(q.x), 0.0));
float x = clamp(q.x, -1.0, 1.0);
// Tapering to the corners, never under a pixel or so.
float w = max(0.6 * pe, width * (0.3 + 0.7 * (1.0 - x * x)));
return clamp((w - d) / pe + 0.5, 0.0, 1.0);
}
void main()
{
float px = 1.0 / min(size.x, size.y);
vec2 extent = size * px;
vec2 p = qt_TexCoord0 * extent;
// Gathered into arrays the loop can index; they arrive as separate
// uniforms because Qt's ShaderEffect has no array properties.
vec4 eyes[24] = vec4[24](eye0, eye1, eye2, eye3, eye4, eye5, eye6, eye7, eye8, eye9, eye10, eye11, eye12, eye13, eye14, eye15, eye16, eye17, eye18, eye19, eye20, eye21, eye22, eye23);
vec4 looks[24] = vec4[24](look0, look1, look2, look3, look4, look5, look6, look7, look8, look9, look10, look11, look12, look13, look14, look15, look16, look17, look18, look19, look20, look21, look22, look23);
float v = smoothstep(0.35, 1.25, length(p - 0.5 * extent));
vec3 colour = mix(base.rgb, deep.rgb, v);
// On a light ground the eyes are drawn in ink, with white sclerae;
// on a dark one their whites are the ink and the lines go to tone.
vec3 lum = vec3(0.2126, 0.7152, 0.0722);
bool lightGround = dot(base.rgb, lum) > dot(ink.rgb, lum);
vec3 sclera = lightGround ? mix(base.rgb, vec3(1.0), 0.7) : mix(ink.rgb, base.rgb, 0.12);
vec3 dark = lightGround ? ink.rgb : deep.rgb * 0.25;
vec3 line = lightGround ? ink.rgb : mix(ink.rgb, tone.rgb, 0.4);
for (int i = 0; i < 24; i++) {
if (float(i) >= count)
break;
vec4 e = eyes[i];
vec4 l = looks[i];
if (e.z <= 0.0 || l.w <= 0.0)
continue;
vec2 q = (p - e.xy) / e.z;
if (abs(q.x) > 1.3 || abs(q.y) > 1.0)
continue;
float pe = px / e.z; // a pixel, in this eye's units
float open = e.w;
// Between the lids: the upper above, the lower below.
float top = lid(q, -UPPER * open);
float bottom = -lid(q, LOWER * open);
float inside = clamp(min(top, bottom) / pe + 0.5, 0.0, 1.0) * clamp(open * 30.0, 0.0, 1.0);
if (inside > 0.0) {
// The white, shaded under the upper lid and into the corners.
vec3 c = sclera * (1.0 - 0.28 * exp(-max(top, 0.0) / 0.1)) * (1.0 - 0.18 * q.x * q.x);
vec2 ic = l.xy * vec2(0.42, 0.2);
vec2 r = q - ic;
float d = length(r);
float ri = 0.4;
float rp = 0.15 * (2.0 - breath);
float ang = atan(r.y, r.x);
vec3 iris = mix(tone.rgb, ink.rgb, 0.08 + 0.3 * l.z);
// Kept well darker than the white, whatever the theme.
iris *= clamp((dot(sclera, lum) - 0.3) / max(dot(iris, lum), 0.01), 0.35, 1.0);
float fibres = 0.84 + 0.16 * sin(ang * 23.0 + 4.0 * sin(ang * 7.0 + 6.2831853 * l.z));
vec3 ic3 = iris * mix(1.0, fibres, smoothstep(rp, ri, d));
ic3 *= 1.0 - 0.3 * smoothstep(0.6 * ri, ri, d);
ic3 += 0.18 * iris * exp(-pow((d - rp * 1.45) / 0.05, 2.0));
ic3 = mix(ic3, dark, 0.65 * smoothstep(ri - 0.08, ri, d));
ic3 *= 1.0 - 0.3 * exp(-max(top, 0.0) / 0.1);
c = mix(c, ic3, clamp((ri - d) / pe + 0.5, 0.0, 1.0));
c = mix(c, dark, clamp((rp - d) / pe + 0.5, 0.0, 1.0));
// A window's glint, up and to the left, and a faint one opposite.
float g = length(r - vec2(-0.13, -0.14));
float g2 = length(r - vec2(0.11, 0.1));
c = mix(c, vec3(1.0), 0.9 * clamp((0.055 - g) / pe + 0.5, 0.0, 1.0));
c = mix(c, vec3(1.0), 0.35 * clamp((0.025 - g2) / pe + 0.5, 0.0, 1.0));
colour = mix(colour, c, inside * l.w);
}
// The lids' edges, which meet in a line when shut; the upper heavier.
float w = 0.035;
float edge = max(stroke(q, -UPPER * open, w, pe), stroke(q, LOWER * open, 0.6 * w, pe));
// And a faint crease above the open eye.
vec2 qc = vec2(q.x / 0.85, q.y);
float crease = abs(qc.x) < 1.0 ? stroke(qc, -(UPPER + 0.16) * open, 0.4 * w, pe) * (1.0 - qc.x * qc.x) : 0.0;
colour = mix(colour, line, max(edge, 0.35 * open * crease) * l.w);
}
vec2 pixel = qt_TexCoord0 * size;
if (grain > 0.0)
colour += (hash12(pixel + seed * 1000.0) - 0.5) * 0.06 * grain;
if (dither > 0.0)
colour += (hash12(pixel + 0.5) + hash12(pixel + 91.3) - 1.0) / 255.0 * dither;
fragColor = vec4(colour, 1.0) * qt_Opacity;
}
neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
7.0 KiB · text
History
e569b6d