Mathfx

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(C# - Mathfx.cs)
Line 10: Line 10:
 
* '''Sinerp''' - Short for 'sinusoidal interpolation', this method will interpolate while easing around the end, when value is near one.
 
* '''Sinerp''' - Short for 'sinusoidal interpolation', this method will interpolate while easing around the end, when value is near one.
 
* '''Berp''' - Short for 'boing-like interpolation', this method will first overshoot, then waver back and forth around the end value before coming to a rest.
 
* '''Berp''' - Short for 'boing-like interpolation', this method will first overshoot, then waver back and forth around the end value before coming to a rest.
 +
* '''SmoothStep''' - Works like Lerp, but has ease-in and ease-out of the values.
 
* '''Lerp''' - Short for 'linearly interpolate', this method is equivalent to Unity's Mathf.Lerp, included for comparison.
 
* '''Lerp''' - Short for 'linearly interpolate', this method is equivalent to Unity's Mathf.Lerp, included for comparison.
 
 
== C# - Mathfx.cs ==
 
== C# - Mathfx.cs ==
 
<csharp>using UnityEngine;
 
<csharp>using UnityEngine;
Line 35: Line 35:
 
     }
 
     }
 
      
 
      
 +
    public static float SmoothStep (float x, float min, float max)
 +
    {
 +
        x = Mathf.Clamp (x, min, max);
 +
        float v1 = (x-min)/(max-min);
 +
        float v2 = (x-min)/(max-min);
 +
        return -2*v1 * v1 *v1 + 3*v2 * v2;
 +
    }
 +
 
     public static float Lerp(float start, float end, float value)
 
     public static float Lerp(float start, float end, float value)
 
     {
 
     {
 
         return ((1.0f - value) * start) + (value * end);
 
         return ((1.0f - value) * start) + (value * end);
 
     }
 
     }
 +
 
}
 
}
 
</csharp>
 
</csharp>

Revision as of 20:55, 21 June 2006

Description

The following snippet provides short functions for floating point numbers. See the usage section for individualized information.

Usage

  • Hermite - This method will interpolate while easing in and out at the limits.
  • Sinerp - Short for 'sinusoidal interpolation', this method will interpolate while easing around the end, when value is near one.
  • Berp - Short for 'boing-like interpolation', this method will first overshoot, then waver back and forth around the end value before coming to a rest.
  • SmoothStep - Works like Lerp, but has ease-in and ease-out of the values.
  • Lerp - Short for 'linearly interpolate', this method is equivalent to Unity's Mathf.Lerp, included for comparison.

C# - Mathfx.cs

<csharp>using UnityEngine; using System;

public class Mathfx {

   public static float Hermite(float start, float end, float value)
   {
       return Mathf.Lerp(start, end, value * value * (3.0f - 2.0f * value));
   }
   
   public static float Sinerp(float start, float end, float value)
   {
       return Mathf.Lerp(start, end, Mathf.Sin(value * Mathf.PI * 0.5f));
   }
   public static float Berp(float start, float end, float value)
   {
       value = Mathf.Clamp01(value);
       value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) * (1f + (1.2f * (1f - value)));
       return start + (end - start) * value;
   }
   
   public static float SmoothStep (float x, float min, float max) 
   {
       x = Mathf.Clamp (x, min, max);
       float v1 = (x-min)/(max-min);
       float v2 = (x-min)/(max-min);
       return -2*v1 * v1 *v1 + 3*v2 * v2;
   }
   public static float Lerp(float start, float end, float value)
   {
       return ((1.0f - value) * start) + (value * end);
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox