Step - Invert - Built-in functions - Shader Learning

Built-in functions

Step - Invert

Task

Write a shader that splits the screen into two parts: the left half should be red and the right half should be black.


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

You can invert the result of the step function by subtracting the step function result from 1.


How It Works


The step function returns 0.0 if the input value is less than the threshold and 1.0 if the input value is greater than or equal to the threshold. By subtracting this result from 1.0, you effectively invert the output:

  • If the original result is 0.0, the inverted result will be 1.0.
  • If the original result is 1.0, the inverted result will be 0.0.

Example


Here's an example code snippet to illustrate how to invert the result of the step function in GLSL:

float invertedStep = 1.0 - step(threshold, value);

As a result, the invertedStep will be 0.0 if the value is greater than or equal to the threshold, and 1.0 otherwise.