之前有说过smoothstep 进行差运算,就可以得到一条直线
加上各种函数值,就可以画出图形了
sin函数
下面是 y = sin(x * 10) * 0.3;
//px 为自变量,py = f(px)
float plot(float px,float py)
{
//0.02 为线的粗细
float a = smoothstep(py - 0.02,py,px);
float b = smoothstep(py,py + 0.02,px);
return a - b;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
//周期和峰值的缩放
float c = plot(sin(uv.x * 10.0) * 0.3 ,uv.y);
fragColor = vec4(vec3(c),1.0);
}
效果:
可以利用iTime,调整相位,就是动画了
//px 为自变量,py = f(px)
float plot(float px,float py)
{
//0.02 为线的粗细
float a = smoothstep(py - 0.02,py,px);
float b = smoothstep(py,py + 0.02,px);
return a - b;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
float c = plot(sin(uv.x * 10.0 + iTime * 2.0) * 0.3 ,uv.y);
fragColor = vec4(vec3(c),1.0);
}
绘制直线 x = 0.0;
//px 为自变量,py = f(px)
float plot(float px,float py)
{
//0.02 为线的粗细
float a = smoothstep(py - 0.02,py,px);
float b = smoothstep(py,py + 0.02,px);
return a - b;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
// Normalized pixel coordinates (from 0 to 1)
vec2 uv = (fragCoord.xy - 0.5 * iResolution.xy) / iResolution.y;
float c = plot(uv.x ,0.0);
fragColor = vec4(vec3(c),1.0);
}
效果:
绘制直线 y=x
float c = plot(uv.x ,uv.y);