Clamp Demo

3 / 30
Use clamp() to keep values in range.

Every color channel must be between 0.0 and 1.0 — values outside that range get silently clipped. clamp explicitly keeps values within a specified range.


What is clamp

clamp(x, min, max) constrains x to [min, max]:

  • x < min → returns min
  • x > max → returns max
  • otherwise → returns x unchanged

This scales and shifts vUv.x so the gradient spans only the middle of the canvas, then clamps to ensure no overflow.


What happens without clamp

vUv.x * 1.6 - 0.3 ranges from -0.3 to 1.3, outside 0–1:

  • Left ~20%: negative → GPU clips to 0 (flat dark plateau)
  • Right ~20%: above 1 → GPU clips to 1 (flat bright plateau)

With clamp, transitions happen at controlled positions.


Exercise

The exercise has t = 0.0 (solid black). Use clamp(vUv.x * 1.6 - 0.3, 0.0, 1.0) to compute t and observe where the gradient starts and ends.

Answer Breakdown

Scale factor 1.6 steepens the gradient, offset -0.3 shifts it left. clamp(..., 0.0, 1.0) produces a dark plateau on the left and a bright plateau on the right.

Try changing 1.6 to 3.0 — the transition zone narrows and starts looking like a step.

GLSL Code Editor

Correct Code Preview

Current Code Preview