drawToScreen = regl({
frag: `
precision highp float;
#extension GL_OES_standard_derivatives : enable
uniform sampler2D src;
uniform float pixelRatio, maxRadius;
uniform vec2 resolution, activePoint;
// Draw contour screen-width grid lines of the distance field
float gridFactor (float parameter, float width, float feather) {
float w1 = width - feather * 0.5;
float d = length(vec2(dFdx(parameter), dFdy(parameter)));
float looped = 0.5 - abs(mod(parameter, 1.0) - 0.5);
return smoothstep(d * (w1 + feather), d * w1, looped);
}
${unpackColor}
${unpackPosition}
void main () {
vec4 packedPositionAndColor = texture2D(src, gl_FragCoord.xy / resolution);
vec3 color = unpackColor(packedPositionAndColor);
vec2 position = unpackPosition(packedPositionAndColor);
float dist = distance(position, gl_FragCoord.xy);
float grid = 0.03 * gridFactor(log2(dist), 0.5 * pixelRatio, 1.0);
//grid += 0.05 * gridFactor(log2(dist) * 8.0, 0.5 * pixelRatio, 1.0);
vec3 baseColor = unpackColor(packedPositionAndColor);
// highlight if it matches the point hovered over
vec4 activeValue = texture2D(src, vec2(0, 1) + vec2(1, -1) * activePoint / resolution);
vec2 activePosition = unpackPosition(activeValue);
vec3 activeColor = unpackColor(activeValue);
if (activePoint.x >= 0.0 && activePosition != vec2(0) && distance(activeColor, baseColor) < 1e-4) {
baseColor = vec3(1, 0.4, 0.2);
}
// Darken by a grid
vec3 c = mix(baseColor, vec3(0), grid);
// Darken the central point
if (maxRadius > 5.0) {
c *= smoothstep(1.0, 2.0, dist / pixelRatio);
}
// Hide the outside if clipping to a circular region
c = mix(vec3(1), c, smoothstep(maxRadius, maxRadius - 1.0, dist));
// Background is white
if (position == vec2(0)) c = vec3(1);
gl_FragColor = vec4(c, 1);
}`,
uniforms: {
src: regl.prop('src'),
pixelRatio: regl.context('pixelRatio'),
maxRadius: (ctx, props) =>
props.circularCutoff ? Math.pow(2, selectedPassCount) : 100000,
activePoint: regl.prop('activePoint')
}
})