第 14 课:Wave Superposition
实现
uniform vec2 iResolution;
uniform float iTime;
float grid(float size) {
vec2 center = iResolution.xy * 0.5;
vec2 dist = abs(center - gl_FragCoord.xy);
vec2 posInCell = fract(dist / size);
float stroke = 1.0 / size;
float t = step(stroke, posInCell.x);
t *= 1.0 - step(1.0 - stroke, posInCell.x);
t *= step(stroke, posInCell.y);
t *= 1.0 - step(1.0 - stroke, posInCell.y);
return 1.0 - t;
}
float sinPlot(float x) {
float _x = (iResolution.x * 0.5 - x)* 0.05;
float _y =sin(_x) * 0.05 * iResolution.y;
return _y;
}
float fbm(float x) {
float sin1 = sinPlot(x - iTime * 100.0);
float sin2 = 2.0 * sinPlot(0.5 * x - iTime * 100.0);
float sin3 = 4.0 * sinPlot(0.25 * x - iTime * 100.0);
return sin1+sin2+sin3;
}
void main() {
vec2 uv = gl_FragCoord.xy / iResolution.xy;
float smallGrid = grid(20.0);
float bigGrid = grid(80.0);
vec3 color = vec3(1.0);
color = mix(color, vec3(0.9, 0.9, 0.9), smallGrid);
color = mix(color, vec3(0.75, 0.75, 0.75), bigGrid);
color = mix(vec3(0.0, 0.0, 0.0), color, step(1.0 / iResolution.x, abs(uv.x - 0.5)));
color = mix(vec3(0.0, 0.0, 0.0), color, step(1.0 / iResolution.y, abs(uv.y - 0.5)));
float y = iResolution.y * 0.5 - fbm(gl_FragCoord.x);
float dx =dFdx(y);
float stroke = 3.0;
float t = smoothstep(0.0, stroke * max(1.0, abs(dx)), abs(gl_FragCoord.y - y));
color = mix(vec3(1.0, 0.0, 0.0), color, t);
gl_FragColor = vec4(color, 1.0);
}