Skip to content

A town simulation in pale isometric line art, implemented with Ritmo ECS and PixiJs 3D

Draw a lone model's silhouette edges as ribbons, as three.js did

Claude5788fad61096
diff --git a/src/render/pixi3d/assets/ink-mesh.ts b/src/render/pixi3d/assets/ink-mesh.ts
index cd262a9..a6835f6 100644
--- a/src/render/pixi3d/assets/ink-mesh.ts
+++ b/src/render/pixi3d/assets/ink-mesh.ts
@@ -7,9 +7,10 @@
* - hulls: smooth props' and foliage's inward hulls, which give curved
* contours a continuous ink silhouette at every zoom;
* - ink: strokes as six-sided rods, and long curved strokes as tubes;
- * - contours: sharp contours' edges as rods carrying both faces' normals
+ * - contours: sharp contours' edges carrying both faces' normals
* (`aInkNormalA`, `aInkNormalB`), shown only where one face turns towards
- * the camera and the other away (see `inkSilhouetteBit`);
+ * the camera and the other away (see `inkSilhouetteBit`): as rods for the
+ * town's shared models, or as ribbons (`edge`) for one model drawn alone;
* - lines: strokes drawn as camera-facing ribbons a world width across
* (see `inkLineBit`), for the town's ground layers.
*/
@@ -18,14 +19,14 @@ import { vertex3 } from "./space.ts"
import { merge, mergeVertices, polygon, rod, triangles, tube, unindexed, withConstant, type MeshData } from "../mesh/geometry.ts"
import { add, cross, dot, mean, normalize, scale, sub, type Vec3 } from "../mesh/vec3.ts"
-export type PartRole = "fill" | "hull" | "ink" | "contour" | "line"
+export type PartRole = "fill" | "hull" | "ink" | "contour" | "line" | "edge"
export interface InkPart {
readonly role: PartRole
readonly color: number
readonly opacity: number
- /** For `line`: two vertices per segment. */
+ /** For `line` and `edge`: two vertices per segment. */
readonly mesh: MeshData
- /** For `line`: the ribbon's width in world units. */
+ /** For `line` and `edge`: the ribbon's width in world units. */
readonly width: number
}
export interface InkMeshOptions {
@@ -37,6 +38,8 @@ export interface InkMeshOptions {
readonly hullContours?: boolean
/** Sharp contours drawn as silhouette edges (default), or left to the faces. */
readonly contours?: boolean
+ /** Silhouette edges as rods (the town's shared models), or as ribbons (default). */
+ readonly contourRods?: boolean
}
const fixed = (p: Vec3) => p.map((n) => n.toFixed(4)).join(",")
@@ -142,6 +145,20 @@ export function inkMesh(asset: InkAsset, options: InkMeshOptions = {}): Array<In
}
// GPU silhouette masks let sharp foliage share instances at every camera angle, without a wireframe or an
// expanded hull crossing the canopy.
+ if (!options.contourRods) {
+ const ribbons = new Map<number, { positions: Array<number>; a: Array<number>; b: Array<number> }>()
+ for (const { a, b, normals, width } of edges.values()) {
+ if (width <= 0) continue
+ const n1 = normals[0]!, n2 = normals[1] ?? scale(n1, -1), batch = ribbons.get(width) ?? { positions: [], a: [], b: [] }
+ batch.positions.push(...a, ...b)
+ batch.a.push(...n1, ...n1)
+ batch.b.push(...n2, ...n2)
+ ribbons.set(width, batch)
+ }
+ for (const [width, { positions, a, b }] of ribbons)
+ parts.push({ role: "edge", color: palette.ink, opacity: 1, width, mesh: { positions: Float32Array.from(positions), normals: new Float32Array(positions.length), index: null, attributes: { aInkNormalA: { data: Float32Array.from(a), size: 3 }, aInkNormalB: { data: Float32Array.from(b), size: 3 } } } })
+ return parts
+ }
const contourBatches = new Map<number, Array<MeshData>>()
for (const { a, b, normals, width } of edges.values()) {
if (width <= 0) continue
diff --git a/src/render/pixi3d/assets/renderer.ts b/src/render/pixi3d/assets/renderer.ts
index 0f0098e..578a420 100644
--- a/src/render/pixi3d/assets/renderer.ts
+++ b/src/render/pixi3d/assets/renderer.ts
@@ -42,7 +42,7 @@ export function buildAsset(asset: InkAsset, options: InkMeshOptions & { surfaceE
setSurface(material, effects.state, [effects.half * floorScale, effects.height / Math.cos(Math.PI / 6), 0.31, 1], 0)
surfaces.push(material)
}
- const mesh = new Mesh3D({ geometry: part.role === "line" ? lineGeometry(part.mesh.positions) : toGeometry(part.mesh), material, castShadow: false })
+ const mesh = new Mesh3D({ geometry: part.role === "line" || part.role === "edge" ? lineGeometry(part.mesh.positions, part.mesh.attributes) : toGeometry(part.mesh), material, castShadow: false })
group.addChild(mesh)
return { part, mesh }
})
diff --git a/src/render/pixi3d/gpu.ts b/src/render/pixi3d/gpu.ts
index 7c7c0e4..b76e35d 100644
--- a/src/render/pixi3d/gpu.ts
+++ b/src/render/pixi3d/gpu.ts
@@ -38,13 +38,17 @@ export function toGeometry(mesh: MeshData, extent?: readonly [Vec3, Vec3]): Geom
/**
* Line segments (`positions`: two points a segment) as ribbons: four corners
* a segment, each carrying both ends and which corner it is, for
- * `inkLineBit` to turn to the camera and widen.
+ * `inkLineBit` to turn to the camera and widen. Each point's `attributes`
+ * (two values a segment) go to its end's two corners.
*/
-export function lineGeometry(positions: Float32Array): Geometry3D {
+export function lineGeometry(positions: Float32Array, attributes: MeshData["attributes"] = {}): Geometry3D {
const segments = positions.length / 6
const at = new Float32Array(segments * 12), start = new Float32Array(segments * 12), end = new Float32Array(segments * 12), corner = new Float32Array(segments * 8)
const index: Array<number> = []
+ const extra = Object.fromEntries(Object.entries(attributes).map(([name, a]) => [name, { data: new Float32Array(segments * 4 * a.size), size: a.size, source: a.data }]))
for (let s = 0; s < segments; s++) {
+ for (const a of Object.values(extra))
+ for (let k = 0; k < 4; k++) a.data.set(a.source.subarray((s * 2 + (k >= 2 ? 1 : 0)) * a.size, (s * 2 + (k >= 2 ? 1 : 0) + 1) * a.size), (s * 4 + k) * a.size)
const a = positions.subarray(s * 6, s * 6 + 3), b = positions.subarray(s * 6 + 3, s * 6 + 6)
for (let k = 0; k < 4; k++) {
const v = s * 4 + k, far = k >= 2
@@ -56,7 +60,7 @@ export function lineGeometry(positions: Float32Array): Geometry3D {
const v = s * 4
index.push(v, v + 2, v + 1, v + 1, v + 2, v + 3)
}
- const mesh: MeshData = { positions: at, normals: new Float32Array(at.length), index: Uint32Array.from(index), attributes: { aLineStart: { data: start, size: 3 }, aLineEnd: { data: end, size: 3 }, aLineCorner: { data: corner, size: 2 } } }
+ const mesh: MeshData = { positions: at, normals: new Float32Array(at.length), index: Uint32Array.from(index), attributes: { aLineStart: { data: start, size: 3 }, aLineEnd: { data: end, size: 3 }, aLineCorner: { data: corner, size: 2 }, ...Object.fromEntries(Object.entries(extra).map(([name, a]) => [name, { data: a.data, size: a.size }])) } }
return toGeometry(mesh)
}
diff --git a/src/render/pixi3d/materials.ts b/src/render/pixi3d/materials.ts
index 2ba15f8..2c3d22a 100644
--- a/src/render/pixi3d/materials.ts
+++ b/src/render/pixi3d/materials.ts
@@ -13,8 +13,9 @@ import { faceBiasBit, GLASS, inkLineBit, inkSilhouetteBit, instanceBit, instance
import type { InkPart, PartRole } from "./assets/ink-mesh.ts"
import type { SurfaceState } from "./surface-state.ts"
-const roleBits: Record<PartRole, ReadonlyArray<ShaderBit>> = { fill: [faceBiasBit], hull: [], ink: [], contour: [inkSilhouetteBit], line: [inkLineBit] }
-const doubleSided = (role: PartRole) => role === "fill" || role === "line"
+const roleBits: Record<PartRole, ReadonlyArray<ShaderBit>> = { fill: [faceBiasBit], hull: [], ink: [], contour: [inkSilhouetteBit], line: [inkLineBit], edge: [inkLineBit, inkSilhouetteBit] }
+const ribbon = (role: PartRole) => role === "line" || role === "edge"
+const doubleSided = (role: PartRole) => role === "fill" || ribbon(role)
/** A part of one model; `surface` gives its faces the structure shading, with its own state. */
export function partMaterial(part: InkPart, surface: boolean): FlatMaterial {
@@ -22,11 +23,11 @@ export function partMaterial(part: InkPart, surface: boolean): FlatMaterial {
baseColor: part.color,
alpha: part.opacity,
// A stroke's soft edge blends, as three.js's alpha-to-coverage lines did.
- alphaMode: part.opacity < 1 || part.role === "line" ? "blend" : "opaque",
+ alphaMode: part.opacity < 1 || ribbon(part.role) ? "blend" : "opaque",
doubleSided: doubleSided(part.role),
extraBits: [...(surface && part.role === "fill" ? [materialSurfaceBit] : []), ...roleBits[part.role]]
})
- if (part.role === "line") {
+ if (ribbon(part.role)) {
material.data.lineWidth = part.width
material.data.lineAlpha = part.opacity
}
diff --git a/src/render/pixi3d/pool.ts b/src/render/pixi3d/pool.ts
index 7d5fd08..d4c94ff 100644
--- a/src/render/pixi3d/pool.ts
+++ b/src/render/pixi3d/pool.ts
@@ -83,7 +83,7 @@ export class ModelPool {
acquire(key: string, create: () => InkAsset): Template {
let entry = this.templates.get(key)
if (!entry) {
- const pieces = inkMesh(create(), { batchFaces: true, meshInk: true, contours: true })
+ const pieces = inkMesh(create(), { batchFaces: true, meshInk: true, contours: true, contourRods: true })
for (const p of pieces) if (p.role === "line" && p.width > 0) throw new Error("Live model ink must be portable mesh geometry")
const solid = pieces.filter((p) => p.role !== "line")
const extents = solid.map((p) => bounds(p.mesh))