Skip to content

Instantly share code, notes, and snippets.

@h3r
Created May 23, 2020 13:14
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 h3r/ee041f578e6e6f55dbb171e6a8564203 to your computer and use it in GitHub Desktop.
Save h3r/ee041f578e6e6f55dbb171e6a8564203 to your computer and use it in GitHub Desktop.
Tweening funcs
/*
This is another set of easing functions I used for VFX, should work in HLSL, GLSL and C/C++, if not leave a comment.
Most of this code hasn't been made by me (maybe partially tweaked to fit) and just collected those snippets from many sources
across the internet. I haven't saved some of the original author names and all the credits
should go to them. I'm pretty some of you may find optimizations to them, feel free to leave a comment.
HPlass (hermann.plass@gmail.com) - 2020
*/
// ------------------------------
// linear
// ------------------------------
//float linear(float start, float end, float ratio) {
// if (ratio <= 0.0f) return start;
// if (ratio >= 1.0f) return end;
// return end*ratio + start*(1 - ratio);
//}
// ------------------------------
// quadratic
// ------------------------------
float quadIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c*ratio*ratio + start;
}
float quadOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return -c * ratio * (ratio - 2.f) + start;
}
float quadInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio * 2.f;
if (t < 1.f) return ((c*0.5f)*t*t) + start;
float tm = t - 1;
return -(c * 0.5f) * (((tm - 2.f)*(tm)) - 1.f) + start;
}
// ------------------------------
// cubic
// ------------------------------
float cubicIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c*ratio*ratio*ratio + start;
}
float cubicOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float ratio2 = ratio - 1;
return c*(ratio2*ratio2*ratio2 + 1) + start;
}
float cubicInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float ratio2 = 2 * ratio;
if (ratio2 < 1) return c / 2 * ratio2*ratio2*ratio2 + start;
ratio2 -= 2;
return c / 2 * (ratio2*ratio2*ratio2 + 2) + start;
}
// ------------------------------
// quart
// ------------------------------
float quartIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c*ratio*ratio*ratio*ratio + start;
}
float quartOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio - 1;
return -c * (t*t*t*t - 1.f) + start;
}
float quartInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio * 2.f;
if (t < 1.f) return c * 0.5f *t*t*t*t + start;
t -= 2.f;
return -c*0.5f * ((t)*t*t*t - 2.f) + start;
}
// ------------------------------
// quint
// ------------------------------
float quintIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c*ratio*ratio*ratio*ratio*ratio + start;
}
float quintOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float ratio2 = ratio - 1.f;
return c*((ratio2)*ratio2*ratio2*ratio2*ratio2 + 1.f) + start;
}
float quintInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio * 2.f;
if (t < 1.f) return c * 0.5f *t*t*t*t*t + start;
t -= 2.f;
return c*0.5f*((t)*t*t*t*t + 2.f) + start;
}
// ------------------------------
// back
// ------------------------------
float backIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float s = 1.70158f;
return c*(ratio)*ratio*((s + 1)*ratio - s) + start;
}
float backOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float s = 1.70158f;
float ratio2 = ratio - 1;
return c*(ratio2*ratio2*((s + 1)*ratio2 + s) + 1) + start;
}
float backInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float s = 1.70158f;
s *= (1.525f);
float ratio2 = ratio;
if ((ratio2 *= 2) < 1) return c / 2 * (ratio2*ratio2*(((s)+1)*ratio2 - s)) + start;
float postFix = ratio2 -= 2;
return c / 2 * ((postFix)*ratio2*(((s)+1)*ratio2 + s) + 2) + start;
}
// ------------------------------
// elastic
// ------------------------------
float elasticIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float p = 0.3f;
float a = c;
float s = p*0.25f;
float ratio2 = ratio;
float postFix = a*pow(2.f, 10.f*(ratio2 -= 1)); // this is a fix, again, with post-increment operators
return -(postFix * sin((float)((ratio2 - s)*(2 * PI) / p))) + start;
}
float elasticOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float p = 0.3f;
float a = c;
float s = p*0.25f;
return (a*pow(2.f, -10 * ratio) * sin((float)((ratio - s)*(2 * PI) / p)) + c + start);
}
float elasticInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float ratio2 = 2 * ratio;
float c = end - start;
float p = (.3f*1.5f);
float a = c;
float s = p*0.25f;
if (ratio2 < 1) {
float postFix = a*pow(2.f, 10.f*(ratio2 -= 1)); // postIncrement is evil
return -.5f*(postFix* sin((float)((ratio2 - s)*(2 * PI) / p))) + start;
}
float postFix = a*pow(2.f, -10.f*(ratio2 -= 1)); // postIncrement is evil
return postFix * sin((float)((ratio2 - s)*(2 * PI) / p))*.5f + c + start;
}
// ------------------------------
// bounce
// ------------------------------
float bounceOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
if (ratio < (1.f / 2.75f)) return c * (7.5625f * ratio * ratio) + start;
if (ratio < (2 / 2.75f)) {
float t = ratio - (1.5f / 2.75f);
return c*(7.5625f*t*t + .75f) + start;
}
if (ratio < (2.5 / 2.75)) {
float t = ratio - (2.25f / 2.75f);
return c*(7.5625f*t*t + .9375f) + start;
}
float t = ratio - (2.625f / 2.75f);
return c*(7.5625f*t*t + .984375f) + start;
}
float bounceIn(float start, float end, float ratio) {
return bounceOut(end, start, 1.0f - ratio);
}
float bounceInOut(float start, float end, float ratio) {
float m = (start + end) * 0.5f;
if (ratio < 0.5f) return bounceIn(start, m, ratio*2.f);
return bounceOut(m, end, ratio*2.f - 1.f);
}
// ------------------------------
// circular
// ------------------------------
float circularIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return -c * (sqrt(1.f - ratio * ratio) - 1.f) + start;
}
float circularOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio - 1.f;
return c * sqrt(1.f - t*t) + start;
}
float circularInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio * 2.f;
if (t < 1.f) return -c*0.5f * (sqrt(1.f - t*t) - 1.f) + start;
float t2 = t - 2.f;
return c*0.5f * (sqrt(1.f - t2*(t2)) + 1.f) + start;
}
// ------------------------------
// expo
// ------------------------------
float expoIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c * pow(2.f, 10 * (ratio - 1.f)) + start;
}
float expoOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c * (-pow(2.f, -10 * ratio) + 1.f) + start;
}
float expoInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
float t = ratio * 2.f;
if (t < 1.f) return c * 0.5f * pow(2.f, 10 * (t - 1.f)) + start;
return c*0.5f * (-pow(2.f, -10.f * --t) + 2) + start;
}
// ------------------------------
// sine
// ------------------------------
float sineIn(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return -c * cos((float)(ratio * PI * 0.5f)) + c + start;
}
float sineOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return c * sin((float)(ratio * PI * 0.5f)) + start;
}
float sineInOut(float start, float end, float ratio) {
if (ratio <= 0.0f) return start;
if (ratio >= 1.0f) return end;
float c = end - start;
return -c * 0.5f * (cos((float)(PI * ratio)) - 1.f) + start;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment