Skip to content

Instantly share code, notes, and snippets.

@mrange

mrange/atari.glsl

Created Mar 3, 2021
Embed
What would you like to do?
atari df
#define RESOLUTION resolution
#define PI 3.141592654
#define TAU (2.0*TAU)
// IQ's polynomial min
float pmin(float a, float b, float k) {
float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0);
return mix(b, a, h) - k*h*(1.0-h);
}
float pmax(float a, float b, float k) {
return -pmin(-a, -b, k);
}
float pabs(float a, float k) {
return pmax(a, -a, k);
}
float circle(vec2 p, float r) {
return length(p) - r;
}
float isosceles(vec2 p, vec2 q) {
p.x = abs(p.x);
vec2 a = p - q*clamp( dot(p,q)/dot(q,q), 0.0, 1.0 );
vec2 b = p - q*vec2( clamp( p.x/q.x, 0.0, 1.0 ), 1.0 );
float s = -sign( q.y );
vec2 d = min( vec2( dot(a,a), s*(p.x*q.y-p.y*q.x) ),
vec2( dot(b,b), s*(p.y-q.y) ));
return -sqrt(d.x)*sign(d.y);
}
float box(vec2 p, vec2 b) {
vec2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
float horseshoe(vec2 p, vec2 c, float r, vec2 w) {
p.x = abs(p.x);
float l = length(p);
p = mat2(-c.x, c.y,
c.y, c.x)*p;
p = vec2((p.y>0.0)?p.x:l*sign(-c.x),
(p.x>0.0)?p.y:l );
p = vec2(p.x,abs(p.y-r))-w;
return length(max(p,0.0)) + min(0.0,max(p.x,p.y));
}
float segment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h );
}
float atari_a(inout vec2 p, vec2 off) {
p -= vec2(0.275, 0.0);
float d0 = isosceles(p*vec2(1.0, -1.0)-vec2(0.0, -0.225), vec2(0.20, 0.65))-0.1;
float d1 = isosceles(p*vec2(1.0, -1.0)-vec2(0.0, -0.18), vec2(0.13, 0.55))-0.005;
float d2 = box(p-vec2(0.0, -0.135), vec2(0.15, 0.06));
float d3 = p.y+0.325;
float d = d0;
d = max(d, -d1);
d = pmin(d, d2, 0.0125);
d = pmax(d, -d3, 0.0125);
p -= vec2(0.275, 0.0) + off;
return d;
}
float atari_i(inout vec2 p, vec2 off) {
p -= vec2(0.07, 0.0);
float d0 = box(p, vec2(0.07, 0.325)-0.0125)-0.0125;
float d = d0;
p -= vec2(0.07, 0.0) + off;
return d;
}
float atari_r(inout vec2 p, vec2 off) {
p -= vec2(0.22, 0.0);
float d0 = p.y+0.325;
float d1 = box(p - vec2(-0.12, -0.075), vec2(0.0, 0.30))-0.1;
const float a = PI/2;
const vec2 c = vec2(cos(a), sin(a));
vec2 hp = p;
hp -= vec2(0.0, 0.14);
hp.xy = -hp.yx;
float d2 = horseshoe(hp, c, 0.125, 0.2175*vec2(0.5,0.275));
float d3 = segment(p-vec2(-0.015, 0.005), vec2(0.0), vec2(0.22, -0.4))-0.07;
float d4 = box(p-vec2(-0.05, -0.152), vec2(0.03, 0.35))-0.0075;
float d = d1;
d = min(d, d2);
d = max(d, -d4);
d = min(d, d3);
d = pmax(d, -d0, 0.0125);
p -= vec2(0.25, 0.0)+off;
return d;
}
float atari_t(inout vec2 p, vec2 off) {
p -= vec2(0.195, 0.0);
float d0 = box(p - vec2(0.0, 0.265), vec2(0.195, 0.06)-0.0125)-0.0125;
float d1 = box(p - vec2(0.0, -0.03), vec2(0.07, 0.295)-0.0125)-0.0125;
float d = d0;
d = pmin(d, d1, 0.0125);
p -= vec2(0.195, 0.0) + off;
return d;
}
float atari_text(vec2 p) {
p -= vec2(-1.0, 0.0);
float d = 1E6;
d = min(d, atari_a(p, vec2(-0.075, 0.0)));
d = min(d, atari_t(p, vec2(-0.055, 0.0)));
d = min(d, atari_a(p, vec2(0.02, 0.0)));
d = min(d, atari_r(p, vec2(0.01, 0.0)));
d = min(d, atari_i(p, vec2(0.0, 0.0)));
return d;
}
float df(vec2 p) {
const float z = 0.5;
float da = atari_text(p/z)*z;
return da;
}
void main(void) {
vec2 q = inData.v_texcoord;
vec2 p = -1. + 2. * q;
p.x *= RESOLUTION.x/RESOLUTION.y;
float aa = 2.0/RESOLUTION.y;
float d = df(p);
vec2 tp = p;
tp -= vec2(-0.04, 0.21);
tp *= 1;
tp.y *= -1;
tp += 0.5;
vec4 tcol = texture(texture0, tp);
vec3 col = vec3(0.1);
// col = mix(col, tcol.xyz, tcol.w);
vec3 acol = mix(col, vec3(1.0), 0.5);
col = mix(col, acol, smoothstep(-aa, aa, -d));
col += vec3(0.5, 0.0, 0.0)*sin(300*d) ;
fragColor = vec4(col, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment