1.smoothstep(a,b,v), a < b
#define PI 3.1415926
vec2 fixUV(in vec2 uv){
return 3.0 * (2.0 * uv - iResolution.xy) /
min(iResolution.x,iResolution.y);
}
vec3 grid(in vec2 uv){
vec3 color = vec3(0.4);
vec2 grid = floor(mod(uv,2.0));
if(grid.x == grid.y){
color = vec3(0.6);
}
float fx = smoothstep(1.1 * fwidth(uv.x),fwidth(uv.x),abs(uv.x));
color = mix(color,vec3(0.0),fx);
float fy = smoothstep(1.1 * fwidth(uv.y),fwidth(uv.y),abs(uv.y));
color = mix(color,vec3(0.0),fy);
return color;
}
float func(float x){
return smoothstep(-1.0,1.0,x);
}
float plotFunc(vec2 uv){
float f = func(uv.x);
return smoothstep(f - 0.01,f + 0.01,uv.y);
}
void mainImage(out vec4 fragColor,in vec2 fragCoord){
vec2 uv = fixUV(fragCoord);
vec3 color = grid(uv);
float count = 0.0;
for(int i = 0;i < 4;i++){
for(int j = 0;j < 4;j++){
vec2 offset = vec2(float(i),float(j)) - 1.0;
count += plotFunc(fixUV(fragCoord + offset));
}
}
if(count > 8.0){
count = 16.0 - count;
}
count = count * 2.0 / 16.0;
color = mix(color,vec3(1.0),vec3(count));
fragColor = vec4(color,1.0);
}
效果图:
由图可知,smoothstep(a,b,v), a < b时,表示,v[HTML_REMOVED]b 时返回1
2.smoothstep(a,b,v), a > b
float func(float x){
return smoothstep(1.0,-1.0,x);
}
效果:
由图可知,smoothstep(a,b,v), a > b时,表示,v[HTML_REMOVED]a 时返回0
3.step
float func(float x){
return step(1.0,x);
}
效果图:
由图可知,小于1时直接返回0,大于1时直接返回1,没有中间过渡。