# About shader function

**URL:** <https://discourse.threejs.org/t/about-shader-function/89037>\
**Category:** Questions\
**Created:** [January 9, 2026, 9:35am UTC](https://discourse.threejs.org/t/about-shader-function/89037 "2026-01-09T09:35:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik\_Mogensen](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/henrik_mogensen/32/58396_2.png) [@Henrik\_Mogensen](https://discourse.threejs.org/u/Henrik_Mogensen)\
**Post date:** [January 9, 2026, 9:35am UTC](https://discourse.threejs.org/t/about-shader-function/89037/1 "2026-01-09T09:35:40Z")

</div>

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:

 ![Kurve01](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/7/e7292a2d49d5ac75fc81ad6d682a8c92ceda4fca.png)

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [January 9, 2026, 3:45pm UTC](https://discourse.threejs.org/t/about-shader-function/89037/2 "2026-01-09T15:45:56Z")

</div>

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.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/b/3b43059438748632d24654f117a78fe1625ffc6a.png)

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.

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [January 9, 2026, 10:26pm UTC](https://discourse.threejs.org/t/about-shader-function/89037/3 "2026-01-09T22:26:26Z")

</div>

You can write the formula out in Desmos to see exactly what each part looks like: [Desmos | Graphing Calculator](https://www.desmos.com/calculator/whhixqudeh)

 ![Screenshot 2026-01-09 at 2.26.45 PM](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/3/1/31baa1911954ae9d10e80bd1615e617d88786220.png)

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.

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [January 10, 2026, 4:17am UTC](https://discourse.threejs.org/t/about-shader-function/89037/4 "2026-01-10T04:17:30Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [January 10, 2026, 8:50am UTC](https://discourse.threejs.org/t/about-shader-function/89037/5 "2026-01-10T08:50:39Z")

</div>

Ah ok, I thought about powers like `**` in JS 🤭
