AppStars

Our R&D, 2023–2024

Spell Fighter: a neural network inside a game loop.

Spell Fighter was an R&D prototype built in Unity. You draw a symbol with the mouse, and an on-device neural network recognises it and scores every class between frames, with no server. We proved the pipeline with a standard MNIST model, then trained our own glyph model and ran it the same way.

Owner
Our own R&D
Industry
Games
Services
Edge AI, computer vision, game development
Platforms
Unity 2022.3 and C#, Barracuda, ONNX, Windows desktop
Our role
We designed and built the prototype, its input pipeline and our own glyph model.

The challenge

We wanted to know whether a neural network could sit inside a real-time game loop and answer one question: what did the player just draw? Draw a symbol, and the game casts the matching spell. No server and no network call, just a model running on the player's own machine between frames.

A model has a narrow contract. A handwriting classifier trained on MNIST expects white strokes on a black background, one greyscale channel, 28×28 pixels, scaled into a smaller box and centred with an empty margin. A player's mouse gives you none of that. You get a stream of screen coordinates, once per frame, at whatever speed and size the player happens to draw. A quick flick across a 1080p window can produce a dozen points hundreds of pixels apart. Feed a network that, and it will confidently tell you nonsense.

It also had to stay out of the frame budget. A game that stutters every time you cast a spell isn't a game.

Our approach

Spike first, train second. Before training anything, we used a standard, publicly available MNIST handwritten-digit model in ONNX format, a network of about 26 KB, and ran it inside Unity through Barracuda, Unity's neural-network inference library at the time. Digits stood in for spell glyphs while we proved the capture, the preprocessing and the runtime.

Once that pipeline worked, we trained our own glyph model on our spell symbols and ran it the same way, on the device. That split shows where the engineering really was: not in the model, but in everything between the player's hand and the model's input.

What we built

Stroke capture and densifying

Capture runs while the left mouse button is held. Releasing it ends the stroke and triggers recognition, and the right button clears the canvas. Mouse positions arrive once per frame, so a fast stroke is a scatter of dots. We fill the gaps by stepping along the line between each pair of samples, one point per pixel of the longer axis, the same idea as a line-drawing routine in a software renderer.

Matching the training data, not the screen

Next we find the stroke's bounding box, scale every point into a 24×24 area and offset it by two pixels into a 28×28 greyscale texture, so there's a margin on every side, the way the training images were framed. Each point is drawn as a 2×2 white block on black, closer to a pen than a single-pixel line. This is the step that made recognition work. One trade-off we'd fix in production: the prototype scaled the two axes separately, which stretches tall, thin shapes, so a production version would scale by the longer side.

Inference inside the game loop

One inference worker is created at start-up and reused for every stroke. Recognition builds a one-channel tensor straight from the texture, runs the model, reads the scores and disposes of the input so nothing leaks. Preprocessing and inference happen once per stroke, not once per frame, and every run logs its own inference time. For a model this small the GPU isn't automatically the right choice, because moving a tiny input across can cost more than the maths, so the CPU path is worth measuring too.

Confidence on screen

The model's raw scores go through a softmax, so they become weights that add up to one, and the screen shows the best match and every class's weight. A stroke that wins at 0.95 is different from one that wins at 0.31 with three close rivals. A game built on this needs a threshold: below it, the spell fizzles rather than casting the wrong one.

Architecture

  1. Mouse strokeScreen positions sampled each frame
  2. DensifyFill the gaps between samples
  3. NormaliseFit to 24×24 and centre in a 28×28 texture
  4. InferenceAn ONNX model through Barracuda, on the device
  5. SoftmaxThe best match and a weight for every class

No step in this flow calls a server.

Results

Under the hood

A learned model, running locally, inside the frame loop.

What we’d do now

A prototype's job is to find the problems. This is what we'd change before anyone relied on it.

  • Keep the aspect ratio. Scale by the stroke's longer side, centre the shorter one, and guard the case of a perfectly straight line, which has no width to divide by.
  • Train for real players. Augment the training glyphs with rotation, scale, slant and stroke-width changes, then test on strokes drawn by people who weren't in the training set. A model that only knows its author's handwriting isn't finished.
  • Give the model a way to say no. Add a "none of these" class, or calibrate a rejection threshold on held-out strokes, so scribbles fizzle instead of casting something.
  • Consider the stroke, not just the picture. A raster image throws away direction and order. For glyphs where the drawing order matters, a small sequence model over the points can tell apart shapes that look the same when finished.
  • Use today's runtime. Unity has since replaced Barracuda with a newer inference engine. The contract-first preprocessing would move across unchanged; that's the point of keeping it separate.

What it taught us

Treat a model as a component with a contract. Write down what it expects (size, channels, scale, framing) and make preprocessing the code that honours it; most "the model is bad" problems are really "the input is wrong". Spike with an off-the-shelf model, measure on the device, and show confidence to whoever acts on the result.

Spell Fighter sits in a long line of our gesture work, from mouse gestures to a Kinect gesture engine, which matched with hand-written rules. This one handed the matching to a learned model. The same pattern now runs on phones and smart glasses, and it's the heart of our wearables and Edge AI and computer vision work, where data shouldn't leave the device.

Questions

Did Spell Fighter use a pre-trained model?

To start, yes. We proved the pipeline with a standard MNIST digit model in ONNX. Once capture, preprocessing and inference worked, we trained our own glyph model on our spell symbols.

How does the model run inside Unity?

Through Barracuda, Unity’s inference library at the time. One worker is created at start-up and reused for every stroke, and each run logs its own inference time.

Why was preprocessing the hard part?

The model expects a centred 28×28 greyscale image, and a mouse gives scattered screen coordinates. Densifying, scaling and padding the stroke is what made recognition work.

Does on-device ML work on phones and wearables?

Yes. The same pattern now runs on phones and smart glasses, and it underpins our wearables and Edge AI work.

Related

The services behind this work, and more stories like it.

Services

Case studies

Bring us the hard problem.

A 45-minute discovery call with the engineer who'll build it.