Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gugray

gugray/frag.glsl Secret

Created November 9, 2022 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gugray/003f8657310a093151da183a754c4e37 to your computer and use it in GitHub Desktop.
Save gugray/003f8657310a093151da183a754c4e37 to your computer and use it in GitHub Desktop.
#version 300 es
precision highp float;
uniform sampler2D u_img;
uniform sampler2D u_coords;
uniform sampler2D u_colors;
uniform vec2 u_canvas_sz;
uniform vec2 u_input_sz;
uniform vec4 u_frame;
uniform float u_seg_len;
uniform float u_rf;
out vec4 outColor;
#define PI 3.1415926538
#define PIFOURTH 0.7853981635
bool clrEq(vec3 a, vec3 b) {
float maxDiff = abs(a.r - b.r);
maxDiff = max(maxDiff, abs(a.g - b.g));
maxDiff = max(maxDiff, abs(a.b - b.b));
return maxDiff < 0.01;
}
vec3 getColor(vec2 pt) {
pt.y = u_canvas_sz.y - pt.y;
return texture(u_img, pt / u_canvas_sz).rgb;
}
void main() {
vec2 uv = gl_FragCoord.xy / u_input_sz;
vec4 txCoords = texture(u_coords, uv);
vec4 txColors = texture(u_colors, uv);
// Colors 1 and 2 in RGB 0..255
float r1 = floor(txColors.x / 256.);
float g1 = txColors.x - r1 * 256.;
float b1 = floor(txColors.y / 256.);
float r2 = txColors.y - b1 * 256.;
float g2 = floor(txColors.z / 256.);
float b2 = txColors.z - g2 * 256.;
vec3 clr1 = vec3(r1, g1, b1);
vec3 clr2 = vec3(r2, g2, b2);
clr1 /= 256.;
clr2 /= 256.;
vec2 pt1 = vec2(txCoords[0], txCoords[1]);
vec2 pt2 = vec2(txCoords[2], txCoords[3]);
vec2 delta = pt2 - pt1;
float len = length(delta);
float nSegs = max(2., floor(len / u_seg_len) + 1.);
// vec2 orto = vec2(-delta.y, delta.x) / len / u_rf;
vec2 orto = vec2(-delta.y, delta.x) / len * 1.8;
mat2 mDiag = mat2(cos(PIFOURTH), -sin(PIFOURTH), sin(PIFOURTH), cos(PIFOURTH));
vec2 diag1 = mDiag * (vec2(-delta.y, delta.x) / len * 1.8);
vec2 diag2 = vec2(-diag1.y, diag1.x);
vec2 pt, firstVisible, lastVisible;
bool gotFirstVisible = false;
for (float i = 0.; i <= nSegs + 0.5; i += 1.) {
pt = pt1 + delta * i / nSegs;
// if (pt.x < 0. || pt.x > u_canvas_sz.x) continue;
// if (pt.y < 0. || pt.y > u_canvas_sz.y) continue;
if (pt.x < u_frame.x || pt.x > u_frame.x + u_frame.z) continue;
if (pt.y < u_frame.y || pt.y > u_frame.y + u_frame.w) continue;
vec3 clrHere = getColor(pt);
bool isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
if (!isVisible) {
clrHere = getColor(pt + orto);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (!isVisible) {
clrHere = getColor(pt - orto);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (!isVisible) {
clrHere = getColor(pt + diag1);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (!isVisible) {
clrHere = getColor(pt - diag1);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (!isVisible) {
clrHere = getColor(pt + diag2);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (!isVisible) {
clrHere = getColor(pt - diag2);
isVisible = clrEq(clrHere, clr1);
if (!isVisible) isVisible = clrEq(clrHere, clr2);
}
if (isVisible) {
lastVisible = pt;
if (!gotFirstVisible) {
firstVisible = pt;
gotFirstVisible = true;
}
}
else if (gotFirstVisible) {
float visibleLen = length(lastVisible - firstVisible);
if (visibleLen > u_seg_len) break;
gotFirstVisible = false;
}
}
// // DBG: keep all inputs
// float fullLen = length(pt2 - pt1);
// if (fullLen > u_seg_len) outColor = vec4(pt1, pt2);
// else outColor = vec4(0.);
// return;
if (!gotFirstVisible) outColor = vec4(0.);
else {
float visibleLen = length(lastVisible - firstVisible);
if (visibleLen > u_seg_len) outColor = vec4(firstVisible, lastVisible);
else outColor = vec4(0.);
}
}
#version 300 es
precision highp float;
in vec2 a_position;
void main() {
gl_Position = vec4(a_position, 0, 1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment