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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(look): one instruction, many strokes: sweep for hair, field for a region
yhhef4ac405721d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 039e1524f..8a30b085e 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,7 +22,7 @@ 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 draw 2D pictures.** `image_write` turns layers into a `.png` beside an `.esimage`: shapes and SVG paths, gradients, strokes, shadows and glows, clipping and cutting, or pixel art as rows of characters. It answers with the picture on a mid ground and a light check and at 96, 64, 48 and 32 pixels, to look at and correct; `image_edit` changes one layer, a row or a block of pixels without restating the rest, and moves layers or wraps them in a group. Strokes can keep sharp corners, and the reply measures where each layer ended up. A layer or group can be blurred, for depth of field, and a fill can be noise for cloud, texture and film grain, the same picture from the same seed. A `paint` layer is a canvas of brush strokes with pressure, softness, tapering, bristles and smudging, added to round by round, for what shapes cannot do. `image_read` enlarges a part, and `image_set` shows several together with the stroke widths and size of each. Drawn on the CPU, so a source makes the same picture on any machine.+- **An AI can draw 2D pictures.** `image_write` turns layers into a `.png` beside an `.esimage`: shapes and SVG paths, gradients, strokes, shadows and glows, clipping and cutting, or pixel art as rows of characters. It answers with the picture on a mid ground and a light check and at 96, 64, 48 and 32 pixels, to look at and correct; `image_edit` changes one layer, a row or a block of pixels without restating the rest, and moves layers or wraps them in a group. Strokes can keep sharp corners, and the reply measures where each layer ended up. A layer or group can be blurred, for depth of field, and a fill can be noise for cloud, texture and film grain, the same picture from the same seed. A `paint` layer is a canvas of brush strokes with pressure, softness, tapering, bristles and smudging, added to round by round, for what shapes cannot do; one `sweep` instruction lays a mass of hair gathered into locks, and one `field` covers a region in strokes along a flow. `image_read` enlarges a part, and `image_set` shows several together with the stroke widths and size of each. Drawn on the CPU, so a source makes the same picture on any machine.- **An AI can build a person, and not a picture of one.** The `humanoid` step makes a whole body from a few numbers, with fingers, a face and ears, seen right from every side, and `rig: { humanoid: true }` gives it bones that fit. `garment` makes clothes from the body's own surface: loose or tight, a bell sleeve that hangs, a clean hem, a thickness; what a body wears now takes its weights from the skin under it, so an arm stays in its sleeve. `pose` holds a body in a pose by angles, or with `reach` by where a hand should be. `organic` gains `limb`, cross-sections along a path, and `fine`, regions split finer on one skin. `displace` gains `folds`, running the way cloth hangs. `scene_views` no longer takes on a shot's lens blur and grade.diff --git a/desktop b/desktopindex 963622c6d..11d4d63fc 160000--- a/desktop+++ b/desktop@@ -1 +1 @@-Subproject commit 963622c6dfa8aa61f566b878751de2572da34017+Subproject commit 11d4d63fc644fa9cfff0300c5f3d5230cfc3ea10diff --git a/pipeline/src/image/imageRaster.ts b/pipeline/src/image/imageRaster.tsindex 1a88f45da..3d9b8e784 100644--- a/pipeline/src/image/imageRaster.ts+++ b/pipeline/src/image/imageRaster.ts@@ -7,7 +7,8 @@* effects, and this owns everything that has to be exact: antialiased edges, gradients, soft shadows.* On the CPU rather than through a canvas, so the same source makes the same bytes on any machine and a test can read them.*/-import type { ImageFill, ImageLayer, ImageSource, ImageEffect, ImageStroke, ImageTransform } from './imageSource';+import { MAX_MARK_STROKES } from './imageSource';+import type { ImageFill, ImageLayer, ImageSource, ImageEffect, ImageField, ImageMark, ImageStroke, ImageSweep, ImageTransform } from './imageSource';export interface RgbaBitmap { width: number; height: number; rgba: Uint8Array }@@ -866,6 +867,163 @@ function paintStroke(c: Canvas, s: ImageStroke, m: Matrix, scale: number, n: num}}+const channelHex = (v: number): string => Math.max(0, Math.min(255, Math.round(v * 255))).toString(16).padStart(2, '0');+const toHex = (c: Rgba, shade = 1): string => `#${channelHex(c[0] * shade)}${channelHex(c[1] * shade)}${channelHex(c[2] * shade)}${channelHex(c[3])}`;++/** A guide at evenly spaced places along its length, smooth through its points. */+function resampled(guide: readonly (readonly number[])[], samples: number): { x: number; y: number; pressure: number }[] {+ const path = brushPath(guide, IDENTITY);+ const total = path[path.length - 1]!.along;+ let index = 0;+ return Array.from({ length: samples }, (_, i) => {+ const at = (total * i) / (samples - 1);+ while (index + 1 < path.length - 1 && path[index + 1]!.along < at) index++;+ const [a, b] = [path[index]!, path[index + 1] ?? path[index]!];+ const t = b.along > a.along ? Math.min(1, (at - a.along) / (b.along - a.along)) : 0;+ return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, pressure: a.pressure + (b.pressure - a.pressure) * t };+ });+}++const SWEEP_SAMPLES = 14;++/** Hairs between the guides. Each belongs to a lock and draws into it toward the tip, and a lock shares a shade, which is what makes a mass read as locks and not as combed lines. */+function sweepStrokes(s: ImageSweep): ImageStroke[] {+ const guides = s.guides.map((g) => resampled(g, SWEEP_SAMPLES));+ const seed = s.seed ?? 1;+ const colors = s.colors.map(parseColor);+ const tip = s.tip ? parseColor(s.tip) : null;+ const clumps = s.clumps ?? Math.max(2, Math.round(s.count / 14));+ const [gather, flyaway, vary, wobble] = [s.gather ?? 0.6, s.flyaway ?? 0.02, s.vary ?? 0.12, s.wobble ?? 1.5];+ const colorAt = (u: number): Rgba => {+ const at = Math.max(0, Math.min(0.9999, u)) * (colors.length - 1);+ const [i, t] = [Math.floor(at), at - Math.floor(at)];+ const [a, b] = [colors[i]!, colors[Math.min(colors.length - 1, i + 1)]!];+ return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t];+ };+ const lockCentre = Array.from({ length: clumps }, (_, k) => (k + 0.2 + hash01(seed * 7 + k, 1) * 0.6) / clumps);+ const lockShade = Array.from({ length: clumps }, (_, k) => 1 + (hash01(seed * 7 + k, 2) - 0.5) * vary * 2.5);+ const out: ImageStroke[] = [];+ for (let j = 0; j < s.count; j++) {+ const pick = (k: number): number => hash01(seed * 131 + j, k);+ const u = (j + pick(1)) / s.count;+ const lock = Math.min(clumps - 1, Math.floor(u * clumps));+ const strays = pick(2) < flyaway;+ const [side, reach] = [pick(3) < 0.5 ? -1 : 1, 0.6 + pick(4) * 1.4];+ const length = 1 - pick(5) * (strays ? 0.05 : 0.16);+ const points: number[][] = [];+ for (let i = 0; i < SWEEP_SAMPLES; i++) {+ const t = (i / (SWEEP_SAMPLES - 1)) * length;+ const draw = strays ? 0 : gather * t * t;+ const where = (u + (lockCentre[lock]! - u) * draw) * (guides.length - 1);+ const [g, f] = [Math.min(guides.length - 2, Math.floor(where)), where - Math.min(guides.length - 2, Math.floor(where))];+ const at = (line: readonly { x: number; y: number }[]): [number, number] => {+ const place = t * (SWEEP_SAMPLES - 1);+ const [low, frac] = [Math.min(SWEEP_SAMPLES - 2, Math.floor(place)), place - Math.min(SWEEP_SAMPLES - 2, Math.floor(place))];+ return [line[low]!.x + (line[low + 1]!.x - line[low]!.x) * frac, line[low]!.y + (line[low + 1]!.y - line[low]!.y) * frac];+ };+ const [ax, ay] = at(guides[g]!);+ const [bx, by] = at(guides[g + 1]!);+ const [x, y] = [ax + (bx - ax) * f, ay + (by - ay) * f];+ const [cx, cy] = [bx - ax, by - ay];+ const across = Math.hypot(cx, cy) || 1;+ const shake = gradientNoise(t * 3.1, j * 0.73 + 0.5, seed) * wobble * 2 * t;+ const stray = strays ? side * reach * t * t * (across * 0.5 + (s.size ?? 1.2) * 14) : 0;+ points.push([Math.round((x + (cx / across) * (shake + stray)) * 100) / 100, Math.round((y + (cy / across) * (shake + stray)) * 100) / 100]);+ }+ const shade = lockShade[lock]! * (1 + (pick(6) - 0.5) * vary);+ const root = colorAt(u + (pick(7) - 0.5) * 0.08);+ const size = (s.size ?? 1.2) * (0.7 + pick(8) * 0.6);+ out.push({+ points, size, hardness: 0.7, color: toHex(root, shade),+ ...(tip ? { to: toHex([root[0] + (tip[0] - root[0]) * 0.8, root[1] + (tip[1] - root[1]) * 0.8, root[2] + (tip[2] - root[2]) * 0.8, root[3]], shade) } : {}),+ opacity: (s.opacity ?? 1) * (strays ? 0.55 : 0.75 + pick(9) * 0.25),+ taper: s.taper ?? [0.04, 0.45],+ });+ }+ return out;+}++/** Strokes across a region, following the guides or an angle. Each takes its colour from `tone` where it starts. */+function fieldStrokes(s: ImageField, w: number, h: number): ImageStroke[] {+ const seed = s.seed ?? 1;+ const regionRuns = s.region ? parsePath(s.region).map((r) => r.points) : null;+ const inside = (x: number, y: number): boolean => {+ if (!regionRuns) return true;+ let winding = 0;+ for (const run of regionRuns) {+ for (let i = 0; i < run.length; i++) {+ const [a, b] = [run[i]!, run[(i + 1) % run.length]!];+ if ((a[1] <= y) === (b[1] <= y)) continue;+ const cross = (b[0] - a[0]) * (y - a[1]) - (x - a[0]) * (b[1] - a[1]);+ if (b[1] > a[1] ? cross > 0 : cross < 0) winding += b[1] > a[1] ? 1 : -1;+ }+ }+ return winding !== 0;+ };+ const all = regionRuns ? regionRuns.flat() : [[0, 0], [w, h]] as Pt[];+ const [x0, x1] = [Math.min(...all.map((p) => p[0])), Math.max(...all.map((p) => p[0]))];+ const [y0, y1] = [Math.min(...all.map((p) => p[1])), Math.max(...all.map((p) => p[1]))];+ const flow = (s.guides ?? []).flatMap((g) => {+ const line = resampled(g, 24);+ return line.slice(0, -1).map((p, i) => {+ const q = line[i + 1]!;+ const len = Math.hypot(q.x - p.x, q.y - p.y) || 1;+ return { x: p.x, y: p.y, dx: (q.x - p.x) / len, dy: (q.y - p.y) / len };+ });+ });+ const direction = (x: number, y: number): [number, number] => {+ if (flow.length === 0) {+ const r = ((s.angle ?? 0) * Math.PI) / 180;+ return [Math.cos(r), Math.sin(r)];+ }+ let [dx, dy] = [0, 0];+ for (const f of flow) {+ const weight = 1 / (1 + ((x - f.x) ** 2 + (y - f.y) ** 2) ** 1.5);+ dx += f.dx * weight; dy += f.dy * weight;+ }+ const len = Math.hypot(dx, dy) || 1;+ return [dx / len, dy / len];+ };+ const paint = paintOf(s.tone, IDENTITY);+ const length = s.length ?? s.size * 3;+ const count = Math.min(MAX_MARK_STROKES, Math.ceil(((s.coverage ?? 2.5) * Math.max(1, (x1 - x0) * (y1 - y0))) / (s.size * length)));+ const spread = ((s.spread ?? 12) * Math.PI) / 180;+ const vary = s.vary ?? 0.05;+ const steps = 5;+ const out: ImageStroke[] = [];+ // One start in each cell of a grid, in shuffled order: wholly random starts leave holes, and starts in rows lay strokes over each other in rows.+ const across = Math.max(1, Math.round(Math.sqrt((count * (x1 - x0 || 1)) / (y1 - y0 || 1))));+ const down = Math.max(1, Math.ceil(count / across));+ const order = Array.from({ length: across * down }, (_, i) => i).sort((a, b) => hash01(seed * 53 + a, 9) - hash01(seed * 53 + b, 9));+ for (const j of order) {+ const pick = (k: number): number => hash01(seed * 197 + j, k);+ const [sx, sy] = [x0 + (((j % across) + pick(1)) / across) * (x1 - x0), y0 + ((Math.floor(j / across) + pick(2)) / down) * (y1 - y0)];+ if (!inside(sx, sy)) continue;+ const turn = (pick(3) - 0.5) * 2 * spread;+ const points: number[][] = [];+ const [back, forward] = [-length / 2, length / steps];+ let [x, y] = [sx, sy];+ const [d0x, d0y] = direction(sx, sy);+ x += d0x * back; y += d0y * back;+ for (let k = 0; k <= steps; k++) {+ points.push([Math.round(x * 100) / 100, Math.round(y * 100) / 100]);+ const [dx, dy] = direction(x, y);+ const [rx, ry] = [dx * Math.cos(turn) - dy * Math.sin(turn), dx * Math.sin(turn) + dy * Math.cos(turn)];+ x += rx * forward; y += ry * forward;+ }+ out.push({+ points, size: s.size * (0.75 + pick(4) * 0.5), hardness: s.hardness ?? 0.55, opacity: s.opacity ?? 0.85,+ color: toHex(paint(sx, sy), 1 + (pick(5) - 0.5) * 2 * vary), taper: [0.12, 0.3],+ });+ }+ return out;+}++const expandMark = (mark: ImageMark, w: number, h: number): readonly ImageStroke[] => {+ if ('type' in mark) return mark.type === 'sweep' ? sweepStrokes(mark) : fieldStrokes(mark, w, h);+ return [mark];+};+function drawPaint(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn, opacity: number, blend: Blend, clip: Float32Array | undefined): void {const { w, h } = c;const m = times(parent, transformOf(layer.transform, [w / 2, h / 2]));@@ -880,7 +1038,10 @@ function drawPaint(c: Canvas, layer: ImageLayer, parent: Matrix, drawn: Drawn, o}}const buffer: StrokeBuffer = { mask: new Float32Array(w * h), rgb: new Float32Array(w * h * 3), touched: [] };- (layer.strokes ?? []).forEach((s, n) => paintStroke(inner, s, m, drawnScale(m), n, buffer));+ let drawnStrokes = 0;+ (layer.strokes ?? []).forEach((mark) => {+ for (const s of expandMark(mark, w, h)) paintStroke(inner, s, m, drawnScale(m), drawnStrokes++, buffer);+ });const alpha = new Float32Array(w * h);for (let i = 0; i < alpha.length; i++) alpha[i] = inner.px[i * 4 + 3]!;if (layer.id) drawn.masks.set(layer.id, alpha);diff --git a/pipeline/src/image/imageSource.ts b/pipeline/src/image/imageSource.tsindex 1d131c70c..f7d5e5ada 100644--- a/pipeline/src/image/imageSource.ts+++ b/pipeline/src/image/imageSource.ts@@ -64,6 +64,53 @@ export interface ImageStroke {blend?: 'normal' | 'multiply' | 'screen' | 'overlay' | 'add';}+/** Many hairs between guide lines, gathered into locks. A few words of intent, and hundreds of strokes when drawn. */+export interface ImageSweep {+ type: 'sweep';+ /** Two or more lines, side by side and in order, each root to tip. */+ guides: readonly (readonly (readonly number[])[])[];+ /** Across the guides, first to last. */+ colors: readonly string[];+ count: number;+ /** What the colours have turned to by the tips. */+ tip?: string;+ /** The width of one hair. */+ size?: number;+ clumps?: number;+ /** How far hairs draw into their lock by the tips, 0..1. */+ gather?: number;+ /** The share of hairs that stray from the mass, 0..1. */+ flyaway?: number;+ vary?: number;+ wobble?: number;+ opacity?: number;+ taper?: readonly [number, number];+ seed?: number;+}++/** A region covered in strokes that follow a flow, each its colour from `tone` where it starts. */+export interface ImageField {+ type: 'field';+ /** An SVG path to start strokes in; the whole picture without it. Strokes may run over its edge. */+ region?: string;+ /** Lines the strokes follow, nearest the most, or a direction in degrees clockwise from right. */+ guides?: readonly (readonly (readonly number[])[])[];+ angle?: number;+ tone: ImageFill;+ size: number;+ length?: number;+ /** How many times over the strokes cover the region. */+ coverage?: number;+ /** Degrees either side of the flow. */+ spread?: number;+ vary?: number;+ opacity?: number;+ hardness?: number;+ seed?: number;+}++export type ImageMark = ImageStroke | ImageSweep | ImageField;+export interface ImageLayer {id?: string;shape: ImageShape;@@ -81,7 +128,7 @@ export interface ImageLayer {palette?: Readonly<Record<string, string>>;scale?: number;layers?: readonly ImageLayer[];- strokes?: readonly ImageStroke[];+ strokes?: readonly ImageMark[];fill?: ImageFill;stroke?: { color: ImageFill; width: number; align?: 'center' | 'inside' | 'outside'; join?: 'round' | 'miter' | 'bevel'; cap?: 'round' | 'butt' | 'square' };opacity?: number;@@ -130,8 +177,49 @@ const FIELDS = ['id', 'shape', 'at', 'size', 'radius', 'radii', 'inner', 'count'const SHAPES: readonly ImageShape[] = ['rect', 'circle', 'ellipse', 'ring', 'polygon', 'line', 'star', 'gear', 'path', 'pixels', 'paint', 'group'];const STROKE_FIELDS = ['points', 'color', 'to', 'size', 'hardness', 'opacity', 'taper', 'bristles', 'strand', 'spread', 'vary', 'wobble', 'seed', 'smudge', 'erase', 'blend'];+const SWEEP_FIELDS = ['type', 'guides', 'colors', 'count', 'tip', 'size', 'clumps', 'gather', 'flyaway', 'vary', 'wobble', 'opacity', 'taper', 'seed'];+const FIELD_FIELDS = ['type', 'region', 'guides', 'angle', 'tone', 'size', 'length', 'coverage', 'spread', 'vary', 'opacity', 'hardness', 'seed'];+const isGuide = (g: unknown): boolean => Array.isArray(g) && g.length >= 2 && g.every((p) => Array.isArray(p) && (p.length === 2 || p.length === 3) && p.every(isNum));+export const MAX_MARK_STROKES = 30000;++function markProblem(s: Record<string, unknown>): string | null {+ const zeroToOne = (keys: readonly string[]): string | null => {+ for (const key of keys) if (s[key] !== undefined && !(isNum(s[key]) && (s[key] as number) >= 0 && (s[key] as number) <= 1)) return `${key} must be 0..1`;+ return null;+ };+ const fields = s.type === 'sweep' ? SWEEP_FIELDS : FIELD_FIELDS;+ for (const key of Object.keys(s)) if (!fields.includes(key)) return `has unknown field "${key}"; a ${String(s.type)} takes ${fields.join(', ')}`;+ if (s.seed !== undefined && !Number.isInteger(s.seed)) return 'seed must be a whole number';+ if (s.type === 'sweep') {+ if (!Array.isArray(s.guides) || s.guides.length < 2 || !s.guides.every(isGuide)) return 'needs guides: two or more lines, each two or more [x, y]';+ if (!Array.isArray(s.colors) || s.colors.length === 0 || !s.colors.every(isColor)) return 'needs colors: one or more, across the guides';+ if (s.tip !== undefined && !isColor(s.tip)) return 'tip must be a colour';+ if (!(Number.isInteger(s.count) && (s.count as number) >= 1 && (s.count as number) <= MAX_MARK_STROKES)) return `count must be a whole number from 1 to ${MAX_MARK_STROKES}`;+ if (s.clumps !== undefined && !(Number.isInteger(s.clumps) && (s.clumps as number) >= 1)) return 'clumps must be a whole number of 1 or more';+ for (const key of ['size', 'wobble']) if (s[key] !== undefined && !(isNum(s[key]) && (s[key] as number) >= 0)) return `${key} must be 0 or more`;+ if (s.taper !== undefined && !(Array.isArray(s.taper) && s.taper.length === 2 && s.taper.every((t) => isNum(t) && t >= 0 && t <= 1))) return 'taper must be [start, end], each 0..1';+ return zeroToOne(['gather', 'flyaway', 'vary', 'opacity']);+ }+ if (s.region !== undefined) {+ if (typeof s.region !== 'string') return 'region must be an SVG path';+ try { parsePath(s.region); } catch (e) { return `region: ${(e as Error).message}`; }+ }+ if (s.guides !== undefined && !(Array.isArray(s.guides) && s.guides.length > 0 && s.guides.every(isGuide))) return 'guides must be one or more lines, each two or more [x, y]';+ if (s.angle !== undefined && !isNum(s.angle)) return 'angle must be degrees';+ const bad = checkFill(s.tone);+ if (bad) return `tone ${bad}`;+ for (const key of ['size', 'length', 'coverage']) {+ if (s[key] !== undefined && !(isNum(s[key]) && (s[key] as number) > 0)) return `${key} must be positive`;+ }+ if (s.size === undefined) return 'needs a positive size';+ if (s.spread !== undefined && !(isNum(s.spread) && (s.spread as number) >= 0)) return 'spread must be 0 or more degrees';+ return zeroToOne(['vary', 'opacity', 'hardness']);+}+function strokeProblem(s: Record<string, unknown>): string | null {if (typeof s !== 'object' || s === null) return 'must be an object';+ if (s.type === 'sweep' || s.type === 'field') return markProblem(s);+ if (s.type !== undefined) return `has type "${String(s.type)}"; there are sweep and field, or none for one stroke`;for (const key of Object.keys(s)) if (!STROKE_FIELDS.includes(key)) return `has unknown field "${key}"; a stroke takes ${STROKE_FIELDS.join(', ')}`;if (!Array.isArray(s.points) || s.points.length === 0 || !s.points.every((p) => Array.isArray(p) && (p.length === 2 || p.length === 3) && p.every(isNum))) return 'needs points, each [x, y] or [x, y, pressure]';if (!(isNum(s.size) && (s.size as number) > 0)) return 'needs a positive size';@@ -267,7 +355,7 @@ export type ImageEdit =| { op: 'insert'; layer: ImageLayer; before?: string; after?: string; into?: string }| { op: 'remove'; layer: string }| { op: 'move'; layer: string; before?: string; after?: string; into?: string }- | { op: 'paint'; layer: string; strokes?: readonly ImageStroke[]; drop?: number }+ | { op: 'paint'; layer: string; strokes?: readonly ImageMark[]; drop?: number }| { op: 'wrap'; layers: readonly string[]; group: Partial<ImageLayer> }| { op: 'unwrap'; layer: string }| {@@ -373,7 +461,7 @@ export function applyImageEdits(source: ImageSource, edits: readonly ImageEdit[]const { list, index } = need(edit.layer);const layer = list[index]!;if (layer.shape !== 'paint') throw new Error(`layer "${edit.layer}" is not paint`);- const had = layer.strokes as readonly ImageStroke[];+ const had = layer.strokes as readonly ImageMark[];const drop = edit.drop ?? 0;if (!Number.isInteger(drop) || drop < 0 || drop > had.length) throw new Error(`layer "${edit.layer}" has ${had.length} strokes: cannot drop ${String(edit.drop)}`);layer.strokes = [...had.slice(0, had.length - drop), ...(edit.strokes ?? [])];diff --git a/pipeline/tests/image-raster.test.ts b/pipeline/tests/image-raster.test.tsindex 8ae02a7c2..d4ba74a78 100644--- a/pipeline/tests/image-raster.test.ts+++ b/pipeline/tests/image-raster.test.ts@@ -748,6 +748,108 @@ describe('brushes', () => {});});+describe('marks that are many strokes', () => {+ const paint = (strokes: object[]): ImageLayer => ({ id: 'p', shape: 'paint', strokes } as never);+ const pair = [[[10, 10], [10, 110]], [[60, 10], [60, 110]]];+ const sweep = (fields: object = {}): object => ({ type: 'sweep', guides: pair, colors: [RED], count: 200, flyaway: 0, wobble: 0, seed: 3, ...fields });++ it('a sweep stays between its guides, and covers them', () => {+ const img = draw([paint([sweep({ gather: 0 })])], [128, 128]);+ expect(px(img, 35, 40)[3]).toBeGreaterThan(100);+ expect(px(img, 2, 40)[3]).toBe(0);+ expect(px(img, 70, 40)[3]).toBe(0);+ expect(px(img, 35, 124)[3]).toBe(0);+ });++ it('a sweep is as dense as its count', () => {+ const coverage = (count: number): number => { const img = draw([paint([sweep({ count, gather: 0 })])], [128, 128]); let n = 0; for (let x = 8; x < 62; x++) if (px(img, x, 40)[3]! > 60) n++; return n; };+ expect(coverage(300)).toBeGreaterThan(coverage(10) * 2);+ });++ it('gathers its hairs into locks toward the tips, and leaves them even without', () => {+ const gaps = (gather: number): number => {+ const img = draw([paint([sweep({ gather, clumps: 4, count: 300, size: 1.2, taper: [0, 0] })])], [128, 128]);+ let g = 0;+ for (let x = 12; x < 58; x++) if (px(img, x, 92)[3]! < 30) g++;+ return g;+ };+ expect(gaps(1)).toBeGreaterThan(gaps(0) + 6);+ });++ it('sends strays out past the mass only when asked', () => {+ const outside = (flyaway: number): number => {+ const img = draw([paint([sweep({ flyaway, gather: 0 })])], [128, 128]);+ let n = 0;+ for (let y = 60; y < 112; y++) for (let x = 0; x < 128; x++) if ((x < 6 || x > 66) && px(img, x, y)[3]! > 20) n++;+ return n;+ };+ expect(outside(0)).toBe(0);+ expect(outside(0.3)).toBeGreaterThan(10);+ });++ it('takes its colours across the guides, and turns to the tip colour', () => {+ const img = draw([paint([sweep({ colors: [RED, BLUE], gather: 0, vary: 0 })])], [128, 128]);+ expect(px(img, 14, 40)[0]).toBeGreaterThan(px(img, 14, 40)[2]!);+ expect(px(img, 56, 40)[2]).toBeGreaterThan(px(img, 56, 40)[0]!);+ const tipped = draw([paint([sweep({ tip: BLUE, gather: 0, vary: 0 })])], [128, 128]);+ expect(px(tipped, 35, 16)[0]).toBeGreaterThan(px(tipped, 35, 16)[2]!);+ expect(px(tipped, 35, 85)[2]).toBeGreaterThan(px(tipped, 35, 16)[2]!);+ });++ const region = 'M20 20 L100 20 L100 100 L20 100 Z';+ const field = (fields: object = {}): object => ({ type: 'field', region, tone: RED, size: 6, length: 12, angle: 0, spread: 0, seed: 2, ...fields });++ it('a field covers its region, and only runs a stroke\'s length past it', () => {+ const img = draw([paint([field()])], [128, 128]);+ let covered = 0;+ for (let y = 24; y < 96; y += 4) for (let x = 24; x < 96; x += 4) if (px(img, x, y)[3]! > 128) covered++;+ expect(covered).toBeGreaterThan(18 * 18 * 0.9);+ expect(px(img, 4, 60)[3]).toBe(0);+ expect(px(img, 60, 4)[3]).toBe(0);+ });++ it('a field runs along its flow: a line changes less along it than across it', () => {+ const stripes = (fields: object): { along: number; across: number } => {+ const img = draw([paint([field({ region: undefined, tone: { type: 'noise', scale: 1, octaves: 1, stops: [[0, '#000000'], [1, '#ffffff']] }, size: 3, length: 40, coverage: 3, ...fields })])], [128, 128]);+ let [along, across] = [0, 0];+ for (let i = 8; i < 118; i++) {+ along += Math.abs(px(img, i, 64)[0]! - px(img, i + 1, 64)[0]!);+ across += Math.abs(px(img, 64, i)[0]! - px(img, 64, i + 1)[0]!);+ }+ return { along, across };+ };+ const flat = stripes({ angle: 0 });+ expect(flat.across).toBeGreaterThan(flat.along * 1.5);+ const down = stripes({ angle: undefined, guides: [[[64, 0], [64, 128]]] });+ expect(down.along).toBeGreaterThan(down.across * 1.5);+ });++ it('a field takes each stroke\'s colour from its tone', () => {+ const img = draw([paint([field({ region: undefined, tone: { type: 'linear', from: [0, 0], to: [128, 0], stops: [[0, RED], [1, BLUE]] }, vary: 0 })])], [128, 128]);+ expect(px(img, 12, 64)[0]).toBeGreaterThan(px(img, 12, 64)[2]!);+ expect(px(img, 116, 64)[2]).toBeGreaterThan(px(img, 116, 64)[0]!);+ });++ it('is the same from the same seed, and another from another', () => {+ expect(draw([paint([field()])]).rgba).toEqual(draw([paint([field()])]).rgba);+ expect(draw([paint([sweep({ flyaway: 0.1 })])]).rgba).toEqual(draw([paint([sweep({ flyaway: 0.1 })])]).rgba);+ expect(draw([paint([sweep({ seed: 8, gather: 0.8 })])]).rgba).not.toEqual(draw([paint([sweep({ seed: 9, gather: 0.8 })])]).rgba);+ });++ it('says what is wrong with one', () => {+ const bad = (mark: object): unknown => validateImageSource({ size: 8, layers: [{ id: 'p', shape: 'paint', strokes: [mark] }] });+ expect(() => bad({ type: 'wash' })).toThrow(/stroke 0 has type "wash"; there are sweep and field/);+ expect(() => bad({ type: 'sweep', guides: [[[0, 0], [1, 1]]], colors: [RED], count: 3 })).toThrow(/two or more lines/);+ expect(() => bad(sweep({ colors: [] }))).toThrow(/needs colors/);+ expect(() => bad(sweep({ count: 0 }))).toThrow(/count must be/);+ expect(() => bad(sweep({ gather: 2 }))).toThrow(/gather must be 0..1/);+ expect(() => bad(sweep({ nope: 1 }))).toThrow(/unknown field "nope"/);+ expect(() => bad(field({ region: 'M0 0 A1 1 0 0 1 2 2' }))).toThrow(/region: path command A/);+ expect(() => bad(field({ tone: 'red' }))).toThrow(/tone/);+ expect(() => bad(field({ size: undefined }))).toThrow(/needs a positive size/);+ });+});+describe('what it measures', () => {it('finds where the picture is solid and how much of it that is', () => {const img = draw([{ shape: 'rect', at: [10, 20], size: [16, 8], fill: RED }]);diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex 29efd60e0..4aed31e1a 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -3888,6 +3888,10 @@ export const NOTED = {{"note":"An AI can draw 2D pictures."},"feat(look): layers moved and wrapped, one pixel changed, a set and a close look drawn":{"note":"An AI can draw 2D pictures."},+ "feat(look): one instruction, many strokes: sweep for hair, field for a region":+ {"note":"An AI can draw 2D pictures."},+ "feat(mcp): image_write documents sweep and field":+ {"note":"An AI can draw 2D pictures."},"feat(look): a paint layer, brush strokes on a canvas replayed in order":{"note":"An AI can draw 2D pictures."},"feat(mcp): image_write and image_edit paint with brush strokes":