GLSL built-in function

trunc()

Truncates toward zero.

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

Syntax

genType trunc(genType x)

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

Parameters

x
Input value or left-hand vector

Return value

The integer part of each component, rounded toward zero and stored in the same floating-point type.

Live example

Edit the code and the preview updates as you type.

What trunc does

Truncates toward zero.

Read the preview

trunc(x*4.0)/4.0 steps toward zero on both sides, leaving a wide zero plateau. Change both 4.0 constants to 2.0 to widen that central plateau further.

Core call

trunc(x*4.0)/4.0;

Unlike floor, trunc(-1.8) is -1.

Experiment

Compare trunc(x*4.0)/4.0 with floor(x*4.0)/4.0. The negative half shifts downward because floor rounds toward negative infinity rather than zero.

Specification

GLSL ES 3.00 built-in functions, chapter 8

Related functions