Color Mixing

15 / 30
Learn to use the mix function for color blending and understand the concept and application of linear interpolation.

To blend between two colors you don't need to do the math yourself — GLSL has mix.


What is mix()

mix(a, b, t) blends between a and b based on the factor t:

  • t = 0.0 → fully a
  • t = 0.5 → halfway between a and b
  • t = 1.0 → fully b

t must be in the 0.0 to 1.0 range — exactly what UV coordinates give you.


Using uv.x for a horizontal gradient

Pass uv.x as t: left side shows color a (t = 0), right side shows color b (t = 1):


Try changing it

Swap in different colors:

Combinationcolor1color2
Orange → Purplevec3(1.0, 0.5, 0.0)vec3(0.5, 0.0, 1.0)
White → Blackvec3(1.0)vec3(0.0)
Green → Yellowvec3(0.0, 0.8, 0.3)vec3(1.0, 0.9, 0.0)

Exercise

The exercise code has mix with a fixed factor of 0.0, so the canvas is all red. Change the factor to uv.x so it fades from red on the left to blue on the right.

Answer Breakdown

Change the third argument from 0.0 to uv.x:

uv.x is 0 on the left (fully red), 1 on the right (fully blue), blending in between.

Try uv.y instead — the gradient flips to bottom-to-top.

GLSL Code Editor

Correct Code Preview

Current Code Preview