04
Mar
08

### Variable Smoothstep Function

```float smoothstepVar(float edge1, float edge2, float curve, float value)
{
float width = edge2 - edge1;
float phase = (value - edge1) / width;
phase = clamp(phase,0.0,1.0);
curve = (curve + 0.025) * 99.075;
float outValue = pow(phase,curve);
return outValue;
}```

A variation on the builtin GLSL Smoothstep function.
Outputs a float in the range 0.0 > 1.0 when value is between Edge1 and Edge2.
The curve parameter changes the curve of the output between Exponential In and Exponential Out shapes.
Curve parameter should be hooked up to an Exponential control. I’ll eventually get around to making the curve variable exponential in the function itself.

In the example below the function is applied to the distance of each pixel from the centre of the texture, with different curve settings.

#### 6 Responses to “Variable Smoothstep Function”

1. 1 Daniel Brauer
May 13, 2010 at 9:21 pm

This page is the first hit for “variable smoothstep” on Google, but unfortunately for everyone, the function described here is not related to the function commonly known as smoothstep(). In GLSL (and every other API that I’m aware of), smoothstep is a Hermite interpolation, and has a slope of 0 at both edges.
Code on Wikipedia: http://en.wikipedia.org/wiki/Smoothstep
And a pretty good explanation here: http://sol.gfxile.net/interpolation/

The function posted above is an exponential function, which has a slope of zero on the left edge, and something higher on the right edge (2 when curve == 0, for instance).

• 2 toneburst
May 21, 2010 at 6:27 pm

Yeah, I know. I just meant it does something similar to the GLSL smoothstep() function, but in a different way. It might be useful to someone out there….

Incidentally, do you happen to know if smoothstep() is directly equivalent to the HLSL lerp() function? I’ve always assumed it was, but ‘lerp’ suggests ‘linear interpolation’, which, as you point out, is different from a hermite curve.

a|x

• 3 Daniel Brauer
June 3, 2010 at 11:36 pm

lerp() is indeed linear interpolation in both languages. smoothstep() is the same Hermite interpolation in both languages.

2. 4 toneburst
June 4, 2010 at 7:32 am

I’m pretty sure there’s no lerp() function in GLSL. I’ve certainly never seen it in any of the documentation. I did read somewhere that there’s a linearstep() function (equivalent to lerp(), presumably), but again, I’ve never seen that in any of the documentation- either in the Orange Book, or the QuickReference card listing all the builtin functions.

a|x

• 5 Daniel Brauer
September 21, 2010 at 2:36 am

Oops, I’m not sure where I got lerp() in GLSL from. The equivalent function is mix(), which is equal to Cg’s lerp(), or 1 – HLSL’s lerp().

3. 6 toneburst
October 18, 2010 at 3:23 pm

Interesting that mix() and lerp() work in opposite directions- I didn’t realise that was the case. I must look out for that if I do any more HLSL > GLSL translation. Thanks for the pointer Daniel.

a|x