Back to Nappakura
SOURCE / PINNED RELEASE

Made of little things.

Nappakura

Release
a75ca0f8b596…
Author-recorded commit
4463126213fa…
License
LICENSE
Author’s source reference
nostr://npub182jczunncwe0jn6frpqwq3e0qjws7yqqnc3auccqv9nte2dnd63scjm4rf/wss%3A%2F%2Fgit.napplet.soy%2F/n-8db03001b62

Archive hash verified: b448d77b767ae754…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

src/depth-renderer.ts
import type { V3 } from './mesh.js';

/** Opaque orthographic rasterization. Centroid sorting cannot handle concave solids. */
export class DepthRenderer {
  readonly image: ImageData;
  readonly depth: Float64Array;
  readonly faces: Int32Array;
  constructor(
    readonly width: number,
    readonly height: number,
  ) {
    this.image = new ImageData(width, height);
    this.depth = new Float64Array(width * height).fill(-Infinity);
    this.faces = new Int32Array(width * height).fill(-1);
  }
  private pixel(x: number, y: number, z: number, color: number[], face?: number) {
    if (x < 0 || y < 0 || x >= this.width || y >= this.height) return;
    const i = y * this.width + x;
    if (z + 1e-6 < this.depth[i]) return;
    if (face !== undefined) {
      this.depth[i] = z;
      this.faces[i] = face;
    }
    this.image.data.set(color, i * 4);
  }
  triangle(p: V3[], color: number[], face: number) {
    const [a, b, c] = p,
      den = (b[1] - c[1]) * (a[0] - c[0]) + (c[0] - b[0]) * (a[1] - c[1]);
    if (Math.abs(den) < 1e-9) return;
    const x0 = Math.max(0, Math.floor(Math.min(...p.map((v) => v[0])))),
      x1 = Math.min(this.width - 1, Math.ceil(Math.max(...p.map((v) => v[0]))));
    const y0 = Math.max(0, Math.floor(Math.min(...p.map((v) => v[1])))),
      y1 = Math.min(this.height - 1, Math.ceil(Math.max(...p.map((v) => v[1]))));
    for (let y = y0; y <= y1; y++)
      for (let x = x0; x <= x1; x++) {
        const u = ((b[1] - c[1]) * (x + 0.5 - c[0]) + (c[0] - b[0]) * (y + 0.5 - c[1])) / den;
        const v = ((c[1] - a[1]) * (x + 0.5 - c[0]) + (a[0] - c[0]) * (y + 0.5 - c[1])) / den;
        if (u >= -1e-9 && v >= -1e-9 && u + v <= 1 + 1e-9)
          this.pixel(x, y, u * a[2] + v * b[2] + (1 - u - v) * c[2], color, face);
      }
  }
  line(a: V3, b: V3, color: number[], width: number) {
    const steps = Math.max(1, Math.ceil(Math.hypot(b[0] - a[0], b[1] - a[1]))),
      radius = Math.max(0.5, width / 2);
    for (let i = 0; i <= steps; i++) {
      const t = i / steps,
        x = a[0] + t * (b[0] - a[0]),
        y = a[1] + t * (b[1] - a[1]),
        z = a[2] + t * (b[2] - a[2]);
      for (let dy = -Math.ceil(radius); dy <= radius; dy++)
        for (let dx = -Math.ceil(radius); dx <= radius; dx++)
          if (dx * dx + dy * dy <= radius * radius + 0.5)
            this.pixel(Math.floor(x + dx), Math.floor(y + dy), z + 1e-4, color);
    }
  }
  visible(p: V3) {
    const x = Math.floor(p[0]),
      y = Math.floor(p[1]);
    return (
      x >= 0 &&
      y >= 0 &&
      x < this.width &&
      y < this.height &&
      p[2] + 1e-4 >= this.depth[y * this.width + x]
    );
  }
  faceAt(x: number, y: number) {
    return this.faces[Math.floor(y) * this.width + Math.floor(x)] ?? -1;
  }
}