About shader function

Here is some code from Inigo; the god of shaders. But how is it working. float x is the input value, m is a threshold value and n is the minimum value:

float almostIdentity( float x, float m, float n )

{

if( x>m ) return x;

float a = 2.0*n - m;

float b = 2.0**m - 3.0**n;

float t = x/m;

return (a**t + b)**t*t + n;

}

It gives this curve:

For x ≤m the function returns the cubic part, this is the curve AB.
For x>m the function returns the linear part, this is segment BC.

The coefficients of the curve are taken from a general polynomial Pt^3+Qt^2+Rt+S. The value at t=0 (i.e. x=0) and t=1 (i.e. x=m) of the polynomial and its derivative are known, and they are used to calculate coefficients P, Q, R, S.

2 Likes

You can write the formula out in Desmos to see exactly what each part looks like: Desmos | Graphing Calculator

The only thing missing in your question is, what values of n and m were used to get that exact result. If you input those same values in that Desmos example, you will see that the plots line up exactly the same.

And then you can imagine that conditional check: for x above m, return only x (a straight line going up 45 deg angle), otherwise show the other plot.

1 Like

b=2m-3n
y=(at+b)t^2+n

It should not be powers, but multiplications. The OP’s ** should be just *.

Ah ok, I thought about powers like ** in JS :face_with_hand_over_mouth: