123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LeanSmooth {
-
- public static float damp(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f)
- {
- if (deltaTime < 0f)
- deltaTime = Time.deltaTime;
- smoothTime = Mathf.Max(0.0001f, smoothTime);
- float num = 2f / smoothTime;
- float num2 = num * deltaTime;
- float num3 = 1f / (1f + num2 + 0.48f * num2 * num2 + 0.235f * num2 * num2 * num2);
- float num4 = current - target;
- float num5 = target;
- if (maxSpeed > 0f)
- {
- float num6 = maxSpeed * smoothTime;
- num4 = Mathf.Clamp(num4, -num6, num6);
- }
- target = current - num4;
- float num7 = (currentVelocity + num * num4) * deltaTime;
- currentVelocity = (currentVelocity - num * num7) * num3;
- float num8 = target + (num4 + num7) * num3;
- if (num5 - current > 0f == num8 > num5)
- {
- num8 = num5;
- currentVelocity = (num8 - num5) / deltaTime;
- }
- return num8;
- }
-
- public static Vector3 damp(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f)
- {
- float x = damp(current.x, target.x, ref currentVelocity.x, smoothTime, maxSpeed, deltaTime);
- float y = damp(current.y, target.y, ref currentVelocity.y, smoothTime, maxSpeed, deltaTime);
- float z = damp(current.z, target.z, ref currentVelocity.z, smoothTime, maxSpeed, deltaTime);
- return new Vector3(x, y, z);
- }
-
- public static Color damp(Color current, Color target, ref Color currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f)
- {
- float r = damp(current.r, target.r, ref currentVelocity.r, smoothTime, maxSpeed, deltaTime);
- float g = damp(current.g, target.g, ref currentVelocity.g, smoothTime, maxSpeed, deltaTime);
- float b = damp(current.b, target.b, ref currentVelocity.b, smoothTime, maxSpeed, deltaTime);
- float a = damp(current.a, target.a, ref currentVelocity.a, smoothTime, maxSpeed, deltaTime);
- return new Color(r, g, b, a);
- }
-
- public static float spring(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f)
- {
- if (deltaTime < 0f)
- deltaTime = Time.deltaTime;
- float diff = target - current;
- currentVelocity += deltaTime / smoothTime * accelRate * diff;
- currentVelocity *= (1f - deltaTime * friction);
- if (maxSpeed > 0f && maxSpeed < Mathf.Abs(currentVelocity))
- currentVelocity = maxSpeed * Mathf.Sign(currentVelocity);
- float returned = current + currentVelocity;
- return returned;
- }
-
- public static Vector3 spring(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f)
- {
- float x = spring(current.x, target.x, ref currentVelocity.x, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- float y = spring(current.y, target.y, ref currentVelocity.y, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- float z = spring(current.z, target.z, ref currentVelocity.z, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- return new Vector3(x, y, z);
- }
-
- public static Color spring(Color current, Color target, ref Color currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f)
- {
- float r = spring(current.r, target.r, ref currentVelocity.r, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- float g = spring(current.g, target.g, ref currentVelocity.g, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- float b = spring(current.b, target.b, ref currentVelocity.b, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- float a = spring(current.a, target.a, ref currentVelocity.a, smoothTime, maxSpeed, deltaTime, friction, accelRate);
- return new Color(r, g, b, a);
- }
-
- public static float linear(float current, float target, float moveSpeed, float deltaTime = -1f)
- {
- if (deltaTime < 0f)
- deltaTime = Time.deltaTime;
- bool targetGreater = (target > current);
- float currentVelocity = deltaTime * moveSpeed * (targetGreater ? 1f : -1f);
- float returned = current + currentVelocity;
- float returnPassed = returned - target;
- if ((targetGreater && returnPassed > 0) || !targetGreater && returnPassed < 0)
- {
- return target;
- }
- return returned;
- }
-
- public static Vector3 linear(Vector3 current, Vector3 target, float moveSpeed, float deltaTime = -1f)
- {
- float x = linear(current.x, target.x, moveSpeed, deltaTime);
- float y = linear(current.y, target.y, moveSpeed, deltaTime);
- float z = linear(current.z, target.z, moveSpeed, deltaTime);
- return new Vector3(x, y, z);
- }
-
- public static Color linear(Color current, Color target, float moveSpeed)
- {
- float r = linear(current.r, target.r, moveSpeed);
- float g = linear(current.g, target.g, moveSpeed);
- float b = linear(current.b, target.b, moveSpeed);
- float a = linear(current.a, target.a, moveSpeed);
- return new Color(r, g, b, a);
- }
-
- public static float bounceOut(float current, float target, ref float currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f, float hitDamping = 0.9f)
- {
- if (deltaTime < 0f)
- deltaTime = Time.deltaTime;
- float diff = target - current;
- currentVelocity += deltaTime / smoothTime * accelRate * diff;
- currentVelocity *= (1f - deltaTime * friction);
- if (maxSpeed > 0f && maxSpeed < Mathf.Abs(currentVelocity))
- currentVelocity = maxSpeed * Mathf.Sign(currentVelocity);
- float returned = current + currentVelocity;
- bool targetGreater = (target > current);
- float returnPassed = returned - target;
- if ((targetGreater && returnPassed > 0) || !targetGreater && returnPassed < 0)
- {
- currentVelocity = -currentVelocity * hitDamping;
- returned = current + currentVelocity;
- }
- return returned;
- }
-
- public static Vector3 bounceOut(Vector3 current, Vector3 target, ref Vector3 currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f, float hitDamping = 0.9f)
- {
- float x = bounceOut(current.x, target.x, ref currentVelocity.x, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- float y = bounceOut(current.y, target.y, ref currentVelocity.y, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- float z = bounceOut(current.z, target.z, ref currentVelocity.z, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- return new Vector3(x, y, z);
- }
-
- public static Color bounceOut(Color current, Color target, ref Color currentVelocity, float smoothTime, float maxSpeed = -1f, float deltaTime = -1f, float friction = 2f, float accelRate = 0.5f, float hitDamping = 0.9f)
- {
- float r = bounceOut(current.r, target.r, ref currentVelocity.r, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- float g = bounceOut(current.g, target.g, ref currentVelocity.g, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- float b = bounceOut(current.b, target.b, ref currentVelocity.b, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- float a = bounceOut(current.a, target.a, ref currentVelocity.a, smoothTime, maxSpeed, deltaTime, friction, accelRate, hitDamping);
- return new Color(r, g, b, a);
- }
- }
|