GLSL Learning

GLSL built-in function

floor()

Rounds x down to the nearest whole number that is less than or equal to x.

Syntax

float floor(float x)
vec2 floor(vec2 x)
vec3 floor(vec3 x)
vec4 floor(vec4 x)

Parameters

x
The value to round down. Vectors are processed per component.

Return value

A value of the same type as x containing whole numbers (still stored as floats).

Live example

Edit the code and the preview updates as you type.

How it works

floor(x) drops everything after the decimal point and always rounds down:

  • floor(2.7) is 2.0
  • floor(2.0) is 2.0
  • floor(-0.3) is -1.0 (down, not toward zero)

Multiplying first and dividing afterwards controls how many steps you get. floor(x * 4.0) / 4.0 snaps x to multiples of 0.25, which is the staircase in the example.

Typical uses

  • Grid cell index: floor(uv * 10.0) tells you which of the 10 x 10 cells a pixel is in. Feed it to a random function to give every cell its own color.
  • Posterizing: floor(color * 4.0) / 4.0 reduces a smooth gradient to 4 bands.
  • Frame stepping: floor(u_time * 12.0) advances only 12 times per second, for a stop-motion look.

Common mistakes

  • Expecting rounding toward zero for negative numbers. floor(-0.3) is -1.0. Use sign(x) * floor(abs(x)) if you need truncation.
  • Comparing the result with == against an integer literal. Write floor(x) == 2.0, with a float literal.

Related functions

Tutorials that use floor()