@flare-engine/shapes 0.4.11 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -1
- package/dist/index.d.mts +148 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +148 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +179 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +175 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -1
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/bounds.ts","../src/draw.ts"],"sourcesContent":["import type { BaseSpec, ShapeSpec, SquareSpec } from './types.js';\n\n/** Per-axis extents in the composition-local frame (the frame `ox`/`oy` live in). */\nexport interface ShapeBoundsBox {\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n}\n\n/** A `ship` part: its hull/weapons are `ShapeSpec`s, its engine a `SquareSpec`. */\ntype Part = ShapeSpec | SquareSpec;\n\n/**\n * Approximate bounding radius for a single primitive. Used to size glow\n * rings and atlas tiles. Returned values are deliberately a touch generous\n * to avoid clipping.\n */\nexport function singleShapeRadius(spec: ShapeSpec): number {\n switch (spec.kind) {\n case 'circle':\n case 'ring':\n case 'ngon':\n return spec.r;\n case 'triangle':\n return spec.size;\n case 'star':\n return spec.rOuter;\n case 'rect':\n case 'gradient-rect':\n return Math.max(spec.w, spec.h) * 0.6;\n case 'polyline': {\n let max = 0;\n for (let i = 0; i < spec.points.length; i += 2) {\n const px = spec.points[i] ?? 0;\n const py = spec.points[i + 1] ?? 0;\n const m = Math.hypot(px, py);\n if (m > max) max = m;\n }\n return max + (spec.strokeWidth ?? 1);\n }\n case 'arc':\n return spec.r;\n case 'annulus':\n return spec.rOuter;\n case 'trapezoid':\n return Math.max(spec.topW, spec.botW, spec.h) * 0.6;\n case 'path':\n return spec.boundsR;\n case 'ship': {\n const { hull, weapons, engine } = spec;\n let max = Math.hypot(hull.ox ?? 0, hull.oy ?? 0) + singleShapeRadius(hull);\n for (let i = 0; i < weapons.length; i++) {\n const w = weapons[i];\n if (w === undefined) continue;\n const reach = Math.hypot(w.ox ?? 0, w.oy ?? 0) + singleShapeRadius(w);\n if (reach > max) max = reach;\n }\n // engine mirrors a w=h=size rect (see drawSquare); 0.6 is the rect face factor used by the 'rect' case above\n const engineReach = Math.hypot(engine.ox ?? 0, engine.oy ?? 0) + engine.size * 0.6;\n if (engineReach > max) max = engineReach;\n return max;\n }\n }\n}\n\n/**\n * Compute axis-aligned bounding radius for a shape composition. Used to\n * size atlas tiles. Returns the max half-extent from origin across all\n * shapes, including their `ox`/`oy` offsets, a stroke-width margin added on\n * top of the base `+2` where a stroke is actually painted, and the extent of\n * a glow ring when `glow` is set (+5.5 friendly, +2.25 hazard, +2.5 + `width`\n * for a `GlowSpec`) — enough to fully contain what `drawShape` paints.\n */\nexport function shapeBoundsRadius(specs: readonly ShapeSpec[]): number {\n let max = 0;\n for (const s of specs) {\n const ox = s.ox ?? 0;\n const oy = s.oy ?? 0;\n const r = boundsR(s);\n const reach = Math.hypot(ox, oy) + r + 2 + strokeMargin(s) + glowMargin(s);\n if (reach > max) max = reach;\n }\n return Math.max(4, max);\n}\n\n/** Allocating form of {@link shapeBoundsBoxInto}. */\nexport function shapeBoundsBox(specs: readonly ShapeSpec[]): ShapeBoundsBox {\n return shapeBoundsBoxInto(specs, { minX: 0, minY: 0, maxX: 0, maxY: 0 });\n}\n\n/**\n * Per-axis sibling of `shapeBoundsRadius`, for hitboxes: the union of every\n * spec's extents at its `ox`/`oy`, each widened by the same `+2` + stroke +\n * glow margin. Exact for unrotated geometry (a `ship` unions hull, weapons\n * and engine); a spec with a non-zero `rot` falls back to its radial reach,\n * so the box never exceeds the radius on any axis. Empty `specs` give all\n * zeros — no 4-unit atlas floor. Writes `out` and returns it; allocates nothing.\n */\nexport function shapeBoundsBoxInto(\n specs: readonly ShapeSpec[],\n out: ShapeBoundsBox,\n): ShapeBoundsBox {\n out.minX = Infinity;\n out.minY = Infinity;\n out.maxX = -Infinity;\n out.maxY = -Infinity;\n for (let i = 0; i < specs.length; i++) {\n const s = specs[i];\n if (s !== undefined) expand(out, s, 0, 0, 0, false);\n }\n if (out.minX === Infinity) {\n out.minX = 0;\n out.minY = 0;\n out.maxX = 0;\n out.maxY = 0;\n }\n return out;\n}\n\n/** Union `out` with `spec` centred at (dx + ox, dy + oy), padded by `pad` plus its own margin. */\nfunction expand(\n out: ShapeBoundsBox,\n spec: Part,\n dx: number,\n dy: number,\n pad: number,\n rotated: boolean,\n): void {\n const cx = dx + (spec.ox ?? 0);\n const cy = dy + (spec.oy ?? 0);\n const m = pad + 2 + strokeMargin(spec) + glowMargin(spec);\n const rot = rotated || (spec.rot ?? 0) !== 0;\n if (spec.kind === 'ship') {\n // drawShape never rotates a part's offset; a rotated ship spins each part about its own centre\n expand(out, spec.hull, cx, cy, m, rot);\n for (let i = 0; i < spec.weapons.length; i++) {\n const w = spec.weapons[i];\n if (w !== undefined) expand(out, w, cx, cy, m, rot);\n }\n expand(out, spec.engine, cx, cy, m, rot);\n return;\n }\n if (!rot) {\n switch (spec.kind) {\n case 'rect':\n case 'gradient-rect':\n include(out, cx, cy, m, -spec.w / 2, -spec.h / 2, spec.w / 2, spec.h / 2);\n return;\n case 'square':\n include(out, cx, cy, m, -spec.size / 2, -spec.size / 2, spec.size / 2, spec.size / 2);\n return;\n case 'trapezoid': {\n const hw = Math.max(spec.topW, spec.botW) / 2;\n include(out, cx, cy, m, -hw, -spec.h / 2, hw, spec.h / 2);\n return;\n }\n case 'triangle':\n include(out, cx, cy, m, -spec.size * 0.6, -spec.size, spec.size * 0.6, spec.size * 0.4);\n return;\n case 'ngon':\n case 'star': {\n // same vertex walk as drawNgon / drawStar\n const n = spec.kind === 'ngon' ? spec.n : spec.points * 2;\n let x0 = 0;\n let y0 = 0;\n let x1 = 0;\n let y1 = 0;\n for (let i = 0; i < n; i++) {\n const a = (i / n) * Math.PI * 2 - Math.PI / 2;\n const vr = spec.kind === 'ngon' ? spec.r : i % 2 === 0 ? spec.rOuter : spec.rInner;\n const px = Math.cos(a) * vr;\n const py = Math.sin(a) * vr;\n if (px < x0) x0 = px;\n else if (px > x1) x1 = px;\n if (py < y0) y0 = py;\n else if (py > y1) y1 = py;\n }\n include(out, cx, cy, m, x0, y0, x1, y1);\n return;\n }\n case 'polyline': {\n const p = spec.points;\n let x0 = p[0] ?? 0;\n let y0 = p[1] ?? 0;\n let x1 = x0;\n let y1 = y0;\n for (let i = 2; i < p.length; i += 2) {\n const px = p[i] ?? 0;\n const py = p[i + 1] ?? 0;\n if (px < x0) x0 = px;\n else if (px > x1) x1 = px;\n if (py < y0) y0 = py;\n else if (py > y1) y1 = py;\n }\n include(out, cx, cy, m + (spec.strokeWidth ?? 1), x0, y0, x1, y1);\n return;\n }\n }\n }\n const e = boundsR(spec);\n include(out, cx, cy, m, -e, -e, e, e);\n}\n\nfunction include(\n out: ShapeBoundsBox,\n cx: number,\n cy: number,\n m: number,\n x0: number,\n y0: number,\n x1: number,\n y1: number,\n): void {\n out.minX = Math.min(out.minX, cx + x0 - m);\n out.minY = Math.min(out.minY, cy + y0 - m);\n out.maxX = Math.max(out.maxX, cx + x1 + m);\n out.maxY = Math.max(out.maxY, cy + y1 + m);\n}\n\n/**\n * Extra reach for a stroke `drawShape` actually paints, else 0. `ring` and\n * `polyline` always stroke (draw.ts falls back to a default color/width);\n * every other kind strokes only when `stroke` is set. `gradient-rect` never\n * strokes, and `annulus`'s ring width is the fill-derived `rOuter - rInner`\n * band (already exactly bounded by `rOuter`), not `strokeWidth` — both are\n * excluded rather than double-counted. `ship` has no top-level stroke of its\n * own; its sub-shapes carry theirs through the nested `boundsR` recursion.\n */\nfunction strokeMargin(spec: Part): number {\n const painted =\n spec.kind === 'ring' || spec.kind === 'polyline'\n ? true\n : spec.kind === 'gradient-rect' || spec.kind === 'annulus' || spec.kind === 'ship'\n ? false\n : spec.stroke !== undefined;\n return painted ? Math.max(2, (spec.strokeWidth ?? 1) / 2) : 0;\n}\n\n/**\n * Extra reach for the glow ring `drawShape` paints outside the body (see\n * `applyFriendlyGlow`/`applyHazardGlow`/`applyGlow`). A `GlowSpec` ring sits\n * 2.5 px out and is `width` wide, so its outer edge is `2.5 + width` beyond\n * the body; a width `applyGlow` refuses to paint adds nothing.\n */\nfunction glowMargin(spec: BaseSpec): number {\n const glow = spec.glow;\n if (glow === undefined) return 0;\n if (glow === 'friendly') return 5.5;\n if (glow === 'hazard') return 2.25;\n return Number.isFinite(glow.width) && glow.width >= 0 ? 2.5 + glow.width : 0;\n}\n\n/** Tighter atlas-tile-sizing radius (rect/gradient-rect use 0.75 to fit corners). */\nfunction boundsR(spec: Part): number {\n switch (spec.kind) {\n case 'rect':\n case 'gradient-rect':\n return Math.max(spec.w, spec.h) * 0.75;\n case 'square':\n return spec.size * 0.75;\n case 'trapezoid':\n return Math.max(spec.topW, spec.botW, spec.h) * 0.75;\n case 'ship':\n return shapeBoundsRadius([\n spec.hull,\n ...spec.weapons,\n {\n kind: 'rect',\n w: spec.engine.size,\n h: spec.engine.size,\n ox: spec.engine.ox,\n oy: spec.engine.oy,\n stroke: spec.engine.stroke,\n strokeWidth: spec.engine.strokeWidth,\n glow: spec.engine.glow,\n },\n ]);\n default:\n return singleShapeRadius(spec);\n }\n}\n","/**\n * Shape drawing dispatcher — consumes a `ShapeSpec` and emits Skia draw\n * calls on a canvas. All shapes draw relative to (x + ox, y + oy), rotated\n * by (rot + spec.rot).\n *\n * The dispatcher is allocation-free per call except where Skia itself\n * allocates path objects. Glow ring is emitted before the shape body so it\n * renders behind the geometry.\n */\n\nimport type { SkCanvas, Skia as SkiaModule, SkPaint, SkPath } from '@shopify/react-native-skia';\nimport { singleShapeRadius } from './bounds.js';\nimport type {\n AnnulusSpec,\n ArcSpec,\n GradientRectSpec,\n NgonSpec,\n PathSpec,\n PolylineSpec,\n RectSpec,\n ShapeSpec,\n ShipSpec,\n SquareSpec,\n StarSpec,\n TrapezoidSpec,\n TriangleSpec,\n} from './types.js';\n\ntype SkiaApi = typeof SkiaModule;\n\n// ---- Glow recipes -----------------------------------------------------------\n\n/** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */\nexport function friendlyGlowArgb(): number {\n return (0x40 << 24) | 0x6fa66b;\n}\n\n/** ARGB for hazardOrange #E84B2A at α ≈ 0.6 (0x99 / 255). */\nexport function hazardGlowArgb(): number {\n return (0x99 << 24) | 0xe84b2a;\n}\n\nexport function applyFriendlyGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n): void {\n applyStroke(paint, friendlyGlowArgb(), 3);\n canvas.drawCircle(cx, cy, r + 4, paint);\n}\n\nexport function applyHazardGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n): void {\n const argb = hazardGlowArgb();\n applyStroke(paint, argb, 0.5);\n canvas.drawCircle(cx, cy, r + 1, paint);\n canvas.drawCircle(cx, cy, r + 2, paint);\n}\n\n/**\n * One stroked ring of `width` px in `color` (ARGB) whose inner edge sits\n * 2.5 px outside `r` — the gap `applyFriendlyGlow` uses, so `width: 3` with\n * `friendlyGlowArgb()` paints exactly the friendly ring. A non-finite or\n * negative `width` paints nothing.\n */\nexport function applyGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n color: number,\n width: number,\n): void {\n if (!Number.isFinite(width) || width < 0) return;\n applyStroke(paint, color, width);\n canvas.drawCircle(cx, cy, r + (2.5 + width / 2), paint);\n}\n\n// ---- Color plumbing ---------------------------------------------------------\n\nconst COLOR = new Float32Array(4);\n\nexport function argbTo(out: Float32Array, argb: number): void {\n out[0] = ((argb >>> 16) & 0xff) / 255;\n out[1] = ((argb >>> 8) & 0xff) / 255;\n out[2] = (argb & 0xff) / 255;\n out[3] = ((argb >>> 24) & 0xff) / 255;\n}\n\nfunction applyFill(paint: SkPaint, argb: number): void {\n argbTo(COLOR, argb);\n paint.setColor(COLOR);\n paint.setStyle(0); // Fill\n}\n\nfunction applyStroke(paint: SkPaint, argb: number, width: number): void {\n argbTo(COLOR, argb);\n paint.setColor(COLOR);\n paint.setStyle(1); // Stroke\n paint.setStrokeWidth(width);\n}\n\n/** Fill, then stroke, the same path; `strokeWidth` defaults to 1. */\nfunction fillStrokePath(\n canvas: SkCanvas,\n paint: SkPaint,\n path: SkPath,\n fill: number | undefined,\n stroke: number | undefined,\n strokeWidth: number | undefined,\n): void {\n if (fill !== undefined) {\n applyFill(paint, fill);\n canvas.drawPath(path, paint);\n }\n if (stroke !== undefined) {\n applyStroke(paint, stroke, strokeWidth ?? 1);\n canvas.drawPath(path, paint);\n }\n}\n\n// ---- Dispatcher -------------------------------------------------------------\n\nexport function drawShape(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ShapeSpec,\n x: number,\n y: number,\n rot: number,\n): void {\n const cx = x + (spec.ox ?? 0);\n const cy = y + (spec.oy ?? 0);\n const r = rot + (spec.rot ?? 0);\n\n if (spec.glow !== undefined) {\n const gr = singleShapeRadius(spec);\n if (spec.glow === 'friendly') {\n applyFriendlyGlow(canvas, paint, cx, cy, gr);\n } else if (spec.glow === 'hazard') {\n applyHazardGlow(canvas, paint, cx, cy, gr);\n } else {\n applyGlow(canvas, paint, cx, cy, gr, spec.glow.color, spec.glow.width);\n }\n }\n\n switch (spec.kind) {\n case 'circle': {\n if (spec.fill !== undefined) {\n applyFill(paint, spec.fill);\n canvas.drawCircle(cx, cy, spec.r, paint);\n }\n if (spec.stroke !== undefined) {\n applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);\n canvas.drawCircle(cx, cy, spec.r, paint);\n }\n return;\n }\n case 'ring': {\n applyStroke(paint, spec.stroke ?? 0xff000000, spec.strokeWidth ?? 1);\n canvas.drawCircle(cx, cy, spec.r, paint);\n return;\n }\n case 'rect':\n drawRect(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'triangle':\n drawTriangle(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'ngon':\n drawNgon(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'star':\n drawStar(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'polyline':\n drawPolyline(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'gradient-rect':\n drawGradientRect(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'arc':\n drawArc(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'annulus':\n drawAnnulus(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'trapezoid':\n drawTrapezoid(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'path':\n drawPath(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'ship':\n drawShip(Skia, canvas, paint, spec, cx, cy, r);\n return;\n }\n}\n\n// ---- Per-primitive renderers -----------------------------------------------\n\nfunction drawRect(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: RectSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n const rect = Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, spec.h);\n if (spec.fill !== undefined) {\n applyFill(paint, spec.fill);\n canvas.drawRect(rect, paint);\n }\n if (spec.stroke !== undefined) {\n applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);\n canvas.drawRect(rect, paint);\n }\n canvas.restore();\n}\n\nfunction drawTriangle(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: TriangleSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const s = spec.size;\n const path = Skia.Path.Make();\n path.moveTo(0, -s);\n path.lineTo(-s * 0.6, s * 0.4);\n path.lineTo(s * 0.6, s * 0.4);\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawNgon(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: NgonSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n for (let i = 0; i < spec.n; i++) {\n const a = (i / spec.n) * Math.PI * 2 - Math.PI / 2;\n const px = Math.cos(a) * spec.r;\n const py = Math.sin(a) * spec.r;\n if (i === 0) path.moveTo(px, py);\n else path.lineTo(px, py);\n }\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawStar(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: StarSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const total = spec.points * 2;\n for (let i = 0; i < total; i++) {\n const a = (i / total) * Math.PI * 2 - Math.PI / 2;\n const r = i % 2 === 0 ? spec.rOuter : spec.rInner;\n const px = Math.cos(a) * r;\n const py = Math.sin(a) * r;\n if (i === 0) path.moveTo(px, py);\n else path.lineTo(px, py);\n }\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawPolyline(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: PolylineSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const pts = spec.points;\n if (pts.length < 4) return;\n const path = Skia.Path.Make();\n path.moveTo(pts[0] ?? 0, pts[1] ?? 0);\n for (let i = 2; i < pts.length; i += 2) {\n path.lineTo(pts[i] ?? 0, pts[i + 1] ?? 0);\n }\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n applyStroke(paint, spec.stroke ?? 0xff000000, spec.strokeWidth ?? 1);\n canvas.drawPath(path, paint);\n canvas.restore();\n}\n\nfunction drawGradientRect(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: GradientRectSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n // Cheap two-half approximation; true shader gradient is overkill at atlas-build time.\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n const half = spec.axis === 1 ? spec.h / 2 : spec.w / 2;\n if (spec.axis === 1) {\n applyFill(paint, spec.colorA);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, half), paint);\n applyFill(paint, spec.colorB);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, 0, spec.w, half), paint);\n } else {\n applyFill(paint, spec.colorA);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, half, spec.h), paint);\n applyFill(paint, spec.colorB);\n canvas.drawRect(Skia.XYWHRect(0, -spec.h / 2, half, spec.h), paint);\n }\n canvas.restore();\n}\n\n// Skia uses degrees, 0° at +X, increasing clockwise. We accept radians (math\n// convention CCW positive) and convert; visual result matches expectations\n// when rotated through the canvas transform.\nfunction radToDeg(rad: number): number {\n return (rad * 180) / Math.PI;\n}\n\nfunction drawArc(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ArcSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const oval = Skia.XYWHRect(-spec.r, -spec.r, spec.r * 2, spec.r * 2);\n const startDeg = radToDeg(spec.startAngle);\n const sweepDeg = radToDeg(spec.endAngle - spec.startAngle);\n if (spec.sector === true) {\n path.moveTo(0, 0);\n path.addArc(oval, startDeg, sweepDeg);\n path.close();\n } else {\n path.addArc(oval, startDeg, sweepDeg);\n }\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawAnnulus(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: AnnulusSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n // Render as a thick stroked circle/arc at the mid-radius. The fill colour\n // becomes the ring colour (more ergonomic than even-odd path subtraction).\n const midR = (spec.rOuter + spec.rInner) / 2;\n const thickness = spec.rOuter - spec.rInner;\n const ringColor = spec.fill ?? spec.stroke ?? 0xff000000;\n\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n\n if (spec.startAngle !== undefined && spec.endAngle !== undefined) {\n const path = Skia.Path.Make();\n const oval = Skia.XYWHRect(-midR, -midR, midR * 2, midR * 2);\n path.addArc(oval, radToDeg(spec.startAngle), radToDeg(spec.endAngle - spec.startAngle));\n applyStroke(paint, ringColor, thickness);\n canvas.drawPath(path, paint);\n } else {\n applyStroke(paint, ringColor, thickness);\n canvas.drawCircle(0, 0, midR, paint);\n }\n\n canvas.restore();\n}\n\nfunction drawTrapezoid(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: TrapezoidSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const halfTopW = spec.topW / 2;\n const halfBotW = spec.botW / 2;\n const halfH = spec.h / 2;\n // Apex up: narrow top edge at -h/2, wide bottom edge at +h/2.\n path.moveTo(-halfTopW, -halfH);\n path.lineTo(halfTopW, -halfH);\n path.lineTo(halfBotW, halfH);\n path.lineTo(-halfBotW, halfH);\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawPath(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: PathSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.MakeFromSVGString(spec.d);\n // Malformed SVG returns null — skip silently rather than throw.\n if (path === null) return;\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawSquare(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: SquareSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const rectSpec: RectSpec = {\n kind: 'rect',\n w: spec.size,\n h: spec.size,\n fill: spec.fill,\n stroke: spec.stroke,\n strokeWidth: spec.strokeWidth,\n ox: spec.ox,\n oy: spec.oy,\n rot: spec.rot,\n glow: spec.glow,\n };\n // Route through the dispatcher (not drawRect) so ox/oy/rot and glow — which\n // drawRect never reads — are actually applied. See F016.\n drawShape(Skia, canvas, paint, rectSpec, cx, cy, rot);\n}\n\n/**\n * Draw a ShipSpec by painting engine → hull → weapons (engine deepest,\n * weapons foremost — painter's algorithm). Zero allocation in the weapons\n * loop: classic indexed for-loop over the readonly array.\n */\nfunction drawShip(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ShipSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n drawSquare(Skia, canvas, paint, spec.engine, cx, cy, rot);\n drawShape(Skia, canvas, paint, spec.hull, cx, cy, rot);\n for (let i = 0; i < spec.weapons.length; i++) {\n const w = spec.weapons[i];\n if (w !== undefined) {\n drawShape(Skia, canvas, paint, w, cx, cy, rot);\n }\n }\n}\n"],"mappings":";;;;;;;AAkBA,SAAgB,kBAAkB,MAAyB;CACzD,QAAQ,KAAK,MAAb;EACE,KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO,KAAK;EACd,KA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AAQ,OAAO;EAb7B,MAAM;EACN,GAAG,KAAK;EACR,GAAG,KAAK;EACR,MAAM,KAAK;EACX,QAAQ,KAAK;EACb,aAAa,KAAK;EAClB,IAAI,KAAK;EACT,IAAI,KAAK;EACT,KAAK,KAAK;EACV,MAAM,KAAK;CAIyB,GAAG,IAAI,IAAI,GAAG;AACtD;;;;;;AAOA,SAAS,SACP,MACA,QACA,OACA,MACA,IACA,IACA,KACM;CACN,WAAW,MAAM,QAAQ,OAAO,KAAK,QAAQ,IAAI,IAAI,GAAG;CACxD,UAAU,MAAM,QAAQ,OAAO,KAAK,MAAM,IAAI,IAAI,GAAG;CACrD,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,QAAQ,KAAK;EAC5C,MAAM,IAAI,KAAK,QAAQ;EACvB,IAAI,MAAM,KAAA,GACR,UAAU,MAAM,QAAQ,OAAO,GAAG,IAAI,IAAI,GAAG;CAEjD;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["argbToFloat4Into","createSpriteSheet","packAtlasRows"],"sources":["../src/bounds.ts","../src/draw.ts","../src/atlas.ts"],"sourcesContent":["import type { BaseSpec, ShapeSpec, SquareSpec } from './types.js';\n\n/** Per-axis extents in the composition-local frame (the frame `ox`/`oy` live in). */\nexport interface ShapeBoundsBox {\n minX: number;\n minY: number;\n maxX: number;\n maxY: number;\n}\n\n/** A `ship` part: its hull/weapons are `ShapeSpec`s, its engine a `SquareSpec`. */\ntype Part = ShapeSpec | SquareSpec;\n\n/**\n * Approximate bounding radius for a single primitive. Used to size glow\n * rings and atlas tiles. Returned values are deliberately a touch generous\n * to avoid clipping.\n */\nexport function singleShapeRadius(spec: ShapeSpec): number {\n switch (spec.kind) {\n case 'circle':\n case 'ring':\n case 'ngon':\n return spec.r;\n case 'triangle':\n return spec.size;\n case 'star':\n return spec.rOuter;\n case 'rect':\n case 'gradient-rect':\n return Math.max(spec.w, spec.h) * 0.6;\n case 'polyline': {\n let max = 0;\n for (let i = 0; i < spec.points.length; i += 2) {\n const px = spec.points[i] ?? 0;\n const py = spec.points[i + 1] ?? 0;\n const m = Math.hypot(px, py);\n if (m > max) max = m;\n }\n return max + (spec.strokeWidth ?? 1);\n }\n case 'arc':\n return spec.r;\n case 'annulus':\n return spec.rOuter;\n case 'trapezoid':\n return Math.max(spec.topW, spec.botW, spec.h) * 0.6;\n case 'path':\n return spec.boundsR;\n case 'ship': {\n const { hull, weapons, engine } = spec;\n let max = Math.hypot(hull.ox ?? 0, hull.oy ?? 0) + singleShapeRadius(hull);\n for (let i = 0; i < weapons.length; i++) {\n const w = weapons[i];\n if (w === undefined) continue;\n const reach = Math.hypot(w.ox ?? 0, w.oy ?? 0) + singleShapeRadius(w);\n if (reach > max) max = reach;\n }\n // engine mirrors a w=h=size rect (see drawSquare); 0.6 is the rect face factor used by the 'rect' case above\n const engineReach = Math.hypot(engine.ox ?? 0, engine.oy ?? 0) + engine.size * 0.6;\n if (engineReach > max) max = engineReach;\n return max;\n }\n }\n}\n\n/**\n * Compute axis-aligned bounding radius for a shape composition. Used to\n * size atlas tiles. Returns the max half-extent from origin across all\n * shapes, including their `ox`/`oy` offsets, a stroke-width margin added on\n * top of the base `+2` where a stroke is actually painted, and the extent of\n * a glow ring when `glow` is set (+5.5 friendly, +2.25 hazard, +2.5 + `width`\n * for a `GlowSpec`) — enough to fully contain what `drawShape` paints.\n */\nexport function shapeBoundsRadius(specs: readonly ShapeSpec[]): number {\n let max = 0;\n for (const s of specs) {\n const ox = s.ox ?? 0;\n const oy = s.oy ?? 0;\n const r = boundsR(s);\n const reach = Math.hypot(ox, oy) + r + 2 + strokeMargin(s) + glowMargin(s);\n if (reach > max) max = reach;\n }\n return Math.max(4, max);\n}\n\n/** Allocating form of {@link shapeBoundsBoxInto}. */\nexport function shapeBoundsBox(specs: readonly ShapeSpec[]): ShapeBoundsBox {\n return shapeBoundsBoxInto(specs, { minX: 0, minY: 0, maxX: 0, maxY: 0 });\n}\n\n/**\n * Per-axis sibling of `shapeBoundsRadius`, for hitboxes: the union of every\n * spec's extents at its `ox`/`oy`, each widened by the same `+2` + stroke +\n * glow margin. Exact for unrotated geometry (a `ship` unions hull, weapons\n * and engine); a spec with a non-zero `rot` falls back to its radial reach,\n * so the box never exceeds the radius on any axis. Empty `specs` give all\n * zeros — no 4-unit atlas floor. Writes `out` and returns it; allocates nothing.\n */\nexport function shapeBoundsBoxInto(\n specs: readonly ShapeSpec[],\n out: ShapeBoundsBox,\n): ShapeBoundsBox {\n out.minX = Infinity;\n out.minY = Infinity;\n out.maxX = -Infinity;\n out.maxY = -Infinity;\n for (let i = 0; i < specs.length; i++) {\n const s = specs[i];\n if (s !== undefined) expand(out, s, 0, 0, 0, false);\n }\n if (out.minX === Infinity) {\n out.minX = 0;\n out.minY = 0;\n out.maxX = 0;\n out.maxY = 0;\n }\n return out;\n}\n\n/** Union `out` with `spec` centred at (dx + ox, dy + oy), padded by `pad` plus its own margin. */\nfunction expand(\n out: ShapeBoundsBox,\n spec: Part,\n dx: number,\n dy: number,\n pad: number,\n rotated: boolean,\n): void {\n const cx = dx + (spec.ox ?? 0);\n const cy = dy + (spec.oy ?? 0);\n const m = pad + 2 + strokeMargin(spec) + glowMargin(spec);\n const rot = rotated || (spec.rot ?? 0) !== 0;\n if (spec.kind === 'ship') {\n // drawShape never rotates a part's offset; a rotated ship spins each part about its own centre\n expand(out, spec.hull, cx, cy, m, rot);\n for (let i = 0; i < spec.weapons.length; i++) {\n const w = spec.weapons[i];\n if (w !== undefined) expand(out, w, cx, cy, m, rot);\n }\n expand(out, spec.engine, cx, cy, m, rot);\n return;\n }\n if (!rot) {\n switch (spec.kind) {\n case 'rect':\n case 'gradient-rect':\n include(out, cx, cy, m, -spec.w / 2, -spec.h / 2, spec.w / 2, spec.h / 2);\n return;\n case 'square':\n include(out, cx, cy, m, -spec.size / 2, -spec.size / 2, spec.size / 2, spec.size / 2);\n return;\n case 'trapezoid': {\n const hw = Math.max(spec.topW, spec.botW) / 2;\n include(out, cx, cy, m, -hw, -spec.h / 2, hw, spec.h / 2);\n return;\n }\n case 'triangle':\n include(out, cx, cy, m, -spec.size * 0.6, -spec.size, spec.size * 0.6, spec.size * 0.4);\n return;\n case 'ngon':\n case 'star': {\n // same vertex walk as drawNgon / drawStar\n const n = spec.kind === 'ngon' ? spec.n : spec.points * 2;\n let x0 = 0;\n let y0 = 0;\n let x1 = 0;\n let y1 = 0;\n for (let i = 0; i < n; i++) {\n const a = (i / n) * Math.PI * 2 - Math.PI / 2;\n const vr = spec.kind === 'ngon' ? spec.r : i % 2 === 0 ? spec.rOuter : spec.rInner;\n const px = Math.cos(a) * vr;\n const py = Math.sin(a) * vr;\n if (px < x0) x0 = px;\n else if (px > x1) x1 = px;\n if (py < y0) y0 = py;\n else if (py > y1) y1 = py;\n }\n include(out, cx, cy, m, x0, y0, x1, y1);\n return;\n }\n case 'polyline': {\n const p = spec.points;\n let x0 = p[0] ?? 0;\n let y0 = p[1] ?? 0;\n let x1 = x0;\n let y1 = y0;\n for (let i = 2; i < p.length; i += 2) {\n const px = p[i] ?? 0;\n const py = p[i + 1] ?? 0;\n if (px < x0) x0 = px;\n else if (px > x1) x1 = px;\n if (py < y0) y0 = py;\n else if (py > y1) y1 = py;\n }\n include(out, cx, cy, m + (spec.strokeWidth ?? 1), x0, y0, x1, y1);\n return;\n }\n }\n }\n const e = boundsR(spec);\n include(out, cx, cy, m, -e, -e, e, e);\n}\n\nfunction include(\n out: ShapeBoundsBox,\n cx: number,\n cy: number,\n m: number,\n x0: number,\n y0: number,\n x1: number,\n y1: number,\n): void {\n out.minX = Math.min(out.minX, cx + x0 - m);\n out.minY = Math.min(out.minY, cy + y0 - m);\n out.maxX = Math.max(out.maxX, cx + x1 + m);\n out.maxY = Math.max(out.maxY, cy + y1 + m);\n}\n\n/**\n * Extra reach for a stroke `drawShape` actually paints, else 0. `ring` and\n * `polyline` always stroke (draw.ts falls back to a default color/width);\n * every other kind strokes only when `stroke` is set. `gradient-rect` never\n * strokes, and `annulus`'s ring width is the fill-derived `rOuter - rInner`\n * band (already exactly bounded by `rOuter`), not `strokeWidth` — both are\n * excluded rather than double-counted. `ship` has no top-level stroke of its\n * own; its sub-shapes carry theirs through the nested `boundsR` recursion.\n */\nfunction strokeMargin(spec: Part): number {\n const painted =\n spec.kind === 'ring' || spec.kind === 'polyline'\n ? true\n : spec.kind === 'gradient-rect' || spec.kind === 'annulus' || spec.kind === 'ship'\n ? false\n : spec.stroke !== undefined;\n return painted ? Math.max(2, (spec.strokeWidth ?? 1) / 2) : 0;\n}\n\n/**\n * Extra reach for the glow ring `drawShape` paints outside the body (see\n * `applyFriendlyGlow`/`applyHazardGlow`/`applyGlow`). A `GlowSpec` ring sits\n * 2.5 px out and is `width` wide, so its outer edge is `2.5 + width` beyond\n * the body; a width `applyGlow` refuses to paint adds nothing.\n */\nfunction glowMargin(spec: BaseSpec): number {\n const glow = spec.glow;\n if (glow === undefined) return 0;\n if (glow === 'friendly') return 5.5;\n if (glow === 'hazard') return 2.25;\n return Number.isFinite(glow.width) && glow.width >= 0 ? 2.5 + glow.width : 0;\n}\n\n/** Tighter atlas-tile-sizing radius (rect/gradient-rect use 0.75 to fit corners). */\nfunction boundsR(spec: Part): number {\n switch (spec.kind) {\n case 'rect':\n case 'gradient-rect':\n return Math.max(spec.w, spec.h) * 0.75;\n case 'square':\n return spec.size * 0.75;\n case 'trapezoid':\n return Math.max(spec.topW, spec.botW, spec.h) * 0.75;\n case 'ship':\n return shapeBoundsRadius([\n spec.hull,\n ...spec.weapons,\n {\n kind: 'rect',\n w: spec.engine.size,\n h: spec.engine.size,\n ox: spec.engine.ox,\n oy: spec.engine.oy,\n stroke: spec.engine.stroke,\n strokeWidth: spec.engine.strokeWidth,\n glow: spec.engine.glow,\n },\n ]);\n default:\n return singleShapeRadius(spec);\n }\n}\n","/**\n * Shape drawing dispatcher — consumes a `ShapeSpec` and emits Skia draw\n * calls on a canvas. All shapes draw relative to (x + ox, y + oy), rotated\n * by (rot + spec.rot).\n *\n * The dispatcher is allocation-free per call except where Skia itself\n * allocates path objects. Glow ring is emitted before the shape body so it\n * renders behind the geometry.\n */\n\nimport { argbToFloat4Into } from '@flare-engine/math';\nimport type { SkCanvas, Skia as SkiaModule, SkPaint, SkPath } from '@shopify/react-native-skia';\nimport { singleShapeRadius } from './bounds.js';\nimport type {\n AnnulusSpec,\n ArcSpec,\n GradientRectSpec,\n NgonSpec,\n PathSpec,\n PolylineSpec,\n RectSpec,\n ShapeSpec,\n ShipSpec,\n SquareSpec,\n StarSpec,\n TrapezoidSpec,\n TriangleSpec,\n} from './types.js';\n\ntype SkiaApi = typeof SkiaModule;\n\n// ---- Glow recipes -----------------------------------------------------------\n\n/** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */\nexport function friendlyGlowArgb(): number {\n return (0x40 << 24) | 0x6fa66b;\n}\n\n/** ARGB for hazardOrange #E84B2A at α ≈ 0.6 (0x99 / 255). */\nexport function hazardGlowArgb(): number {\n return (0x99 << 24) | 0xe84b2a;\n}\n\nexport function applyFriendlyGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n): void {\n applyStroke(paint, friendlyGlowArgb(), 3);\n canvas.drawCircle(cx, cy, r + 4, paint);\n}\n\nexport function applyHazardGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n): void {\n const argb = hazardGlowArgb();\n applyStroke(paint, argb, 0.5);\n canvas.drawCircle(cx, cy, r + 1, paint);\n canvas.drawCircle(cx, cy, r + 2, paint);\n}\n\n/**\n * One stroked ring of `width` px in `color` (ARGB) whose inner edge sits\n * 2.5 px outside `r` — the gap `applyFriendlyGlow` uses, so `width: 3` with\n * `friendlyGlowArgb()` paints exactly the friendly ring. A non-finite or\n * negative `width` paints nothing.\n */\nexport function applyGlow(\n canvas: SkCanvas,\n paint: SkPaint,\n cx: number,\n cy: number,\n r: number,\n color: number,\n width: number,\n): void {\n if (!Number.isFinite(width) || width < 0) return;\n applyStroke(paint, color, width);\n canvas.drawCircle(cx, cy, r + (2.5 + width / 2), paint);\n}\n\n// ---- Color plumbing ---------------------------------------------------------\n\nconst COLOR = new Float32Array(4);\n\n/** Same as `argbToFloat4Into` from `@flare-engine/math`, kept as the shapes entry point. */\nexport function argbTo(out: Float32Array, argb: number): void {\n argbToFloat4Into(out, argb);\n}\n\nfunction applyFill(paint: SkPaint, argb: number): void {\n argbToFloat4Into(COLOR, argb);\n paint.setColor(COLOR);\n paint.setStyle(0); // Fill\n}\n\nfunction applyStroke(paint: SkPaint, argb: number, width: number): void {\n argbToFloat4Into(COLOR, argb);\n paint.setColor(COLOR);\n paint.setStyle(1); // Stroke\n paint.setStrokeWidth(width);\n}\n\n/** Fill, then stroke, the same path; `strokeWidth` defaults to 1. */\nfunction fillStrokePath(\n canvas: SkCanvas,\n paint: SkPaint,\n path: SkPath,\n fill: number | undefined,\n stroke: number | undefined,\n strokeWidth: number | undefined,\n): void {\n if (fill !== undefined) {\n applyFill(paint, fill);\n canvas.drawPath(path, paint);\n }\n if (stroke !== undefined) {\n applyStroke(paint, stroke, strokeWidth ?? 1);\n canvas.drawPath(path, paint);\n }\n}\n\n// ---- Dispatcher -------------------------------------------------------------\n\nexport function drawShape(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ShapeSpec,\n x: number,\n y: number,\n rot: number,\n): void {\n const cx = x + (spec.ox ?? 0);\n const cy = y + (spec.oy ?? 0);\n const r = rot + (spec.rot ?? 0);\n\n if (spec.glow !== undefined) {\n const gr = singleShapeRadius(spec);\n if (spec.glow === 'friendly') {\n applyFriendlyGlow(canvas, paint, cx, cy, gr);\n } else if (spec.glow === 'hazard') {\n applyHazardGlow(canvas, paint, cx, cy, gr);\n } else {\n applyGlow(canvas, paint, cx, cy, gr, spec.glow.color, spec.glow.width);\n }\n }\n\n switch (spec.kind) {\n case 'circle': {\n if (spec.fill !== undefined) {\n applyFill(paint, spec.fill);\n canvas.drawCircle(cx, cy, spec.r, paint);\n }\n if (spec.stroke !== undefined) {\n applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);\n canvas.drawCircle(cx, cy, spec.r, paint);\n }\n return;\n }\n case 'ring': {\n applyStroke(paint, spec.stroke ?? 0xff000000, spec.strokeWidth ?? 1);\n canvas.drawCircle(cx, cy, spec.r, paint);\n return;\n }\n case 'rect':\n drawRect(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'triangle':\n drawTriangle(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'ngon':\n drawNgon(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'star':\n drawStar(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'polyline':\n drawPolyline(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'gradient-rect':\n drawGradientRect(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'arc':\n drawArc(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'annulus':\n drawAnnulus(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'trapezoid':\n drawTrapezoid(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'path':\n drawPath(Skia, canvas, paint, spec, cx, cy, r);\n return;\n case 'ship':\n drawShip(Skia, canvas, paint, spec, cx, cy, r);\n return;\n }\n}\n\n// ---- Per-primitive renderers -----------------------------------------------\n\nfunction drawRect(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: RectSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n const rect = Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, spec.h);\n if (spec.fill !== undefined) {\n applyFill(paint, spec.fill);\n canvas.drawRect(rect, paint);\n }\n if (spec.stroke !== undefined) {\n applyStroke(paint, spec.stroke, spec.strokeWidth ?? 1);\n canvas.drawRect(rect, paint);\n }\n canvas.restore();\n}\n\nfunction drawTriangle(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: TriangleSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const s = spec.size;\n const path = Skia.Path.Make();\n path.moveTo(0, -s);\n path.lineTo(-s * 0.6, s * 0.4);\n path.lineTo(s * 0.6, s * 0.4);\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawNgon(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: NgonSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n for (let i = 0; i < spec.n; i++) {\n const a = (i / spec.n) * Math.PI * 2 - Math.PI / 2;\n const px = Math.cos(a) * spec.r;\n const py = Math.sin(a) * spec.r;\n if (i === 0) path.moveTo(px, py);\n else path.lineTo(px, py);\n }\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawStar(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: StarSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const total = spec.points * 2;\n for (let i = 0; i < total; i++) {\n const a = (i / total) * Math.PI * 2 - Math.PI / 2;\n const r = i % 2 === 0 ? spec.rOuter : spec.rInner;\n const px = Math.cos(a) * r;\n const py = Math.sin(a) * r;\n if (i === 0) path.moveTo(px, py);\n else path.lineTo(px, py);\n }\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawPolyline(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: PolylineSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const pts = spec.points;\n if (pts.length < 4) return;\n const path = Skia.Path.Make();\n path.moveTo(pts[0] ?? 0, pts[1] ?? 0);\n for (let i = 2; i < pts.length; i += 2) {\n path.lineTo(pts[i] ?? 0, pts[i + 1] ?? 0);\n }\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n applyStroke(paint, spec.stroke ?? 0xff000000, spec.strokeWidth ?? 1);\n canvas.drawPath(path, paint);\n canvas.restore();\n}\n\nfunction drawGradientRect(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: GradientRectSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n // Cheap two-half approximation; true shader gradient is overkill at atlas-build time.\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate((rot * 180) / Math.PI, 0, 0);\n const half = spec.axis === 1 ? spec.h / 2 : spec.w / 2;\n if (spec.axis === 1) {\n applyFill(paint, spec.colorA);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, spec.w, half), paint);\n applyFill(paint, spec.colorB);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, 0, spec.w, half), paint);\n } else {\n applyFill(paint, spec.colorA);\n canvas.drawRect(Skia.XYWHRect(-spec.w / 2, -spec.h / 2, half, spec.h), paint);\n applyFill(paint, spec.colorB);\n canvas.drawRect(Skia.XYWHRect(0, -spec.h / 2, half, spec.h), paint);\n }\n canvas.restore();\n}\n\n// Skia uses degrees, 0° at +X, increasing clockwise. We accept radians (math\n// convention CCW positive) and convert; visual result matches expectations\n// when rotated through the canvas transform.\nfunction radToDeg(rad: number): number {\n return (rad * 180) / Math.PI;\n}\n\nfunction drawArc(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ArcSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const oval = Skia.XYWHRect(-spec.r, -spec.r, spec.r * 2, spec.r * 2);\n const startDeg = radToDeg(spec.startAngle);\n const sweepDeg = radToDeg(spec.endAngle - spec.startAngle);\n if (spec.sector === true) {\n path.moveTo(0, 0);\n path.addArc(oval, startDeg, sweepDeg);\n path.close();\n } else {\n path.addArc(oval, startDeg, sweepDeg);\n }\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawAnnulus(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: AnnulusSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n // Render as a thick stroked circle/arc at the mid-radius. The fill colour\n // becomes the ring colour (more ergonomic than even-odd path subtraction).\n const midR = (spec.rOuter + spec.rInner) / 2;\n const thickness = spec.rOuter - spec.rInner;\n const ringColor = spec.fill ?? spec.stroke ?? 0xff000000;\n\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n\n if (spec.startAngle !== undefined && spec.endAngle !== undefined) {\n const path = Skia.Path.Make();\n const oval = Skia.XYWHRect(-midR, -midR, midR * 2, midR * 2);\n path.addArc(oval, radToDeg(spec.startAngle), radToDeg(spec.endAngle - spec.startAngle));\n applyStroke(paint, ringColor, thickness);\n canvas.drawPath(path, paint);\n } else {\n applyStroke(paint, ringColor, thickness);\n canvas.drawCircle(0, 0, midR, paint);\n }\n\n canvas.restore();\n}\n\nfunction drawTrapezoid(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: TrapezoidSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.Make();\n const halfTopW = spec.topW / 2;\n const halfBotW = spec.botW / 2;\n const halfH = spec.h / 2;\n // Apex up: narrow top edge at -h/2, wide bottom edge at +h/2.\n path.moveTo(-halfTopW, -halfH);\n path.lineTo(halfTopW, -halfH);\n path.lineTo(halfBotW, halfH);\n path.lineTo(-halfBotW, halfH);\n path.close();\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawPath(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: PathSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const path = Skia.Path.MakeFromSVGString(spec.d);\n // Malformed SVG returns null — skip silently rather than throw.\n if (path === null) return;\n canvas.save();\n canvas.translate(cx, cy);\n if (rot !== 0) canvas.rotate(radToDeg(rot), 0, 0);\n fillStrokePath(canvas, paint, path, spec.fill, spec.stroke, spec.strokeWidth);\n canvas.restore();\n}\n\nfunction drawSquare(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: SquareSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n const rectSpec: RectSpec = {\n kind: 'rect',\n w: spec.size,\n h: spec.size,\n fill: spec.fill,\n stroke: spec.stroke,\n strokeWidth: spec.strokeWidth,\n ox: spec.ox,\n oy: spec.oy,\n rot: spec.rot,\n glow: spec.glow,\n };\n // Route through the dispatcher (not drawRect) so ox/oy/rot and glow — which\n // drawRect never reads — are actually applied. See F016.\n drawShape(Skia, canvas, paint, rectSpec, cx, cy, rot);\n}\n\n/**\n * Draw a ShipSpec by painting engine → hull → weapons (engine deepest,\n * weapons foremost — painter's algorithm). Zero allocation in the weapons\n * loop: classic indexed for-loop over the readonly array.\n */\nfunction drawShip(\n Skia: SkiaApi,\n canvas: SkCanvas,\n paint: SkPaint,\n spec: ShipSpec,\n cx: number,\n cy: number,\n rot: number,\n): void {\n drawSquare(Skia, canvas, paint, spec.engine, cx, cy, rot);\n drawShape(Skia, canvas, paint, spec.hull, cx, cy, rot);\n for (let i = 0; i < spec.weapons.length; i++) {\n const w = spec.weapons[i];\n if (w !== undefined) {\n drawShape(Skia, canvas, paint, w, cx, cy, rot);\n }\n }\n}\n","/**\n * Shape atlas bake — rasterizes `ShapeSpec`s once into one image, so that\n * shape-built entities draw as sprites through a batch instead of paying\n * `drawShape`'s one Skia draw per fill, stroke and glow ring.\n */\n\nimport {\n createSpriteSheet,\n packAtlasRows,\n type SkImageLike,\n type SpriteFrame,\n type SpriteSheet,\n} from '@flare-engine/assets';\nimport type { SkCanvas, Skia as SkiaModule, SkPaint } from '@shopify/react-native-skia';\nimport { type ShapeBoundsBox, shapeBoundsBoxInto } from './bounds.js';\nimport { drawShape } from './draw.js';\nimport type { ShapeSpec } from './types.js';\n\n/** Longest side a baked cell may have, in atlas pixels. */\nexport const SHAPE_ATLAS_MAX_CELL = 2048;\n\n// The re-raster band: a baked atlas is kept while the wanted scale stays\n// within these factors of the scale it was baked at.\nconst BAND_MIN = 0.75;\nconst BAND_MAX = 1.5;\n\n/** The paint members the bake and `drawShape` call. */\nexport interface ShapeAtlasPaint {\n setColor(color: Float32Array): void;\n /** 0 = fill, 1 = stroke. */\n setStyle(style: number): void;\n setStrokeWidth(width: number): void;\n}\n\n/** The canvas members the bake and `drawShape` call. `rotate` takes degrees. */\nexport interface ShapeAtlasCanvas {\n save(): unknown;\n restore(): void;\n translate(dx: number, dy: number): void;\n scale(sx: number, sy: number): void;\n rotate(degrees: number, rx: number, ry: number): void;\n clear(color: Float32Array): void;\n drawCircle(cx: number, cy: number, r: number, paint: ShapeAtlasPaint): void;\n drawRect(rect: unknown, paint: ShapeAtlasPaint): void;\n drawPath(path: unknown, paint: ShapeAtlasPaint): void;\n}\n\n/** An offscreen surface the bake draws into and snapshots. */\nexport interface ShapeAtlasSurface {\n getCanvas(): ShapeAtlasCanvas;\n makeImageSnapshot(): SkImageLike;\n /** Called once after the snapshot, when present, to free the surface. */\n dispose?(): void;\n}\n\n/**\n * The Skia handle `bakeShapeAtlas` needs: what `drawShape` reads plus an\n * offscreen surface factory. `Skia` from `@shopify/react-native-skia`\n * satisfies it; on web, `createWebShapeSkia(kit)` from\n * `@flare-engine/render-web` builds one over CanvasKit.\n */\nexport interface ShapeAtlasSkia {\n Surface: { Make(width: number, height: number): ShapeAtlasSurface | null };\n Paint(): ShapeAtlasPaint;\n Path: {\n Make(): {\n moveTo(x: number, y: number): unknown;\n lineTo(x: number, y: number): unknown;\n close(): unknown;\n addArc(oval: unknown, startDeg: number, sweepDeg: number): unknown;\n };\n MakeFromSVGString(d: string): unknown;\n };\n XYWHRect(x: number, y: number, w: number, h: number): unknown;\n}\n\n/** Layout options for `bakeShapeAtlas`. */\nexport interface ShapeAtlasOptions {\n /** Atlas width at which cells wrap to a new row, in pixels. Default 2048. */\n maxWidth?: number;\n /** Transparent gap between cells, in pixels, against sampling bleed. Default 2. */\n padding?: number;\n}\n\n/** One baked spec: where it sits in the atlas and how an instance maps onto it. */\nexport interface ShapeAtlasCell {\n /** The cell's rectangle in the atlas image, in pixels. */\n readonly frame: SpriteFrame;\n /** The spec's origin inside the cell, as a fraction of its width. */\n readonly anchorX: number;\n /** The spec's origin inside the cell, as a fraction of its height. */\n readonly anchorY: number;\n /** Cell width in the spec's own units: what an instance at scale 1 covers. */\n readonly width: number;\n /** Cell height in the spec's own units. */\n readonly height: number;\n /**\n * Pixels per spec unit this cell was rasterized at. Equal to the atlas\n * scale unless the cell hit `SHAPE_ATLAS_MAX_CELL`, in which case it is lower.\n */\n readonly scale: number;\n}\n\n/** A baked shape atlas: the `{ image, sheet }` pair a sprite batch draws, plus a cell per spec. */\nexport interface ShapeAtlas {\n readonly image: SkImageLike;\n /** One frame per distinct spec, named by its index in `sheet.frames`. */\n readonly sheet: SpriteSheet;\n /** Atlas width in pixels. */\n readonly width: number;\n /** Atlas height in pixels. */\n readonly height: number;\n /** The raster scale passed to the bake. */\n readonly scale: number;\n /** `cells[i]` belongs to `specs[i]`. A spec object that repeats shares one cell. */\n readonly cells: readonly ShapeAtlasCell[];\n /** A copy of the baked spec list. */\n readonly specs: readonly ShapeSpec[];\n readonly maxWidth: number;\n readonly padding: number;\n}\n\n/** The one `SpriteBatch` method `drawBakedShape` calls. */\nexport interface ShapeBatchLike {\n draw(\n image: SkImageLike,\n frame: SpriteFrame,\n dstX: number,\n dstY: number,\n dstW?: number,\n dstH?: number,\n effect?: undefined,\n anchorX?: number,\n anchorY?: number,\n alpha?: number,\n rotation?: number,\n stretch?: boolean,\n blend?: 'normal' | 'additive',\n flipX?: boolean,\n flipY?: boolean,\n color?: number,\n ): void;\n}\n\nconst BOX: ShapeBoundsBox = { minX: 0, minY: 0, maxX: 0, maxY: 0 };\nconst TRANSPARENT = new Float32Array(4);\n\n/**\n * Rasterize `specs` into one atlas image at `scale` pixels per spec unit.\n *\n * `scale` is explicit because nothing the bake can see knows the device\n * scale: pass the pixels one spec unit covers on screen (pixel density times\n * camera zoom times the usual instance scale).\n *\n * Each distinct spec object is drawn once by `drawShape`, with its own fill,\n * stroke and glow colours, into a cell sized from `shapeBoundsBoxInto`. A cell\n * whose longer side would exceed `SHAPE_ATLAS_MAX_CELL` is rasterized at a\n * lower scale of its own so that side is exactly the cap; it still draws at\n * the same size, only softer.\n *\n * Not for per-frame use: a spec whose parameters animate belongs on\n * `drawShape`. Throws if `scale` is not a positive finite number or the\n * surface cannot be created.\n */\nexport function bakeShapeAtlas(\n Skia: ShapeAtlasSkia,\n specs: readonly ShapeSpec[],\n scale: number,\n options: ShapeAtlasOptions = {},\n): ShapeAtlas {\n if (!(scale > 0) || scale === Infinity) {\n throw new Error(`bakeShapeAtlas: raster scale must be a positive finite number, got ${scale}`);\n }\n const maxWidth = options.maxWidth ?? SHAPE_ATLAS_MAX_CELL;\n const padding = options.padding ?? 2;\n\n const baked = specs.slice();\n const cells: ShapeAtlasCell[] = [];\n const bySpec = new Map<ShapeSpec, ShapeAtlasCell>();\n // Per distinct spec, parallel to `sheet.frames`: where its origin falls inside its cell.\n const originX: number[] = [];\n const originY: number[] = [];\n const sheet = createSpriteSheet();\n const single: ShapeSpec[] = [];\n\n for (const spec of baked) {\n let cell = bySpec.get(spec);\n if (cell === undefined) {\n single[0] = spec;\n shapeBoundsBoxInto(single, BOX);\n const boxW = BOX.maxX - BOX.minX;\n const boxH = BOX.maxY - BOX.minY;\n const cellScale = Math.min(scale, SHAPE_ATLAS_MAX_CELL / Math.max(boxW, boxH));\n // The min() guards the cap against float error in the division above.\n const w = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxW * cellScale)));\n const h = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxH * cellScale)));\n const name = String(sheet.frames.length);\n cell = {\n frame: { name, x: 0, y: 0, w, h },\n anchorX: (-BOX.minX * cellScale) / w,\n anchorY: (-BOX.minY * cellScale) / h,\n width: w / cellScale,\n height: h / cellScale,\n scale: cellScale,\n };\n bySpec.set(spec, cell);\n sheet.frameMap.set(name, sheet.frames.length);\n sheet.frames.push(cell.frame);\n originX.push(-BOX.minX * cellScale);\n originY.push(-BOX.minY * cellScale);\n }\n cells.push(cell);\n }\n\n const layout = packAtlasRows(sheet.frames, maxWidth, padding);\n const surface = Skia.Surface.Make(layout.width, layout.height);\n if (surface === null) {\n throw new Error(\n `bakeShapeAtlas: Skia.Surface.Make returned null for ${layout.width}x${layout.height}`,\n );\n }\n\n const canvas = surface.getCanvas();\n const paint = Skia.Paint();\n canvas.clear(TRANSPARENT);\n let i = 0;\n for (const [spec, cell] of bySpec) {\n const pos = layout.rects[i];\n if (pos === undefined) break;\n cell.frame.x = pos.x;\n cell.frame.y = pos.y;\n canvas.save();\n canvas.translate(pos.x + (originX[i] ?? 0), pos.y + (originY[i] ?? 0));\n canvas.scale(cell.scale, cell.scale);\n // `ShapeAtlasSkia` declares exactly the members `drawShape` reads, which is\n // what lets a CanvasKit handle stand in for the react-native-skia one here.\n drawShape(Skia as typeof SkiaModule, canvas as SkCanvas, paint as SkPaint, spec, 0, 0, 0);\n canvas.restore();\n i++;\n }\n\n const image = surface.makeImageSnapshot();\n surface.dispose?.();\n\n return {\n image,\n sheet,\n width: layout.width,\n height: layout.height,\n scale,\n cells,\n specs: baked,\n maxWidth,\n padding,\n };\n}\n\n/**\n * True when `scale` has left the re-raster band of `atlas`: below 0.75x or\n * above 1.5x of the scale it was baked at. Inside the band the baked pixels\n * are close enough to draw as they are. A `scale` that is not a positive\n * finite number returns false. Allocates nothing; safe to call every frame.\n */\nexport function shapeAtlasNeedsRebake(atlas: ShapeAtlas, scale: number): boolean {\n if (!(scale > 0) || scale === Infinity) return false;\n return scale < atlas.scale * BAND_MIN || scale > atlas.scale * BAND_MAX;\n}\n\n/**\n * Return `atlas` itself while `scale` is inside its re-raster band (see\n * `shapeAtlasNeedsRebake`), allocating nothing; otherwise bake the same specs\n * with the same options at `scale` and return the new atlas. Cell indices\n * carry over exactly, and so do each spec's origin and drawn size\n * (`anchorX * width` is always `-minX`), so instance draws need no change. A\n * cell's `width`/`height` and fractional anchors can differ by the rounding\n * margin of either bake, because each cell is rounded up to whole pixels. The old\n * image is not released: that is the caller's to drop.\n */\nexport function rebakeShapeAtlas(\n Skia: ShapeAtlasSkia,\n atlas: ShapeAtlas,\n scale: number,\n): ShapeAtlas {\n if (!shapeAtlasNeedsRebake(atlas, scale)) return atlas;\n return bakeShapeAtlas(Skia, atlas.specs, scale, {\n maxWidth: atlas.maxWidth,\n padding: atlas.padding,\n });\n}\n\n/**\n * Queue one instance of baked spec `index` on `batch`, with the spec's origin\n * at (`x`, `y`). `rot` is radians, clockwise, about that origin; `scale` is\n * uniform and multiplies stroke and glow widths along with everything else.\n * `color` is an optional packed-ARGB multiplier over the baked colours and\n * `alpha` an opacity in [0, 1], both as `SpriteBatch.draw` takes them.\n *\n * The whole cell turns rigidly, including each part's `ox`/`oy` offset, which\n * `drawShape`'s `rot` leaves unrotated. An `index` with no cell draws nothing.\n * Allocates nothing.\n */\nexport function drawBakedShape(\n batch: ShapeBatchLike,\n atlas: ShapeAtlas,\n index: number,\n x: number,\n y: number,\n rot = 0,\n scale = 1,\n color?: number,\n alpha = 1,\n): void {\n const cell = atlas.cells[index];\n if (cell === undefined) return;\n batch.draw(\n atlas.image,\n cell.frame,\n x,\n y,\n cell.width * scale,\n cell.height * scale,\n undefined,\n cell.anchorX,\n cell.anchorY,\n alpha,\n rot,\n false,\n 'normal',\n false,\n false,\n color,\n );\n}\n"],"mappings":";;;;;;;;;AAkBA,SAAgB,kBAAkB,MAAyB;CACzD,QAAQ,KAAK,MAAb;EACE,KAAK;EACL,KAAK;EACL,KAAK,QACH,OAAO,KAAK;EACd,KAAK,YACH,OAAO,KAAK;EACd,KAAK,QACH,OAAO,KAAK;EACd,KAAK;EACL,KAAK,iBACH,OAAO,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,IAAI;EACpC,KAAK,YAAY;GACf,IAAI,MAAM;GACV,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,OAAO,QAAQ,KAAK,GAAG;IAC9C,MAAM,KAAK,KAAK,OAAO,MAAM;IAC7B,MAAM,KAAK,KAAK,OAAO,IAAI,MAAM;IACjC,MAAM,IAAI,KAAK,MAAM,IAAI,EAAE;IAC3B,IAAI,IAAI,KAAK,MAAM;GACrB;GACA,OAAO,OAAO,KAAK,eAAe;EACpC;EACA,KAAK,OACH,OAAO,KAAK;EACd,KAAK,WACH,OAAO,KAAK;EACd,KAAK,aACH,OAAO,KAAK,IAAI,KAAK,MAAM,KAAK,MAAM,KAAK,CAAC,IAAI;EAClD,KAAK,QACH,OAAO,KAAK;EACd,KAAK,QAAQ;GACX,MAAM,EAAE,MAAM,SAAS,WAAW;GAClC,IAAI,MAAM,KAAK,MAAM,KAAK,MAAM,GAAG,KAAK,MAAM,CAAC,IAAI,kBAAkB,IAAI;GACzE,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;IACvC,MAAM,IAAI,QAAQ;IAClB,IAAI,MAAM,K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,MAAM,MAAM,OAAO,OAAO;EAC9C,UAAU,MAAM;EAChB,SAAS,MAAM;CACjB,CAAC;AACH;;;;;;;;;;;;AAaA,SAAgB,eACd,OACA,OACA,OACA,GACA,GACA,MAAM,GACN,QAAQ,GACR,OACA,QAAQ,GACF;CACN,MAAM,OAAO,MAAM,MAAM;CACzB,IAAI,SAAS,KAAA,GAAW;CACxB,MAAM,KACJ,MAAM,OACN,KAAK,OACL,GACA,GACA,KAAK,QAAQ,OACb,KAAK,SAAS,OACd,KAAA,GACA,KAAK,SACL,KAAK,SACL,OACA,KACA,OACA,UACA,OACA,OACA,KACF;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createSpriteSheet, packAtlasRows } from "@flare-engine/assets";
|
|
2
|
+
import { argbToFloat4Into } from "@flare-engine/math";
|
|
1
3
|
// src/bounds.ts
|
|
2
4
|
/**
|
|
3
5
|
* Approximate bounding radius for a single primitive. Used to size glow
|
|
@@ -79,15 +81,15 @@ function shapeBoundsBox(specs) {
|
|
|
79
81
|
* zeros — no 4-unit atlas floor. Writes `out` and returns it; allocates nothing.
|
|
80
82
|
*/
|
|
81
83
|
function shapeBoundsBoxInto(specs, out) {
|
|
82
|
-
out.minX =
|
|
83
|
-
out.minY =
|
|
84
|
-
out.maxX = -
|
|
85
|
-
out.maxY = -
|
|
84
|
+
out.minX = 1e400;
|
|
85
|
+
out.minY = 1e400;
|
|
86
|
+
out.maxX = -1e400;
|
|
87
|
+
out.maxY = -1e400;
|
|
86
88
|
for (let i = 0; i < specs.length; i++) {
|
|
87
89
|
const s = specs[i];
|
|
88
90
|
if (s !== void 0) expand(out, s, 0, 0, 0, false);
|
|
89
91
|
}
|
|
90
|
-
if (out.minX ===
|
|
92
|
+
if (out.minX === 1e400) {
|
|
91
93
|
out.minX = 0;
|
|
92
94
|
out.minY = 0;
|
|
93
95
|
out.maxX = 0;
|
|
@@ -223,6 +225,15 @@ function boundsR(spec) {
|
|
|
223
225
|
}
|
|
224
226
|
}
|
|
225
227
|
// src/draw.ts
|
|
228
|
+
/**
|
|
229
|
+
* Shape drawing dispatcher — consumes a `ShapeSpec` and emits Skia draw
|
|
230
|
+
* calls on a canvas. All shapes draw relative to (x + ox, y + oy), rotated
|
|
231
|
+
* by (rot + spec.rot).
|
|
232
|
+
*
|
|
233
|
+
* The dispatcher is allocation-free per call except where Skia itself
|
|
234
|
+
* allocates path objects. Glow ring is emitted before the shape body so it
|
|
235
|
+
* renders behind the geometry.
|
|
236
|
+
*/
|
|
226
237
|
/** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */
|
|
227
238
|
function friendlyGlowArgb() {
|
|
228
239
|
return 1081058923;
|
|
@@ -252,19 +263,17 @@ function applyGlow(canvas, paint, cx, cy, r, color, width) {
|
|
|
252
263
|
canvas.drawCircle(cx, cy, r + (2.5 + width / 2), paint);
|
|
253
264
|
}
|
|
254
265
|
const COLOR = /* @__PURE__ */ new Float32Array(4);
|
|
266
|
+
/** Same as `argbToFloat4Into` from `@flare-engine/math`, kept as the shapes entry point. */
|
|
255
267
|
function argbTo(out, argb) {
|
|
256
|
-
out
|
|
257
|
-
out[1] = (argb >>> 8 & 255) / 255;
|
|
258
|
-
out[2] = (argb & 255) / 255;
|
|
259
|
-
out[3] = (argb >>> 24 & 255) / 255;
|
|
268
|
+
argbToFloat4Into(out, argb);
|
|
260
269
|
}
|
|
261
270
|
function applyFill(paint, argb) {
|
|
262
|
-
|
|
271
|
+
argbToFloat4Into(COLOR, argb);
|
|
263
272
|
paint.setColor(COLOR);
|
|
264
273
|
paint.setStyle(0);
|
|
265
274
|
}
|
|
266
275
|
function applyStroke(paint, argb, width) {
|
|
267
|
-
|
|
276
|
+
argbToFloat4Into(COLOR, argb);
|
|
268
277
|
paint.setColor(COLOR);
|
|
269
278
|
paint.setStyle(1);
|
|
270
279
|
paint.setStrokeWidth(width);
|
|
@@ -523,6 +532,160 @@ function drawShip(Skia, canvas, paint, spec, cx, cy, rot) {
|
|
|
523
532
|
if (w !== void 0) drawShape(Skia, canvas, paint, w, cx, cy, rot);
|
|
524
533
|
}
|
|
525
534
|
}
|
|
526
|
-
|
|
535
|
+
// src/atlas.ts
|
|
536
|
+
/**
|
|
537
|
+
* Shape atlas bake — rasterizes `ShapeSpec`s once into one image, so that
|
|
538
|
+
* shape-built entities draw as sprites through a batch instead of paying
|
|
539
|
+
* `drawShape`'s one Skia draw per fill, stroke and glow ring.
|
|
540
|
+
*/
|
|
541
|
+
/** Longest side a baked cell may have, in atlas pixels. */
|
|
542
|
+
const SHAPE_ATLAS_MAX_CELL = 2048;
|
|
543
|
+
const BAND_MIN = .75;
|
|
544
|
+
const BAND_MAX = 1.5;
|
|
545
|
+
const BOX = {
|
|
546
|
+
minX: 0,
|
|
547
|
+
minY: 0,
|
|
548
|
+
maxX: 0,
|
|
549
|
+
maxY: 0
|
|
550
|
+
};
|
|
551
|
+
const TRANSPARENT = /* @__PURE__ */ new Float32Array(4);
|
|
552
|
+
/**
|
|
553
|
+
* Rasterize `specs` into one atlas image at `scale` pixels per spec unit.
|
|
554
|
+
*
|
|
555
|
+
* `scale` is explicit because nothing the bake can see knows the device
|
|
556
|
+
* scale: pass the pixels one spec unit covers on screen (pixel density times
|
|
557
|
+
* camera zoom times the usual instance scale).
|
|
558
|
+
*
|
|
559
|
+
* Each distinct spec object is drawn once by `drawShape`, with its own fill,
|
|
560
|
+
* stroke and glow colours, into a cell sized from `shapeBoundsBoxInto`. A cell
|
|
561
|
+
* whose longer side would exceed `SHAPE_ATLAS_MAX_CELL` is rasterized at a
|
|
562
|
+
* lower scale of its own so that side is exactly the cap; it still draws at
|
|
563
|
+
* the same size, only softer.
|
|
564
|
+
*
|
|
565
|
+
* Not for per-frame use: a spec whose parameters animate belongs on
|
|
566
|
+
* `drawShape`. Throws if `scale` is not a positive finite number or the
|
|
567
|
+
* surface cannot be created.
|
|
568
|
+
*/
|
|
569
|
+
function bakeShapeAtlas(Skia, specs, scale, options = {}) {
|
|
570
|
+
if (!(scale > 0) || scale === 1e400) throw new Error(`bakeShapeAtlas: raster scale must be a positive finite number, got ${scale}`);
|
|
571
|
+
const maxWidth = options.maxWidth ?? 2048;
|
|
572
|
+
const padding = options.padding ?? 2;
|
|
573
|
+
const baked = specs.slice();
|
|
574
|
+
const cells = [];
|
|
575
|
+
const bySpec = /* @__PURE__ */ new Map();
|
|
576
|
+
const originX = [];
|
|
577
|
+
const originY = [];
|
|
578
|
+
const sheet = createSpriteSheet();
|
|
579
|
+
const single = [];
|
|
580
|
+
for (const spec of baked) {
|
|
581
|
+
let cell = bySpec.get(spec);
|
|
582
|
+
if (cell === void 0) {
|
|
583
|
+
single[0] = spec;
|
|
584
|
+
shapeBoundsBoxInto(single, BOX);
|
|
585
|
+
const boxW = BOX.maxX - BOX.minX;
|
|
586
|
+
const boxH = BOX.maxY - BOX.minY;
|
|
587
|
+
const cellScale = Math.min(scale, SHAPE_ATLAS_MAX_CELL / Math.max(boxW, boxH));
|
|
588
|
+
const w = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxW * cellScale)));
|
|
589
|
+
const h = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxH * cellScale)));
|
|
590
|
+
const name = String(sheet.frames.length);
|
|
591
|
+
cell = {
|
|
592
|
+
frame: {
|
|
593
|
+
name,
|
|
594
|
+
x: 0,
|
|
595
|
+
y: 0,
|
|
596
|
+
w,
|
|
597
|
+
h
|
|
598
|
+
},
|
|
599
|
+
anchorX: -BOX.minX * cellScale / w,
|
|
600
|
+
anchorY: -BOX.minY * cellScale / h,
|
|
601
|
+
width: w / cellScale,
|
|
602
|
+
height: h / cellScale,
|
|
603
|
+
scale: cellScale
|
|
604
|
+
};
|
|
605
|
+
bySpec.set(spec, cell);
|
|
606
|
+
sheet.frameMap.set(name, sheet.frames.length);
|
|
607
|
+
sheet.frames.push(cell.frame);
|
|
608
|
+
originX.push(-BOX.minX * cellScale);
|
|
609
|
+
originY.push(-BOX.minY * cellScale);
|
|
610
|
+
}
|
|
611
|
+
cells.push(cell);
|
|
612
|
+
}
|
|
613
|
+
const layout = packAtlasRows(sheet.frames, maxWidth, padding);
|
|
614
|
+
const surface = Skia.Surface.Make(layout.width, layout.height);
|
|
615
|
+
if (surface === null) throw new Error(`bakeShapeAtlas: Skia.Surface.Make returned null for ${layout.width}x${layout.height}`);
|
|
616
|
+
const canvas = surface.getCanvas();
|
|
617
|
+
const paint = Skia.Paint();
|
|
618
|
+
canvas.clear(TRANSPARENT);
|
|
619
|
+
let i = 0;
|
|
620
|
+
for (const [spec, cell] of bySpec) {
|
|
621
|
+
const pos = layout.rects[i];
|
|
622
|
+
if (pos === void 0) break;
|
|
623
|
+
cell.frame.x = pos.x;
|
|
624
|
+
cell.frame.y = pos.y;
|
|
625
|
+
canvas.save();
|
|
626
|
+
canvas.translate(pos.x + (originX[i] ?? 0), pos.y + (originY[i] ?? 0));
|
|
627
|
+
canvas.scale(cell.scale, cell.scale);
|
|
628
|
+
drawShape(Skia, canvas, paint, spec, 0, 0, 0);
|
|
629
|
+
canvas.restore();
|
|
630
|
+
i++;
|
|
631
|
+
}
|
|
632
|
+
const image = surface.makeImageSnapshot();
|
|
633
|
+
surface.dispose?.();
|
|
634
|
+
return {
|
|
635
|
+
image,
|
|
636
|
+
sheet,
|
|
637
|
+
width: layout.width,
|
|
638
|
+
height: layout.height,
|
|
639
|
+
scale,
|
|
640
|
+
cells,
|
|
641
|
+
specs: baked,
|
|
642
|
+
maxWidth,
|
|
643
|
+
padding
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* True when `scale` has left the re-raster band of `atlas`: below 0.75x or
|
|
648
|
+
* above 1.5x of the scale it was baked at. Inside the band the baked pixels
|
|
649
|
+
* are close enough to draw as they are. A `scale` that is not a positive
|
|
650
|
+
* finite number returns false. Allocates nothing; safe to call every frame.
|
|
651
|
+
*/
|
|
652
|
+
function shapeAtlasNeedsRebake(atlas, scale) {
|
|
653
|
+
if (!(scale > 0) || scale === 1e400) return false;
|
|
654
|
+
return scale < atlas.scale * BAND_MIN || scale > atlas.scale * BAND_MAX;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Return `atlas` itself while `scale` is inside its re-raster band (see
|
|
658
|
+
* `shapeAtlasNeedsRebake`), allocating nothing; otherwise bake the same specs
|
|
659
|
+
* with the same options at `scale` and return the new atlas. Cell indices
|
|
660
|
+
* carry over exactly, and so do each spec's origin and drawn size
|
|
661
|
+
* (`anchorX * width` is always `-minX`), so instance draws need no change. A
|
|
662
|
+
* cell's `width`/`height` and fractional anchors can differ by the rounding
|
|
663
|
+
* margin of either bake, because each cell is rounded up to whole pixels. The old
|
|
664
|
+
* image is not released: that is the caller's to drop.
|
|
665
|
+
*/
|
|
666
|
+
function rebakeShapeAtlas(Skia, atlas, scale) {
|
|
667
|
+
if (!shapeAtlasNeedsRebake(atlas, scale)) return atlas;
|
|
668
|
+
return bakeShapeAtlas(Skia, atlas.specs, scale, {
|
|
669
|
+
maxWidth: atlas.maxWidth,
|
|
670
|
+
padding: atlas.padding
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* Queue one instance of baked spec `index` on `batch`, with the spec's origin
|
|
675
|
+
* at (`x`, `y`). `rot` is radians, clockwise, about that origin; `scale` is
|
|
676
|
+
* uniform and multiplies stroke and glow widths along with everything else.
|
|
677
|
+
* `color` is an optional packed-ARGB multiplier over the baked colours and
|
|
678
|
+
* `alpha` an opacity in [0, 1], both as `SpriteBatch.draw` takes them.
|
|
679
|
+
*
|
|
680
|
+
* The whole cell turns rigidly, including each part's `ox`/`oy` offset, which
|
|
681
|
+
* `drawShape`'s `rot` leaves unrotated. An `index` with no cell draws nothing.
|
|
682
|
+
* Allocates nothing.
|
|
683
|
+
*/
|
|
684
|
+
function drawBakedShape(batch, atlas, index, x, y, rot = 0, scale = 1, color, alpha = 1) {
|
|
685
|
+
const cell = atlas.cells[index];
|
|
686
|
+
if (cell === void 0) return;
|
|
687
|
+
batch.draw(atlas.image, cell.frame, x, y, cell.width * scale, cell.height * scale, void 0, cell.anchorX, cell.anchorY, alpha, rot, false, "normal", false, false, color);
|
|
688
|
+
}
|
|
689
|
+
export { SHAPE_ATLAS_MAX_CELL, applyFriendlyGlow, applyGlow, applyHazardGlow, argbTo, bakeShapeAtlas, drawBakedShape, drawShape, friendlyGlowArgb, hazardGlowArgb, rebakeShapeAtlas, shapeAtlasNeedsRebake, shapeBoundsBox, shapeBoundsBoxInto, shapeBoundsRadius, singleShapeRadius };
|
|
527
690
|
|
|
528
691
|
//# sourceMappingURL=index.mjs.map
|