Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(modeling): a drawing stands up as a solid, sculpt works a surface by stroke, and a view takes coordinates off a picture

yhh3fafbb7dedc5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0ce71b947..3787acd49 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,10 @@ published separately; it ships inside the editor.
- **Glyphs can be put on the atlas before they are shown.** The first page of a novel is hundreds of new characters, all landing in one frame. `preloadGlyphs(app, label, text, { budgetMs })` makes them ahead of time, a slice a frame, drawn as that `Text` will draw them, and returns `progress`, `done` and `release()`. On a Snapdragon 8 Elite a book's 2992 characters at 28 px took 145 ms over 25 frames with the worst frame at 9.5 ms, after which the first page's frame went from 49 to 11 ms and no page made a new glyph. Preloaded glyphs hold their atlas pages (4 MB each, a few hundred characters at that size) until `release()`.
- **Voxel GI lights what the camera does not see, and bounces light more than once.** The cache now reads the sun's shadow map back into the world, so a surface the sun lights behind or beside the camera still sends light into the view. Each surface also gives off again the bounce light it received, so a corner or a floor in shade gets light that took two or more bounces to reach it. A ray now brings what it meets at full strength however far it went (it used to fade with distance, 40% at 4 m with 12 cm voxels). Each voxel is weighed by how much of it a surface covers, so a bright patch no longer lights like the whole voxel around it. Together these keep the result close to a path tracer's whatever the voxel size. In a sunlit test room the whole frame carries 0.95 of Cycles' light at the default size and 0.74 at 12 cm voxels; before, it was 0.54 at the default and darker the finer the grid. With the lit patch behind the camera, the walls went from no bounce light to 25–57%. A closed room the sun only lights from outside stays dark. Two inspector controls tune it: **Off-screen Sun Albedo** (the colour the shadow map cannot give, assumed) and **Multi-bounce**. A fine grid no longer drops every voxel: a count truncated by the cache's ageing had kept them below the two samples a ray asks for. The WebGL2 fallback does the same in its own passes: each voxel asks the sun's map whether a sunlit surface stands in it, and traces two rays through last frame's grid for the light it is given. In the test room it carries 1.00 of Cycles' light at the default size (it was 0.56) and 0.80 at 12 cm; with the lit patch behind the camera 0.20 (it was 0). Its voxel pass costs about 1.3 ms more at 720p on a desktop.
+- **An AI can read a shape off a picture and model it.** `reference_view` takes a `ruler`, a numbered grid of where the picture falls in the world through a camera, so a nose or a fold can be read off as coordinates. An `organic` part can be an `outline`, a closed drawing stood up as a rounded solid, so a traced profile is the silhouette. `sculpt` works a surface stroke by stroke: `press` along a line for a fold or a seam, `inflate`, `move`, `smooth`, and with `from` a stroke or a `stain` line is given in the two axes of a view and lands on what that view sees.
+
+- **`reference_compare` with a `region` shows the same part of both pictures.** Through a camera it drew the game at the shape of the part, so the cut took another place from another picture. `field_sweep` did the same.
+
- **An AI can model a character's hair by where it goes, and draw on a surface.** `hair` takes `guides`, lines the cards follow over the head rather than through it, with `gather` closing them into a ponytail. `stain` draws along a `line`: an eyelid, a lip, a seam. `scatter` puts hundreds of copies of a shape across a surface, along a path or through a box. `material.emissive` makes a model give off light, past 1 for what glows and blooms. `apply_scene_ops` now says which field a component does not have, where it used to drop it without a word.
- **An AI can put the game beside the pictures it is imitating.** The `look` tool group takes pictures or a video's frames, and `reference_compare` sets a frame of the game beside one with the reference's outline drawn over it and the exact colours of both. `field_sweep` shows a field at several values on one sheet, so what a number does is seen before it is chosen. `motion_sheet` lays the running game out over time, under a video's frames. `scene_views` shows the scene as the engine draws it from every side, in clay, paint and wireframe.
diff --git a/desktop b/desktop
index fc2d9925f..6424ca765 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit fc2d9925f0cce7478608ef2aa102dfa62c58c040
+Subproject commit 6424ca7657534201c33397596050f56885ba412b
diff --git a/pipeline/src/look/pictures.ts b/pipeline/src/look/pictures.ts
index 0b7c2344b..edf7c72f1 100644
--- a/pipeline/src/look/pictures.ts
+++ b/pipeline/src/look/pictures.ts
@@ -219,6 +219,80 @@ const filled = (width: number, height: number): Uint8Array => {
return rgba;
};
+/** A 3x5 digit, row by row from the top, one bit a column. */
+const GLYPHS: Record<string, readonly number[]> = {
+ '0': [7, 5, 5, 5, 7], '1': [2, 6, 2, 2, 7], '2': [7, 1, 7, 4, 7], '3': [7, 1, 7, 1, 7], '4': [5, 5, 7, 1, 1],
+ '5': [7, 4, 7, 1, 7], '6': [7, 4, 7, 5, 7], '7': [7, 1, 2, 2, 2], '8': [7, 5, 7, 5, 7], '9': [7, 5, 7, 1, 7],
+ '-': [0, 0, 7, 0, 0], '.': [0, 0, 0, 0, 2],
+};
+const GLYPH_SCALE = 2;
+const RULER_WIDTH = 1400;
+
+function label(image: RgbaImage, text: string, x: number, y: number): void {
+ const { width, height, rgba } = image;
+ const w = text.length * 4 * GLYPH_SCALE + GLYPH_SCALE, h = 7 * GLYPH_SCALE;
+ const set = (px: number, py: number, v: number): void => {
+ if (px < 0 || py < 0 || px >= width || py >= height) return;
+ const at = (py * width + px) * 4;
+ rgba[at] = v; rgba[at + 1] = v; rgba[at + 2] = v;
+ };
+ for (let py = 0; py < h; py++) for (let px = 0; px < w; px++) set(x + px, y + py, 0);
+ [...text].forEach((c, n) => {
+ const rows = GLYPHS[c];
+ if (!rows) return;
+ rows.forEach((bits, row) => {
+ for (let col = 0; col < 3; col++) {
+ if (!((bits >> (2 - col)) & 1)) continue;
+ for (let a = 0; a < GLYPH_SCALE; a++) for (let b = 0; b < GLYPH_SCALE; b++) set(x + GLYPH_SCALE + (n * 4 + col) * GLYPH_SCALE + a, y + GLYPH_SCALE + row * GLYPH_SCALE + b, 255);
+ }
+ });
+ });
+}
+
+/** What a picture's edges are in some other measure: its left and right, its top and bottom. */
+export interface RulerSpan { left: number; right: number; top: number; bottom: number }
+
+/** A round distance between lines that puts about a dozen of them across this span. */
+export function rulerStep(across: number): number {
+ const rough = Math.abs(across) / 12;
+ const power = 10 ** Math.floor(Math.log10(rough));
+ return [1, 2, 5, 10].map((k) => k * power).find((k) => k >= rough)!;
+}
+
+/**
+ * A picture with a numbered grid on it, so a place in it can be read off as coordinates: what is seen can then be written down
+ * as an outline or a line to model. Enlarged first, as a close-up is few pixels and the numbers want room. Every fifth line is stronger.
+ */
+export function rulerOver(source: RgbaImage, span: RulerSpan, step = rulerStep(span.right - span.left)): RgbaImage {
+ if (!(step > 0)) throw new Error(`step ${step} must be positive`);
+ const k = Math.max(1, RULER_WIDTH / source.width);
+ const scaled = k === 1 ? source : resizeImage(source, Math.round(source.width * k), Math.round(source.height * k));
+ const image: RgbaImage = { width: scaled.width, height: scaled.height, rgba: Uint8Array.from(scaled.rgba) };
+ const { width, height, rgba } = image;
+ const lighten = (at: number, share: number): void => {
+ rgba[at] = rgba[at]! + (255 - rgba[at]!) * share; rgba[at + 1] = rgba[at + 1]! + (255 - rgba[at + 1]!) * share; rgba[at + 2] = rgba[at + 2]! + (255 - rgba[at + 2]!) * share;
+ };
+ const digits = Math.max(0, Math.ceil(-Math.log10(step) - 1e-9));
+ const lines = (lo: number, hi: number): number[] => {
+ const [a, b] = lo < hi ? [lo, hi] : [hi, lo];
+ const out: number[] = [];
+ for (let n = Math.ceil(a / step - 1e-9); n * step <= b + step * 1e-9; n++) out.push(n);
+ return out;
+ };
+ const across = lines(span.left, span.right), down = lines(span.top, span.bottom);
+ for (const n of across) {
+ const x = Math.round(((n * step - span.left) / (span.right - span.left)) * (width - 1));
+ for (let y = 0; y < height; y++) lighten((y * width + x) * 4, n % 5 === 0 ? 0.55 : 0.22);
+ }
+ for (const n of down) {
+ const y = Math.round(((n * step - span.top) / (span.bottom - span.top)) * (height - 1));
+ for (let x = 0; x < width; x++) lighten((y * width + x) * 4, n % 5 === 0 ? 0.55 : 0.22);
+ }
+ for (const n of across) label(image, (n * step).toFixed(digits), Math.round(((n * step - span.left) / (span.right - span.left)) * (width - 1)) + 2, 0);
+ for (const n of down) label(image, (n * step).toFixed(digits), 0, Math.round(((n * step - span.top) / (span.bottom - span.top)) * (height - 1)) + 2);
+ return image;
+}
+
/** Pictures side by side at one height, with a thin line between. */
export function sideBySide(images: readonly RgbaImage[], height = 360): RgbaImage {
const scaled = images.map((img) => resizeImage(img, Math.max(1, Math.round((img.width * height) / img.height)), height));
diff --git a/pipeline/src/modeling/modelSource.ts b/pipeline/src/modeling/modelSource.ts
index 9880d4502..ffff46f3a 100644
--- a/pipeline/src/modeling/modelSource.ts
+++ b/pipeline/src/modeling/modelSource.ts
@@ -21,6 +21,8 @@ import { subdivide } from './subdivide';
import { decimate, triangleCount } from './decimate';
import { displace, DISPLACE_PATTERNS } from './displace';
import { stain } from './stain';
+import { sculpt } from './sculpt';
+import { VIEW_NAMES } from './surfaceLine';
import { cardsOnly, hair, hasCards, type HairSway } from './hair';
import { scatter } from './scatter';
import { checkAnimations, checkController, checkRig, checkShapes, resolveJoints, type AnimationSource, type ControllerSource, type RigSource, type ShapeSource } from './rig';
@@ -110,7 +112,7 @@ export interface NormalBakeSource {
}
type Kind = 'number' | 'integer' | 'boolean' | 'string' | 'vec3' | 'number|vec3' | 'number|vec2' | 'number|numbers'
- | 'color' | 'points' | 'faces' | 'edges' | 'axis' | 'pivot' | 'parts' | 'noise' | 'sections' | 'section' | 'vec3s' | 'numbers' | 'operation' | 'uvmode' | 'number|vec2only' | 'vec2' | 'pattern' | 'near' | 'where' | 'guides' | 'line';
+ | 'color' | 'points' | 'faces' | 'edges' | 'axis' | 'pivot' | 'parts' | 'noise' | 'sections' | 'section' | 'vec3s' | 'numbers' | 'operation' | 'uvmode' | 'number|vec2only' | 'vec2' | 'pattern' | 'near' | 'where' | 'guides' | 'line' | 'view' | 'strokes';
interface OpSpec {
params: Record<string, Kind>;
@@ -146,6 +148,7 @@ export const MODEL_OPS: Record<string, OpSpec> = {
hair: { params: { faces: 'faces', count: 'integer', length: 'number', guides: 'guides', gather: 'number', width: 'number', segments: 'integer', droop: 'number', comb: 'vec3', spread: 'number', uneven: 'number', wave: 'number', taper: 'number', seed: 'integer', color: 'color', tipColor: 'color', tag: 'string' }, required: ['faces'], run: hair as unknown as OpSpec['run'], says: 'grows cards of hair from these faces, falling around what is there, or along `guides` for a ponytail or a fringe; run it again for another layer, and only the cards are kept' },
scatter: { params: { source: 'faces', count: 'integer', faces: 'faces', path: 'vec3s', within: 'vec3s', scale: 'number|numbers', taper: 'number', spin: 'number', tilt: 'number', colors: 'vec3s', keep: 'boolean', seed: 'integer', tag: 'string' }, required: ['source', 'count'], run: scatter as unknown as OpSpec['run'], says: 'puts copies of the `source` faces across `faces`, along a `path` or through a box `within`, each sized, turned and coloured at random: feathers, rivets, the lights of a far city' },
stain: { params: { faces: 'faces', near: 'near', line: 'line', hard: 'boolean', color: 'color', roughness: 'number', metallic: 'number', alpha: 'number', amount: 'number', pattern: 'pattern', scale: 'number', octaves: 'integer', seed: 'integer', stretch: 'vec3', warp: 'number', width: 'number', axis: 'axis', mirror: 'axis', threshold: 'number', softness: 'number', where: 'where', reach: 'number', facing: 'vec3', detail: 'number' }, required: [], run: ((mesh: PolyMesh, params: never) => stain(mesh, params)) as OpSpec['run'], says: 'lays colour, `roughness`, `metallic` or `alpha` over the surface by a pattern, by shape (`where`: cavity for dirt in what is sunken, edges for wear on what stands out), by which way it faces, around a spot, or along a `line`: pupils, eyelids, seams, moss, grime' },
+ sculpt: { params: { faces: 'faces', from: 'view', strokes: 'strokes', detail: 'number' }, required: ['strokes'], run: sculpt as unknown as OpSpec['run'], says: 'works a surface stroke by stroke where one particular fold or hollow goes: `press` along a line, `move`, `inflate`, `smooth`; `from` takes places in the two axes of a view, as read off a picture' },
decimate: { params: { from: 'string', faces: 'faces', ratio: 'number', triangles: 'integer', error: 'number' }, required: [], run: decimate as unknown as OpSpec['run'], says: 'leaves fewer triangles over the same surface: `ratio` to keep or `triangles` to end with, stopping sooner if the surface would move more than `error` (default 0.02) of its size; `from` thins the surface as it stood after that step, before its fine detail' },
uv: { params: { faces: 'faces', mode: 'uvmode', axis: 'string', at: 'vec3', scale: 'number|vec2only', repeat: 'number|vec2only', offset: 'vec2', rotate: 'number' }, required: ['mode'], run: uvStep as unknown as OpSpec['run'], says: 'sets how faces take a texture: box, planar, cylinder or sphere' },
delete: { params: { faces: 'faces' }, required: ['faces'], run: ops.deleteFaces as OpSpec['run'], says: 'removes faces, leaving a hole' },
@@ -217,7 +220,7 @@ function checkNoise(v: unknown): string | null {
}
const PART_FIELDS: Record<string, (v: unknown) => string | null> = {
- shape: (v) => (['sphere', 'ellipsoid', 'box', 'torus', 'chain'].includes(v as string) ? null : 'must be sphere, ellipsoid, box, torus or chain'),
+ shape: (v) => (['sphere', 'ellipsoid', 'box', 'torus', 'chain', 'outline'].includes(v as string) ? null : 'must be sphere, ellipsoid, box, torus, chain or outline'),
tag: (v) => (typeof v === 'string' ? null : 'must be a string'),
at: (v) => (isNums(v, 3) ? null : 'must be [x, y, z]'),
rotate: (v) => (isNums(v, 3) ? null : 'must be [x, y, z] degrees'),
@@ -226,7 +229,10 @@ const PART_FIELDS: Record<string, (v: unknown) => string | null> = {
size: (v) => (isNums(v, 3) ? null : 'must be [x, y, z]'),
rounding: (v) => (isNum(v) ? null : 'must be a number'),
tube: (v) => (isNum(v) ? null : 'must be a number'),
- points: (v) => (Array.isArray(v) && v.every((q) => isNums(q, 4)) ? null : 'must be an array of [x, y, z, radius]'),
+ points: (v) => (Array.isArray(v) && (v.every((q) => isNums(q, 4)) || v.every((q) => isNums(q, 2))) ? null : 'must be an array of [x, y, z, radius] for a chain, or [x, y] for an outline'),
+ depth: (v) => (isNum(v) ? null : 'must be a number'),
+ round: (v) => (isNum(v) ? null : 'must be a number'),
+ curve: (v) => (Number.isInteger(v) && (v as number) >= 1 && (v as number) <= 32 ? null : 'must be a whole number, 1..32'),
blend: (v) => (isNum(v) ? null : 'must be a number'),
subtract: (v) => (typeof v === 'boolean' ? null : 'must be true or false'),
mirror: (v) => (v === 'x' || v === 'y' || v === 'z' ? null : 'must be x, y or z'),
@@ -305,8 +311,13 @@ const KIND_CHECK: Record<Kind, (v: unknown) => string | null> = {
vec2: (v) => (isNums(v, 2) ? null : 'must be [u, v]'),
line: (v) => {
const n = v as { points?: unknown; width?: unknown } | null;
- return typeof n === 'object' && n !== null && Array.isArray(n.points) && n.points.length >= 2 && n.points.every((q) => isNums(q, 3)) && isNum(n.width) && Object.keys(n).every((k) => k === 'points' || k === 'width') ? null : 'must be { points: [[x, y, z], ...], width }';
+ const line = n as { points?: unknown; width?: unknown; from?: unknown } | null;
+ if (line?.from !== undefined && !VIEW_NAMES.includes(line.from as string)) return `from must be one of ${VIEW_NAMES.join(', ')}`;
+ const size = line?.from !== undefined ? 2 : 3;
+ return typeof n === 'object' && n !== null && Array.isArray(n.points) && n.points.length >= 2 && n.points.every((q) => isNums(q, size)) && isNum(n.width) && Object.keys(n).every((k) => k === 'points' || k === 'width' || k === 'from') ? null : (size === 2 ? 'must be { points: [[a, b], ...], width, from }, the points in the two axes of the view' : 'must be { points: [[x, y, z], ...], width }');
},
+ view: (v) => (VIEW_NAMES.includes(v as string) ? null : `must be one of ${VIEW_NAMES.join(', ')}`),
+ strokes: (v) => (Array.isArray(v) && v.length > 0 && v.every((s) => typeof s === 'object' && s !== null) ? null : 'must be a non-empty array of strokes, each { brush, ... }'),
guides: (v) => (Array.isArray(v) && v.every((line) => Array.isArray(line) && line.length >= 2 && line.every((q) => isNums(q, 3))) ? null : 'must be an array of lines, each two or more [x, y, z]'),
pattern: (v) => (DISPLACE_PATTERNS.includes(v as never) ? null : `must be one of ${DISPLACE_PATTERNS.join(', ')}`),
where: (v) => (v === 'cavity' || v === 'edges' ? null : 'must be cavity or edges'),
diff --git a/pipeline/src/modeling/organic.ts b/pipeline/src/modeling/organic.ts
index 1d88e9595..56d6065f6 100644
--- a/pipeline/src/modeling/organic.ts
+++ b/pipeline/src/modeling/organic.ts
@@ -16,8 +16,9 @@
* marked smooth, and each carries the tag and colour of the part nearest it.
*/
import { add, type Face, type PolyMesh, type Vec3 } from './polyMesh';
+import { curveThrough } from './surfaces';
-export type OrganicShape = 'sphere' | 'ellipsoid' | 'box' | 'torus' | 'chain';
+export type OrganicShape = 'sphere' | 'ellipsoid' | 'box' | 'torus' | 'chain' | 'outline';
export interface OrganicNoise {
/** How far the surface moves, in and out. */
@@ -44,8 +45,14 @@ export interface OrganicPart {
rounding?: number;
/** torus's thickness. */
tube?: number;
- /** chain: a limb, a tail, a branch, as [x, y, z, radius] along its length. */
- points?: readonly (readonly [number, number, number, number])[];
+ /** chain: a limb, a tail, a branch, as [x, y, z, radius] along its length. outline: a closed drawing, as [x, y] round it. */
+ points?: readonly (readonly number[])[];
+ /** outline: half how thick it stands, along z. */
+ depth?: number;
+ /** outline: how far in from its edge it takes to come up to `depth`. Default `depth`, a full roundness; small, a slab with soft edges. */
+ round?: number;
+ /** outline: this many segments between points, passing through each. A point given twice keeps a corner there. */
+ curve?: number;
/** How far this melts into what is before it; 0 is a hard crease. Defaults to the step's. */
blend?: number;
/** Carves this volume out of what is before it: an eye socket, a mouth. */
@@ -128,6 +135,16 @@ function inverseRotation(degrees: Vec3): (x: number, y: number, z: number, out:
};
}
+/** Part space to world: X, then Y, then Z. */
+function rotateForward(degrees: Vec3, p: Vec3): Vec3 {
+ const [cx, sx] = [Math.cos(degrees[0] * DEG), Math.sin(degrees[0] * DEG)];
+ const [cy, sy] = [Math.cos(degrees[1] * DEG), Math.sin(degrees[1] * DEG)];
+ const [cz, sz] = [Math.cos(degrees[2] * DEG), Math.sin(degrees[2] * DEG)];
+ const [y1, z1] = [cx * p[1] - sx * p[2], sx * p[1] + cx * p[2]];
+ const [x2, z2] = [cy * p[0] + sy * z1, -sy * p[0] + cy * z1];
+ return [cz * x2 - sz * y1, sz * x2 + cz * y1, z2];
+}
+
/** The part's distance function in its own frame, and how far from `at` it can reach. */
function shapeField(part: OrganicPart): { field: Eval; reach: number; thinnest: number } {
const need = (name: keyof OrganicPart): void => {
@@ -187,29 +204,71 @@ function shapeField(part: OrganicPart): { field: Eval; reach: number; thinnest:
need('points');
const pts = part.points!;
if (pts.length < 2) throw new Error(`a chain needs at least 2 points, not ${pts.length}`);
- if (pts.some((p) => p[3] <= 0)) throw new Error('every chain point needs a positive radius, its fourth number');
+ if (pts.some((p) => p.length !== 4 || !(p[3]! > 0))) throw new Error('every chain point is [x, y, z, radius], with a positive radius');
return {
field: (x, y, z) => {
let best = Infinity;
for (let i = 0; i + 1 < pts.length; i++) {
const a = pts[i]!, b = pts[i + 1]!;
- const [bx, by, bz] = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
- const [px, py, pz] = [x - a[0], y - a[1], z - a[2]];
+ const [bx, by, bz] = [b[0]! - a[0]!, b[1]! - a[1]!, b[2]! - a[2]!];
+ const [px, py, pz] = [x - a[0]!, y - a[1]!, z - a[2]!];
const len2 = bx * bx + by * by + bz * bz;
const t = len2 > 0 ? Math.min(1, Math.max(0, (px * bx + py * by + pz * bz) / len2)) : 0;
const [dx, dy, dz] = [px - bx * t, py - by * t, pz - bz * t];
- const d = Math.sqrt(dx * dx + dy * dy + dz * dz) - (a[3] + (b[3] - a[3]) * t);
+ const d = Math.sqrt(dx * dx + dy * dy + dz * dz) - (a[3]! + (b[3]! - a[3]!) * t);
if (d < best) best = d;
}
return best;
},
- reach: Math.max(...pts.map((p) => Math.hypot(p[0], p[1], p[2]) + p[3])),
+ reach: Math.max(...pts.map((p) => Math.hypot(p[0]!, p[1]!, p[2]!) + p[3]!)),
// Its widest: a limb tapering to a point past the grid is a limb, and only one wholly under it goes missing.
- thinnest: Math.max(...pts.map((p) => p[3])),
+ thinnest: Math.max(...pts.map((p) => p[3]!)),
+ };
+ }
+ case 'outline': {
+ need('points');
+ need('depth');
+ const depth = part.depth!;
+ if (depth <= 0) throw new Error('depth must be positive');
+ const round = part.round ?? depth;
+ if (round <= 0) throw new Error('round must be positive');
+ if (part.points!.length < 3) throw new Error(`an outline needs at least 3 points, not ${part.points!.length}`);
+ if (part.points!.some((p) => p.length !== 2)) throw new Error('every outline point is [x, y]');
+ const drawn = curveThrough(part.points!, part.curve ?? 1, true);
+ const pts = drawn.filter((p, i) => { const q = drawn[(i + 1) % drawn.length]!; return p[0] !== q[0] || p[1] !== q[1]; });
+ const count = pts.length;
+ const [xs, ys] = [new Float64Array(count), new Float64Array(count)];
+ pts.forEach((p, i) => { xs[i] = p[0]!; ys[i] = p[1]!; });
+ const r2 = round * round, d2 = depth * depth;
+ const reach = Math.max(...pts.map((p) => Math.hypot(p[0]!, p[1]!))) + depth;
+ return {
+ // The drawing's distance, then an ellipse across its edge: the same estimate as an ellipsoid's, in the plane of edge and depth.
+ field: (x, y, z) => {
+ let best = Infinity;
+ let inside = false;
+ for (let i = 0, j = count - 1; i < count; j = i, i++) {
+ const [ax, ay, bx, by] = [xs[j]!, ys[j]!, xs[i]!, ys[i]!];
+ const [ex, ey, px, py] = [bx - ax, by - ay, x - ax, y - ay];
+ const len = ex * ex + ey * ey;
+ const t = len > 0 ? Math.min(1, Math.max(0, (px * ex + py * ey) / len)) : 0;
+ const [qx, qy] = [px - ex * t, py - ey * t];
+ const dist = qx * qx + qy * qy;
+ if (dist < best) best = dist;
+ if ((by > y) !== (ay > y) && x < ax + ((y - ay) * (bx - ax)) / (by - ay)) inside = !inside;
+ }
+ const edge = Math.sqrt(best) * (inside ? -1 : 1);
+ const u = edge + round;
+ if (u <= 0) return Math.abs(z) - depth;
+ const k0 = Math.sqrt((u * u) / r2 + (z * z) / d2);
+ const k1 = Math.sqrt((u * u) / (r2 * r2) + (z * z) / (d2 * d2));
+ return k1 > 1e-12 ? (k0 * (k0 - 1)) / k1 : -Math.min(round, depth);
+ },
+ reach,
+ thinnest: depth,
};
}
default:
- throw new Error(`unknown shape ${JSON.stringify(part.shape)}; there are sphere, ellipsoid, box, torus, chain`);
+ throw new Error(`unknown shape ${JSON.stringify(part.shape)}; there are sphere, ellipsoid, box, torus, chain, outline`);
}
}
@@ -261,8 +320,19 @@ function compilePart(part: OrganicPart, index: number, params: OrganicParams): C
if (part.shape === 'chain') {
// A chain's points are already placed by `at`'s offset.
const pts = part.points!;
- min = [0, 1, 2].map((k) => Math.min(...pts.map((p) => p[k]! - p[3])) + at[k]! - margin) as unknown as Vec3;
- max = [0, 1, 2].map((k) => Math.max(...pts.map((p) => p[k]! + p[3])) + at[k]! + margin) as unknown as Vec3;
+ min = [0, 1, 2].map((k) => Math.min(...pts.map((p) => p[k]! - p[3]!)) + at[k]! - margin) as unknown as Vec3;
+ max = [0, 1, 2].map((k) => Math.max(...pts.map((p) => p[k]! + p[3]!)) + at[k]! + margin) as unknown as Vec3;
+ } else if (part.shape === 'outline') {
+ // Round what is drawn and not round `at`: a drawing is traced where it lies, far from the origin, and a ball about the origin would spend the grid on nothing.
+ const pts = part.points!;
+ // A curve can bow past the points it passes through.
+ const pad = (part.curve ?? 1) > 1 ? 0.25 * Math.max(Math.max(...pts.map((p) => p[0]!)) - Math.min(...pts.map((p) => p[0]!)), Math.max(...pts.map((p) => p[1]!)) - Math.min(...pts.map((p) => p[1]!))) / Math.max(1, pts.length - 2) : 0;
+ const [lx, hx, ly, hy] = [Math.min(...pts.map((p) => p[0]!)) - pad, Math.max(...pts.map((p) => p[0]!)) + pad, Math.min(...pts.map((p) => p[1]!)) - pad, Math.max(...pts.map((p) => p[1]!)) + pad];
+ const depth = part.depth!;
+ const corners: Vec3[] = [];
+ for (const x of [lx, hx]) for (const y of [ly, hy]) for (const z of [-depth, depth]) corners.push(rotateForward(part.rotate ?? [0, 0, 0], [x, y, z]));
+ min = [0, 1, 2].map((k) => Math.min(...corners.map((c) => c[k]!)) + at[k]! - margin) as unknown as Vec3;
+ max = [0, 1, 2].map((k) => Math.max(...corners.map((c) => c[k]!)) + at[k]! + margin) as unknown as Vec3;
} else {
min = [at[0] - extent, at[1] - extent, at[2] - extent];
max = [at[0] + extent, at[1] + extent, at[2] + extent];
diff --git a/pipeline/src/modeling/sculpt.ts b/pipeline/src/modeling/sculpt.ts
new file mode 100644
index 000000000..feaa73546
--- /dev/null
+++ b/pipeline/src/modeling/sculpt.ts
@@ -0,0 +1,258 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file sculpt.ts — the `sculpt` step: a surface worked by hand, stroke by stroke.
+ *
+ * A pattern is good for what repeats and says nothing of where one particular fold goes: the crease
+ * from a shoulder to an elbow, the hollow of an eye. A stroke names that place. Each works on a surface already
+ * made and needs no thought of its topology: it splits the faces it reaches as far as it needs, and moves
+ * them. With `from`, places are given in the two axes of a view, so a line read off a picture can be written down.
+ */
+import { MAX_DISPLACE_TRIANGLES, refine } from './displace';
+import { boundsOf, compact, faceArea, faceNormal, selectFaces, type FaceSelector, type PolyMesh, type Vec3 } from './polyMesh';
+import { facesInView, lineMeasure, seenFrom, viewOf, type View, type ViewName } from './surfaceLine';
+
+/** A place: [x, y, z], or with `from` the two axes of the view. */
+type Place = readonly number[];
+
+export interface PressStroke {
+ brush: 'press';
+ /** The line of the fold. */
+ points: readonly Place[];
+ /** How far to either side it reaches. */
+ width: number;
+ /** How deep, into the surface; negative raises a ridge. */
+ depth: number;
+ /** Puts the weight to one side, -1..1: a fold falls away steeply on one side and slowly on the other. Positive widens the side to the left of the line's direction. */
+ lean?: number;
+ /** A soft swell or trough with no crease along its middle. */
+ round?: boolean;
+ /** The share of its length each end fades over, 0..0.5. Default 0.25; 0 for a seam, which stops dead. */
+ taper?: number;
+}
+
+export interface MoveStroke {
+ brush: 'move';
+ at: Place;
+ radius: number;
+ /** Where the middle goes, the rest following by how near it is. */
+ by: Vec3;
+}
+
+export interface InflateStroke {
+ brush: 'inflate';
+ at: Place;
+ radius: number;
+ /** How far out along the surface's own direction; negative hollows it: a cheek, an eye socket. */
+ by: number;
+}
+
+export interface SmoothStroke {
+ brush: 'smooth';
+ at: Place;
+ radius: number;
+ /** How many times over. Default 3. */
+ passes?: number;
+}
+
+export type SculptStroke = PressStroke | MoveStroke | InflateStroke | SmoothStroke;
+
+export interface SculptParams {
+ /** Only these faces move. */
+ faces?: FaceSelector;
+ /** The view the places are given in: "+z" is seen from +z, each place being [x, y], and a stroke lands on what that view sees. */
+ from?: ViewName;
+ strokes: readonly SculptStroke[];
+ /** The longest edge to leave where a stroke acts. Default what its size wants. */
+ detail?: number;
+}
+
+/** How far a surface has to face a view, as a cosine, before its strokes act in full. */
+const RIM_FADE = 0.45;
+
+const smooth01 = (t: number): number => (t <= 0 ? 0 : t >= 1 ? 1 : t * t * (3 - 2 * t));
+
+function vertexNormals(mesh: PolyMesh): Float64Array {
+ const normals = new Float64Array(mesh.positions.length * 3);
+ for (const face of mesh.faces) {
+ const n = faceNormal(mesh, face);
+ const area = faceArea(mesh, face);
+ for (const v of face.v) { normals[v * 3]! += n[0] * area; normals[v * 3 + 1]! += n[1] * area; normals[v * 3 + 2]! += n[2] * area; }
+ }
+ for (let v = 0; v < mesh.positions.length; v++) {
+ const l = Math.hypot(normals[v * 3]!, normals[v * 3 + 1]!, normals[v * 3 + 2]!) || 1;
+ normals[v * 3]! /= l; normals[v * 3 + 1]! /= l; normals[v * 3 + 2]! /= l;
+ }
+ return normals;
+}
+
+/** How long an edge a stroke wants, and how far it reaches past what it names. */
+function needs(stroke: SculptStroke): { longest: number; reach: number; places: readonly Place[] } {
+ if (stroke.brush === 'press') return { longest: stroke.width / 4, reach: stroke.width * (1 + Math.abs(stroke.lean ?? 0)), places: stroke.points };
+ return { longest: stroke.radius / 6, reach: stroke.radius, places: [stroke.at] };
+}
+
+function checkStroke(stroke: SculptStroke, view: View | null): void {
+ const size = view ? 2 : 3;
+ const place = (p: unknown, name: string): void => {
+ if (!Array.isArray(p) || p.length !== size || !p.every(Number.isFinite)) throw new Error(`${name} must be ${view ? `two numbers, as \`from\` gives the view` : '[x, y, z]'}`);
+ };
+ if (stroke.brush === 'press') {
+ if (!(stroke.points?.length >= 2)) throw new Error('press needs `points`, two or more');
+ stroke.points.forEach((p) => place(p, 'a point'));
+ if (!(stroke.width > 0)) throw new Error(`width ${stroke.width} must be positive`);
+ if (!Number.isFinite(stroke.depth)) throw new Error('press needs `depth`');
+ if (stroke.lean !== undefined && !(Math.abs(stroke.lean) < 1)) throw new Error(`lean ${stroke.lean} must be between -1 and 1`);
+ if (stroke.taper !== undefined && !(stroke.taper >= 0 && stroke.taper <= 0.5)) throw new Error(`taper ${stroke.taper} must be 0..0.5`);
+ return;
+ }
+ if (stroke.brush !== 'move' && stroke.brush !== 'inflate' && stroke.brush !== 'smooth') throw new Error(`unknown brush ${JSON.stringify((stroke as { brush?: unknown }).brush)}; there are press, move, inflate, smooth`);
+ place(stroke.at, '`at`');
+ if (!(stroke.radius > 0)) throw new Error(`radius ${stroke.radius} must be positive`);
+}
+
+function applyStroke(mesh: PolyMesh, stroke: SculptStroke, selected: ReadonlySet<number>, view: View | null, detail: number | undefined, notes: string[]): PolyMesh {
+ const { longest, reach, places } = needs(stroke);
+ const dims = view ? 2 : 3;
+ const lo = Array.from({ length: dims }, (_, k) => Math.min(...places.map((p) => p[k]!)) - reach);
+ const hi = Array.from({ length: dims }, (_, k) => Math.max(...places.map((p) => p[k]!)) + reach);
+ const inSelection = [...selected];
+ const hit = view
+ ? facesInView(mesh, inSelection, view, [lo[0]!, lo[1]!], [hi[0]!, hi[1]!])
+ : inSelection.filter((f) => {
+ const { min, max } = boundsOf(mesh, mesh.faces[f]!.v);
+ return [0, 1, 2].every((k) => max[k as 0] >= lo[k]! && min[k as 0] <= hi[k]!);
+ });
+ if (hit.length === 0) throw new Error(`${stroke.brush} near ${JSON.stringify(places[0])} reaches no surface`);
+
+ const hitSet = new Set(hit);
+ // A smoothing makes nothing finer than what it is given, and has nothing to land on any finer.
+ const { mesh: fine, short } = stroke.brush === 'smooth' ? { mesh, short: false } : refine(mesh, hitSet, detail ?? longest, MAX_DISPLACE_TRIANGLES);
+ if (short) notes.push(`a ${stroke.brush} wanted edges of ${(detail ?? longest).toPrecision(3)} and stopped at ${MAX_DISPLACE_TRIANGLES} triangles, so it is coarser than asked`);
+
+ // Splitting keeps the faces it left alone in their order, then those it split. A vertex a face outside the selection also uses stays put, or the surface opens.
+ const held = new Uint8Array(fine.positions.length);
+ const active: number[] = [];
+ if (fine === mesh) {
+ mesh.faces.forEach((face, f) => {
+ if (hitSet.has(f)) { for (const v of face.v) active.push(v); } else if (!selected.has(f)) for (const v of face.v) held[v] = 1;
+ });
+ } else {
+ const outside: number[] = [];
+ mesh.faces.forEach((_, f) => { if (!hitSet.has(f)) outside.push(f); });
+ fine.faces.forEach((face, f) => {
+ if (f >= outside.length) { for (const v of face.v) active.push(v); } else if (!selected.has(outside[f]!)) for (const v of face.v) held[v] = 1;
+ });
+ }
+ const vertices = [...new Set(active)].filter((v) => !held[v]);
+ const normals = vertexNormals(fine);
+ const planar = (at: Vec3): number[] => (view ? [at[view.u], at[view.v]] : [at[0], at[1], at[2]]);
+ const positions: Vec3[] = [...fine.positions];
+
+ if (stroke.brush === 'smooth') {
+ const faceOf = new Set(vertices);
+ const links = new Map<number, Set<number>>();
+ for (const face of fine.faces) {
+ for (let i = 0; i < face.v.length; i++) {
+ const [a, b] = [face.v[i]!, face.v[(i + 1) % face.v.length]!];
+ if (faceOf.has(a)) (links.get(a) ?? links.set(a, new Set()).get(a)!).add(b);
+ if (faceOf.has(b)) (links.get(b) ?? links.set(b, new Set()).get(b)!).add(a);
+ }
+ }
+ for (let pass = 0; pass < (stroke.passes ?? 3); pass++) {
+ const moved = new Map<number, Vec3>();
+ for (const v of vertices) {
+ const at = positions[v]!;
+ const w = 1 - Math.hypot(...planar(at).map((c, k) => c - stroke.at[k]!)) / stroke.radius;
+ if (w <= 0) continue;
+ const around = [...links.get(v)!];
+ const mean = [0, 1, 2].map((k) => around.reduce((sum, n) => sum + positions[n]![k]!, 0) / around.length);
+ const share = smooth01(w);
+ moved.set(v, [at[0] + (mean[0]! - at[0]) * share, at[1] + (mean[1]! - at[1]) * share, at[2] + (mean[2]! - at[2]) * share]);
+ }
+ for (const [v, at] of moved) positions[v] = at;
+ }
+ return { positions, faces: fine.faces };
+ }
+
+ if (stroke.brush === 'move') {
+ for (const v of vertices) {
+ const at = positions[v]!;
+ const w = smooth01(1 - Math.hypot(...planar(at).map((c, k) => c - stroke.at[k]!)) / stroke.radius);
+ if (w > 0) positions[v] = [at[0] + stroke.by[0] * w, at[1] + stroke.by[1] * w, at[2] + stroke.by[2] * w];
+ }
+ return { positions, faces: fine.faces };
+ }
+
+ const seen = view ? seenFrom(fine, normals, view, vertices, Math.max(reach / 2, averageEdge(fine, vertices))) : null;
+ // A view's stroke fades out as the surface turns from it. At full strength up to the rim, each vertex there is pushed sideways along a normal that swings
+ // from one to the next, and the outline tears into a saw edge.
+ const facing = (v: number): number => (view ? smooth01((normals[v * 3 + view.axis]! * view.sign) / RIM_FADE) : 1);
+ if (stroke.brush === 'inflate') {
+ for (const v of vertices) {
+ if (seen && !seen[v]) continue;
+ const at = positions[v]!;
+ const w = smooth01(1 - Math.hypot(...planar(at).map((c, k) => c - stroke.at[k]!)) / stroke.radius) * facing(v);
+ if (w > 0) {
+ const push = stroke.by * w;
+ positions[v] = [at[0] + normals[v * 3]! * push, at[1] + normals[v * 3 + 1]! * push, at[2] + normals[v * 3 + 2]! * push];
+ }
+ }
+ return { positions, faces: fine.faces };
+ }
+
+ const measure = lineMeasure(stroke.points);
+ const lean = stroke.lean ?? 0;
+ const taper = stroke.taper ?? 0.25;
+ for (const v of vertices) {
+ if (seen && !seen[v]) continue;
+ const at = positions[v]!;
+ const n: Vec3 = [normals[v * 3]!, normals[v * 3 + 1]!, normals[v * 3 + 2]!];
+ const hitLine = measure(planar(at), n);
+ const u = hitLine.distance / (stroke.width * (1 + lean * hitLine.side));
+ if (u >= 1) continue;
+ const fade = taper > 0 ? smooth01(hitLine.along / taper) * smooth01((1 - hitLine.along) / taper) : 1;
+ if (fade <= 0) continue;
+ // A crease comes to an edge along its middle, which is what catches the light; a swell does not.
+ const shape = stroke.round ? (1 - u * u) ** 2 : (1 - u) ** 2;
+ const push = -stroke.depth * shape * fade * facing(v);
+ positions[v] = [at[0] + n[0] * push, at[1] + n[1] * push, at[2] + n[2] * push];
+ }
+ return { positions, faces: fine.faces };
+}
+
+function averageEdge(mesh: PolyMesh, vertices: readonly number[]): number {
+ const wanted = new Set(vertices);
+ let total = 0, count = 0;
+ for (const face of mesh.faces) {
+ if (!wanted.has(face.v[0]!)) continue;
+ const [p, q] = [mesh.positions[face.v[0]!]!, mesh.positions[face.v[1]!]!];
+ total += Math.hypot(p[0] - q[0], p[1] - q[1], p[2] - q[2]);
+ count++;
+ }
+ return count ? (total / count) * 2 : 0;
+}
+
+export function sculpt(mesh: PolyMesh, p: SculptParams): PolyMesh {
+ if (!p.strokes?.length) throw new Error('`strokes` names nothing to do');
+ if (p.detail !== undefined && !(p.detail > 0)) throw new Error(`detail ${p.detail} must be positive`);
+ const view = p.from ? viewOf(p.from) : null;
+ p.strokes.forEach((stroke, i) => {
+ try { checkStroke(stroke, view); } catch (e) { throw new Error(`stroke ${i}: ${(e as Error).message}`); }
+ });
+ const selector = p.faces ?? 'all';
+ let current = mesh;
+ const notes: string[] = [];
+ if (selectFaces(current, selector).length === 0) throw new Error(`\`faces\` ${JSON.stringify(selector)} matched no face`);
+ p.strokes.forEach((stroke, i) => {
+ // Asked again each time, as a stroke splits and reorders the faces and a selector asks by what a face is.
+ const selected = new Set(selectFaces(current, selector));
+ try {
+ current = applyStroke(current, stroke, selected, view, p.detail, notes);
+ } catch (e) {
+ throw new Error(`stroke ${i}: ${(e as Error).message}`);
+ }
+ });
+ const out = compact(current);
+ return notes.length ? { ...out, notes } : out;
+}
diff --git a/pipeline/src/modeling/stain.ts b/pipeline/src/modeling/stain.ts
index 8036552c4..fc75505cd 100644
--- a/pipeline/src/modeling/stain.ts
+++ b/pipeline/src/modeling/stain.ts
@@ -10,6 +10,7 @@
*/
import { DISPLACE_PATTERNS, finestShare, mirroredPoint, nearWeight, patternField, reachedFaces, refine, MAX_DISPLACE_TRIANGLES, type DisplaceParams, type DisplacePattern } from './displace';
import { compact, faceArea, faceNormal, normalize, selectFaces, type Face, type FaceSelector, type PolyMesh, type Vec3 } from './polyMesh';
+import { facesInView, lineMeasure, seenFrom, viewOf, type ViewName } from './surfaceLine';
/** How far a mirrored point is from a line of points. */
function toLine(points: readonly Vec3[], mirror: StainParams['mirror'], x: number, y: number, z: number): number {
@@ -28,8 +29,8 @@ export interface StainParams {
faces?: FaceSelector;
/** Only around here. Fades to its edge, or with `hard` stops there: a pupil. */
near?: { at: Vec3; radius: number };
- /** Only along a line drawn on the surface, its points on or near it: an eyelid, a lip, a seam. Fades to `width` either side, or with `hard` stops there. */
- line?: { points: readonly Vec3[]; width: number };
+ /** Only along a line drawn on the surface, its points on or near it: an eyelid, a lip, a seam. Fades to `width` either side, or with `hard` stops there. With `from: "+z"` its points are [x, y], and it lands on what that view sees. */
+ line?: { points: readonly (readonly number[])[]; width: number; from?: ViewName };
hard?: boolean;
/** Linear [r, g, b], over what is there. */
color?: readonly number[];
@@ -144,7 +145,12 @@ export function stain(mesh: PolyMesh, p: StainParams, budget = MAX_DISPLACE_TRIA
hit = reachedFaces(mesh, hit, p.near, p.mirror);
if (hit.length === 0) throw new Error(`no surface of \`faces\` within ${p.near.radius} of ${JSON.stringify(p.near.at)}`);
}
- if (p.line) {
+ const lineView = p.line?.from ? viewOf(p.line.from) : null;
+ if (p.line && lineView) {
+ const { points, width } = p.line;
+ hit = facesInView(mesh, hit, lineView, [Math.min(...points.map((q) => q[0]!)) - width, Math.min(...points.map((q) => q[1]!)) - width], [Math.max(...points.map((q) => q[0]!)) + width, Math.max(...points.map((q) => q[1]!)) + width]);
+ if (hit.length === 0) throw new Error(`no surface of \`faces\` within ${width} of the line, seen from ${p.line.from}`);
+ } else if (p.line) {
const { points, width } = p.line;
const [lo, hi] = [[0, 1, 2].map((k) => Math.min(...points.map((q) => q[k]!))), [0, 1, 2].map((k) => Math.max(...points.map((q) => q[k]!)))];
const around = { at: [0, 1, 2].map((k) => (lo[k]! + hi[k]!) / 2) as unknown as Vec3, radius: Math.hypot(hi[0]! - lo[0]!, hi[1]! - lo[1]!, hi[2]! - lo[2]!) / 2 + width };
@@ -173,6 +179,13 @@ export function stain(mesh: PolyMesh, p: StainParams, budget = MAX_DISPLACE_TRIA
normals[v * 3]! /= l; normals[v * 3 + 1]! /= l; normals[v * 3 + 2]! /= l;
}
const shape = p.where ? relief(fine, normals, p.reach) : null;
+ let lineSeen: Uint8Array | null = null;
+ const measured = p.line && lineView ? lineMeasure(p.line.points) : null;
+ if (p.line && lineView) {
+ const vertices: number[] = [];
+ fine.faces.forEach((face, f) => { if (isChosen(f)) vertices.push(...face.v); });
+ lineSeen = seenFrom(fine, normals, lineView, vertices, p.line.width * 2);
+ }
const facing = p.facing ? normalize(p.facing) : null;
const field = p.pattern ? patternField(asPattern) : null;
const threshold = p.threshold ?? 0.5, softness = p.softness ?? 0.15;
@@ -190,7 +203,8 @@ export function stain(mesh: PolyMesh, p: StainParams, budget = MAX_DISPLACE_TRIA
if (m === 0) return 0;
}
if (p.line) {
- const t = 1 - toLine(p.line.points, p.mirror, x, y, z) / p.line.width;
+ if (lineSeen && !lineSeen[v]) return 0;
+ const t = 1 - (measured && lineView ? measured([at[lineView.u], at[lineView.v]], null).distance : toLine(p.line.points as readonly Vec3[], p.mirror, x, y, z)) / p.line.width;
m *= t <= 0 ? 0 : p.hard ? 1 : t * t * (3 - 2 * t);
if (m === 0) return 0;
}
diff --git a/pipeline/src/modeling/surfaceLine.ts b/pipeline/src/modeling/surfaceLine.ts
new file mode 100644
index 000000000..26c932033
--- /dev/null
+++ b/pipeline/src/modeling/surfaceLine.ts
@@ -0,0 +1,103 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file surfaceLine.ts — a line drawn on a surface as seen from one side.
+ *
+ * What is traced from a picture has no depth: a crease in a sleeve is known by where it is on the
+ * screen, not by how far the cloth stands out there. So a line can be given in the two axes of a view,
+ * and it lands on whatever faces that view and is nearest it, as a pen on the picture would.
+ */
+import { boundsOf, type PolyMesh, type Vec3 } from './polyMesh';
+
+export type ViewName = '+x' | '-x' | '+y' | '-y' | '+z' | '-z';
+export const VIEW_NAMES: readonly string[] = ['+x', '-x', '+y', '-y', '+z', '-z'];
+
+export interface View {
+ /** The axis looked along, and which end of it the eye is at. */
+ axis: 0 | 1 | 2;
+ sign: 1 | -1;
+ /** The two axes the picture has, in x, y, z order. */
+ u: 0 | 1 | 2;
+ v: 0 | 1 | 2;
+}
+
+export function viewOf(name: ViewName): View {
+ const axis = ({ x: 0, y: 1, z: 2 } as const)[name[1] as 'x' | 'y' | 'z'];
+ const [u, v] = axis === 0 ? [1, 2] as const : axis === 1 ? [0, 2] as const : [0, 1] as const;
+ return { axis, sign: name[0] === '+' ? 1 : -1, u, v };
+}
+
+export interface LineHit {
+ distance: number;
+ /** How far along the whole line the nearest point is, 0..1. */
+ along: number;
+ /** Which side of the line the point is on, by the line's direction: in a view, left of it is +1. */
+ side: number;
+}
+
+/** A polyline in any number of coordinates, and what is nearest a point on it. */
+export function lineMeasure(points: readonly (readonly number[])[]): (at: readonly number[], normal: Vec3 | null) => LineHit {
+ const dims = points[0]!.length;
+ const lengths: number[] = [];
+ let total = 0;
+ for (let i = 1; i < points.length; i++) {
+ let sum = 0;
+ for (let k = 0; k < dims; k++) sum += (points[i]![k]! - points[i - 1]![k]!) ** 2;
+ const len = Math.sqrt(sum);
+ lengths.push(len);
+ total += len;
+ }
+ return (at, normal) => {
+ let best = Infinity, along = 0, run = 0, seg = 0, bestT = 0;
+ for (let i = 1; i < points.length; i++) {
+ const a = points[i - 1]!, b = points[i]!, len = lengths[i - 1]!;
+ let dotted = 0;
+ for (let k = 0; k < dims; k++) dotted += (at[k]! - a[k]!) * (b[k]! - a[k]!);
+ const t = len > 0 ? Math.max(0, Math.min(1, dotted / (len * len))) : 0;
+ let sum = 0;
+ for (let k = 0; k < dims; k++) sum += (at[k]! - a[k]! - (b[k]! - a[k]!) * t) ** 2;
+ if (sum < best) { best = sum; along = run + len * t; seg = i; bestT = t; }
+ run += len;
+ }
+ const a = points[seg - 1]!, b = points[seg]!;
+ const [dx, dy, dz] = [b[0]! - a[0]!, b[1]! - a[1]!, (b[2] ?? 0) - (a[2] ?? 0)];
+ const [ox, oy, oz] = [at[0]! - a[0]! - dx * bestT, at[1]! - a[1]! - dy * bestT, (at[2] ?? 0) - (a[2] ?? 0) - dz * bestT];
+ // In a view, the screen's own "out"; on the surface, the way it faces.
+ const [nx, ny, nz] = dims === 2 || !normal ? [0, 0, 1] : normal;
+ const side = (ny * dz - nz * dy) * ox + (nz * dx - nx * dz) * oy + (nx * dy - ny * dx) * oz;
+ return { distance: Math.sqrt(best), along: total > 0 ? along / total : 0, side: side >= 0 ? 1 : -1 };
+ };
+}
+
+/** Those of @p hit whose outline in the view comes within the box. */
+export function facesInView(mesh: PolyMesh, hit: readonly number[], view: View, lo: readonly [number, number], hi: readonly [number, number]): number[] {
+ return hit.filter((f) => {
+ const { min, max } = boundsOf(mesh, mesh.faces[f]!.v);
+ return max[view.u] >= lo[0] && min[view.u] <= hi[0] && max[view.v] >= lo[1] && min[view.v] <= hi[1];
+ });
+}
+
+/** Which vertices the view sees: facing it, and at the front of what lies along the line of sight there, to within a little. */
+export function seenFrom(mesh: PolyMesh, normals: Float64Array, view: View, vertices: readonly number[], cell: number): Uint8Array {
+ const seen = new Uint8Array(mesh.positions.length);
+ const front = new Map<number, number>();
+ const key = (a: number, b: number): number => a * 1048576 + b;
+ const place = (at: Vec3): [number, number] => [Math.floor(at[view.u] / cell) + 500000, Math.floor(at[view.v] / cell) + 500000];
+ for (const v of vertices) {
+ const at = mesh.positions[v]!;
+ const depth = at[view.axis] * view.sign;
+ const [a, b] = place(at);
+ const k = key(a, b);
+ const was = front.get(k);
+ if (was === undefined || depth > was) front.set(k, depth);
+ }
+ for (const v of vertices) {
+ if (normals[v * 3 + view.axis]! * view.sign <= 0.02) continue;
+ const at = mesh.positions[v]!;
+ const [a, b] = place(at);
+ let nearest = -Infinity;
+ for (let da = -1; da <= 1; da++) for (let db = -1; db <= 1; db++) nearest = Math.max(nearest, front.get(key(a + da, b + db)) ?? -Infinity);
+ if (at[view.axis] * view.sign >= nearest - cell * 3) seen[v] = 1;
+ }
+ return seen;
+}
diff --git a/pipeline/src/modeling/surfaces.ts b/pipeline/src/modeling/surfaces.ts
index 0d236c724..092b262e4 100644
--- a/pipeline/src/modeling/surfaces.ts
+++ b/pipeline/src/modeling/surfaces.ts
@@ -18,7 +18,7 @@ const EPS = 1e-6;
* Catmull-Rom through @p points, `steps` segments for each span; the points themselves stay exactly where they are.
* A point given twice stops the curve there, which is how a corner is kept. Works on any tuple length.
*/
-function curveThrough(points: readonly (readonly number[])[], steps: number, closed: boolean): number[][] {
+export function curveThrough(points: readonly (readonly number[])[], steps: number, closed: boolean): number[][] {
if (steps <= 1 || points.length < 3) return points.map((p) => [...p]);
const n = points.length;
const at = (i: number): readonly number[] => (closed ? points[((i % n) + n) % n]! : points[Math.max(0, Math.min(n - 1, i))]!);
diff --git a/pipeline/tests/look-pictures.test.ts b/pipeline/tests/look-pictures.test.ts
index 8f6b852ce..661ce19f8 100644
--- a/pipeline/tests/look-pictures.test.ts
+++ b/pipeline/tests/look-pictures.test.ts
@@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/** Pictures laid out for an agent to look at: each one holds what it says, and the colours read are the colours there. */
import { describe, expect, it } from 'vitest';
-import { activeArea, blendImages, colorsOf, cropImage, gridOf, outlineOver, resizeImage, sideBySide, toShape, type RgbaImage } from '../src/look/pictures';
+import { activeArea, blendImages, colorsOf, cropImage, gridOf, outlineOver, resizeImage, rulerOver, rulerStep, sideBySide, toShape, type RgbaImage } from '../src/look/pictures';
type Shade = (x: number, y: number) => readonly [number, number, number];
function image(width: number, height: number, shade: Shade): RgbaImage {
@@ -18,6 +18,55 @@ function image(width: number, height: number, shade: Shade): RgbaImage {
const flat = (r: number, g: number, b: number, w = 64, h = 36): RgbaImage => image(w, h, () => [r, g, b]);
const at = (img: RgbaImage, x: number, y: number): number[] => Array.from(img.rgba.subarray((y * img.width + x) * 4, (y * img.width + x) * 4 + 3));
+describe('rulerOver', () => {
+ const dark = flat(10, 10, 10, 1400, 700);
+ const span = { left: -1, right: 1, top: 1, bottom: 0 };
+ /** The columns of a row, away from the numbers, that a line lightened. */
+ const columnsLit = (img: RgbaImage, y: number): number[] => {
+ const out: number[] = [];
+ for (let x = 0; x < img.width; x++) if (at(img, x, y)[0]! > 40) out.push(x);
+ return out;
+ };
+
+ it('puts a line where each number falls on the picture', () => {
+ const ruled = rulerOver(dark, span, 0.5);
+ const lit = columnsLit(ruled, 100);
+ // -1, -0.5, 0, 0.5, 1 across 1400 pixels.
+ for (const want of [0, 350, 700, 1049, 1399]) expect(lit.some((x) => Math.abs(x - want) <= 1), `a line at ${want}: ${lit.slice(0, 12)}`).toBe(true);
+ });
+
+ it('runs its rows top to bottom by the numbers, whichever way they go', () => {
+ const ruled = rulerOver(dark, span, 0.5);
+ // 1 at the top, 0.5 half way, 0 at the bottom.
+ expect(at(ruled, 500, 349)[0]! > 40 || at(ruled, 500, 350)[0]! > 40).toBe(true);
+ });
+
+ it('draws every fifth line stronger', () => {
+ const ruled = rulerOver(dark, span, 0.1);
+ const strong = at(ruled, 700, 100)[0]!, weak = at(ruled, 840, 100)[0]!;
+ expect(strong).toBeGreaterThan(weak + 30);
+ });
+
+ it('enlarges a small close-up so the numbers have room, and leaves what it was given alone', () => {
+ const small = flat(10, 10, 10, 200, 100);
+ const before = Uint8Array.from(small.rgba);
+ const ruled = rulerOver(small, span);
+ expect(ruled.width).toBe(1400);
+ expect(ruled.height).toBe(700);
+ expect(small.rgba).toEqual(before);
+ });
+
+ it('chooses a round step of about a dozen lines', () => {
+ expect(rulerStep(2)).toBeCloseTo(0.2);
+ expect(rulerStep(0.3)).toBeCloseTo(0.05);
+ expect(rulerStep(-2)).toBeCloseTo(0.2);
+ });
+
+ it('refuses no step at all', () => {
+ expect(() => rulerOver(dark, span, 0)).toThrow(/positive/);
+ });
+});
+
describe('activeArea', () => {
it('finds the picture inside black bars', () => {
const barred = image(100, 200, (_, y) => (y < 0.4 || y >= 0.6 ? [0, 0, 0] : [120, 90, 60]));
diff --git a/pipeline/tests/modeling-sculpt.test.ts b/pipeline/tests/modeling-sculpt.test.ts
new file mode 100644
index 000000000..819732e82
--- /dev/null
+++ b/pipeline/tests/modeling-sculpt.test.ts
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/** What is read off a picture goes into a model: a drawing stands up as a solid, a stroke moves the surface where it says and nowhere else, and a view lands on what it sees. */
+import { describe, expect, it } from 'vitest';
+import { bakeModel, validateModelSource, type ModelStep } from '../src/modeling';
+
+type Mesh = ReturnType<typeof bakeModel>['poly'];
+const build = (steps: ModelStep[]): Mesh => bakeModel({ version: 1, steps }).poly;
+const bounds = (m: Mesh): { min: number[]; max: number[] } => ({
+ min: [0, 1, 2].map((k) => Math.min(...m.positions.map((p) => p[k]!))),
+ max: [0, 1, 2].map((k) => Math.max(...m.positions.map((p) => p[k]!))),
+});
+/** The surface's height toward +z or -z over a place in x, y, or null if nothing is there. */
+const heightAt = (m: Mesh, x: number, y: number, sign: 1 | -1, within = 0.012): number | null => {
+ let best: number | null = null;
+ for (const p of m.positions) {
+ if (Math.hypot(p[0] - x, p[1] - y) > within || p[2] * sign < 0) continue;
+ if (best === null || p[2] * sign > best) best = p[2] * sign;
+ }
+ return best;
+};
+
+// A plate 1 across, thick enough to have a front and a back, with an edge well away from every stroke.
+const plate: ModelStep = { id: 'form', op: 'box', size: [1, 1, 0.2], tag: 'plate' };
+
+describe('organic outline', () => {
+ const notch = [[0, 0], [1, 0], [1, 1], [0.6, 1], [0.5, 0.4], [0.4, 1], [0, 1]];
+ const drawn = (extra: Record<string, unknown> = {}): Mesh => build([{ op: 'organic', resolution: 90, parts: [{ shape: 'outline', points: notch, depth: 0.1, round: 0.05, ...extra }] }]);
+
+ it('stands where it is drawn, to its depth, and spends the grid on the drawing and not on the distance from the origin', () => {
+ const m = build([{ op: 'organic', resolution: 80, parts: [{ shape: 'outline', points: notch.map(([x, y]) => [x! + 40, y! + 40]), depth: 0.1, round: 0.05 }] }]);
+ const b = bounds(m);
+ expect(b.min[0]).toBeCloseTo(40, 1);
+ expect(b.max[0]).toBeCloseTo(41, 1);
+ expect(b.max[1]).toBeCloseTo(41, 1);
+ expect(b.max[2]).toBeCloseTo(0.1, 1);
+ expect(m.faces.length).toBeGreaterThan(5000);
+ });
+
+ it('follows a concave drawing: the notch is empty', () => {
+ const m = drawn();
+ expect(heightAt(m, 0.5, 0.9, 1)).toBeNull();
+ expect(heightAt(m, 0.5, 0.2, 1)).toBeCloseTo(0.1, 1);
+ expect(heightAt(m, 0.2, 0.9, 1)).toBeCloseTo(0.1, 1);
+ });
+
+ it('is rounded across its edge by `round`, and full over its middle', () => {
+ const wide = build([{ op: 'organic', resolution: 120, parts: [{ shape: 'outline', points: [[0, 0], [1, 0], [1, 1], [0, 1]], depth: 0.1, round: 0.2 }] }]);
+ expect(heightAt(wide, 0.5, 0.5, 1)).toBeCloseTo(0.1, 2);
+ // A tenth in from the edge of an ellipse 0.2 wide: 0.1 * sqrt(1 - 0.5²).
+ expect(heightAt(wide, 0.1, 0.5, 1, 0.01)!).toBeCloseTo(0.0866, 1);
+ expect(heightAt(wide, 0.1, 0.5, 1, 0.01)!).toBeLessThan(0.095);
+ });
+
+ it('turns with `rotate`, and its bounds go with it', () => {
+ const m = build([{ op: 'organic', resolution: 60, parts: [{ shape: 'outline', points: [[0, 0], [1, 0], [1, 1], [0, 1]], depth: 0.05, rotate: [0, 90, 0] }] }]);
+ const b = bounds(m);
+ // Y 90 takes +x to -z: the drawing now stands across z and y.
+ expect(b.min[2]).toBeCloseTo(-1, 1);
+ expect(b.max[2]).toBeCloseTo(0, 1);
+ expect(b.max[0]! - b.min[0]!).toBeLessThan(0.15);
+ });
+
+ it('rounds a drawing through its points with `curve`, and keeps a corner given twice', () => {
+ const curved = build([{ op: 'organic', resolution: 80, parts: [{ shape: 'outline', curve: 6, depth: 0.05, points: [[0, 0], [1, 0], [1, 1], [0, 1]] }] }]);
+ // A square drawn with a curve bows out past its sides.
+ expect(bounds(curved).max[0]).toBeGreaterThan(1.02);
+ const cornered = build([{ op: 'organic', resolution: 80, parts: [{ shape: 'outline', curve: 6, depth: 0.05, points: [[0, 0], [0, 0], [1, 0], [1, 0], [1, 1], [1, 1], [0, 1], [0, 1]] }] }]);
+ expect(bounds(cornered).max[0]).toBeLessThan(1.02);
+ });
+
+ it('carves, as any part does', () => {
+ const m = build([{ op: 'organic', resolution: 80, parts: [
+ { shape: 'box', size: [1, 1, 0.2] },
+ { shape: 'outline', subtract: true, points: [[-0.1, -0.1], [0.1, -0.1], [0.1, 0.1], [-0.1, 0.1]], depth: 0.3, round: 0.01 },
+ ] }]);
+ expect(heightAt(m, 0, 0, 1, 0.02)).toBeNull();
+ });
+
+ it('refuses what cannot be drawn', () => {
+ expect(() => build([{ op: 'organic', parts: [{ shape: 'outline', points: [[0, 0], [1, 0]], depth: 0.1 }] }])).toThrow(/at least 3/);
+ expect(() => build([{ op: 'organic', parts: [{ shape: 'outline', points: notch }] }])).toThrow(/depth/);
+ expect(() => build([{ op: 'organic', parts: [{ shape: 'outline', points: notch, depth: -1 }] }])).toThrow(/positive/);
+ expect(() => build([{ op: 'organic', parts: [{ shape: 'outline', points: notch, depth: 0.1, round: 0 }] }])).toThrow(/positive/);
+ expect(() => validateModelSource({ steps: [{ op: 'organic', parts: [{ shape: 'outline', points: notch, depth: 0.1, curve: 99 }] }] })).toThrow(/curve/);
+ });
+
+ it('a chain still takes four numbers a point', () => {
+ expect(() => build([{ op: 'organic', parts: [{ shape: 'chain', points: [[0, 0], [1, 0]] }] }])).toThrow(/x, y, z, radius/);
+ });
+});
+
+describe('sculpt', () => {
+ const stroke = (s: Record<string, unknown>, extra: Record<string, unknown> = {}): Mesh => build([plate, { op: 'sculpt', from: '+z', strokes: [s], ...extra }]);
+ const press = { brush: 'press', points: [[-0.3, 0], [0.3, 0]], width: 0.08, depth: 0.03 };
+
+ it('presses along a line, deepest down its middle and nothing past its width', () => {
+ const m = stroke(press);
+ expect(heightAt(m, 0, 0, 1)!).toBeLessThan(0.1 - 0.02);
+ expect(heightAt(m, 0, 0.06, 1)!).toBeGreaterThan(heightAt(m, 0, 0, 1)!);
+ expect(heightAt(m, 0, 0.15, 1)).toBeCloseTo(0.1, 5);
+ });
+
+ it('raises with a negative depth', () => {
+ expect(heightAt(stroke({ ...press, depth: -0.03 }), 0, 0, 1)!).toBeGreaterThan(0.1 + 0.02);
+ });
+
+ it('fades toward its ends, or with taper 0 stops dead', () => {
+ const faded = stroke(press), dead = stroke({ ...press, taper: 0 });
+ expect(heightAt(faded, -0.29, 0, 1, 0.01)!).toBeGreaterThan(0.095);
+ expect(heightAt(dead, -0.29, 0, 1, 0.01)!).toBeLessThan(0.08);
+ });
+
+ it('leans: wider on one side of the line, by the way it runs', () => {
+ const m = stroke({ ...press, lean: 0.6 });
+ // Running along +x, left of it is +y.
+ expect(heightAt(m, 0, 0.09, 1)!).toBeLessThan(0.1 - 1e-3);
+ expect(heightAt(m, 0, -0.09, 1)).toBeCloseTo(0.1, 5);
+ const other = stroke({ ...press, lean: -0.6 });
+ expect(heightAt(other, 0, 0.09, 1)).toBeCloseTo(0.1, 5);
+ expect(heightAt(other, 0, -0.09, 1)!).toBeLessThan(0.1 - 1e-3);
+ });
+
+ it('from a view, lands on the near side and not on the far one', () => {
+ const m = stroke(press);
+ expect(heightAt(m, 0, 0, -1)).toBeCloseTo(0.1, 5);
+ const back = build([plate, { op: 'sculpt', from: '-z', strokes: [press] }]);
+ expect(heightAt(back, 0, 0, -1)!).toBeLessThan(0.1 - 0.02);
+ expect(heightAt(back, 0, 0, 1)).toBeCloseTo(0.1, 5);
+ });
+
+ it('from a view, lands on what is in front: a thing behind another is not touched', () => {
+ const stacked = build([
+ { op: 'box', size: [1, 1, 0.2], tag: 'far' },
+ { op: 'box', size: [1, 1, 0.2], at: [0, 0, 1], tag: 'near' },
+ { op: 'sculpt', from: '+z', strokes: [press] },
+ ]);
+ expect(heightAt(stacked, 0, 0, 1)!).toBeLessThan(1.1 - 0.02);
+ const farFront = stacked.positions.filter((p) => Math.hypot(p[0], p[1]) < 0.05 && p[2] > 0 && p[2] < 0.5).map((p) => p[2]);
+ expect(Math.min(...farFront)).toBeCloseTo(0.1, 5);
+ });
+
+ it('in three dimensions, takes [x, y, z] and reaches only what is that near', () => {
+ const m = build([plate, { op: 'sculpt', strokes: [{ brush: 'press', points: [[-0.3, 0, 0.1], [0.3, 0, 0.1]], width: 0.08, depth: 0.03 }] }]);
+ expect(heightAt(m, 0, 0, 1)!).toBeLessThan(0.08);
+ // The back has the box's corners and nothing else: it was not reached, so not even split.
+ expect(m.positions.filter((p) => p[2] < 0).every((p) => Math.abs(p[2] + 0.1) < 1e-9)).toBe(true);
+ });
+
+ it('moves what is round a place, the middle by all of it', () => {
+ const m = stroke({ brush: 'move', at: [0, 0], radius: 0.2, by: [0, 0, 0.05] });
+ expect(heightAt(m, 0, 0, 1, 0.02)!).toBeGreaterThan(0.145);
+ expect(heightAt(m, 0.35, 0, 1)).toBeCloseTo(0.1, 5);
+ });
+
+ it('inflates along the way the surface faces, and hollows', () => {
+ expect(heightAt(stroke({ brush: 'inflate', at: [0, 0], radius: 0.2, by: 0.04 }), 0, 0, 1, 0.02)!).toBeGreaterThan(0.135);
+ expect(heightAt(stroke({ brush: 'inflate', at: [0, 0], radius: 0.2, by: -0.04 }), 0, 0, 1, 0.02)!).toBeLessThan(0.065);
+ });
+
+ it('smooths what is rough, and only there', () => {
+ const rough = build([plate, { op: 'sculpt', from: '+z', strokes: [{ ...press, taper: 0 }, { brush: 'smooth', at: [0, 0], radius: 0.12, passes: 12 }] }]);
+ const sharp = stroke({ ...press, taper: 0 });
+ expect(heightAt(rough, 0, 0, 1)!).toBeGreaterThan(heightAt(sharp, 0, 0, 1)! + 0.005);
+ expect(heightAt(rough, 0.2, 0, 1)!).toBeLessThan(0.1 - 0.01);
+ });
+
+ it('moves only the faces it is given, and the surface does not open where they meet others', () => {
+ const m = build([
+ { op: 'box', size: [1, 1, 0.2] },
+ { op: 'slice', axis: 'x', at: 0 },
+ { op: 'tag', faces: { within: { min: [0, null, null] } }, tag: 'right' },
+ { op: 'sculpt', faces: { tag: 'right' }, from: '+z', strokes: [{ brush: 'inflate', at: [0, 0], radius: 0.4, by: 0.05 }] },
+ ]);
+ expect(m.positions.filter((p) => p[0] < -1e-6 && p[2] > 0).every((p) => Math.abs(p[2] - 0.1) < 1e-9)).toBe(true);
+ expect(heightAt(m, 0.1, 0, 1, 0.05)!).toBeGreaterThan(0.11);
+ // Where the two meet, the vertices both use stay put, or the surface would open.
+ const seam = m.positions.filter((p) => Math.abs(p[0]) < 1e-6 && p[2] > 0).map((p) => p[2]);
+ expect(seam.length).toBeGreaterThan(1);
+ expect(Math.max(...seam)).toBeCloseTo(0.1, 9);
+ });
+
+ it('keeps what a face is: its tag and its colour', () => {
+ const m = build([{ ...plate, color: [0.2, 0.4, 0.6] }, { op: 'sculpt', from: '+z', strokes: [press] }]);
+ expect(m.faces.every((f) => f.tag === 'plate')).toBe(true);
+ expect(m.faces[m.faces.length - 1]!.color?.[1]).toBeCloseTo(0.4, 5);
+ });
+
+ it('fades out toward a rim, and leaves no fold or torn edge on a round form', () => {
+ const ball = build([
+ { op: 'organic', resolution: 60, parts: [{ shape: 'sphere', radius: 0.5 }] },
+ { op: 'sculpt', from: '+z', strokes: [{ brush: 'press', points: [[-0.6, 0], [0.6, 0]], width: 0.1, depth: 0.04, taper: 0 }] },
+ ]);
+ // On the rim the surface faces across the view: it stays where the sphere has it.
+ const rim = ball.positions.filter((p) => Math.abs(p[1]) < 0.01 && Math.abs(p[2]) < 0.02);
+ expect(rim.length).toBeGreaterThan(0);
+ for (const p of rim) expect(Math.hypot(p[0], p[1], p[2])).toBeCloseTo(0.5, 2);
+ // Straight on, it moves in full.
+ expect(heightAt(ball, 0, 0, 1, 0.02)!).toBeLessThan(0.5 - 0.025);
+ });
+
+ it('does the same every time', () => {
+ expect(stroke(press).positions).toEqual(stroke(press).positions);
+ });
+
+ it('says what is wrong with a stroke, naming it', () => {
+ const refused = (s: Record<string, unknown>, from: string | null = '+z'): (() => void) => () => build([plate, { op: 'sculpt', ...(from ? { from } : {}), strokes: [{ brush: 'smooth', at: from ? [0, 0] : [0, 0, 0], radius: 0.1 }, s] } as ModelStep]);
+ expect(refused({ brush: 'chisel', at: [0, 0], radius: 1 })).toThrow(/stroke 1.*chisel.*press, move, inflate, smooth/);
+ expect(refused({ ...press, width: 0 })).toThrow(/stroke 1.*width/);
+ expect(refused({ ...press, depth: undefined })).toThrow(/stroke 1.*depth/);
+ expect(refused({ ...press, lean: 1 })).toThrow(/stroke 1.*lean/);
+ expect(refused({ ...press, taper: 0.8 })).toThrow(/stroke 1.*taper/);
+ expect(refused({ ...press, points: [[0, 0]] })).toThrow(/stroke 1.*points/);
+ expect(refused({ ...press, points: [[0, 0, 0], [1, 0, 0]] })).toThrow(/stroke 1.*two numbers/);
+ expect(refused({ ...press, points: [[0, 0], [1, 0]] }, null)).toThrow(/stroke 1.*\[x, y, z\]/);
+ expect(refused({ brush: 'move', at: [50, 50], radius: 0.1, by: [0, 0, 1] })).toThrow(/stroke 1.*reaches no surface/);
+ expect(() => build([plate, { op: 'sculpt', faces: 'nothing', from: '+z', strokes: [press] } as ModelStep])).toThrow(/matched no face/);
+ expect(() => validateModelSource({ steps: [plate, { op: 'sculpt', from: 'z', strokes: [press] }] })).toThrow(/from must be one of/);
+ expect(() => validateModelSource({ steps: [plate, { op: 'sculpt', strokes: [] }] })).toThrow(/strokes/);
+ });
+});
+
+describe('a stain seen from a view', () => {
+ const coloured: ModelStep = { ...plate, color: [1, 1, 1] };
+ const line = { points: [[-0.3, 0], [0.3, 0]], width: 0.05, from: '+z' };
+ const darkAt = (m: Mesh, sign: 1 | -1): number => m.faces.filter((f) => {
+ const [a, b, c] = f.v.map((v) => m.positions[v]!);
+ return (a![2] + b![2] + c![2]) * sign > 0.25 && (f.color?.[0] ?? 1) < 0.5;
+ }).length;
+
+ it('lands on the side it is seen from, along the line', () => {
+ const m = build([coloured, { op: 'stain', color: [0, 0, 0], line, detail: 0.01 }]);
+ expect(darkAt(m, 1)).toBeGreaterThan(100);
+ expect(darkAt(m, -1)).toBe(0);
+ });
+
+ it('takes two numbers a point from a view and three without', () => {
+ expect(() => validateModelSource({ steps: [plate, { op: 'stain', color: [0, 0, 0], line: { points: [[0, 0, 0], [1, 0, 0]], width: 0.1, from: '+z' } }] })).toThrow(/two axes/);
+ expect(() => validateModelSource({ steps: [plate, { op: 'stain', color: [0, 0, 0], line: { points: [[0, 0], [1, 0]], width: 0.1 } }] })).toThrow(/x, y, z/);
+ expect(() => validateModelSource({ steps: [plate, { op: 'stain', color: [0, 0, 0], line: { points: [[0, 0], [1, 0]], width: 0.1, from: 'up' } }] })).toThrow(/from must be/);
+ });
+
+ it('says when nothing is there', () => {
+ expect(() => build([coloured, { op: 'stain', color: [0, 0, 0], line: { points: [[50, 50], [51, 50]], width: 0.1, from: '+z' } }])).toThrow(/within 0.1 of the line, seen from \+z/);
+ });
+});
diff --git a/pipeline/tests/modeling.test.ts b/pipeline/tests/modeling.test.ts
index 26235cb7c..897907a40 100644
--- a/pipeline/tests/modeling.test.ts
+++ b/pipeline/tests/modeling.test.ts
@@ -299,7 +299,7 @@ describe('the source format', () => {
});
it('refuses an unknown op, listing them', () => {
- expect(() => validateModelSource({ steps: [{ op: 'sculpt' }] })).toThrow(/unknown op "sculpt"; there are box/);
+ expect(() => validateModelSource({ steps: [{ op: 'chisel' }] })).toThrow(/unknown op "chisel"; there are box/);
});
it('refuses a missing required field and a wrong type', () => {
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 47c51c6c0..75431119b 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -3884,6 +3884,10 @@ export const NOTED = {
{"note":"A material's normal and metal-rough maps are read as data in the editor's Play."},
"fix(minigame): no mini-game module needs BigInt64Array, so WeChat DevTools boots the game":
{"note":"A WeChat mini-game opens in WeChat DevTools."},
+ "feat(modeling): a drawing stands up as a solid, sculpt works a surface by stroke, and a view takes coordinates off a picture":
+ {"note":"An AI can read a shape off a picture and model it."},
+ "feat(mcp): reference_view numbers a grid on a picture, and a close-up shows the same part of both":
+ {"note":"An AI can read a shape off a picture and model it."},
"feat(modeling): hair follows guides over the head, stain draws lines, scatter lays out copies, and a material gives off light":
{"note":"An AI can model a character's hair by where it goes, and draw on a surface."},
"feat(mcp): model_write describes the new steps, and apply_scene_ops says which field a component does not have":