@wildshard/shard-format 0.44.0 → 0.45.0

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/shard-format",
3
- "version": "0.44.0",
3
+ "version": "0.45.0",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -159,8 +159,97 @@ function skinMaterialsFault(
159
159
  : `skin material ${graph} is a graph material: a skin part takes a family material`
160
160
  }
161
161
 
162
+ /*
163
+ * ONE-FORMAT CC51c: one species of a packed skin GLB. A cell's pack step may put several small species in one GLB,
164
+ * each under its own node; their joints, sockets and clips keep their plain names, so two species on the same skeleton
165
+ * share every name. A skin row's `rig.mesh` names its species' node, and the skin reads only that node's subtree: its
166
+ * one skinned mesh, its joints and sockets, and the clips whose every channel moves a node in it. A row without
167
+ * `rig.mesh` reads the whole file, as before.
168
+ */
169
+
170
+ /** The GLB node fields skin admission reads. */
171
+ interface PackedNode {
172
+ readonly name?: string | undefined
173
+ readonly mesh?: number | undefined
174
+ readonly skin?: number | undefined
175
+ readonly children: readonly number[]
176
+ }
177
+ /** A GLB animation as skin admission reads it: its name and the nodes its channels move. */
178
+ interface PackedClip {
179
+ readonly name?: string | undefined
180
+ readonly channels: readonly {
181
+ readonly target: { readonly node?: number | undefined }
182
+ }[]
183
+ }
184
+ /** The parts of a GLB a packed species is cut from. */
185
+ interface PackedRig {
186
+ readonly nodes: readonly PackedNode[]
187
+ readonly animations: readonly PackedClip[]
188
+ }
189
+ type Fail = (reason: string) => never
190
+
191
+ /** The one node named `mesh`. */
192
+ function packedRoot(
193
+ nodes: readonly PackedNode[],
194
+ mesh: string,
195
+ fail: Fail,
196
+ ): number {
197
+ const roots = nodes.flatMap((node, index): number[] =>
198
+ node.name === mesh ? [index] : [],
199
+ )
200
+ const [root, other] = roots
201
+ if (root === undefined || other !== undefined) {
202
+ fail(`expected one packed node ${mesh}`)
203
+ }
204
+ return root
205
+ }
206
+
207
+ /** Every node under `root`, itself included; a node reached twice refuses. */
208
+ function subtree(
209
+ nodes: readonly PackedNode[],
210
+ root: number,
211
+ fail: Fail,
212
+ ): ReadonlySet<number> {
213
+ const inside = new Set<number>()
214
+ const stack = [root]
215
+ for (let next = stack.pop(); next !== undefined; next = stack.pop()) {
216
+ if (inside.has(next)) {
217
+ fail(`packed node ${String(root)} reaches node ${String(next)} twice`)
218
+ }
219
+ inside.add(next)
220
+ stack.push(...(nodes[next]?.children ?? []))
221
+ }
222
+ return inside
223
+ }
224
+
225
+ /**
226
+ * The species `mesh` names, cut from `rig`: nodes outside its subtree keep their index but lose their name, mesh and
227
+ * skin, and clips moving any node outside it go. Without `mesh`, `rig` as it is.
228
+ */
229
+ function packedSpecies(
230
+ rig: PackedRig,
231
+ mesh: string | undefined,
232
+ fail: Fail,
233
+ ): PackedRig {
234
+ if (mesh === undefined) {
235
+ return rig
236
+ }
237
+ const inside = subtree(rig.nodes, packedRoot(rig.nodes, mesh, fail), fail)
238
+ const owns = (channel: PackedClip['channels'][number]): boolean =>
239
+ channel.target.node !== undefined && inside.has(channel.target.node)
240
+ return {
241
+ nodes: rig.nodes.map((node, index): PackedNode =>
242
+ inside.has(index) ? node : { children: [] },
243
+ ),
244
+ animations: rig.animations.filter(
245
+ (clip): boolean => clip.channels.some(owns) && clip.channels.every(owns),
246
+ ),
247
+ }
248
+ }
249
+
162
250
  export {
163
251
  SKIN_MATERIAL_PARTS,
252
+ packedSpecies,
164
253
  SkinGlbMaterialsSchema,
165
254
  SkinMaterialsSchema,
166
255
  SkinPrimitiveSchema,
@@ -168,5 +257,6 @@ export {
168
257
  skinPartsFault,
169
258
  skinPrimitiveCount,
170
259
  type SkinGlbMaterial,
260
+ type PackedRig,
171
261
  type SkinPrimitive,
172
262
  }
@@ -17,10 +17,9 @@ import type { Shardfile } from './shardSchema.ts';
17
17
  import { parseGlbData as parseGlb } from './glbCost.ts';
18
18
  import { skinLayerDecoded } from './skinLayers.ts';
19
19
  import { MaterialsSchema } from './materials.ts';
20
- import { SkinGlbMaterialsSchema, SkinPrimitiveSchema, skinMaterialsFault, skinPartsFault, skinPrimitiveCount } from './skin-materials.ts';
20
+ import { SkinGlbMaterialsSchema, SkinPrimitiveSchema, packedSpecies, skinMaterialsFault, skinPartsFault, skinPrimitiveCount } from './skin-materials.ts';
21
21
 
22
22
  import { SKIN_LOOK_RECIPE, SKIN_CLIPS_RECIPE } from './skins.ts';
23
- import type { SkinLookParameters } from './skins.ts';
24
23
  import { lookSkins, resolveSkinLookFiles } from './skin-variants.ts';
25
24
  import { skinLookParameters } from './skin-file.ts';
26
25
 
@@ -35,7 +34,7 @@ function skinLookRules(source: Pick<Shardfile, 'rows' | 'files' | 'library'>): s
35
34
  const kinds = new Map<string, string>(); for (const file of source.files) {kinds.set(file.hash, file.kind);}
36
35
  for (const row of source.rows.looks) {
37
36
  if (row.recipe !== SKIN_LOOK_RECIPE) { if (row.animation.recipe === SKIN_CLIPS_RECIPE) {errors.push('skin clips without a skin look');} continue; }
38
- let parameters: SkinLookParameters; try { parameters = skinLookParameters(row); } catch { errors.push('skin look parameters'); continue; }
37
+ let parameters: ReturnType<typeof skinLookParameters>; try { parameters = skinLookParameters(row); } catch { errors.push('skin look parameters'); continue; }
39
38
  const { skin, variants, phone } = parameters, file = files.get(skin), glb = files.get(file?.dependencies[0] ?? '');
40
39
  // OF27b: a skin file depends on its GLB, then its material's textures (KTX2 files or commons)
41
40
  const textures = file?.dependencies.slice(1) ?? [];
@@ -47,20 +46,26 @@ function skinLookRules(source: Pick<Shardfile, 'rows' | 'files' | 'library'>): s
47
46
  return errors;
48
47
  }
49
48
 
49
+ // ONE-FORMAT CC51c: a packed GLB's species is a node's subtree, and its clips the ones moving only that subtree
50
+ const nodeChildren = v.optional(v.array(v.number()), []);
51
+ const channelTarget = v.object({ node: v.optional(v.number()) });
52
+ const clipChannels = v.optional(v.array(v.object({ target: channelTarget })), []);
50
53
  const glbRigSchema = v.object({
51
- nodes: v.array(v.object({ name: v.optional(v.string()), mesh: v.optional(v.number()), skin: v.optional(v.number()) })),
54
+ nodes: v.array(v.object({ name: v.optional(v.string()), mesh: v.optional(v.number()), skin: v.optional(v.number()), children: nodeChildren })),
52
55
  meshes: v.array(v.object({ primitives: v.array(SkinPrimitiveSchema) })),
53
56
  materials: SkinGlbMaterialsSchema,
54
57
  skins: v.array(v.object({ joints: v.array(v.number()) })),
55
- animations: v.optional(v.array(v.object({ name: v.optional(v.string()) })), []),
58
+ animations: v.optional(v.array(v.object({ name: v.optional(v.string()), channels: clipChannels })), []),
56
59
  });
57
60
 
58
61
  /** Check admitted GLB joint order, sockets and clips before a renderer allocates the creature view. */
59
62
  function validateRigBytes(glb: Uint8Array, row: SkinRow, parts: Readonly<Record<string, string>> | undefined): void {
60
63
  // ParseGlb already bounds and validates the header, JSON chunk and every node/accessor reference.
61
64
  const length = new DataView(glb.buffer, glb.byteOffset, glb.byteLength).getUint32(12, true);
62
- const rig = v.parse(glbRigSchema, JSON.parse(new TextDecoder().decode(glb.subarray(20, 20 + length))));
63
65
  function fail(reason: string): never { throw new Error(`skin ${row.id}: ${reason}`); }
66
+ // ONE-FORMAT CC51c: a packed GLB's skin reads only its own species' nodes and clips (`./skin-materials`)
67
+ const parsed = v.parse(glbRigSchema, JSON.parse(new TextDecoder().decode(glb.subarray(20, 20 + length))));
68
+ const rig = { ...parsed, ...packedSpecies(parsed, row.rig.mesh, fail) };
64
69
  const named = rig.nodes.flatMap(node => node.name === undefined ? [] : [node.name]);
65
70
  const meshes = rig.nodes.filter(node => node.skin !== undefined && node.mesh !== undefined);
66
71
  const mesh = meshes[0];
@@ -25,10 +25,11 @@ const joint = v.pipe(v.string(), v.regex(/^[A-Za-z][A-Za-z0-9_.-]*$/u), v.maxLen
25
25
  const bytes = v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(25_000_000));
26
26
 
27
27
  /** One exported skin: the GLB hash and rig. Clips may be empty for a poser-driven
28
- * rig; loop bindings must still name a declared clip. */
28
+ * rig; loop bindings must still name a declared clip. `rig.mesh` (ONE-FORMAT CC51c) names the node holding this
29
+ * species in a packed GLB of several species: its skinned mesh, joints, sockets and clips are the ones under it. */
29
30
  const SkinRowSchema: v.GenericSchema<SkinRowInput, SkinRow> = v.object({
30
31
  id, file: hash, family: v.picklist(['toon', 'pbr']),
31
- rig: v.strictObject({ skeleton: v.pipe(v.string(), v.regex(/^[a-z][A-Za-z0-9.]*$/u), v.maxLength(128)), joints: v.pipe(v.array(v.pipe(v.array(joint), v.minLength(1), v.maxLength(128))), v.length(1)), clips: v.pipe(v.array(CreatureSkinClipNameSchema), v.maxLength(32)), sockets: v.pipe(v.array(joint), v.maxLength(32)) }),
32
+ rig: v.strictObject({ skeleton: v.pipe(v.string(), v.regex(/^[a-z][A-Za-z0-9.]*$/u), v.maxLength(128)), joints: v.pipe(v.array(v.pipe(v.array(joint), v.minLength(1), v.maxLength(128))), v.length(1)), clips: v.pipe(v.array(CreatureSkinClipNameSchema), v.maxLength(32)), sockets: v.pipe(v.array(joint), v.maxLength(32)), mesh: v.exactOptional(joint) }),
32
33
  cost: v.strictObject({ decoded: bytes, gpu: bytes, triangles: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(400_000)), draws: v.pipe(v.number(), v.integer(), v.minValue(0), v.maxValue(512)) }),
33
34
  });
34
35
  /** A validated exported skin row. */
@@ -83,6 +84,7 @@ interface SkinRowInputFields {
83
84
  joints: ((string)[])[];
84
85
  clips: (SkinClipNameRowsInput)[];
85
86
  sockets: (string)[];
87
+ mesh?: string;
86
88
  };
87
89
  cost: {
88
90
  decoded: number;
@@ -101,6 +103,7 @@ interface SkinRowFields {
101
103
  joints: ((string)[])[];
102
104
  clips: CreatureSkinClipName[];
103
105
  sockets: (string)[];
106
+ mesh?: string;
104
107
  };
105
108
  cost: {
106
109
  decoded: number;