Saturday, November 13, 2021

Unit curves for controller response

My controller, e.g. MIDI fader, has linear range normalized to [0, 1].

My software has a control normalized to [0, 1].

But when I map the controller to the control, it doesn't feel right. It isn't expressive, all the interesting result is in one part of the controller.

So I use some function to map [0, 1] to [0, 1] in a non-linear way. The following are some practical functions which are fairly simple and supported by most environments

sqrt(x) is fast in, linear out

x^2 ease in, linear out


of course sqrt(x) and x^2 are both from the general form y=x^t where t=1 is linear, values of 0.75 0.5 0.25 will make the start fast and faster, 1.5 2.0 4.0 will make the start slower and slower. 

arcsin seat

Fast in, fast out, gives more control over the middle. In the following formula, the 1.5 is a tuning factor for how extreme the seat is, a value of 0 gives a linear response, 2.7 is the practical maximum with a large flat spot in the center.

In DDMathParser and VDMX, this is 
((0.5+asin(2*($VAL-0.5))/pi())-$VAL) * 1.5+$VAL

tanh s-curve

Speeds through the middle, gives more control over the edges.

The formula is a bit kludgy to fit the tanh to the unit square, and give values slightly beyond the [0, 1] range at the edges. The 1 is a tuning factor, reduce to 0.8 or 0.5 to give a softer response. Increasing above 1.0 will result in bigger dead spaces at the ends where the result is outside the [0, 1] range.

haversin s-curve

Speeds through the middle, gives more control over the edges. 0 and 1 and 0.5 map perfectly.

The haversin is equivalent to (1-cos(x))/2. Multiply x by PI to fit it into [0, 1].

You can also find deviation from linear by subtracting X, tune the deviation and add back. Tuning factor below 1 will make the curve subtler, but you can't tune above 1 without exceeding the output range.



double exponential seat & s-curve

If your environment supports switching based on x value, then it is easy to combine two exponential functions for the left and right half, to make a seat or s-curve which is tunable to be very extreme. See here for more information.

notes


Graphs from Desmos.
Reference for math functions.
For more inspiration, see The Book of Shaders and resources linked from there.