Rectangle Color Split

6 / 15

📝 Exercise Goal

Divide the screen area into two colors (left and right) using an if condition, mastering fragment judgment logic.

💡 💡 Tutorial Content

Divide the screen area into two colors (left and right) using an if condition, mastering fragment judgment logic.

Overview

  • Implement a horizontal gradient using UV as the factor.

Learning Objectives

  • Understand how to use if conditional statements in GLSL.
  • Learn to divide the screen based on the x-value of UV coordinates.
  • Master the method of assigning different colors to different regions.

Prerequisites

  • uv-coordinates
  • solid-color

Key Concepts

  • A horizontal gradient uses a 0-1 factor (usually UV) to blend colors.
float t = vUv.x;
vec3 color = vec3(t);
  • Keep the factor inside [0,1].
t = clamp(t, 0.0, 1.0);

How To Implement (Step-by-step)

  • Set factor: t = vUv.x.
  • Map t to a color (grayscale or mix).
  • Output gl_FragColor with alpha=1.

Self-check

  • Does it compile without errors?
  • Does the output match the goal?
  • Are key values kept in [0,1]?

Common Mistakes

  • If output is black, check your mask/factor isn’t always 0.

GLSL Code Editor

Correct Code Preview

Current Code Preview