@wildshard/shard-format 0.5.4 → 0.5.5

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.5.4",
3
+ "version": "0.5.5",
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": {
@@ -108,6 +108,7 @@
108
108
  "./detail-layer": "./src/format/detail-layer.ts",
109
109
  "./pbr-emissive": "./src/format/pbr-emissive.ts",
110
110
  "./scene-render": "./src/format/scene-render.ts",
111
+ "./look-sky": "./src/format/look-sky.ts",
111
112
  "./directorDocument": "./src/format/directorDocument.ts",
112
113
  "./directorStages": "./src/format/directorStages.ts",
113
114
  "./directorWhen": "./src/format/directorWhen.ts",
@@ -0,0 +1,165 @@
1
+ /**
2
+ * `look.sky` (meta SHARD-SOURCE AP14): a painted sky, as a material graph the engine draws on its sky dome in place of
3
+ * the two-colour gradient. The engine filters the look's environment (`look.light.environment`) from the same dome, so
4
+ * a shiny surface or a pond reflects the painted sky, not the gradient. Absent, the dome keeps its keys' zenith and
5
+ * horizon and an older shard file keeps its bytes.
6
+ *
7
+ * The graph is an unlit material (`kind: 'material'`, `model: 'unlit'`) whose `surface.colour` is the sky's linear
8
+ * colour along the dome's direction: the dome is centred on the camera, so `normalize(positionLocal)` is the view ray
9
+ * (y up). It may read params (bound to a day key or public state like any graph), time and the sky light's direction,
10
+ * but no texture, and it draws no vertex stage, alpha, outline or lighting stage. It is costed under its own budget,
11
+ * {@link LOOK_SKY_BUDGET}: the dome covers at most the screen once, like one post pass. A sky is inline graph IR
12
+ * (`{ graph }`); the SDK writes it from TSL (`world.sky(...)`).
13
+ *
14
+ * Plain data and checks, no three.js.
15
+ */
16
+ import { pipe, rawTransform, strictObject, unknown } from 'valibot'
17
+
18
+ import { isJsonData } from './json.ts'
19
+ import { validateGraph } from './materialGraph.ts'
20
+
21
+ import type { GraphBudget, GraphIr } from './materialGraph.ts'
22
+ import type { GenericSchema } from 'valibot'
23
+
24
+ /** What validation names beside the graph: the day keys and public state fields its params may bind (any when absent). */
25
+ interface LookSkyBindings {
26
+ readonly dayKeys?: readonly string[]
27
+ readonly stateFields?: readonly string[]
28
+ }
29
+ /** The bindings a shard admits, as `graphBindingSources` reads them. */
30
+ interface LookSkySources {
31
+ readonly day: ReadonlyMap<string, unknown>
32
+ readonly state: readonly string[]
33
+ }
34
+ /** A checked sky: the graph, or every reason it is not one. */
35
+ type LookSkyCheck =
36
+ | { readonly ok: true; readonly graph: GraphIr }
37
+ | { readonly ok: false; readonly errors: readonly string[] }
38
+ /** Admitted data contracts. */
39
+ type LookSkyGraphInput = unknown
40
+ type LookSkyGraph = GraphIr
41
+ interface LookSkyInput {
42
+ graph: LookSkyGraphInput
43
+ }
44
+ interface LookSky {
45
+ graph: LookSkyGraph
46
+ }
47
+
48
+ /** A sky's ceilings: a painted dome with clouds is a few hundred nodes of value noise; no samplers */
49
+ const LOOK_SKY_BUDGET: GraphBudget = Object.freeze({
50
+ nodes: 640,
51
+ samplers: 0,
52
+ instructions: 1024,
53
+ })
54
+ const NONE = 0
55
+
56
+ /** A sky is an unlit dome material: no points, no other model. */
57
+ function modelErrors(graph: GraphIr): string[] {
58
+ const errors: string[] = []
59
+ if (graph.kind !== 'material' || graph.model !== 'unlit') {
60
+ errors.push('sky: an unlit material graph (kind material, model unlit)')
61
+ }
62
+ if (graph.primitive !== undefined && graph.primitive !== 'mesh') {
63
+ errors.push('sky: drawn on the dome mesh, not points')
64
+ }
65
+ return errors
66
+ }
67
+
68
+ /** A sky draws only its surface colour. */
69
+ function stageErrors(graph: GraphIr): string[] {
70
+ const errors: string[] = [],
71
+ stages = Object.keys(graph.stages).filter(
72
+ (stage: string): boolean => stage !== 'surface',
73
+ )
74
+ if (stages.length > NONE) {
75
+ errors.push(`sky: only the surface stage (not ${stages.join(', ')})`)
76
+ }
77
+ const surface = Object.keys(graph.stages.surface ?? {})
78
+ if (!surface.includes('colour')) {
79
+ errors.push('sky: surface.colour is the sky colour')
80
+ }
81
+ const extra = surface.filter((output: string): boolean => output !== 'colour')
82
+ if (extra.length > NONE) {
83
+ errors.push(`sky: surface.colour only (not ${extra.join(', ')})`)
84
+ }
85
+ return errors
86
+ }
87
+
88
+ /** The sky graph, or every reason `ir` is not one. */
89
+ function checkLookSky(
90
+ ir: unknown,
91
+ bindings: LookSkyBindings = {},
92
+ ): LookSkyCheck {
93
+ if (!isJsonData(ir)) {
94
+ return { ok: false, errors: ['sky: plain JSON data'] }
95
+ }
96
+ const checked = validateGraph(ir, { ...bindings, budget: LOOK_SKY_BUDGET })
97
+ if (!checked.ok) {
98
+ return {
99
+ ok: false,
100
+ errors: checked.errors.map((error: string): string => `sky: ${error}`),
101
+ }
102
+ }
103
+ const errors = [...modelErrors(checked.graph), ...stageErrors(checked.graph)]
104
+ return errors.length === NONE
105
+ ? { ok: true, graph: checked.graph }
106
+ : { ok: false, errors }
107
+ }
108
+
109
+ /** Every reason `ir` is not a sky (empty: it is one). */
110
+ function lookSkyErrors(
111
+ ir: unknown,
112
+ bindings: LookSkyBindings = {},
113
+ ): readonly string[] {
114
+ const checked = checkLookSky(ir, bindings)
115
+ return checked.ok ? [] : checked.errors
116
+ }
117
+
118
+ const SkyGraphSchema: GenericSchema<LookSkyGraphInput, LookSkyGraph> = pipe(
119
+ unknown(),
120
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- valibot's transform context, which it owns
121
+ rawTransform(({ dataset, addIssue, NEVER }): GraphIr => {
122
+ const checked = checkLookSky(dataset.value)
123
+ if (!checked.ok) {
124
+ addIssue({ message: checked.errors.join('; ') })
125
+ return NEVER
126
+ }
127
+ return checked.graph
128
+ }),
129
+ )
130
+
131
+ /** `look.sky`: the dome's painted sky. */
132
+ const LookSkySchema: GenericSchema<LookSkyInput, LookSky> = strictObject({
133
+ graph: SkyGraphSchema,
134
+ })
135
+
136
+ /** The schema's rule for a sky (`look.sky?.graph`): its params bind only to the shard's day keys and public numeric state. */
137
+ function lookSkyRules(
138
+ graph: unknown,
139
+ sources: LookSkySources,
140
+ ): readonly string[] {
141
+ if (graph === undefined) {
142
+ return []
143
+ }
144
+ return lookSkyErrors(graph, {
145
+ dayKeys: [...sources.day.keys()],
146
+ stateFields: sources.state,
147
+ })
148
+ }
149
+
150
+ export {
151
+ LOOK_SKY_BUDGET,
152
+ LookSkySchema,
153
+ checkLookSky,
154
+ lookSkyErrors,
155
+ lookSkyRules,
156
+ }
157
+ export type {
158
+ LookSky,
159
+ LookSkyBindings,
160
+ LookSkyCheck,
161
+ LookSkyGraph,
162
+ LookSkyGraphInput,
163
+ LookSkyInput,
164
+ LookSkySources,
165
+ }
@@ -51,8 +51,10 @@ import { AccentSchema } from './accent.ts';
51
51
  import { MapSchema, PresentationSchema } from './presentation.ts';
52
52
  import { MigrationsSchema, migrationRules } from './migrations.ts';
53
53
  import { skinLookRules } from './skinAssets.ts';
54
- import { MaterialsSchema, FamilyLooksSchema, MaterialCurvesSchema, TextureSamplersSchema, materialExists, materialTextureRefs, materialGraphRules, materialCurveRules, textureSamplerRules } from './materials.ts';
54
+ import { MaterialsSchema, FamilyLooksSchema, MaterialCurvesSchema, TextureSamplersSchema, materialExists, materialTextureRefs, materialGraphRules, materialCurveRules, textureSamplerRules, graphBindingSources } from './materials.ts';
55
55
  import { PostStackSchema, postStackRules } from './postStack.ts';
56
+ import { LookSkySchema, lookSkyRules } from './look-sky.ts';
57
+ import type { LookSky, LookSkyInput } from './look-sky.ts';
56
58
  import { LookBloomSchema, LookLightSchema, LookToneSchema } from './look-light.ts';
57
59
  import { LookVignetteSchema } from './look-render.ts';
58
60
  import type { LookBloom, LookBloomInput, LookLight, LookLightInput, LookTone } from './look-light.ts';
@@ -76,7 +78,7 @@ const rawSchema: v.GenericSchema<ShardfileRowsInput, ShardfileRows> & { readonly
76
78
  map: v.exactOptional(MapSchema),
77
79
  requires: RequiresSchema,
78
80
  budgets: BudgetsSchema,
79
- look: v.strictObject({ families: names, materials: v.optional(MaterialsSchema, {}), familyLooks: v.optional(FamilyLooksSchema, {}), curves: v.exactOptional(MaterialCurvesSchema), samplers: v.exactOptional(TextureSamplersSchema), post: v.exactOptional(PostStackSchema), light: v.exactOptional(LookLightSchema), grade: lookGrade, clock: v.literal('engine'), day: v.optional(day), dayOverride: v.nullable(channel), keys: v.pipe(v.array(key), v.maxLength(64)) }),
81
+ look: v.strictObject({ families: names, materials: v.optional(MaterialsSchema, {}), familyLooks: v.optional(FamilyLooksSchema, {}), curves: v.exactOptional(MaterialCurvesSchema), samplers: v.exactOptional(TextureSamplersSchema), post: v.exactOptional(PostStackSchema), sky: v.exactOptional(LookSkySchema), light: v.exactOptional(LookLightSchema), grade: lookGrade, clock: v.literal('engine'), day: v.optional(day), dayOverride: v.nullable(channel), keys: v.pipe(v.array(key), v.maxLength(64)) }),
80
82
  sim: SimSchema,
81
83
  state: StateSchema,
82
84
  migrations: v.optional(MigrationsSchema, []),
@@ -185,6 +187,7 @@ export interface Shardfile {
185
187
  curves?: v.InferOutput<typeof MaterialCurvesSchema>;
186
188
  samplers?: v.InferOutput<typeof TextureSamplersSchema>;
187
189
  post?: v.InferOutput<typeof PostStackSchema>;
190
+ sky?: LookSky;
188
191
  light?: LookLight;
189
192
  grade: { exposure: number; saturation: number; contrast: number; lut: string | null; bloom?: LookBloom; tone?: LookTone; vignette?: LookVignette };
190
193
  clock: 'engine';
@@ -252,7 +255,7 @@ export function shardfileRules(s: Shardfile): string[] {
252
255
  errors.push(...stateRules(s.state));
253
256
  if ((s.clientChannels?.length ?? 0) > 0 && !pageOwner && s.runtime?.binds?.includes('clientChannels') !== true) errors.push('native visual channels require their declared page or runtime owner');
254
257
  if (s.runtime?.binds?.includes('state') === true && (s.state.player.length > 0 || s.state.shared.some((field) => field.privacy !== 'host' && !(field.privacy === 'public' && field.type !== 'string' && (s.runtime?.spawns?.bosses.length ?? 0) > 0)))) errors.push('runtime-bound state supports host fields and retained-actor numeric fields');
255
- errors.push(...materialGraphRules(s), ...materialCurveRules(s), ...textureSamplerRules(s.look, s.files), ...postStackRules(s));
258
+ errors.push(...materialGraphRules(s), ...materialCurveRules(s), ...textureSamplerRules(s.look, s.files), ...postStackRules(s), ...lookSkyRules(s.look.sky?.graph, graphBindingSources(s)));
256
259
  const materialRefs = materialTextureRefs(s.look.materials), libraryClosure = new Set<string>();
257
260
  const libraryPending = [...s.library];
258
261
  while (libraryPending.length > 0) {
@@ -392,6 +395,7 @@ interface ShardfileRowsInputFields {
392
395
  curves?: v.InferInput<typeof MaterialCurvesSchema>;
393
396
  samplers?: v.InferInput<typeof TextureSamplersSchema>;
394
397
  post?: v.InferInput<typeof PostStackSchema>;
398
+ sky?: LookSkyInput;
395
399
  light?: LookLightInput;
396
400
  grade: {
397
401
  exposure: v.InferInput<typeof finite>;
@@ -468,6 +472,7 @@ interface ShardfileRowsFields {
468
472
  curves?: v.InferOutput<typeof MaterialCurvesSchema>;
469
473
  samplers?: v.InferOutput<typeof TextureSamplersSchema>;
470
474
  post?: v.InferOutput<typeof PostStackSchema>;
475
+ sky?: LookSky;
471
476
  light?: LookLight;
472
477
  grade: {
473
478
  exposure: v.InferOutput<typeof finite>;