Docs / Shaping functions

Shaping functions: custom functions

A curve of your own on BaseShapingFunction, with parameters if it needs them, used wherever a shaping function is taken, and saved under a name of your own.

Color legend plain property method

When none of the 44 curves fits, write your own. A custom function derives from the same base as the library’s, so it goes wherever a shaping function goes: the audio fades and distance curves, a tween, a picker. Names are colored by what they are: a property or a method; values and fields keep the plain color.

A curve of your own

Derive from BaseShapingFunction, pass a name and, if the curve is a polynomial, its degree to the base constructor, and override CalculateCore. The smoothstep curve, 3x² − 2x³:

using BorrowedTorch.ShapingFunctions;

public sealed class SmoothStep : BaseShapingFunction
{
    public SmoothStep()
        : base("Smooth Step", 3)
    {
    }

    protected override float CalculateCore(float input)
    {
        return input * input * (3.0f - 2.0f * input);
    }
}

CalculateOutput clamps the input to 0 to 1 and then calls CalculateCore, so the curve only ever sees inputs from 0 to 1, or NaN. CalculateOutput isn’t virtual: the clamp always happens.

A custom function’s Id is ShapingFunctionId.Custom and its Category ShapingFunctionCategory.Custom, both −1. The other IDs are the library’s: its own functions are built with a constructor only the library can call, so every ID but Custom always names a function the factory can create.

Code built on shaping functions counts on what every curve promises. The library tests its own functions for it; a custom function should keep it too:

  • Exact ends. Return exactly 0 for 0 and exactly 1 for 1, so animations land on their values. Smoothstep does: 0 * 0 * 3 is 0 and 1 * 1 * 1 is 1. Formulas that only reach 1 approximately can test for the ends and return them, as the library’s exponential and elastic curves do.
  • Within 0 to 1, unless the curve overshoots on purpose.
  • Pure. Change nothing in CalculateCore, so the same input always gives the same output and several threads can evaluate one function at once.
  • NaN in, NaN out. Arithmetic on NaN gives NaN by itself; a curve that branches on its input should keep it so.

BaseShapingFunction protected members

Member Description
BaseShapingFunction(string name, int? order = null) The constructor of a custom function: its Name and its Order, null when it isn’t a polynomial. Its Id and Category are Custom.
CalculateCore(float input) Abstract. The curve: the output for an input from 0 to 1, or NaN.
ClampParameter(float value, float min = 0, float max = 1) Static. The value clamped to min to max, for a parameter’s setter. Throws ArgumentOutOfRangeException for NaN.
ParameterEpsilon Constant. 0.00001, the margin that keeps a parameter off 0 and 1 where the curve would divide by 0.

Parameters of your own

To give a custom function parameters, implement the parameter interfaces and clamp the values in their setters with ClampParameter. Schlick’s bias curve passes through (0.5, A): an ease in below the diagonal, an ease out above it, and the straight line at A = 0.5.

using BorrowedTorch.ShapingFunctions;

public sealed class Bias : BaseShapingFunction, IOneParameter
{
    // the straight line until A is set
    private float _a = 0.5f;

    public Bias()
        : base("Bias")
    {
    }

    public float A
    {
        get => _a;
        // the curve divides by A, and by 0 at the start when A is 1: keep A off both ends, as the library does
        set => _a = ClampParameter(value, ParameterEpsilon, 1.0f - ParameterEpsilon);
    }

    protected override float CalculateCore(float input)
    {
        return input / ((1.0f / A - 2.0f) * (1.0f - input) + 1.0f);
    }
}

The curve gives exactly 0 for 0, and exactly 1 for 1, where the bracket is 0. The rest follows from the interface: ParameterCount is 1, Parametric is true and HasVariableOrder is false, and code that sets the parameters of the library’s functions through IOneParameter, such as the curve picker, sets this one’s too. For B to D implement ITwoParameters to IFourParameters, which include the ones before them, and for an order implement INOrder.

The library starts its parameters at the bottom of their range. A custom function can start where it suits it best: here at 0.5, the straight line.

Using a custom function

A custom function goes wherever a BaseShapingFunction goes:

fade.FadeInFunction = new SmoothStep();          // a fade of BorrowedTorch.Audio
distance.DistanceVolumeAttenuationFunction = new Bias { A = 0.2f };

The factory and the catalog know only the library’s functions: ShapingFunctionFactory.Create throws for Custom, and ShapingFunctionCatalog never lists it. To offer custom functions in a picker, add them to its list yourself.

Saving a custom function

Every custom function has the ID Custom, so the ID can’t say which one to create again. Save a name of your own instead, and keep a table from the names to the functions:

using BorrowedTorch.ShapingFunctions;

public static class GameCurves
{
    // the game's own curves, under names that go into save files and never change
    private static readonly Dictionary<string, Func<BaseShapingFunction>> Custom = new()
    {
        ["SmoothStep"] = () => new SmoothStep(),
        ["Bias"] = () => new Bias()
    };

    // a saved curve: the number of a library ID, or Custom (-1) and the name of one of the game's own
    public static BaseShapingFunction Create(int id, string? customName)
    {
        if ((ShapingFunctionId)id != ShapingFunctionId.Custom)
            return ShapingFunctionFactory.Create((ShapingFunctionId)id);

        if (customName is not null && Custom.TryGetValue(customName, out var create))
            return create();

        throw new ArgumentException($"No custom curve is called {customName}.", nameof(customName));
    }
}

The parameters are saved and set again through the interfaces, the same way for both kinds, as the catalog page does for the library’s functions.