@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 CHANGED
@@ -12,7 +12,7 @@ Mobile and indie game prototypes don't need sprite sheets to start. Compose ship
12
12
  bun add @flare-engine/shapes @shopify/react-native-skia
13
13
  ```
14
14
 
15
- `@shopify/react-native-skia` is an **optional, type-only peer** — `src/draw.ts` imports it via `import type` only (for the injected `Skia` / `canvas` / `paint` handles), so it never enters the runtime bundle. Skip it entirely on web/CanvasKit; add it only where you actually render on native Skia.
15
+ `@shopify/react-native-skia` is an **optional, type-only peer** — `src/draw.ts` imports it via `import type` only (for the injected `Skia` / `canvas` / `paint` handles), so it never enters the runtime bundle. Skip it entirely on web/CanvasKit; add it only where you actually render on native Skia. On web, the `Skia` handle is `createWebShapeSkia(kit)` from `@flare-engine/render-web` (see [Baking an atlas](#baking-an-atlas)).
16
16
 
17
17
  ## Usage
18
18
 
@@ -113,6 +113,63 @@ const hitHeight = hit.maxY - hit.minY;
113
113
  const glowR = singleShapeRadius({ kind: 'circle', r: 14 });
114
114
  ```
115
115
 
116
+ ## Baking an atlas
117
+
118
+ `drawShape` costs one Skia draw per fill, one per stroke and one or two per glow ring, so a scene of shape-built entities pays several draw calls per entity. `bakeShapeAtlas` rasterizes each spec once into one image; the instances then draw as sprites through `SpriteBatch`, and N of them are one draw call.
119
+
120
+ ```ts
121
+ import { SpriteBatch } from '@flare-engine/render';
122
+ import {
123
+ bakeShapeAtlas,
124
+ drawBakedShape,
125
+ rebakeShapeAtlas,
126
+ type ShapeAtlasSkia,
127
+ type ShapeSpec,
128
+ } from '@flare-engine/shapes';
129
+
130
+ // Native: `Skia` from @shopify/react-native-skia.
131
+ // Web: `createWebShapeSkia(kit)` from @flare-engine/render-web.
132
+ declare const skia: ShapeAtlasSkia;
133
+ declare const batch: SpriteBatch;
134
+ declare const pixelDensity: number;
135
+ declare const zoom: number;
136
+
137
+ const specs: ShapeSpec[] = [
138
+ { kind: 'circle', r: 10, fill: 0xffc9a24a, glow: 'friendly' },
139
+ { kind: 'star', points: 5, rOuter: 12, rInner: 5, fill: 0xffff2b2b, stroke: 0xff000000 },
140
+ ];
141
+
142
+ // Once, outside the frame loop. The third argument is the raster scale:
143
+ // atlas pixels per spec unit.
144
+ let atlas = bakeShapeAtlas(skia, specs, pixelDensity * zoom);
145
+
146
+ function frame(): void {
147
+ // Returns the same atlas, allocation-free, until the scale leaves the band.
148
+ atlas = rebakeShapeAtlas(skia, atlas, pixelDensity * zoom);
149
+
150
+ // index, x, y, then optional rotation (radians), uniform scale, colour, alpha.
151
+ drawBakedShape(batch, atlas, 0, 120, 80);
152
+ drawBakedShape(batch, atlas, 1, 200, 80, Math.PI / 4, 1.5);
153
+ drawBakedShape(batch, atlas, 1, 260, 80, 0, 1, 0x80ffffff);
154
+ }
155
+ ```
156
+
157
+ `atlas.image` and `atlas.sheet` are the pair `SpriteBatch` draws, and `atlas.cells[i]` describes the cell baked from `specs[i]`. A spec object listed twice is baked once.
158
+
159
+ **The raster scale is explicit.** The bake cannot see the device scale, so you pass it: the number of screen pixels one spec unit covers, which is pixel density times camera zoom times the instance scale you mostly draw at. Baking at 1 on a 3× display draws blurry shapes.
160
+
161
+ **The re-raster rule.** A baked atlas is kept while the scale you want stays between 0.75× and 1.5× of the scale it was baked at; inside that band the baked pixels are drawn as they are. `shapeAtlasNeedsRebake(atlas, scale)` is the test, and `rebakeShapeAtlas` applies it: inside the band it returns the atlas it was given, outside it bakes the same specs at the new scale. Cell indices do not change across a re-bake, and neither do a spec's origin and drawn size, so instance draws need no change. A cell's `width`/`height` and its fractional anchors can differ by the rounding margin of either bake, because each cell is rounded up to whole pixels. The previous image is yours to release.
162
+
163
+ **The 2048 px cell cap.** No cell is rasterized larger than `SHAPE_ATLAS_MAX_CELL` (2048 px) on its longer side. A spec that would need more is baked at a lower scale of its own, so its longer side is exactly 2048 px; `cell.scale` reports the scale it got. It still draws at the right size, only softer. Nothing throws. The cap is per cell: the atlas wraps rows at `maxWidth` (default 2048) but grows as tall as its rows need, so bake very large or very many specs into more than one atlas.
164
+
165
+ **Limits.**
166
+
167
+ - **Animated specs stay on `drawShape`.** The bake is a snapshot. A spec whose parameters change every frame, such as an `arc` used as a cooldown sweep, would re-bake every frame.
168
+ - **Stroke and glow scale with the instance.** A baked instance is a sprite: scaling it scales its stroke width and glow ring too, including across the re-raster band.
169
+ - **Rotation is rigid.** `drawBakedShape` turns the whole cell about the spec's origin. `drawShape`'s `rot` turns each part about its own centre and leaves `ox`/`oy` unrotated, so a rotated `ship` or offset spec looks different on the two paths.
170
+ - **Colours are baked in.** The optional `color` argument multiplies the baked pixels (it is `SpriteBatch`'s per-sprite colour); it cannot recolour one part. Instances drawn with a `color` and instances drawn without one form separate runs.
171
+ - **Uniform scale only** on the batched path.
172
+
116
173
  ## Adding a new primitive
117
174
 
118
175
  Three ways, in order of friction:
package/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { SkImageLike, SpriteFrame, SpriteSheet } from "@flare-engine/assets";
1
2
  import { SkCanvas, SkPaint, Skia } from "@shopify/react-native-skia";
2
3
  /**
3
4
  * Shape primitive vocabulary. 12 named atomic shapes + 1 SVG escape hatch.
@@ -168,6 +169,151 @@ interface ShipSpec extends BaseSpec {
168
169
  readonly engine: SquareSpec;
169
170
  }
170
171
  type ShapeSpec = CircleSpec | RectSpec | TriangleSpec | NgonSpec | StarSpec | RingSpec | PolylineSpec | GradientRectSpec | ArcSpec | AnnulusSpec | TrapezoidSpec | PathSpec | ShipSpec;
172
+ /** Longest side a baked cell may have, in atlas pixels. */
173
+ export declare const SHAPE_ATLAS_MAX_CELL = 2048;
174
+ /** The paint members the bake and `drawShape` call. */
175
+ interface ShapeAtlasPaint {
176
+ setColor(color: Float32Array): void;
177
+ /** 0 = fill, 1 = stroke. */
178
+ setStyle(style: number): void;
179
+ setStrokeWidth(width: number): void;
180
+ }
181
+ /** The canvas members the bake and `drawShape` call. `rotate` takes degrees. */
182
+ interface ShapeAtlasCanvas {
183
+ save(): unknown;
184
+ restore(): void;
185
+ translate(dx: number, dy: number): void;
186
+ scale(sx: number, sy: number): void;
187
+ rotate(degrees: number, rx: number, ry: number): void;
188
+ clear(color: Float32Array): void;
189
+ drawCircle(cx: number, cy: number, r: number, paint: ShapeAtlasPaint): void;
190
+ drawRect(rect: unknown, paint: ShapeAtlasPaint): void;
191
+ drawPath(path: unknown, paint: ShapeAtlasPaint): void;
192
+ }
193
+ /** An offscreen surface the bake draws into and snapshots. */
194
+ interface ShapeAtlasSurface {
195
+ getCanvas(): ShapeAtlasCanvas;
196
+ makeImageSnapshot(): SkImageLike;
197
+ /** Called once after the snapshot, when present, to free the surface. */
198
+ dispose?(): void;
199
+ }
200
+ /**
201
+ * The Skia handle `bakeShapeAtlas` needs: what `drawShape` reads plus an
202
+ * offscreen surface factory. `Skia` from `@shopify/react-native-skia`
203
+ * satisfies it; on web, `createWebShapeSkia(kit)` from
204
+ * `@flare-engine/render-web` builds one over CanvasKit.
205
+ */
206
+ interface ShapeAtlasSkia {
207
+ Surface: {
208
+ Make(width: number, height: number): ShapeAtlasSurface | null;
209
+ };
210
+ Paint(): ShapeAtlasPaint;
211
+ Path: {
212
+ Make(): {
213
+ moveTo(x: number, y: number): unknown;
214
+ lineTo(x: number, y: number): unknown;
215
+ close(): unknown;
216
+ addArc(oval: unknown, startDeg: number, sweepDeg: number): unknown;
217
+ };
218
+ MakeFromSVGString(d: string): unknown;
219
+ };
220
+ XYWHRect(x: number, y: number, w: number, h: number): unknown;
221
+ }
222
+ /** Layout options for `bakeShapeAtlas`. */
223
+ interface ShapeAtlasOptions {
224
+ /** Atlas width at which cells wrap to a new row, in pixels. Default 2048. */
225
+ maxWidth?: number;
226
+ /** Transparent gap between cells, in pixels, against sampling bleed. Default 2. */
227
+ padding?: number;
228
+ }
229
+ /** One baked spec: where it sits in the atlas and how an instance maps onto it. */
230
+ interface ShapeAtlasCell {
231
+ /** The cell's rectangle in the atlas image, in pixels. */
232
+ readonly frame: SpriteFrame;
233
+ /** The spec's origin inside the cell, as a fraction of its width. */
234
+ readonly anchorX: number;
235
+ /** The spec's origin inside the cell, as a fraction of its height. */
236
+ readonly anchorY: number;
237
+ /** Cell width in the spec's own units: what an instance at scale 1 covers. */
238
+ readonly width: number;
239
+ /** Cell height in the spec's own units. */
240
+ readonly height: number;
241
+ /**
242
+ * Pixels per spec unit this cell was rasterized at. Equal to the atlas
243
+ * scale unless the cell hit `SHAPE_ATLAS_MAX_CELL`, in which case it is lower.
244
+ */
245
+ readonly scale: number;
246
+ }
247
+ /** A baked shape atlas: the `{ image, sheet }` pair a sprite batch draws, plus a cell per spec. */
248
+ interface ShapeAtlas {
249
+ readonly image: SkImageLike;
250
+ /** One frame per distinct spec, named by its index in `sheet.frames`. */
251
+ readonly sheet: SpriteSheet;
252
+ /** Atlas width in pixels. */
253
+ readonly width: number;
254
+ /** Atlas height in pixels. */
255
+ readonly height: number;
256
+ /** The raster scale passed to the bake. */
257
+ readonly scale: number;
258
+ /** `cells[i]` belongs to `specs[i]`. A spec object that repeats shares one cell. */
259
+ readonly cells: readonly ShapeAtlasCell[];
260
+ /** A copy of the baked spec list. */
261
+ readonly specs: readonly ShapeSpec[];
262
+ readonly maxWidth: number;
263
+ readonly padding: number;
264
+ }
265
+ /** The one `SpriteBatch` method `drawBakedShape` calls. */
266
+ interface ShapeBatchLike {
267
+ draw(image: SkImageLike, frame: SpriteFrame, dstX: number, dstY: number, dstW?: number, dstH?: number, effect?: undefined, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean, blend?: 'normal' | 'additive', flipX?: boolean, flipY?: boolean, color?: number): void;
268
+ }
269
+ /**
270
+ * Rasterize `specs` into one atlas image at `scale` pixels per spec unit.
271
+ *
272
+ * `scale` is explicit because nothing the bake can see knows the device
273
+ * scale: pass the pixels one spec unit covers on screen (pixel density times
274
+ * camera zoom times the usual instance scale).
275
+ *
276
+ * Each distinct spec object is drawn once by `drawShape`, with its own fill,
277
+ * stroke and glow colours, into a cell sized from `shapeBoundsBoxInto`. A cell
278
+ * whose longer side would exceed `SHAPE_ATLAS_MAX_CELL` is rasterized at a
279
+ * lower scale of its own so that side is exactly the cap; it still draws at
280
+ * the same size, only softer.
281
+ *
282
+ * Not for per-frame use: a spec whose parameters animate belongs on
283
+ * `drawShape`. Throws if `scale` is not a positive finite number or the
284
+ * surface cannot be created.
285
+ */
286
+ export declare function bakeShapeAtlas(Skia: ShapeAtlasSkia, specs: readonly ShapeSpec[], scale: number, options?: ShapeAtlasOptions): ShapeAtlas;
287
+ /**
288
+ * True when `scale` has left the re-raster band of `atlas`: below 0.75x or
289
+ * above 1.5x of the scale it was baked at. Inside the band the baked pixels
290
+ * are close enough to draw as they are. A `scale` that is not a positive
291
+ * finite number returns false. Allocates nothing; safe to call every frame.
292
+ */
293
+ export declare function shapeAtlasNeedsRebake(atlas: ShapeAtlas, scale: number): boolean;
294
+ /**
295
+ * Return `atlas` itself while `scale` is inside its re-raster band (see
296
+ * `shapeAtlasNeedsRebake`), allocating nothing; otherwise bake the same specs
297
+ * with the same options at `scale` and return the new atlas. Cell indices
298
+ * carry over exactly, and so do each spec's origin and drawn size
299
+ * (`anchorX * width` is always `-minX`), so instance draws need no change. A
300
+ * cell's `width`/`height` and fractional anchors can differ by the rounding
301
+ * margin of either bake, because each cell is rounded up to whole pixels. The old
302
+ * image is not released: that is the caller's to drop.
303
+ */
304
+ export declare function rebakeShapeAtlas(Skia: ShapeAtlasSkia, atlas: ShapeAtlas, scale: number): ShapeAtlas;
305
+ /**
306
+ * Queue one instance of baked spec `index` on `batch`, with the spec's origin
307
+ * at (`x`, `y`). `rot` is radians, clockwise, about that origin; `scale` is
308
+ * uniform and multiplies stroke and glow widths along with everything else.
309
+ * `color` is an optional packed-ARGB multiplier over the baked colours and
310
+ * `alpha` an opacity in [0, 1], both as `SpriteBatch.draw` takes them.
311
+ *
312
+ * The whole cell turns rigidly, including each part's `ox`/`oy` offset, which
313
+ * `drawShape`'s `rot` leaves unrotated. An `index` with no cell draws nothing.
314
+ * Allocates nothing.
315
+ */
316
+ export declare function drawBakedShape(batch: ShapeBatchLike, atlas: ShapeAtlas, index: number, x: number, y: number, rot?: number, scale?: number, color?: number, alpha?: number): void;
171
317
  /** Per-axis extents in the composition-local frame (the frame `ox`/`oy` live in). */
172
318
  interface ShapeBoundsBox {
173
319
  minX: number;
@@ -215,7 +361,8 @@ export declare function applyHazardGlow(canvas: SkCanvas, paint: SkPaint, cx: nu
215
361
  * negative `width` paints nothing.
216
362
  */
217
363
  export declare function applyGlow(canvas: SkCanvas, paint: SkPaint, cx: number, cy: number, r: number, color: number, width: number): void;
364
+ /** Same as `argbToFloat4Into` from `@flare-engine/math`, kept as the shapes entry point. */
218
365
  export declare function argbTo(out: Float32Array, argb: number): void;
219
366
  export declare function drawShape(Skia: SkiaApi, canvas: SkCanvas, paint: SkPaint, spec: ShapeSpec, x: number, y: number, rot: number): void;
220
- export type { AnnulusSpec, ArcSpec, BaseSpec, CircleSpec, GlowSpec, GradientRectSpec, NgonSpec, PathSpec, PolylineSpec, RectSpec, RingSpec, ShapeBoundsBox, ShapeSpec, ShipSpec, SquareSpec, StarSpec, TrapezoidSpec, TriangleSpec };
367
+ export type { AnnulusSpec, ArcSpec, BaseSpec, CircleSpec, GlowSpec, GradientRectSpec, NgonSpec, PathSpec, PolylineSpec, RectSpec, RingSpec, ShapeAtlas, ShapeAtlasCanvas, ShapeAtlasCell, ShapeAtlasOptions, ShapeAtlasPaint, ShapeAtlasSkia, ShapeAtlasSurface, ShapeBatchLike, ShapeBoundsBox, ShapeSpec, ShipSpec, SquareSpec, StarSpec, TrapezoidSpec, TriangleSpec };
221
368
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":["SkiaModule"],"sources":["../src/types.ts","../src/bounds.ts","../src/draw.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;UAyBiB;WACN;WACA;;UAGM;WACN;WACA;WACA;;WAEA;WACA;;WAEA;;;;;;;;;WASA,+BAA+B;;UAGzB,mBAAmB;WACzB;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,yBAAyB;WAC/B;WACA;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,gBAAgB;WACtB;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,oBAAoB;WAC1B;WACA;WACA;;WAEA;WACA;;;;;;;UAQM,sBAAsB;WAC5B;WACA;WACA;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA;;;;;;WAMA;;;UAIM,mBAAmB;WACzB;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA,MAAM;WACN,kBAAkB;WAClB,QAAQ;;KAGP,YACR,aACA,WACA,eACA,WACA,WACA,WACA,eACA,mBACA,UACA,cACA,gBACA,WACA;;UCnMa;EACf;EACA;EACA;EACA;;;;;;;wBAWc,kBAAkB,MAAM;;;;;;;;;wBAwDxB,kBAAkB,gBAAgB;;wBAalC,eAAe,gBAAgB,cAAc;;;;;;;;;wBAY7C,mBACd,gBAAgB,aAChB,KAAK,iBACJ;KC1EE,iBAAiBA;;wBAKN;;wBAKA;wBAIA,kBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;wBAMc,gBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;;;;;;;wBAcc,UACd,QAAQ,UACR,OAAO,SACP,YACA,YACA,WACA,eACA;wBAWc,OAAO,KAAK,cAAc;wBAyC1B,UACd,MAAM,SACN,QAAQ,UACR,OAAO,SACP,MAAM,WACN,WACA,WACA"}
1
+ {"version":3,"file":"index.d.mts","names":["SkiaModule"],"sources":["../src/types.ts","../src/atlas.ts","../src/bounds.ts","../src/draw.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;UAyBiB;WACN;WACA;;UAGM;WACN;WACA;WACA;;WAEA;WACA;;WAEA;;;;;;;;;WASA,+BAA+B;;UAGzB,mBAAmB;WACzB;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,yBAAyB;WAC/B;WACA;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,gBAAgB;WACtB;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,oBAAoB;WAC1B;WACA;WACA;;WAEA;WACA;;;;;;;UAQM,sBAAsB;WAC5B;WACA;WACA;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA;;;;;;WAMA;;;UAIM,mBAAmB;WACzB;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA,MAAM;WACN,kBAAkB;WAClB,QAAQ;;KAGP,YACR,aACA,WACA,eACA,WACA,WACA,WACA,eACA,mBACA,UACA,cACA,gBACA,WACA;;qBCnLS;;UAQI;EACf,SAAS,OAAO;;EAEhB,SAAS;EACT,eAAe;;;UAIA;EACf;EACA;EACA,UAAU,YAAY;EACtB,MAAM,YAAY;EAClB,OAAO,iBAAiB,YAAY;EACpC,MAAM,OAAO;EACb,WAAW,YAAY,YAAY,WAAW,OAAO;EACrD,SAAS,eAAe,OAAO;EAC/B,SAAS,eAAe,OAAO;;;UAIhB;EACf,aAAa;EACb,qBAAqB;;EAErB;;;;;;;;UASe;EACf;IAAW,KAAK,eAAe,iBAAiB;;EAChD,SAAS;EACT;IACE;MACE,OAAO,WAAW;MAClB,OAAO,WAAW;MAClB;MACA,OAAO,eAAe,kBAAkB;;IAE1C,kBAAkB;;EAEpB,SAAS,WAAW,WAAW,WAAW;;;UAI3B;;EAEf;;EAEA;;;UAIe;;WAEN,OAAO;;WAEP;;WAEA;;WAEA;;WAEA;;;;;WAKA;;;UAIM;WACN,OAAO;;WAEP,OAAO;;WAEP;;WAEA;;WAEA;;WAEA,gBAAgB;;WAEhB,gBAAgB;WAChB;WACA;;;UAIM;EACf,KACE,OAAO,aACP,OAAO,aACP,cACA,cACA,eACA,eACA,oBACA,kBACA,kBACA,gBACA,mBACA,mBACA,+BACA,iBACA,iBACA;;;;;;;;;;;;;;;;;;;wBAwBY,eACd,MAAM,gBACN,gBAAgB,aAChB,eACA,UAAS,oBACR;;;;;;;wBA8Fa,sBAAsB,OAAO,YAAY;;;;;;;;;;;wBAezC,iBACd,MAAM,gBACN,OAAO,YACP,gBACC;;;;;;;;;;;;wBAmBa,eACd,OAAO,gBACP,OAAO,YACP,eACA,WACA,WACA,cACA,gBACA,gBACA;;UCnTe;EACf;EACA;EACA;EACA;;;;;;;wBAWc,kBAAkB,MAAM;;;;;;;;;wBAwDxB,kBAAkB,gBAAgB;;wBAalC,eAAe,gBAAgB,cAAc;;;;;;;;;wBAY7C,mBACd,gBAAgB,aAChB,KAAK,iBACJ;KCzEE,iBAAiBA;;wBAKN;;wBAKA;wBAIA,kBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;wBAMc,gBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;;;;;;;wBAcc,UACd,QAAQ,UACR,OAAO,SACP,YACA,YACA,WACA,eACA;;wBAYc,OAAO,KAAK,cAAc;wBAsC1B,UACd,MAAM,SACN,QAAQ,UACR,OAAO,SACP,MAAM,WACN,WACA,WACA"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { SkImageLike, SpriteFrame, SpriteSheet } from "@flare-engine/assets";
1
2
  import { SkCanvas, SkPaint, Skia } from "@shopify/react-native-skia";
2
3
  /**
3
4
  * Shape primitive vocabulary. 12 named atomic shapes + 1 SVG escape hatch.
@@ -168,6 +169,151 @@ interface ShipSpec extends BaseSpec {
168
169
  readonly engine: SquareSpec;
169
170
  }
170
171
  type ShapeSpec = CircleSpec | RectSpec | TriangleSpec | NgonSpec | StarSpec | RingSpec | PolylineSpec | GradientRectSpec | ArcSpec | AnnulusSpec | TrapezoidSpec | PathSpec | ShipSpec;
172
+ /** Longest side a baked cell may have, in atlas pixels. */
173
+ export declare const SHAPE_ATLAS_MAX_CELL = 2048;
174
+ /** The paint members the bake and `drawShape` call. */
175
+ interface ShapeAtlasPaint {
176
+ setColor(color: Float32Array): void;
177
+ /** 0 = fill, 1 = stroke. */
178
+ setStyle(style: number): void;
179
+ setStrokeWidth(width: number): void;
180
+ }
181
+ /** The canvas members the bake and `drawShape` call. `rotate` takes degrees. */
182
+ interface ShapeAtlasCanvas {
183
+ save(): unknown;
184
+ restore(): void;
185
+ translate(dx: number, dy: number): void;
186
+ scale(sx: number, sy: number): void;
187
+ rotate(degrees: number, rx: number, ry: number): void;
188
+ clear(color: Float32Array): void;
189
+ drawCircle(cx: number, cy: number, r: number, paint: ShapeAtlasPaint): void;
190
+ drawRect(rect: unknown, paint: ShapeAtlasPaint): void;
191
+ drawPath(path: unknown, paint: ShapeAtlasPaint): void;
192
+ }
193
+ /** An offscreen surface the bake draws into and snapshots. */
194
+ interface ShapeAtlasSurface {
195
+ getCanvas(): ShapeAtlasCanvas;
196
+ makeImageSnapshot(): SkImageLike;
197
+ /** Called once after the snapshot, when present, to free the surface. */
198
+ dispose?(): void;
199
+ }
200
+ /**
201
+ * The Skia handle `bakeShapeAtlas` needs: what `drawShape` reads plus an
202
+ * offscreen surface factory. `Skia` from `@shopify/react-native-skia`
203
+ * satisfies it; on web, `createWebShapeSkia(kit)` from
204
+ * `@flare-engine/render-web` builds one over CanvasKit.
205
+ */
206
+ interface ShapeAtlasSkia {
207
+ Surface: {
208
+ Make(width: number, height: number): ShapeAtlasSurface | null;
209
+ };
210
+ Paint(): ShapeAtlasPaint;
211
+ Path: {
212
+ Make(): {
213
+ moveTo(x: number, y: number): unknown;
214
+ lineTo(x: number, y: number): unknown;
215
+ close(): unknown;
216
+ addArc(oval: unknown, startDeg: number, sweepDeg: number): unknown;
217
+ };
218
+ MakeFromSVGString(d: string): unknown;
219
+ };
220
+ XYWHRect(x: number, y: number, w: number, h: number): unknown;
221
+ }
222
+ /** Layout options for `bakeShapeAtlas`. */
223
+ interface ShapeAtlasOptions {
224
+ /** Atlas width at which cells wrap to a new row, in pixels. Default 2048. */
225
+ maxWidth?: number;
226
+ /** Transparent gap between cells, in pixels, against sampling bleed. Default 2. */
227
+ padding?: number;
228
+ }
229
+ /** One baked spec: where it sits in the atlas and how an instance maps onto it. */
230
+ interface ShapeAtlasCell {
231
+ /** The cell's rectangle in the atlas image, in pixels. */
232
+ readonly frame: SpriteFrame;
233
+ /** The spec's origin inside the cell, as a fraction of its width. */
234
+ readonly anchorX: number;
235
+ /** The spec's origin inside the cell, as a fraction of its height. */
236
+ readonly anchorY: number;
237
+ /** Cell width in the spec's own units: what an instance at scale 1 covers. */
238
+ readonly width: number;
239
+ /** Cell height in the spec's own units. */
240
+ readonly height: number;
241
+ /**
242
+ * Pixels per spec unit this cell was rasterized at. Equal to the atlas
243
+ * scale unless the cell hit `SHAPE_ATLAS_MAX_CELL`, in which case it is lower.
244
+ */
245
+ readonly scale: number;
246
+ }
247
+ /** A baked shape atlas: the `{ image, sheet }` pair a sprite batch draws, plus a cell per spec. */
248
+ interface ShapeAtlas {
249
+ readonly image: SkImageLike;
250
+ /** One frame per distinct spec, named by its index in `sheet.frames`. */
251
+ readonly sheet: SpriteSheet;
252
+ /** Atlas width in pixels. */
253
+ readonly width: number;
254
+ /** Atlas height in pixels. */
255
+ readonly height: number;
256
+ /** The raster scale passed to the bake. */
257
+ readonly scale: number;
258
+ /** `cells[i]` belongs to `specs[i]`. A spec object that repeats shares one cell. */
259
+ readonly cells: readonly ShapeAtlasCell[];
260
+ /** A copy of the baked spec list. */
261
+ readonly specs: readonly ShapeSpec[];
262
+ readonly maxWidth: number;
263
+ readonly padding: number;
264
+ }
265
+ /** The one `SpriteBatch` method `drawBakedShape` calls. */
266
+ interface ShapeBatchLike {
267
+ draw(image: SkImageLike, frame: SpriteFrame, dstX: number, dstY: number, dstW?: number, dstH?: number, effect?: undefined, anchorX?: number, anchorY?: number, alpha?: number, rotation?: number, stretch?: boolean, blend?: 'normal' | 'additive', flipX?: boolean, flipY?: boolean, color?: number): void;
268
+ }
269
+ /**
270
+ * Rasterize `specs` into one atlas image at `scale` pixels per spec unit.
271
+ *
272
+ * `scale` is explicit because nothing the bake can see knows the device
273
+ * scale: pass the pixels one spec unit covers on screen (pixel density times
274
+ * camera zoom times the usual instance scale).
275
+ *
276
+ * Each distinct spec object is drawn once by `drawShape`, with its own fill,
277
+ * stroke and glow colours, into a cell sized from `shapeBoundsBoxInto`. A cell
278
+ * whose longer side would exceed `SHAPE_ATLAS_MAX_CELL` is rasterized at a
279
+ * lower scale of its own so that side is exactly the cap; it still draws at
280
+ * the same size, only softer.
281
+ *
282
+ * Not for per-frame use: a spec whose parameters animate belongs on
283
+ * `drawShape`. Throws if `scale` is not a positive finite number or the
284
+ * surface cannot be created.
285
+ */
286
+ export declare function bakeShapeAtlas(Skia: ShapeAtlasSkia, specs: readonly ShapeSpec[], scale: number, options?: ShapeAtlasOptions): ShapeAtlas;
287
+ /**
288
+ * True when `scale` has left the re-raster band of `atlas`: below 0.75x or
289
+ * above 1.5x of the scale it was baked at. Inside the band the baked pixels
290
+ * are close enough to draw as they are. A `scale` that is not a positive
291
+ * finite number returns false. Allocates nothing; safe to call every frame.
292
+ */
293
+ export declare function shapeAtlasNeedsRebake(atlas: ShapeAtlas, scale: number): boolean;
294
+ /**
295
+ * Return `atlas` itself while `scale` is inside its re-raster band (see
296
+ * `shapeAtlasNeedsRebake`), allocating nothing; otherwise bake the same specs
297
+ * with the same options at `scale` and return the new atlas. Cell indices
298
+ * carry over exactly, and so do each spec's origin and drawn size
299
+ * (`anchorX * width` is always `-minX`), so instance draws need no change. A
300
+ * cell's `width`/`height` and fractional anchors can differ by the rounding
301
+ * margin of either bake, because each cell is rounded up to whole pixels. The old
302
+ * image is not released: that is the caller's to drop.
303
+ */
304
+ export declare function rebakeShapeAtlas(Skia: ShapeAtlasSkia, atlas: ShapeAtlas, scale: number): ShapeAtlas;
305
+ /**
306
+ * Queue one instance of baked spec `index` on `batch`, with the spec's origin
307
+ * at (`x`, `y`). `rot` is radians, clockwise, about that origin; `scale` is
308
+ * uniform and multiplies stroke and glow widths along with everything else.
309
+ * `color` is an optional packed-ARGB multiplier over the baked colours and
310
+ * `alpha` an opacity in [0, 1], both as `SpriteBatch.draw` takes them.
311
+ *
312
+ * The whole cell turns rigidly, including each part's `ox`/`oy` offset, which
313
+ * `drawShape`'s `rot` leaves unrotated. An `index` with no cell draws nothing.
314
+ * Allocates nothing.
315
+ */
316
+ export declare function drawBakedShape(batch: ShapeBatchLike, atlas: ShapeAtlas, index: number, x: number, y: number, rot?: number, scale?: number, color?: number, alpha?: number): void;
171
317
  /** Per-axis extents in the composition-local frame (the frame `ox`/`oy` live in). */
172
318
  interface ShapeBoundsBox {
173
319
  minX: number;
@@ -215,7 +361,8 @@ export declare function applyHazardGlow(canvas: SkCanvas, paint: SkPaint, cx: nu
215
361
  * negative `width` paints nothing.
216
362
  */
217
363
  export declare function applyGlow(canvas: SkCanvas, paint: SkPaint, cx: number, cy: number, r: number, color: number, width: number): void;
364
+ /** Same as `argbToFloat4Into` from `@flare-engine/math`, kept as the shapes entry point. */
218
365
  export declare function argbTo(out: Float32Array, argb: number): void;
219
366
  export declare function drawShape(Skia: SkiaApi, canvas: SkCanvas, paint: SkPaint, spec: ShapeSpec, x: number, y: number, rot: number): void;
220
- export type { AnnulusSpec, ArcSpec, BaseSpec, CircleSpec, GlowSpec, GradientRectSpec, NgonSpec, PathSpec, PolylineSpec, RectSpec, RingSpec, ShapeBoundsBox, ShapeSpec, ShipSpec, SquareSpec, StarSpec, TrapezoidSpec, TriangleSpec };
367
+ export type { AnnulusSpec, ArcSpec, BaseSpec, CircleSpec, GlowSpec, GradientRectSpec, NgonSpec, PathSpec, PolylineSpec, RectSpec, RingSpec, ShapeAtlas, ShapeAtlasCanvas, ShapeAtlasCell, ShapeAtlasOptions, ShapeAtlasPaint, ShapeAtlasSkia, ShapeAtlasSurface, ShapeBatchLike, ShapeBoundsBox, ShapeSpec, ShipSpec, SquareSpec, StarSpec, TrapezoidSpec, TriangleSpec };
221
368
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":["SkiaModule"],"sources":["../src/types.ts","../src/bounds.ts","../src/draw.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;UAyBiB;WACN;WACA;;UAGM;WACN;WACA;WACA;;WAEA;WACA;;WAEA;;;;;;;;;WASA,+BAA+B;;UAGzB,mBAAmB;WACzB;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,yBAAyB;WAC/B;WACA;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,gBAAgB;WACtB;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,oBAAoB;WAC1B;WACA;WACA;;WAEA;WACA;;;;;;;UAQM,sBAAsB;WAC5B;WACA;WACA;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA;;;;;;WAMA;;;UAIM,mBAAmB;WACzB;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA,MAAM;WACN,kBAAkB;WAClB,QAAQ;;KAGP,YACR,aACA,WACA,eACA,WACA,WACA,WACA,eACA,mBACA,UACA,cACA,gBACA,WACA;;UCnMa;EACf;EACA;EACA;EACA;;;;;;;wBAWc,kBAAkB,MAAM;;;;;;;;;wBAwDxB,kBAAkB,gBAAgB;;wBAalC,eAAe,gBAAgB,cAAc;;;;;;;;;wBAY7C,mBACd,gBAAgB,aAChB,KAAK,iBACJ;KC1EE,iBAAiBA;;wBAKN;;wBAKA;wBAIA,kBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;wBAMc,gBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;;;;;;;wBAcc,UACd,QAAQ,UACR,OAAO,SACP,YACA,YACA,WACA,eACA;wBAWc,OAAO,KAAK,cAAc;wBAyC1B,UACd,MAAM,SACN,QAAQ,UACR,OAAO,SACP,MAAM,WACN,WACA,WACA"}
1
+ {"version":3,"file":"index.d.ts","names":["SkiaModule"],"sources":["../src/types.ts","../src/atlas.ts","../src/bounds.ts","../src/draw.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;UAyBiB;WACN;WACA;;UAGM;WACN;WACA;WACA;;WAEA;WACA;;WAEA;;;;;;;;;WASA,+BAA+B;;UAGzB,mBAAmB;WACzB;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;WACA;WACA;;UAGM,iBAAiB;WACvB;WACA;;UAGM,qBAAqB;WAC3B;WACA;;UAGM,yBAAyB;WAC/B;WACA;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,gBAAgB;WACtB;WACA;WACA;WACA;;WAEA;;;;;;;;;;UAWM,oBAAoB;WAC1B;WACA;WACA;;WAEA;WACA;;;;;;;UAQM,sBAAsB;WAC5B;WACA;WACA;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA;;;;;;WAMA;;;UAIM,mBAAmB;WACzB;WACA;;;;;;;;;;UAWM,iBAAiB;WACvB;WACA,MAAM;WACN,kBAAkB;WAClB,QAAQ;;KAGP,YACR,aACA,WACA,eACA,WACA,WACA,WACA,eACA,mBACA,UACA,cACA,gBACA,WACA;;qBCnLS;;UAQI;EACf,SAAS,OAAO;;EAEhB,SAAS;EACT,eAAe;;;UAIA;EACf;EACA;EACA,UAAU,YAAY;EACtB,MAAM,YAAY;EAClB,OAAO,iBAAiB,YAAY;EACpC,MAAM,OAAO;EACb,WAAW,YAAY,YAAY,WAAW,OAAO;EACrD,SAAS,eAAe,OAAO;EAC/B,SAAS,eAAe,OAAO;;;UAIhB;EACf,aAAa;EACb,qBAAqB;;EAErB;;;;;;;;UASe;EACf;IAAW,KAAK,eAAe,iBAAiB;;EAChD,SAAS;EACT;IACE;MACE,OAAO,WAAW;MAClB,OAAO,WAAW;MAClB;MACA,OAAO,eAAe,kBAAkB;;IAE1C,kBAAkB;;EAEpB,SAAS,WAAW,WAAW,WAAW;;;UAI3B;;EAEf;;EAEA;;;UAIe;;WAEN,OAAO;;WAEP;;WAEA;;WAEA;;WAEA;;;;;WAKA;;;UAIM;WACN,OAAO;;WAEP,OAAO;;WAEP;;WAEA;;WAEA;;WAEA,gBAAgB;;WAEhB,gBAAgB;WAChB;WACA;;;UAIM;EACf,KACE,OAAO,aACP,OAAO,aACP,cACA,cACA,eACA,eACA,oBACA,kBACA,kBACA,gBACA,mBACA,mBACA,+BACA,iBACA,iBACA;;;;;;;;;;;;;;;;;;;wBAwBY,eACd,MAAM,gBACN,gBAAgB,aAChB,eACA,UAAS,oBACR;;;;;;;wBA8Fa,sBAAsB,OAAO,YAAY;;;;;;;;;;;wBAezC,iBACd,MAAM,gBACN,OAAO,YACP,gBACC;;;;;;;;;;;;wBAmBa,eACd,OAAO,gBACP,OAAO,YACP,eACA,WACA,WACA,cACA,gBACA,gBACA;;UCnTe;EACf;EACA;EACA;EACA;;;;;;;wBAWc,kBAAkB,MAAM;;;;;;;;;wBAwDxB,kBAAkB,gBAAgB;;wBAalC,eAAe,gBAAgB,cAAc;;;;;;;;;wBAY7C,mBACd,gBAAgB,aAChB,KAAK,iBACJ;KCzEE,iBAAiBA;;wBAKN;;wBAKA;wBAIA,kBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;wBAMc,gBACd,QAAQ,UACR,OAAO,SACP,YACA,YACA;;;;;;;wBAcc,UACd,QAAQ,UACR,OAAO,SACP,YACA,YACA,WACA,eACA;;wBAYc,OAAO,KAAK,cAAc;wBAsC1B,UACd,MAAM,SACN,QAAQ,UACR,OAAO,SACP,MAAM,WACN,WACA,WACA"}
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _flare_engine_assets = require("@flare-engine/assets");
3
+ let _flare_engine_math = require("@flare-engine/math");
2
4
  // src/bounds.ts
3
5
  /**
4
6
  * Approximate bounding radius for a single primitive. Used to size glow
@@ -80,15 +82,15 @@ function shapeBoundsBox(specs) {
80
82
  * zeros — no 4-unit atlas floor. Writes `out` and returns it; allocates nothing.
81
83
  */
82
84
  function shapeBoundsBoxInto(specs, out) {
83
- out.minX = Infinity;
84
- out.minY = Infinity;
85
- out.maxX = -Infinity;
86
- out.maxY = -Infinity;
85
+ out.minX = 1e400;
86
+ out.minY = 1e400;
87
+ out.maxX = -1e400;
88
+ out.maxY = -1e400;
87
89
  for (let i = 0; i < specs.length; i++) {
88
90
  const s = specs[i];
89
91
  if (s !== void 0) expand(out, s, 0, 0, 0, false);
90
92
  }
91
- if (out.minX === Infinity) {
93
+ if (out.minX === 1e400) {
92
94
  out.minX = 0;
93
95
  out.minY = 0;
94
96
  out.maxX = 0;
@@ -224,6 +226,15 @@ function boundsR(spec) {
224
226
  }
225
227
  }
226
228
  // src/draw.ts
229
+ /**
230
+ * Shape drawing dispatcher — consumes a `ShapeSpec` and emits Skia draw
231
+ * calls on a canvas. All shapes draw relative to (x + ox, y + oy), rotated
232
+ * by (rot + spec.rot).
233
+ *
234
+ * The dispatcher is allocation-free per call except where Skia itself
235
+ * allocates path objects. Glow ring is emitted before the shape body so it
236
+ * renders behind the geometry.
237
+ */
227
238
  /** ARGB for safe #6FA66B at α ≈ 0.25 (0x40 / 255). */
228
239
  function friendlyGlowArgb() {
229
240
  return 1081058923;
@@ -253,19 +264,17 @@ function applyGlow(canvas, paint, cx, cy, r, color, width) {
253
264
  canvas.drawCircle(cx, cy, r + (2.5 + width / 2), paint);
254
265
  }
255
266
  const COLOR = /* @__PURE__ */ new Float32Array(4);
267
+ /** Same as `argbToFloat4Into` from `@flare-engine/math`, kept as the shapes entry point. */
256
268
  function argbTo(out, argb) {
257
- out[0] = (argb >>> 16 & 255) / 255;
258
- out[1] = (argb >>> 8 & 255) / 255;
259
- out[2] = (argb & 255) / 255;
260
- out[3] = (argb >>> 24 & 255) / 255;
269
+ (0, _flare_engine_math.argbToFloat4Into)(out, argb);
261
270
  }
262
271
  function applyFill(paint, argb) {
263
- argbTo(COLOR, argb);
272
+ (0, _flare_engine_math.argbToFloat4Into)(COLOR, argb);
264
273
  paint.setColor(COLOR);
265
274
  paint.setStyle(0);
266
275
  }
267
276
  function applyStroke(paint, argb, width) {
268
- argbTo(COLOR, argb);
277
+ (0, _flare_engine_math.argbToFloat4Into)(COLOR, argb);
269
278
  paint.setColor(COLOR);
270
279
  paint.setStyle(1);
271
280
  paint.setStrokeWidth(width);
@@ -524,13 +533,172 @@ function drawShip(Skia, canvas, paint, spec, cx, cy, rot) {
524
533
  if (w !== void 0) drawShape(Skia, canvas, paint, w, cx, cy, rot);
525
534
  }
526
535
  }
536
+ // src/atlas.ts
537
+ /**
538
+ * Shape atlas bake — rasterizes `ShapeSpec`s once into one image, so that
539
+ * shape-built entities draw as sprites through a batch instead of paying
540
+ * `drawShape`'s one Skia draw per fill, stroke and glow ring.
541
+ */
542
+ /** Longest side a baked cell may have, in atlas pixels. */
543
+ const SHAPE_ATLAS_MAX_CELL = 2048;
544
+ const BAND_MIN = .75;
545
+ const BAND_MAX = 1.5;
546
+ const BOX = {
547
+ minX: 0,
548
+ minY: 0,
549
+ maxX: 0,
550
+ maxY: 0
551
+ };
552
+ const TRANSPARENT = /* @__PURE__ */ new Float32Array(4);
553
+ /**
554
+ * Rasterize `specs` into one atlas image at `scale` pixels per spec unit.
555
+ *
556
+ * `scale` is explicit because nothing the bake can see knows the device
557
+ * scale: pass the pixels one spec unit covers on screen (pixel density times
558
+ * camera zoom times the usual instance scale).
559
+ *
560
+ * Each distinct spec object is drawn once by `drawShape`, with its own fill,
561
+ * stroke and glow colours, into a cell sized from `shapeBoundsBoxInto`. A cell
562
+ * whose longer side would exceed `SHAPE_ATLAS_MAX_CELL` is rasterized at a
563
+ * lower scale of its own so that side is exactly the cap; it still draws at
564
+ * the same size, only softer.
565
+ *
566
+ * Not for per-frame use: a spec whose parameters animate belongs on
567
+ * `drawShape`. Throws if `scale` is not a positive finite number or the
568
+ * surface cannot be created.
569
+ */
570
+ function bakeShapeAtlas(Skia, specs, scale, options = {}) {
571
+ if (!(scale > 0) || scale === 1e400) throw new Error(`bakeShapeAtlas: raster scale must be a positive finite number, got ${scale}`);
572
+ const maxWidth = options.maxWidth ?? 2048;
573
+ const padding = options.padding ?? 2;
574
+ const baked = specs.slice();
575
+ const cells = [];
576
+ const bySpec = /* @__PURE__ */ new Map();
577
+ const originX = [];
578
+ const originY = [];
579
+ const sheet = (0, _flare_engine_assets.createSpriteSheet)();
580
+ const single = [];
581
+ for (const spec of baked) {
582
+ let cell = bySpec.get(spec);
583
+ if (cell === void 0) {
584
+ single[0] = spec;
585
+ shapeBoundsBoxInto(single, BOX);
586
+ const boxW = BOX.maxX - BOX.minX;
587
+ const boxH = BOX.maxY - BOX.minY;
588
+ const cellScale = Math.min(scale, SHAPE_ATLAS_MAX_CELL / Math.max(boxW, boxH));
589
+ const w = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxW * cellScale)));
590
+ const h = Math.min(SHAPE_ATLAS_MAX_CELL, Math.max(1, Math.ceil(boxH * cellScale)));
591
+ const name = String(sheet.frames.length);
592
+ cell = {
593
+ frame: {
594
+ name,
595
+ x: 0,
596
+ y: 0,
597
+ w,
598
+ h
599
+ },
600
+ anchorX: -BOX.minX * cellScale / w,
601
+ anchorY: -BOX.minY * cellScale / h,
602
+ width: w / cellScale,
603
+ height: h / cellScale,
604
+ scale: cellScale
605
+ };
606
+ bySpec.set(spec, cell);
607
+ sheet.frameMap.set(name, sheet.frames.length);
608
+ sheet.frames.push(cell.frame);
609
+ originX.push(-BOX.minX * cellScale);
610
+ originY.push(-BOX.minY * cellScale);
611
+ }
612
+ cells.push(cell);
613
+ }
614
+ const layout = (0, _flare_engine_assets.packAtlasRows)(sheet.frames, maxWidth, padding);
615
+ const surface = Skia.Surface.Make(layout.width, layout.height);
616
+ if (surface === null) throw new Error(`bakeShapeAtlas: Skia.Surface.Make returned null for ${layout.width}x${layout.height}`);
617
+ const canvas = surface.getCanvas();
618
+ const paint = Skia.Paint();
619
+ canvas.clear(TRANSPARENT);
620
+ let i = 0;
621
+ for (const [spec, cell] of bySpec) {
622
+ const pos = layout.rects[i];
623
+ if (pos === void 0) break;
624
+ cell.frame.x = pos.x;
625
+ cell.frame.y = pos.y;
626
+ canvas.save();
627
+ canvas.translate(pos.x + (originX[i] ?? 0), pos.y + (originY[i] ?? 0));
628
+ canvas.scale(cell.scale, cell.scale);
629
+ drawShape(Skia, canvas, paint, spec, 0, 0, 0);
630
+ canvas.restore();
631
+ i++;
632
+ }
633
+ const image = surface.makeImageSnapshot();
634
+ surface.dispose?.();
635
+ return {
636
+ image,
637
+ sheet,
638
+ width: layout.width,
639
+ height: layout.height,
640
+ scale,
641
+ cells,
642
+ specs: baked,
643
+ maxWidth,
644
+ padding
645
+ };
646
+ }
647
+ /**
648
+ * True when `scale` has left the re-raster band of `atlas`: below 0.75x or
649
+ * above 1.5x of the scale it was baked at. Inside the band the baked pixels
650
+ * are close enough to draw as they are. A `scale` that is not a positive
651
+ * finite number returns false. Allocates nothing; safe to call every frame.
652
+ */
653
+ function shapeAtlasNeedsRebake(atlas, scale) {
654
+ if (!(scale > 0) || scale === 1e400) return false;
655
+ return scale < atlas.scale * BAND_MIN || scale > atlas.scale * BAND_MAX;
656
+ }
657
+ /**
658
+ * Return `atlas` itself while `scale` is inside its re-raster band (see
659
+ * `shapeAtlasNeedsRebake`), allocating nothing; otherwise bake the same specs
660
+ * with the same options at `scale` and return the new atlas. Cell indices
661
+ * carry over exactly, and so do each spec's origin and drawn size
662
+ * (`anchorX * width` is always `-minX`), so instance draws need no change. A
663
+ * cell's `width`/`height` and fractional anchors can differ by the rounding
664
+ * margin of either bake, because each cell is rounded up to whole pixels. The old
665
+ * image is not released: that is the caller's to drop.
666
+ */
667
+ function rebakeShapeAtlas(Skia, atlas, scale) {
668
+ if (!shapeAtlasNeedsRebake(atlas, scale)) return atlas;
669
+ return bakeShapeAtlas(Skia, atlas.specs, scale, {
670
+ maxWidth: atlas.maxWidth,
671
+ padding: atlas.padding
672
+ });
673
+ }
674
+ /**
675
+ * Queue one instance of baked spec `index` on `batch`, with the spec's origin
676
+ * at (`x`, `y`). `rot` is radians, clockwise, about that origin; `scale` is
677
+ * uniform and multiplies stroke and glow widths along with everything else.
678
+ * `color` is an optional packed-ARGB multiplier over the baked colours and
679
+ * `alpha` an opacity in [0, 1], both as `SpriteBatch.draw` takes them.
680
+ *
681
+ * The whole cell turns rigidly, including each part's `ox`/`oy` offset, which
682
+ * `drawShape`'s `rot` leaves unrotated. An `index` with no cell draws nothing.
683
+ * Allocates nothing.
684
+ */
685
+ function drawBakedShape(batch, atlas, index, x, y, rot = 0, scale = 1, color, alpha = 1) {
686
+ const cell = atlas.cells[index];
687
+ if (cell === void 0) return;
688
+ 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);
689
+ }
690
+ exports.SHAPE_ATLAS_MAX_CELL = SHAPE_ATLAS_MAX_CELL;
527
691
  exports.applyFriendlyGlow = applyFriendlyGlow;
528
692
  exports.applyGlow = applyGlow;
529
693
  exports.applyHazardGlow = applyHazardGlow;
530
694
  exports.argbTo = argbTo;
695
+ exports.bakeShapeAtlas = bakeShapeAtlas;
696
+ exports.drawBakedShape = drawBakedShape;
531
697
  exports.drawShape = drawShape;
532
698
  exports.friendlyGlowArgb = friendlyGlowArgb;
533
699
  exports.hazardGlowArgb = hazardGlowArgb;
700
+ exports.rebakeShapeAtlas = rebakeShapeAtlas;
701
+ exports.shapeAtlasNeedsRebake = shapeAtlasNeedsRebake;
534
702
  exports.shapeBoundsBox = shapeBoundsBox;
535
703
  exports.shapeBoundsBoxInto = shapeBoundsBoxInto;
536
704
  exports.shapeBoundsRadius = shapeBoundsRadius;