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)is2.0floor(2.0)is2.0floor(-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.0reduces 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. Usesign(x) * floor(abs(x))if you need truncation. - Comparing the result with
==against an integer literal. Writefloor(x) == 2.0, with a float literal.
Related functions
Tutorials that use floor()
- Value Steps (Quantization)Quantize a smooth gradient into discrete steps using floor().
- Hash Grid NoiseGenerate a random value per grid cell using a small hash function.
- Brick WallPractice building repeating patterns from UV coordinates.
- Toon LightingLearn core lighting terms (diffuse/specular/rim) on a simple sphere.
- Material Lighting CompareCompare Lambert, Phong, Blinn-Phong, and rim-light effects in one scene to understand their visual differences.
- Animated NoiseLearn procedural noise building blocks and common stylizations.