what is the case for edge0 greater than or equal to edge1 for smoothstep function glsl

1.5k Views Asked by At

I am looking into smoothstep(edge0, edge1, x) function. docs say results are undefined if edge0 >= edge1.

In a shader there is a line:

smoothstep(radius + SIZE, radius + SIZE / 1.2, dist);

this means edge0 >= edge1 it still works fine, how is that possible?

2

There are 2 best solutions below

3
WilliamNHarvey On BEST ANSWER

Looks to me like the docs are wrong.

Here's from playing around with smoothstep:

y = smoothstep(1.0,-1.0,x); enter image description here

y = smoothstep(-1.0,1.0,x); enter image description here

It looks like when edge0 > edge1, it flips the side at 1 to be at negative infinity, and the side at 0 to be at positive infinity.

Another example:

#ifdef GL_ES
precision mediump float;
#endif

#define PI 3.14159265359

uniform vec2 u_resolution;

float plot(vec2 st, float pct){
  return  smoothstep( pct+0.02, pct, st.y) -
          smoothstep( pct, pct-0.02, st.y);
}

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution;

    // Smooth interpolation between 0.1 and 0.9
    float y = smoothstep(0.1,0.9,st.x);

    vec3 color = vec3(y);

    float pct = plot(st,y);
    color = (1.0-pct)*color+pct*vec3(0.0,1.0,0.0);

    gl_FragColor = vec4(color,1.0);
}

enter image description here

Changing y to a step from 0.9 to 0.1 changes the output to this:

enter image description here

1
UnixNoob On

Adding to @Asthamatic's answer above, from Nvidia developer docs:

https://developer.download.nvidia.com/cg/smoothstep.html

Interpolates smoothly from 0 to 1 based on x compared to a and b.

1) Returns 0 if x < a < b or x > a > b 
1) Returns 1 if x < b < a or x > b > a 
3) Returns a value in the range [0,1] for the domain [a,b]. 

The slope of smoothstep(a,b,a) and smoothstep(a,b,b) is zero.

For vectors, the returned vector contains the smooth interpolation of each element of the vector x

This explains any kind of behavior represented by smoothstep functions.