Skip to content

Instantly share code, notes, and snippets.

@gvergnaud
Last active October 26, 2019 08:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvergnaud/6ed97e9780df4f51545ff353b950ee8b to your computer and use it in GitHub Desktop.
Save gvergnaud/6ed97e9780df4f51545ff353b950ee8b to your computer and use it in GitHub Desktop.
// Shaping functions
float impulse(float k, float x){
float h = k * x;
return h * exp(1.0 - h);
}
float parabola(float x, float k){
return pow(4.0 * x * (1.0 - x), k);
}
float cubicPulse(float c, float w, float x){
x = abs(x - c);
if(x > w) return 0.0;
x /= w;
return 1.0 - x * x * (3.0 - 2.0 * x);
}
float pcurve(float x, float a, float b){
float k = pow(a + b,a + b) / (pow(a, a) * pow(b, b));
return k * pow(x, a) * pow(1.0 - x, b);
}
// Shapes
float box(in vec2 _st, in vec2 _size){
_size = vec2(0.5) - _size * 0.5;
vec2 uv = smoothstep(
_size,
_size + vec2(0.001),
_st
);
uv *= smoothstep(
_size,
_size + vec2(0.001),
vec2(1.0) - _st
);
return uv.x * uv.y;
}
float cross(in vec2 _st, float _size){
return (
box(_st, vec2(_size,_size/4.)) +
box(_st, vec2(_size/4.,_size))
);
}
float circle(vec2 st, float radius) {
return 1. -smoothstep(radius - .005,radius + .005, length(st));
}
float circleOutline(vec2 st, float radius, float border) {
return circle(st, radius) - circle(st, radius - border);
}
float fadingCircle(vec2 st, float radius, float border) {
return circle(st, radius) * (atan(st.y, st.x)) / PI;
}
float polygon(vec2 st, int n, float size) {
vec2 uv = st * 2. - 1.;
float a = atan(uv.x, uv.y) + PI;
float r = TWO_PI / float(n);
float d = cos(floor(.5 + a / r) * r - a) * length(uv);
return 1.0 - smoothstep(size,size + .01, d);
}
// Transformations
vec2 rotate2d(vec2 st, float angle) {
return (
(st - vec2(.5)) *
mat2(
cos(angle), -sin(angle),
sin(angle), cos(angle)
) +
vec2(.5)
);
}
vec2 scale(vec2 st, vec2 s) {
return st * s;
}
vec2 translate(vec2 st, vec2 t) {
return st + t;
}
vec2 skewY(vec2 st, float angle) {
return (
(st - vec2(.5)) *
mat2(
1., 0.,
tan(angle), 1.
) +
vec2(.5)
);
}
// Misc
void apply(vec4 newColor, out vec4 color) {
color = mix(color, newColor, newColor.w);
}
float map(float x, float a1, float a2, float b1, float b2) {
return b1 + (x - a1) * (b2 - b1) / (a2 - a1);
}
float random (in vec2 st) {
return fract(
sin(
dot(st.xy, vec2(12.9898,78.233))
) * 43758.5453123
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment