Skip to content

Instantly share code, notes, and snippets.

@jbeda
Last active June 15, 2019 23:39
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 jbeda/7ba3e53985b5e17abac07def8d9b1276 to your computer and use it in GitHub Desktop.
Save jbeda/7ba3e53985b5e17abac07def8d9b1276 to your computer and use it in GitHub Desktop.
Shader Functions
// Robert Penner's easing functions in GLSL
// https://github.com/stackgl/glsl-easings
float linear(float t) {
return t;
}
float exponentialIn(float t) {
return t == 0.0 ? t : pow(2.0, 10.0 * (t - 1.0));
}
float exponentialOut(float t) {
return t == 1.0 ? t : 1.0 - pow(2.0, -10.0 * t);
}
float exponentialInOut(float t) {
return t == 0.0 || t == 1.0
? t
: t < 0.5
? +0.5 * pow(2.0, (20.0 * t) - 10.0)
: -0.5 * pow(2.0, 10.0 - (t * 20.0)) + 1.0;
}
float sineIn(float t) {
return sin((t - 1.0) * HALF_PI) + 1.0;
}
float sineOut(float t) {
return sin(t * HALF_PI);
}
float sineInOut(float t) {
return -0.5 * (cos(PI * t) - 1.0);
}
float qinticIn(float t) {
return pow(t, 5.0);
}
float qinticOut(float t) {
return 1.0 - (pow(t - 1.0, 5.0));
}
float qinticInOut(float t) {
return t < 0.5
? +16.0 * pow(t, 5.0)
: -0.5 * pow(2.0 * t - 2.0, 5.0) + 1.0;
}
float quarticIn(float t) {
return pow(t, 4.0);
}
float quarticOut(float t) {
return pow(t - 1.0, 3.0) * (1.0 - t) + 1.0;
}
float quarticInOut(float t) {
return t < 0.5
? +8.0 * pow(t, 4.0)
: -8.0 * pow(t - 1.0, 4.0) + 1.0;
}
float quadraticInOut(float t) {
float p = 2.0 * t * t;
return t < 0.5 ? p : -p + (4.0 * t) - 1.0;
}
float quadraticIn(float t) {
return t * t;
}
float quadraticOut(float t) {
return -t * (t - 2.0);
}
float cubicIn(float t) {
return t * t * t;
}
float cubicOut(float t) {
float f = t - 1.0;
return f * f * f + 1.0;
}
float cubicInOut(float t) {
return t < 0.5
? 4.0 * t * t * t
: 0.5 * pow(2.0 * t - 2.0, 3.0) + 1.0;
}
float elasticIn(float t) {
return sin(13.0 * t * HALF_PI) * pow(2.0, 10.0 * (t - 1.0));
}
float elasticOut(float t) {
return sin(-13.0 * (t + 1.0) * HALF_PI) * pow(2.0, -10.0 * t) + 1.0;
}
float elasticInOut(float t) {
return t < 0.5
? 0.5 * sin(+13.0 * HALF_PI * 2.0 * t) * pow(2.0, 10.0 * (2.0 * t - 1.0))
: 0.5 * sin(-13.0 * HALF_PI * ((2.0 * t - 1.0) + 1.0)) * pow(2.0, -10.0 * (2.0 * t - 1.0)) + 1.0;
}
float circularIn(float t) {
return 1.0 - sqrt(1.0 - t * t);
}
float circularOut(float t) {
return sqrt((2.0 - t) * t);
}
float circularInOut(float t) {
return t < 0.5
? 0.5 * (1.0 - sqrt(1.0 - 4.0 * t * t))
: 0.5 * (sqrt((3.0 - 2.0 * t) * (2.0 * t - 1.0)) + 1.0);
}
float bounceOut(float t) {
const float a = 4.0 / 11.0;
const float b = 8.0 / 11.0;
const float c = 9.0 / 10.0;
const float ca = 4356.0 / 361.0;
const float cb = 35442.0 / 1805.0;
const float cc = 16061.0 / 1805.0;
float t2 = t * t;
return t < a
? 7.5625 * t2
: t < b
? 9.075 * t2 - 9.9 * t + 3.4
: t < c
? ca * t2 - cb * t + cc
: 10.8 * t * t - 20.52 * t + 10.72;
}
float bounceIn(float t) {
return 1.0 - bounceOut(1.0 - t);
}
float bounceInOut(float t) {
return t < 0.5
? 0.5 * (1.0 - bounceOut(1.0 - t * 2.0))
: 0.5 * bounceOut(t * 2.0 - 1.0) + 0.5;
}
float backIn(float t) {
return pow(t, 3.0) - t * sin(t * PI);
}
float backOut(float t) {
float f = 1.0 - t;
return 1.0 - (pow(f, 3.0) - f * sin(f * PI));
}
float backInOut(float t) {
float f = t < 0.5
? 2.0 * t
: 1.0 - (2.0 * t - 1.0);
float g = pow(f, 3.0) - f * sin(f * PI);
return t < 0.5
? 0.5 * g
: 0.5 * (1.0 - g) + 0.5;
}
float box(in vec2 _st, in vec2 _sizein float _softness){
_size = vec2(0.5)-_size*0.5;
vec2 uv = smoothstep(_size,_size+vec2(_softness),_st);
uv *= smoothstep(_size,_size+vec2(_softness),vec2(1.0)-_st);
return uv.x*uv.y;
}
float circle(in vec2 _st, in float _radius, in float _softness){
vec2 l = _st-vec2(0.5);
return 1.-smoothstep(_radius-_softness,
_radius+_softness,
dot(l,l)*4.0);
}
vec2 puzzleShuffle(vec2 _st, float _zoom){
_st *= _zoom;
// Here is where the offset is happening
if (mod(u_time, 2.) > 1.0) {
_st.x += floor(mod(_st.y,2.)) * fract(u_time);
} else {
_st.y += floor(mod(_st.x,2.)) * fract(u_time);
}
return fract(_st);
}
#define PI 3.141592653589793
#define HALF_PI 1.5707963267948966
float timeSawtooth(float t) {
return abs(fract(t)*2.0-1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment