Skip to main content
Shaders for Games (GLSL) Lesson 18 of 20

Sampling an image

Feed a picture into a shader and read its pixels with texture2D — the door to filters.

So far every color came from math. But shaders can also read an actual IMAGE — a photo, a sprite, the previous frame — and do things to it. The image is handed to the shader as a texture in the uniform u_texture, and you read a pixel from it with texture2D(u_texture, coord), where coord is a 0-to-1 point just like st.

A test image is provided for you here in u_texture. Sampling it at st draws it straight onto the screen; because coord is just a coordinate, anything you do to that coordinate before sampling — flip it, scale it, swirl it — transforms the image. This is the whole basis of photo filters, sprite effects and screen post-processing.

That read the supplied image and multiplied its colors by a warm tint. Try sampling at 1.0 - st to flip it, or st * 2.0 to zoom in. The image is your raw material now.

Tip The provided image is fixed, so this intro is a playground — the next lesson turns sampling into a real, graded image filter. Try flipping, zooming or tinting it here first.
Quick check

How does a shader read a pixel from an image?

▶ Play with the image Sample u_texture at a flipped, zoomed or swirled coordinate to warp the picture.
Wayline
Tools, assets and tutorials for game makers
Get started