@wildshard/sdk 0.3.1 → 0.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/sdk",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "The author's toolbox: the `wildshard` CLI that bakes shard source into a shardfile, the author SDK, the local dev server (engine client against engine server), the explorers, arena and drafts tool.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -66,6 +66,50 @@ async function assertProductCarries(
66
66
  }
67
67
  }
68
68
 
69
+ /** The string at `keys` in parsed JSON, if there is one. */
70
+ function stringAt(value: unknown, keys: readonly string[]): string | undefined {
71
+ let at = value
72
+ for (const key of keys) {
73
+ at =
74
+ typeof at === 'object' && at !== null ? Reflect.get(at, key) : undefined
75
+ }
76
+ return typeof at === 'string' ? at : undefined
77
+ }
78
+
79
+ /**
80
+ * SA51: the slug of a product built as one output of a source that emits several (Open Plots: 17 outputs, each with its
81
+ * own card), when it differs from the source's own slug; undefined for the source's own product. Such an output never
82
+ * carries the source's hero files, so the carry check does not apply to it.
83
+ */
84
+ async function otherOutput(
85
+ product: string,
86
+ shard: string,
87
+ ): Promise<string | undefined> {
88
+ const output = stringAt(await readJson(path.join(product, 'shard.json')), [
89
+ 'identity',
90
+ 'slug',
91
+ ])
92
+ const own = stringAt(await readJson(path.join(shard, MANIFEST)), ['slug'])
93
+ return output !== undefined && own !== undefined && output !== own
94
+ ? output
95
+ : undefined
96
+ }
97
+
98
+ /** A fresh hero's line: the product carries it, unless the product is another output of its source (SA51). */
99
+ async function freshLine(
100
+ product: string,
101
+ shard: string,
102
+ hero: HeroState,
103
+ ): Promise<string> {
104
+ const captured = `hero: current (captured ${hero.captured ?? UNKNOWN_TIME})`
105
+ const output = await otherOutput(product, shard)
106
+ if (output === undefined) {
107
+ await assertProductCarries(product, shard)
108
+ return captured
109
+ }
110
+ return `${captured}; ${output} is one output of this source and wears its own card, so the source's hero is not looked for in it`
111
+ }
112
+
69
113
  /** Why a hero that is neither fresh nor generated stops the cut, and the fix. */
70
114
  function refusal(shard: string, hero: HeroState): string {
71
115
  const name = path.basename(shard)
@@ -101,8 +145,7 @@ async function assertHeroCurrent(
101
145
  }
102
146
  const hero = await heroState(shard)
103
147
  if (hero.state === 'fresh') {
104
- await assertProductCarries(product, shard)
105
- report(`hero: current (captured ${hero.captured ?? UNKNOWN_TIME})`)
148
+ report(await freshLine(product, shard, hero))
106
149
  } else if (hero.state === 'generated') {
107
150
  report(
108
151
  `hero: generated card, no captured hero; capture one: ${captureCommand(shard)}`,
@@ -240,6 +240,19 @@ function primitivesOf(merge: Merge, entry: Entry, name: string, castShadow: bool
240
240
  });
241
241
  }
242
242
 
243
+ /** A primitive's GLB bytes, near enough to batch by: 44 a vertex (position, normal, uv, colour, one more channel), 4 an index, 76 an instance. */
244
+ function glbBytes(primitive: GlbPrimitive): number {
245
+ return primitive.geometry.getAttribute('position').count * 44 + (primitive.geometry.getIndex()?.count ?? 0) * 4 + (primitive.instances?.length ?? 0) * 76;
246
+ }
247
+
248
+ /** `from`'s vertices and triangles added to `into` (AP16's panels: one merge a material across tiles). */
249
+ function appendMerge(into: Merge, from: Merge): void {
250
+ const base = into.position.length / 3;
251
+ // element by element: a spread of a large merge overflows the call stack
252
+ for (const channel of ['position', 'normal', 'uv', 'colour', 'sway', 'corner'] as const) for (const value of from[channel]) into[channel].push(value);
253
+ for (const at of from.index) into.index.push(at + base);
254
+ }
255
+
243
256
  /** AP16: what the library roots cost with their dependencies (format asset admission's `budgets.library` check). */
244
257
  function libraryCost(files: Shardfile['files'], roots: readonly string[]): { resident: number; compressed: number } {
245
258
  const byHash = new Map(files.map((f) => [f.hash, f])), seen = new Set<string>(), pending = [...roots];
@@ -520,7 +533,7 @@ export function sceneShardfile(scene: ShardScene, options: ShardfileOptions = {}
520
533
  // a GLB holds at most 25 MB (shard-format glbCost): batch the primitives into as many files as the tile needs
521
534
  const batches: GlbPrimitive[][] = []; let batch: GlbPrimitive[] = [], bytes = 0;
522
535
  for (const primitive of primitives) {
523
- const size = primitive.geometry.getAttribute('position').count * 44 + (primitive.geometry.getIndex()?.count ?? 0) * 4 + (primitive.instances?.length ?? 0) * 76;
536
+ const size = glbBytes(primitive);
524
537
  if (batch.length > 0 && bytes + size > 18_000_000) { batches.push(batch); batch = []; bytes = 0; }
525
538
  batch.push(primitive); bytes += size;
526
539
  }
@@ -550,14 +563,21 @@ export function sceneShardfile(scene: ShardScene, options: ShardfileOptions = {}
550
563
  // panels (AP16): one library GLB each, its texture slots as dependencies
551
564
  const panels = panelParts.map(({ id, merges, shadows }) => {
552
565
  const primitives: GlbPrimitive[] = [], dependencies = new Set<string>();
566
+ // the panel is one GLB: a material's triangles in every tile it crosses are one merge again, so one draw, not one a tile
567
+ const byKey = new Map<string, Merge>();
553
568
  for (const [, tile] of [...merges].sort(([a], [b]) => a.localeCompare(b))) {
554
- for (const [key, merge] of [...tile].sort(([a], [b]) => a.localeCompare(b))) {
555
- const entry = entries.get(key); if (entry === undefined || merge.position.length === 0) continue;
556
- primitives.push(...primitivesOf(merge, entry, glbNames.get(key) ?? key, shadows.get(key) ?? true));
557
- const file = entry.material.map === null ? undefined : textureFiles.get(entry.material.map); if (file !== undefined) dependencies.add(file.hash);
558
- }
569
+ for (const [key, merge] of [...tile].sort(([a], [b]) => a.localeCompare(b))) { const whole = byKey.get(key); if (whole === undefined) byKey.set(key, merge); else appendMerge(whole, merge); }
570
+ }
571
+ for (const [key, merge] of [...byKey].sort(([a], [b]) => a.localeCompare(b))) {
572
+ const entry = entries.get(key); if (entry === undefined || merge.position.length === 0) continue;
573
+ primitives.push(...primitivesOf(merge, entry, glbNames.get(key) ?? key, shadows.get(key) ?? true));
574
+ const file = entry.material.map === null ? undefined : textureFiles.get(entry.material.map); if (file !== undefined) dependencies.add(file.hash);
559
575
  }
560
576
  if (primitives.length === 0) throw new SceneRefusalError(id, 'a panel part draws nothing', 'Give the panel part a visible mesh, or drop its panel option');
577
+ // a panel is one library file (format props `panels[].file`), and a GLB holds at most 25 MB: the tiles' 18 MB batch is
578
+ // not open to it, so a panel past that is refused here, by name, not at admission
579
+ const size = primitives.reduce((n, primitive) => n + glbBytes(primitive), 0);
580
+ if (size > 18_000_000) throw new SceneRefusalError(id, `a panel part's mesh is ${(size / 1e6).toFixed(1)} MB, over the 18 MB a panel GLB holds`, 'Split it into smaller panel parts, or drop detail from its meshes');
561
581
  return { id, file: store(staticGlb(primitives, `scene.panel.${id}`), 'glb', [...dependencies].sort()).hash, visible: true };
562
582
  });
563
583
  const library = panels.map((panel) => panel.file);
@@ -15,8 +15,8 @@
15
15
  "share": 0.00951
16
16
  },
17
17
  "pond": {
18
- "mean": 3.268,
19
- "share": 0.04278
18
+ "mean": 3.499,
19
+ "share": 0.0484
20
20
  }
21
21
  }
22
22
  }