fract
Returns the fractional part of a value.
Test fract in a live shader
Open the playground, start from a visual preset, and wire fract into the fragment stage to see how it behaves with real values.
Open Playgroundfract :: (T x) -> T {...}
The fract function returns the fractional part of the input value, computed as x - floor(x). The result is always in the range [0, 1).
Where T can be float, float2, float3, or float4.
Example - Tiling Pattern
Parameters
| Parameter | Type | Description |
|---|---|---|
x | T | The input value |
Return Value
Returns the fractional part of x, always in range [0, 1).
Common Use Cases
UV Tiling
Use fract to tile textures or patterns multiple times across a surface.
Checkerboard Pattern
Combine fract with step to create classic checker patterns.
Scrolling Animation
Use fract with time to create seamless scrolling effects.
Brick Pattern
Offset alternating rows using fract to create brick layouts.
Radial Repetition
Use fract with polar coordinates for radial patterns.
Negative Values
For negative inputs, fract still returns values in [0, 1). For example, fract(-0.3) returns 0.7, not -0.3. This is because fract(x) = x - floor(x) and floor(-0.3) = -1.
Avoiding Seams
When using fract for tiling, multiply by whole numbers to ensure clean tile boundaries. Non-integer multiples can cause visible seams at texture edges.
Compiled Output
When compiled to GLSL:
fract(x)
When compiled to HLSL:
Uses the target backend's native fractional-part operation.
When compiled to Metal:
fract(x)