How Marchfire works

Marchfire is a squad shooter for the browser, made for a phone held upright and playable on a computer too. Your squad runs up the road and fires on its own. You steer it, free prisoners to make it bigger, and pull one soldier out of the way when a sniper takes aim. It's free on itch.io, with nothing to install and no account.

It's TypeScript and three.js on Vite, with no game engine. Sound goes through the Web Audio API, and the trees come from EZ-Tree, a library that grows them in code.

A straight road, drawn curved

The game itself runs on a straight lane. Steering, aiming and every collision only deal with how far across and how far up the road something is. The road you see winds, because every point is shifted sideways on its way to the screen, by an amount that depends on how far up the road it is.

three.js builds all its shaders from shared pieces, so replacing one piece bends every material at once, shadows included. One line in it does the bending:

vec4 mvPosition = vec4( transformed, 1.0 );
#ifdef USE_BATCHING
  mvPosition = batchingMatrix * mvPosition;
#endif
#ifdef USE_INSTANCING
  mvPosition = instanceMatrix * mvPosition;
#endif
mvPosition = modelMatrix * mvPosition;
mvPosition.xyz = bendWorld( mvPosition.xyz );
mvPosition = viewMatrix * mvPosition;
gl_Position = projectionMatrix * mvPosition;

Each mission lays out its own bends. They're stored as a small table the shaders read, so a new mission doesn't compile any new shaders.

The catch is that anything worked out outside the shaders has to bend too. three.js skips objects that are off screen, but it checked them at their straight-lane position, so crates disappeared while you were looking at them. Bullets followed the road's curve and sagged up to about 2 m over 30 m. Both are fixed: the off-screen test bends first, and bullets fly straight between where the gun and the target are drawn. A tap on the screen is un-bent before the game reads it.

Every mission from a seed

Each mission is generated from a seed made from its number, so mission 12 builds the same road for everyone, every time. There's no last mission. The road keeps going, and the enemies keep getting tougher.

Random layouts can be unfair. A minefield and a row of sandbags can leave no gap to pass, or a crate can land past the point where the squad stops for the last fight. So a script generates the plans for 60 missions, each from five different seeds, without playing any of them. It checks that a soldier can pass at every half metre of road, that rewards sit on the road and clear of mines, and that nothing is left where it can't be reached.

Black on a real phone

The first test on a real phone, a Pixel 9a, drew the sky, the ground, the trees and the soldiers black in five of the seven regions. The HUD and the smoke still drew, and there were no errors.

The cause was the sun. The sky photos store light as numbers, and the sun was brighter than the largest number a half-precision float can hold, 65504. One sky peaked at 211771. three.js blurs the sky into half-precision textures to light the scene. A desktop graphics card clamps a number that's too big, but this phone's Mali GPU turned it into infinity. The blur then turned that into "not a number", and anything lit by the sky came out black. The fix caps every value just under the limit, after the game has measured where the sun is:

/** the largest finite half-float, minus a little so linear filtering can't round past it */
const HALF_MAX = 65000;

for (let k = 0; k < data.length; k++) if (data[k] > HALF_MAX) data[k] = HALF_MAX;

The capped sky looks the same as it did on desktop. The sky files are now stored at a quarter of their brightness as well, so the sun fits.

A phone that runs hot

That phone first played missions at 41 to 49 fps, with stutters up to 225 ms. Measuring settings on the phone itself found two costs that mattered: smoothing edges with 2x multisampling, and the sharpest filtering on the ground textures. Bloom, shadows and triangle counts barely moved it. Phones now get a cheaper edge pass, lighter filtering and a slightly smaller 3D view, and the HUD stays sharp because it's HTML. Missions hold 60 fps, the most Chrome gives a page on that phone.

Then it got hot. After a few minutes Android slowed the phone down, and frames came in uneven bursts with pauses of up to 433 ms. They still averaged 65 to 75 fps, so the automatic quality, which only watched the average, never stepped in. Now it also counts frames that miss the screen's refresh. After three bad seconds it steps down once, effects first and then the size of the 3D view, and it never steps back up, so it can't swing back and forth:

function adaptQuality() {
  const slow = slowFrames;
  slowFrames = 0;
  if (params.has('fixedq') || document.hidden || game.paused) return;
  const playing = game.state === 'play' || game.state === 'finale';
  // measured against the chosen frame rate: at 30 fps a 33 ms frame is on time
  if (playing && (game.stats.fps < targetFps() * 0.75 || slow >= targetFps() / 10)) lowStreak++; else lowStreak = Math.max(0, lowStreak - 1);
  if (lowStreak < 3) return;
  lowStreak = 0;
  if (game.fx.quality > 0.4) game.fx.quality = Math.max(0.4, +(game.fx.quality - 0.3).toFixed(2));
  else if (settings.data.graphics === 'auto' && game.renderer.pixelRatio > 1.25) {
    game.renderer.pixelRatio = Math.max(1.25, +(game.renderer.pixelRatio * 0.86).toFixed(2));
    resize();
  }
}

A lighter scene came after, with compressed textures and smaller buffers. Played from cold for six and a half minutes, every mission held 60 fps with no frame over 50 ms. A 30 fps setting halves the work for anyone who'd rather save battery.

Built by scripts

The soldiers are made by Python scripts that run Blender with no window. They start from a free body model, slim it down, bake the weapon grip into the hands, cut the bones from 65 to 23 and reduce it for phones. Helmets, vests and packs are fitted to it the same way. In the game a soldier's body and kit merge into one mesh, so a fully kitted soldier is a single draw call.

Explosions, smoke and fire are baked too. A script renders 3D noise in a browser with no window and saves it as sprite sheets of about half a megabyte. Sound is recorded clips only, mixed for phone speakers, and nothing is generated while you play.

No server

Progress is saved in the browser, with no account. A save the game can't read is set aside and a fresh one starts. If the browser blocks storage, the game still plays and says progress isn't being saved.

Its own site is a static Next.js export. The field guide on it is generated from the game's source, so a new class or enemy shows up there without anyone writing it in.

What's next

Friends played it and said what bothered them. Dragging felt laggy, the squad filled up within seconds, enemies walked in a line, and the graphics looked average. Each of those is a planned step, starting with the controls, since everything else is played through them. Nobody noted which phone or mission they were on, so each step starts by reproducing the problem.

Back to devedco