Step - 3

136 阅读1分钟

Task

Using the max and step functions, paint only those pixels whose normalized x-coordinate is less than 0.25 or greater than 0.75.

使用maxstep函数,仅绘制那些标准化坐标下 x 坐标小于0.25或大于等于0.75

Requirements

The shader should avoid using branching or conditional statements in its code, and instead rely on the step and max functions to determine the color of each pixel.

着色器应避免在其代码中使用分支或条件语句,而是依靠stepmax函数来确定每个像素的颜色。

Theory

GLSL 中的函数max用于返回两个输入值中的最大值。它接受两个参数,并返回两个值中较大的一个。以下是该max函数在 GLSL 中的详细解释:

函数

float max(float x, float y);

  • 如果x大于或等于y,则函数返回x
  • 如果y大于x,则函数返回y

示例用法

float a = 5.0;

float b = 3.0;

float result = max(a, b); // will be 5.0

Answer

uniform vec2 iResolution;

void main() {
  // Normalized pixel coordinates (from 0 to 1)
  vec2 uv = gl_FragCoord.xy / iResolution.xy;

  vec3 color = vec3(1.0, 0.3, 0.3);
  float t1 = 1.0 - step(0.25, uv.x);
  float t2 = step(0.75, uv.x);

  gl_FragColor = vec4(color * max(t1, t2), 1.0);
}

效果

image.png

练习

Step

最后

如果你觉得这篇文章有用,记得点赞、关注、收藏,学Shader更轻松!!