跳到正文
DEV Community· Voxipaw·· 41 分鐘前AI 評分49

在普通 2D 畫布上繪製體素寵物(不用 WebGL 或 three.js)

Drawing voxel pets on a plain 2D canvas (no WebGL, no three.js)

AI 導讀

Voxipaw 以約 350 行 JavaScript 編寫的小型光線投射器,在普通 2D 畫布上繪製及動畫呈現體素寵物,無需 WebGL 或 3D 函式庫。場景物件會預先繪製並快取,只有移動中的寵物需要重新繪製;「lite」模式會降低寵物重繪頻率,並每秒計算水面光線 20 次。寵物會在約 50 小時的專注時間內,透過調整身體比例從幼犬逐漸長大。

正文

I wanted chunky, cute voxel pets for a focus timer, and I wanted them to stay smooth on a cheap Android phone. So instead of WebGL or a 3D library, the pets are drawn by a tiny ray caster that writes pixels into an ordinary 2D canvas. The whole engine is about 350 lines.

Here's how it works, and what I learned. (The app is Voxipaw: your pet grows up a little with every minute you focus. Everything below is running there.)

1. A pet is a list of boxes

Every pet is just an array of oriented boxes: body, head, snout, ears, four legs, tail. Each box has a centre, half-sizes, an optional rotation matrix, a colour family (fur, belly, ear…) and a part id.

function block(c, size, fam, part, opts = {}) {
  return { c, h: { x: size.x / 2, y: size.y / 2, z: size.z / 2 }, fam, part, ...opts };
}

Animation never touches pixels. A pose is a handful of numbers (tail wag, head turn, leg swing, ear angle), and each frame we rebuild the box list by rotating parts around pivot points: legs around the hip, the tail around its base, ears around the top of the head.

2. Each pixel shoots a ray

The camera is a true isometric one (yaw 45°, pitch 30°), so it's orthographic: every ray is parallel. For each pixel in the sprite's bounding box we start a ray far in front of the pet and test it against every box.

The ray-box test happens in the box's own frame (multiply by the inverse rotation), which turns it into the classic axis-aligned slab test. It returns the distance, which axis was hit (x, y or z) and the sign. That's the face.

// nearest box wins
for (const b of blocks) {
  const hit = hitBlock(origin, dir, b);
  if (hit && hit.t < depth[i]) { depth[i] = hit.t; id[i] = b.part; col[i] = shadeOf(b, hit); }
}

It sounds slow, and per pixel it is, but the sprites are small and a pet is only a few dozen boxes, so it's fast enough to animate smoothly in plain JavaScript.

3. Shading with four tones, not lighting maths

Real lighting looks muddy at this size. Instead, every colour family has a ramp of four hand-picked shades, and the face normal picks one:

const lam = dot(normal, LIGHT);
const tone = normal.y > 0.6 ? 0 : lam > 0.4 ? 1 : lam > 0.18 ? 2 : 3;

Tops are always the brightest tone, faces towards the light the next, and so on. That's the whole "voxel art" look: flat, crisp, readable.

4. Outlines for free

Because we kept a part id for every pixel, outlines are a second pass: if a pixel's neighbour belongs to a different part (or is empty), darken it. You get the dark edge around the silhouette and the soft lines between head and body, with no geometry work.

Faces (eyes, nose, a little mouth when it barks) are painted by a per-face callback: a box can say "on my x+ face, at these local coordinates, paint eye".

5. Growing up is just proportions

The pet grows from a tiny puppy to a grown dog over about 50 hours of focus. There is only one rig. Age is a number from 0 to 1, and it changes proportions:

  • head size shrinks relative to the body,
  • legs and body get longer,
  • the snout gets longer, ears less floppy.

So the same box list, with different sizes, covers every age. No new art per stage.

A voxel golden retriever on day 1, after 10 hours and after 50 hours of focus

6. Keeping it smooth on cheap phones

  • Ray-cast only what moves. The home's scenery and items (kennel, trees, toys) are ray-cast once and cached as images; only the pet is re-rendered as it moves.
  • Draw the pet at 1×, scale with imageSmoothingEnabled = false. The canvas does the upscaling for free and it stays crisp.
  • A "lite" mode. If the frame time stays above ~22 ms, the app re-renders the pet less often and works out the water light only 20 times a second. It tries normal mode again later, so one slow moment doesn't stick.

What I'd do differently

  • I started with AI-generated illustrations cut into a 2D rig. It never looked right when the pet moved. Going fully voxel and drawing everything from boxes fixed that, and it made the art far easier to change.
  • The pet is still ray-cast fresh for each pose. Caching recent poses would save more battery on slow phones, and it's next on my list.

Try it

The app is free in the browser, no account: voxipaw.kionesis.com. Pick an animal, start a timer, and watch it nap. If you're curious how a specific part works (the water, the walking, the home growing), ask in the comments.

Transparency note: Voxipaw was built with the help of an AI coding assistant, and this post was drafted with AI help too. The pets and art are drawn by the code, not by image AI.

來源:DEV Community · dev.to