Skip to content

Instantly share code, notes, and snippets.

@celechii
Last active October 4, 2020 18:00
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 celechii/9625779fa906036ea8bcb48716ff10a3 to your computer and use it in GitHub Desktop.
Save celechii/9625779fa906036ea8bcb48716ff10a3 to your computer and use it in GitHub Desktop.
a smoothdamp function for unity colours. not optimized or rlly thought thru, i just replaced a bunch of shit
// based on how ive been told unity did it:
// https://stackoverflow.com/questions/61372498/how-does-mathf-smoothdamp-work-what-is-it-algorithm
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime) =>
ColourSmoothDamp(current, target, ref currentVelocity, smoothTime, Mathf.Infinity, Time.deltaTime);
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime, float maxSpeed) =>
ColourSmoothDamp(current, target, ref currentVelocity, smoothTime, maxSpeed, Time.deltaTime);
public static Color ColourSmoothDamp(Color current, Color target, ref Vector4 currentVelocity, float smoothTime, float maxSpeed, float deltaTime) {
// Based on Game Programming Gems 4 Chapter 1.10
smoothTime = Mathf.Max(0.0001f, smoothTime);
float omega = 2f / smoothTime;
Vector4 currentColour = new Vector4(current.r, current.g, current.b, current.a);
Vector4 targetColour = new Vector4(target.r, target.g, target.b, target.a);
float x = omega * deltaTime;
float exp = 1f / (1f + x + 0.48f * x * x + 0.235f * x * x * x);
Vector4 change = currentColour - targetColour;
Vector4 originalTo = targetColour;
// Clamp maximum speed
Vector4 maxChange = maxSpeed * smoothTime * Vector4.one;
change.x = Mathf.Clamp(change.x, -maxChange.x, maxChange.x);
change.y = Mathf.Clamp(change.y, -maxChange.y, maxChange.y);
change.z = Mathf.Clamp(change.z, -maxChange.z, maxChange.z);
change.w = Mathf.Clamp(change.w, -maxChange.w, maxChange.w);
targetColour = currentColour - change;
Vector4 temp = (currentVelocity + (omega * change)) * deltaTime;
currentVelocity = (currentVelocity - omega * temp) * exp;
Vector4 output = targetColour + (change + temp) * exp;
// Prevent overshooting
if (originalTo.x - currentColour.x > 0.0f == output.x > originalTo.x) {
output.x = originalTo.x;
currentVelocity.x = (output.x - originalTo.x) / deltaTime;
}
if (originalTo.y - currentColour.y > 0.0f == output.y > originalTo.y) {
output.y = originalTo.y;
currentVelocity.y = (output.y - originalTo.y) / deltaTime;
}
if (originalTo.z - currentColour.z > 0.0f == output.z > originalTo.z) {
output.z = originalTo.z;
currentVelocity.z = (output.z - originalTo.z) / deltaTime;
}
if (originalTo.w - currentColour.w > 0.0f == output.w > originalTo.w) {
output.w = originalTo.w;
currentVelocity.w = (output.w - originalTo.w) / deltaTime;
}
return new Color(output.x, output.y, output.z, output.w);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment