wiwis.dev

Textures without images: painting pixels with code

· Warfront

Warfront in first-person view: a red team soldier facing the player, a blue soldier holding his rifle in both hands

In Superkart and Warfront, the grass, the asphalt, the bricks, the sandbags, the flags and even the crowd in the grandstand are all pixel textures. Yet neither game contains a single image file. Every texture is painted by code, pixel by pixel, in a fraction of a second at startup. Here's how.

A texture is just an array of numbers

To a graphics card, an image is nothing more than a sequence of numbers: four per pixel, for red, green, blue and transparency. The engine behind these games creates that array directly in memory, then fills it in cell by cell with a small function that takes the pixel's position and returns its color. No drawing with a mouse, no file to download: just a few lines of code per texture.

The scale is the same everywhere: 16 pixels per meter. A one-meter block is therefore a 16 by 16 pixel tile, and every object in both games shares the same level of detail.

The recipe for grain

A flat color looks dull and artificial. The whole challenge is producing a natural grain without sliding into the kind of random noise that looks like TV static. The method used here mixes two ingredients:

The mix doesn't produce the color directly. It's used to pick a shade from a short palette of three to five similar colors, for example five greens for grass. That's what gives the pixel-art look: each pixel takes one of these few shades, never anything in between. So that the change from one shade to the next doesn't form hard borders, a two-by-two ordered dither alternates the two neighboring shades in the transition zones, just like the consoles of old used to.

All the randomness is driven by fixed seeds: the same calculation produces exactly the same texture on every launch, on every device.

Details layered on top

On top of that base, each texture adds its own details, using simple rules:

Warfront follows the same method for a larger catalog. The ground has four materials, in 4-meter tiles: grass dotted with the odd yellow flower, dirt with its pebbles, mud with puddles, and gravel. The blocks all fit in a single atlas: stone with staggered joints, cracked stone, planks, sandbags stacked in offset rows, bark and tree rings, foliage, crates, concrete, riveted metal, cobblestones, bricks, hay, tent canvas, blue and red flags, and the steel of the tank traps. The vehicles have their mottled olive camouflage, with their team's square marking, and the rifle has its own little texture.

Materials that blend in pixels

On Warfront's ground, you need to go from grass to dirt, or from dirt to mud. A smooth fade would have clashed with the style. Instead, each ground pixel picks a single material, whichever one dominates at that spot, after a small random roll. The border between two materials thus becomes a ragged edge of pixels, as in a block game, rather than a blurry gradient.

Keeping pixels sharp

A pixel texture loses all its appeal if the graphics card smooths it. So both games display their textures with no smoothing at all: magnified, each pixel stays a crisp square. The ground poses a particular problem: in the distance, a highly detailed texture seen at a grazing angle shimmers. For the ground alone, the engine prepares scaled-down versions of the texture, and the graphics card picks the right one based on distance, with filtering that keeps the grass clean all the way to the horizon.

The textures are packed side by side in large atlases, so they can be sent to the graphics card in one go. Their coordinates are pulled slightly inward from the edges of each cell, by a hundredth of a pixel, so that an edge never picks up the color of the neighboring cell.

Even the character

Dylan_PvP, the character shared by both games, is painted the same way. His texture is generated on a 256 by 64 pixel atlas, and his face uses pixels four times finer than the rest of his body. When he blinks, the game simply repaints the face area. The article Dylan_PvP: one character, two games goes into how he's built.

Why go to all this trouble

Painting textures with code takes more thought than drawing them in an editor. But the result has three advantages. It's lightweight: a few lines of code replace dozens of images, and the game loads quickly, even on a slow connection. It's consistent: every texture follows the same scale, the same grain method and the same color palettes. And it's easy to change: adjusting the shade of the grass or the density of the gravel comes down to changing a number. The engine that displays these textures is covered in the article a hand-written 3D engine.

Play WarfrontGame page

Read next