Files
snippets/glsl.snippets
2025-03-21 11:40:50 +01:00

241 lines
6.6 KiB
Plaintext

snippet glsl1 "glsl template 1" b
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define PI 3.14159265359
const vec3 uAColor = vec3(.5,.5,.5);
const vec3 uBColor = vec3(.5,.5,.5);
const vec3 uCColor = vec3(1.,1.,1.);
const vec3 uDColor = vec3(.0,.33,.67);
//get colors from http://dev.thi.ng/gradients/
vec3 cosPalette(float t) { return uAColor + uBColor*cos(6.28318*(uCColor*t+uDColor)); }
void main() {
vec2 st = (gl_FragCoord.xy*2. - u_resolution.xy)/u_resolution.y;
vec3 color = vec3(0.0,0.0,0.0);
float alpha = 1.0;
$0
gl_FragColor = vec4(color, alpha);
}
endsnippet
snippet fn_dot_circle "function to generate a circle using dotproduct" b
float circle(in vec2 st, in float radius){
vec2 dist = st;
return 1.-smoothstep(radius-(radius*.01),
radius+(radius*.01),
dot(st,st)*4.);
}
endsnippet
snippet fn_stroke "function to draw a stroke/line using step" b
float stroke(in float x_coor, float s, float width){
float d = step(s,x_coor+width*.5)-step(s,x_coor-width*.5);
return clamp(d, 0.,1.);
}
endsnippet
snippet fn_circle "signed distance field circle function" b
float circleSDF(vec2 st) { return length(st); }
endsnippet
snippet fn_fill "fills idk... should really have more experience with this one" b
float fill(float x, float size) { return 1.-step(size, x); }
endsnippet
snippet fn_rect "signed distance field rectangle function" b
float rectSDF(vec2 st, vec2 s) {
return max(abs(st.x/s.x),abs(st.y/s.y));
}
endsnippet
snippet fn_colorpalette "cos color pallete function" b
vec3 cosPalette(float t, vec3 uAColor, vec3 uBColor, vec3 uCColor, vec3 uDColor) { return uAColor + uBColor*cos(6.28318*(uCColor*t+uDColor)); }
endsnippet
snippet fn_cross "signed distance field cross function" b
float crossSDF(vec2 st, float s) {
vec2 size = vec2(.25, s);
float a = max(abs(st.x/size.x),abs(st.y/size.y));
float b = max(abs(st.x/size.y),abs(st.y/size.x));
return min(a,b);
}
endsnippet
snippet fn_ndot "negative dotproduct" b
// via https://iquilezles.org/articles/distfunctions2d/
float ndot(vec2 a, vec2 b) {return a.x*b.x - a.y*b.y;}
endsnippet
snippet fn_rhomb "rhombus sdf function, like a diamond shape" b
float rhombSDF(vec2 st) {
vec2 st1 = (st)*2.;
vec2 st2 = (vec2(st.x,0.-st.y)*2.-1.)*2.;
float triangle1 = max(abs(st1.x)*.866025+st1.y*.5,-st1.y*.5);
float triangle2 = max(abs(st2.x)*.866025+st2.y*.5,-st2.y*.5);
return max(triangle1, triangle2);
}
endsnippet
snippet fn_flip "flips values in v compared to pct" b
float flip(float v, float pct) { return mix(v, 1.-v, pct); }
endsnippet
snippet fn_vesica "sdf for vesica shape (like a leaf)" b
float vesicaSDF(vec2 st, float w) {
vec2 offset1 = st-vec2(w,.0);
vec2 offset2 = st+vec2(w,.0);
float circle1 = length(offset1);
float circle2 = length(offset2);
return max(circle1, circle2);
}
endsnippet
snippet fn_tri "sdf for triangle shape" b
float triSDF(vec2 st) {
st = st*2.;
return max(abs(st.x)*.866025+st.y*.5,-st.y*.5);
}
endsnippet
snippet fn_rot "rotate around angle (in radians?)" b
vec2 rotate(vec2 st, float a) {
return mat2(cos(a),-sin(a),sin(a),cos(a))*(st);
}
endsnippet
snippet fn_pnoise2 "2d perlin noise fn via stegu/webgl-noise" b
//2d perlin noise fn via stegu/webgl-noise
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+10.0)*x);}
vec4 taylorInvSqrt(vec4 r){ return 1.79284291400159 - 0.85373472095314 * r; }
vec2 fade(vec2 t) { return t*t*t*(t*(t*6.0-15.0)+10.0); }
// Classic Perlin noise
float cnoise(vec2 P) {
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
// Classic Perlin noise, periodic variant
float pnoise(vec2 P, vec2 rep) {
vec4 Pi = floor(P.xyxy) + vec4(0.0, 0.0, 1.0, 1.0);
vec4 Pf = fract(P.xyxy) - vec4(0.0, 0.0, 1.0, 1.0);
Pi = mod(Pi, rep.xyxy); // To create noise with explicit period
Pi = mod289(Pi); // To avoid truncation effects in permutation
vec4 ix = Pi.xzxz;
vec4 iy = Pi.yyww;
vec4 fx = Pf.xzxz;
vec4 fy = Pf.yyww;
vec4 i = permute(permute(ix) + iy);
vec4 gx = fract(i * (1.0 / 41.0)) * 2.0 - 1.0 ;
vec4 gy = abs(gx) - 0.5 ;
vec4 tx = floor(gx + 0.5);
gx = gx - tx;
vec2 g00 = vec2(gx.x,gy.x);
vec2 g10 = vec2(gx.y,gy.y);
vec2 g01 = vec2(gx.z,gy.z);
vec2 g11 = vec2(gx.w,gy.w);
vec4 norm = taylorInvSqrt(vec4(dot(g00, g00), dot(g01, g01), dot(g10, g10), dot(g11, g11)));
g00 *= norm.x;
g01 *= norm.y;
g10 *= norm.z;
g11 *= norm.w;
float n00 = dot(g00, vec2(fx.x, fy.x));
float n10 = dot(g10, vec2(fx.y, fy.y));
float n01 = dot(g01, vec2(fx.z, fy.z));
float n11 = dot(g11, vec2(fx.w, fy.w));
vec2 fade_xy = fade(Pf.xy);
vec2 n_x = mix(vec2(n00, n01), vec2(n10, n11), fade_xy.x);
float n_xy = mix(n_x.x, n_x.y, fade_xy.y);
return 2.3 * n_xy;
}
endsnippet
snippet fn_poly "sdf function to create a polygon" b
float polySDF(vec2 st, float angles) {
float a = atan(st.x,st.y)+PI;
float r = length(st);
float v = (2.*PI)/angles;
return cos(floor(.5+a/v)*v-a)*r;
}
endsnippet
snippet fn_hex "sdf hex function" b
float hexSDF(vec2 st) {
st = abs(st);
return max(abs(st.y),st.x*.866025+st.y*.5);
}
endsnippet
snippet fn_scale "scale function" b
vec2 scale(vec2 st, vec2 scale) { return mat2(scale.x,.0,.0,scale.y)*st; }
endsnippet
snippet fn_tile "tiles the screenspace" b
vec2 tile(vec2 st, float tiles) {return fract(st*tiles)-.5; }
endsnippet
snippet fn_star "star sdf function" b
float starSDF(vec2 st, float V, float s) {
st = st*2.;
float a = atan(st.y, st.x)/(2.*3.14159265359);
float seg = a * V;
a = ((floor(seg) + .5)/V + mix(s, -s,step(.5,fract(seg)))) * (2.*3.14159265359);
return abs(dot(vec2(cos(a),sin(a)),st));
}
endsnippet