nearest: Nearest Neighbor Quantization
Coordinate Discretization and Pixelation
The nearest
function quantizes continuous coordinates to discrete grid positions, creating pixelation effects and nearest-neighbor sampling patterns. This transformation maps smooth coordinates to the nearest grid points at a specified resolution.
Mathematical Foundation
The quantization formula applies discrete sampling with offset correction:
Where:
- = input coordinates [0,1]
- = resolution vector (grid dimensions)
- The offset term centers samples within grid cells
ライブエディター
const fragment = () => { const pixelated = nearest(uv, vec2(16, 12)) const pattern = sin(pixelated.x.mul(iTime)).mul(cos(pixelated.y.mul(iTime))) const color = pattern.mul(0.5).add(0.5) return vec4(vec3(color), 1) }