GLSL built-in function

modf()

Returns the signed fractional part and writes the integer part.

Demo version: GLSL ES 3.00 / WebGL 2Demo stage: Fragment

Syntax

genType modf(genType x, out genType integerPart)

genType means float / vec2 / vec3 / vec4; matching placeholders use the same type.

Parameters

x
Input value or left-hand vector
integerPart
Output integer part

Return value

The signed fractional part of x, with the integer part written to integerPart; both preserve x's sign and type.

Live example

Edit the code and the preview updates as you type.

What modf does

Returns the signed fractional part and writes the integer part.

Read the preview

modf(x*4.0,integerPart) draws signed fractional ramps, with negative values on the left. Change 4.0 to 2.0 to halve the number of resets.

Core call

modf(x*4.0,integerPart);

The fraction has the input sign, unlike fract's nonnegative fraction.

Experiment

After the modf call, add result=integerPart/4.0;. The preview switches from fractional ramps to the stepped whole-number part written through the out argument.

Specification

GLSL ES 3.00 built-in functions, chapter 8

Related functions