Intrinsics1 min read

floor

Rounds a value down to the nearest integer.

Reading Time
1 min
Word Count
125
Sections
12
Try It Live

Test floor in a live shader

Open the playground, start from a visual preset, and wire floor into the fragment stage to see how it behaves with real values.

Open Playground

Live Demo

The floor function returns the largest integer that is less than or equal to the input value. This rounds toward negative infinity.

Signature

bwsl
floor :: (T x) -> T {...}

Where T can be float, float2, float3, or float4.

Parameters

ParameterTypeDescription
xTThe input value

Return Value

Returns the largest integer less than or equal to x.

Example

bwsl
pipeline GridPattern {
fragment {
// Create a colored grid
float2 uv = input.uv * 8.0;
float2 cell = floor(uv);
// Color based on cell coordinates
float3 color = float3(
fract(cell.x * 0.1 + 0.5),
fract(cell.y * 0.1 + 0.5),
fract((cell.x + cell.y) * 0.1)
);
output.color = float4(color, 1.0);
}
}

Common Use Cases

Grid Cell Identification

bwsl
// Determine which grid cell a point is in
int2 cellCoord = int2(floor(worldPos.xz / cellSize));

Quantization

bwsl
// Quantize to discrete levels
float quantized = floor(value * levels) / levels;

Brick Pattern Offset

bwsl
// Offset every other row
float2 uv = input.uv * float2(10.0, 5.0);
float row = floor(uv.y);
uv.x += floor(mod(row, 2.0)) * 0.5;

Integer Extraction

bwsl
// Get integer part of value
float intPart = floor(value);
float fracPart = value - intPart; // Same as fract(value)

Stepped Animation

bwsl
// Create stepped/quantized animation
float frame = floor(time * framesPerSecond);
float frameUV = frame / totalFrames;

Negative Values

For negative numbers, floor rounds toward negative infinity: floor(-1.5) returns -2.0, not -1.0.

Compiled Output

When compiled to GLSL:

glsl
floor(x)

When compiled to HLSL:

hlsl
floor(x)

When compiled to Metal:

metal
floor(x)

See Also