Step - Built-in functions - Shader Learning

Built-in functions

Step

Task

Write a shader that splits the screen into two parts: the left half should be black and the right half should be red. The shader should work for any screen resolution. To achieve this, consider using the uv coordinates to control the position of the split.


Requirements


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

Theory

The step is a threshold function. It returns 0.0 if the passed x value is less than the edge threshold value, otherwise it returns 1.0.


Function Signature

float step(float edge, float x)

How It Works


Here's a breakdown of how the step function works:

  • If x < edge, the function returns 0.0.
  • If x >= edge, the function returns 1.0.

Practical Applications


This function is commonly used in shaders to create step-like effects, such as creating sharp transitions or defining regions based on a threshold value.


Example Usage


Let's look at an example of how the Step function can be used in GLSL shaders:

float edge = 0.5;
float x = 0.3;
float result = step(edge, x); // will return 0.0 since `x` < `edge`