A hand-written 3D engine, with no libraries at all

Most 3D games on the web are built on an existing library or game engine that takes care of rendering, lighting and shadows. Superkart and Warfront use neither. Their renderer fits in a single file, written specifically for them, that talks directly to the graphics card. The result: Superkart weighs less than 500 kilobytes in total, 210 of which are the menu font. Here's how the engine works, and why it can be so small.
WebGL, with nothing in between
Browsers give access to the graphics card through WebGL. It's a very low-level interface: you send it lists of vertices, images, and small programs called shaders, which run on the graphics card to work out the position and color of every pixel. Everything else, you have to write yourself.
The engine first asks for WebGL 2, the newer version, and falls back to WebGL 1 if the browser doesn't support it. The shaders are written once, in the older version's syntax, then automatically rewritten for the newer one with simple text replacements. The engine also requests antialiasing, which smooths the edges of objects, and the most powerful graphics card in the machine when there are two.
Lighting, baked once and for all
In a typical engine, lighting is recalculated for every pixel on every frame, based on the orientation of the surfaces. Here, there's no lighting on the graphics card at all. Each face gets a fixed brightness when the object is built, based on its direction relative to an imaginary sun: the tops of objects are brightest, the bottoms darkest, and the sides somewhere in between. The values range from about 62% to 100% of the texture's color.
That brightness is stored in each vertex's color. All the shader has to do is multiply the texture color by the vertex color. It doesn't even receive the orientation of the faces, which would be no use to it. It's the classic lighting of block games: simple, readable, and almost free to draw.
The shader does two other things. It discards transparent texture pixels, which makes it possible to draw leaves or grates without sorting objects. And it adds fog, which gradually blends the scenery into the sky color with distance. In Superkart, it starts at 160 meters; in Warfront, it starts at 70 meters, and the scenery vanishes completely at 330 meters.
An entire scene in a single draw call
What's expensive in 3D isn't so much the number of triangles as the number of calls to the graphics card: every "draw this" has a fixed cost. So the engine groups together everything that doesn't move.
Objects are described as lists of boxes, quads and triangles. At build time, each shape is placed in its final position, and all of its vertices are copied into one big array: three numbers for the position, two for the texture, three for the color. In Superkart, the entire scenery, including the barriers, the gantry, the grandstand and its crowd, the flags and the hundred and ten trees, makes up 25,820 triangles in a single array, with a single texture, drawn in a single call. The whole world comes to just over 31,000 triangles.
A kart, on the other hand, is made of five parts and 288 triangles in total, and its bodywork is tinted in its driver's color. All in all, a frame of Superkart takes about a hundred draw calls, which a phone's graphics chip handles with ease.
Warfront takes this principle further. Its world is a grid 256 by 160 meters, and 32 high, made of one-meter blocks. The engine keeps only the faces exposed to air: a face pressed against another block will never be seen, so there's no point drawing it. The ground is a heightmap with 2-meter cells, and a strip of terrain rising over 14 meters all around creates the illusion of distant hills.
Square shadows
Computing real shadows means drawing the scene a second time, from the sun's point of view. The engine does without. Under each kart and each soldier, it places three nested, semi-transparent squares, slightly rotated: up close, a soft, square shadow that fits the game's style. In Superkart, it's dark green and never black, so it looks like a shadow on grass rather than a hole.
A sky with a square sun
The sky isn't an image. It's a single triangle big enough to cover the whole screen, behind the scenery. For each pixel, the shader works out the viewing direction and derives the color from it: a gradient from the horizon to the top of the sky, and a square sun, in the spirit of block games, also computed directly by the shader. The clouds, meanwhile, drift slowly by.
Little rendering tricks
- Layers on the ground. Superkart's track, curbs and lines are flat surfaces laid at the same height. Without care, the graphics card can't tell which one is on top, and the image flickers. The engine offsets them ever so slightly in depth, layer by layer.
- Texture edges. Textures are packed side by side in large atlases. With antialiasing, the graphics card may read a pixel just outside the right cell, and a foreign color shows up along the edges. WebGL 2 makes it possible to prevent this by sampling the texture at the center of the pixels actually covered.
- Particles. Sparks and flames are simple square points whose size depends on distance, up to 1,500 at a time, added onto the image so they glow.
- Free motion. Water, clouds and tires are animated simply by sliding their texture.
- Warfront's rifle. The first-person weapon is drawn last, after clearing the depth buffer, with its own perspective: it never clips into a wall.
Superkart's camera
The camera follows the kart with a slight lag, which gives a sense of weight. It pulls back and rises a little with speed, and the field of view widens: from 64 degrees at a standstill to nearly 80 at full speed, plus another 7 during a boost. It's this widening, more than the speed itself, that creates the feeling of going fast. During a drift, it lags a little behind the kart's rotation, so you can see it slide. Before the start, it slowly circles the player's kart.
Time, sliced into fixed steps
Rendering follows the screen's refresh rate, but the physics advances in fixed steps: 120 per second in Superkart, 60 in Warfront. If a frame falls behind, the game runs several steps at once to catch up. That way, a kart goes exactly the same speed on a 60 Hz screen as on a 144 Hz one.
Why write it all yourself
A general-purpose engine can do everything, and you have to download all of it, even if you only use a small part. An engine written for one specific style contains only what that style needs. Here, there are no dynamic lights, no computed shadows, no loading of models or images: everything is built by code at startup. Even the textures are painted pixel by pixel, as explained in the article on textures without images.