uvec2
Converts other data types, such as float vectors, booleans, or single numbers, into a 2D vector of two unsigned integers (uvec2). The conversion works by truncating (discarding) any fractional part and is mainly used to handle discrete, non-negative coordinates like grid indices or screen pixel coordinates.
Core Advantages
Implements coordinate discretization logic. By converting continuous float coordinates (like UVs) into integer coordinates, it becomes easy to determine which grid cell a point belongs to, forming the basis for all grid- or pixel-based algorithms like checkerboards and pixelation filters.
Common Uses
Creating procedural grid patterns (e.g., checkerboard)
Pixelation/mosaic post-processing filters
Using `texelFetch` for precise texture reads
How to adjust
The output of uvec2 depends entirely on the input value. Its core behavior is 'truncation,' not 'rounding'; for example, uvec2(vec2(9.99, 0.01)) results in uvec2(9, 0). If a single value is input (e.g., uvec2(5.8)), it is truncated and applied to both components, resulting in uvec2(5, 5).
Code Examples
1// Scale continuous UV coordinates (0-1) to define grid density
2const scaledUV = uv().mul( 10 );
3
4// Convert to integer coordinates, e.g., vec2(5.7, 2.3) -> uvec2(5, 2)
5const gridCoord = uvec2( scaledUV );