public static float Wrap01(float value) { int n = Mathf.FloorToInt(value); float result = value - n; GLDebug.Assert(InRange01(result), "result is not in [0, 1)"); return(result); }
/// <summary> /// Linearly interpolates between two values between 0 and 1 if values wrap around from 1 back to 0. /// </summary> /// <remarks>This is useful, for example, in lerping between angles.</remarks> /// <example> /// <code>float angleInRad1 = 1; /// float angleInRad2 = 5; /// float revolution = Mathf.PI * 2; /// float interpolation = WLerp(angleInRad1 / revolution, angleInRad2 / revolution, 0.5f); /// /// //interpolation == (5 + 1 + Mathf.PI * 2)/2 = 3 + Mathf.PI /// </code> /// </example> public static float Wlerp01(float v1, float v2, float t) { GLDebug.Assert(InRange(v1, 0, 1), "v1 is not in [0, 1)"); GLDebug.Assert(InRange(v2, 0, 1), "v2 is not in [0, 1)"); if (Mathf.Abs(v1 - v2) <= 0.5f) { return(Mathf.Lerp(v1, v2, t)); } else if (v1 <= v2) { return(Frac(Mathf.Lerp(v1 + 1, v2, t))); } else { return(Frac(Mathf.Lerp(v1, v2 + 1, t))); } }