Skip to content

Instantly share code, notes, and snippets.

@mrange

mrange/experiment.glsl

Last active Mar 21, 2021
Embed
What would you like to do?
experimenting
#define RESOLUTION resolution
#define SCA(x) vec2(sin(x), cos(x))
#define PI pi
#define TAU tau
#define TIME time
#define TTIME (TAU*TIME)
#define RESOLUTION resolution
#define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a))
#define PSIN(a) (0.5+0.5*sin(a))
#define L2(x) dot(x, x)
const float pi = acos(-1.0);
const float tau = 2.0*pi;
float hash(float co) {
co += 1;
return fract(sin(co*12.9898) * 13758.5453);
}
// IQ's smooth 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);
}
vec2 mod2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5,size) - size*0.5;
return c;
}
float modPolar(inout vec2 p, float repetitions) {
float angle = TAU/repetitions;
float a = atan(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = mod(a,angle) - angle/2.;
p = vec2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2)) c = abs(c);
return c;
}
float circle(vec2 p, float r) {
return length(p) - r;
}
float df(vec2 p) {
const float[] rs = float[](0.35, 0.1 , 0.05);
const float[] os = float[](2.0 , 0.85 , 0.2);
const float[] ns = float[](3.0 , 5.0 , 5.0);
const float[] sms = float[](0.1 , 0.025 , 0.0125);
vec2 pp = p;
float np = 0;
float hh = 0.5;
float sm = 0.1;
float d = 1E6;
float a = TIME*0.125;
d = min(d, circle(pp, 1.0));
for (int i = 0; i < 3; ++i) {
float r = rs[i];
float o = os[i];
float n = ns[i];
n = i == 2 ? round(5*fract(hh*137)+1.0) : n;
pp *= ROT(a);
np = modPolar(pp, n);
hh = hash(np);
a += (hh-0.5)*TIME*1;
pp.x -= o;
float dd = circle(pp, r);
sm = dd < d ? sms[i] : sm;
d = pmax(d, -dd, sm);
d = min(d, dd);
}
return d;
}
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 z = 0.5;
float d = df(p/z)*z;
vec3 col = vec3(0.1);
col = mix(col, vec3(1.0), smoothstep(-aa, aa, -d));
fragColor = vec4(col, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment