@womp/spark-agent 14.0.1 → 15.0.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.
@@ -1,193 +0,0 @@
1
- import { z } from "zod";
2
- import { INTERNAL_UNITS_PER_MM } from "../coordinates.js";
3
- import { makeMaterialNode } from "./graph.js";
4
- export const MATERIAL_RECIPE_RELIEF_GUIDANCE = `Pattern-specific calibration observations (native strength, object coordinates, detail 4, height and bump tested independently):
5
- - Dual-scale grid, featureSize 1: height 0.0003–0.001; start at 0.0003.
6
- - Noise, featureSize 0.04: 0.01–0.03 produced visible fine relief.
7
- - Crossed periodic threads, featureSize 0.04: 0.003–0.03 retained rounded thread shading.
8
- - Noise, featureSize 0.08: 0.01–0.03 produced low-amplitude grain.
9
- - Fine directional grain, featureSize {x:0.015,y:0.015,z:2}: 0.0003–0.003 produced faint directional relief; 0.01 and above produced stronger ridges.
10
- These observations on curved and flat fixtures under fixed lighting are starting points, not quality targets or upper limits. Use independently weighted structural and micro height fields. Choose visible relief by its actual gradient, scale and requested finish; universal bump multipliers can destroy gloss. Compare close and grazing views without changing the scene. Height and bump strengths are not generally interchangeable.`;
11
- const positiveVector = z.object({ x: z.number().finite().positive(), y: z.number().finite().positive(), z: z.number().finite().positive() }).strict();
12
- const color = z.object({ x: z.number().min(0).max(1), y: z.number().min(0).max(1), z: z.number().min(0).max(1) }).strict();
13
- export const materialRecipeSchema = z.object({
14
- pattern: z.enum(["noise", "grain", "woven", "grid"]),
15
- coordinateSpace: z.enum(["object", "world"]),
16
- featureSize: z.union([z.number().finite().positive(), positiveVector]),
17
- colorA: color,
18
- colorB: color,
19
- roughnessA: z.number().min(0).max(1),
20
- roughnessB: z.number().min(0).max(1),
21
- metalness: z.number().min(0).max(1).optional(),
22
- detail: z.number().int().min(1).max(16).optional(),
23
- relief: z.enum(["height", "bump"]),
24
- reliefScale: z.number().finite(),
25
- }).strict();
26
- export const materialFieldSchema = materialRecipeSchema.pick({ coordinateSpace: true, featureSize: true, detail: true }).extend({
27
- pattern: z.enum(["noise", "grain", "woven", "grid", "cells"]),
28
- rotation: z.object({ x: z.number().finite(), y: z.number().finite(), z: z.number().finite() }).strict().optional(),
29
- cellMode: z.enum(["Distance", "Edge distance", "Color", "Smooth Distance", "Smooth Color"]).optional(),
30
- remap: z.object({ low: z.number().finite(), high: z.number().finite() }).strict().refine(range => range.low !== range.high, "Remap endpoints must differ").optional(),
31
- }).strict().refine(options => options.cellMode === undefined || options.pattern === "cells", "cellMode requires cells");
32
- export function materialFieldOperations(options) {
33
- return [options.coordinateSpace === "object" ? "InputPosition" : "InputWorldPosition", "Mapping", "MathLerp",
34
- ...(["noise", "grain"].includes(options.pattern) ? ["NoiseFBM"] : []),
35
- ...(options.pattern === "cells" ? ["NoiseVoronoi"] : []),
36
- ...(["woven", "grid"].includes(options.pattern) ? [options.coordinateSpace === "object" ? "InputNormal" : "InputWorldNormal", "Projection", "GetComponent"] : []),
37
- ...(["woven", "grid"].includes(options.pattern) || options.remap ? ["Math"] : [])];
38
- }
39
- export function materialRecipeOperations(options) {
40
- return [...materialFieldOperations(options), "Constant", "OutputMaterial",
41
- ...(options.relief === "height" ? ["OutputHeight"] : ["Bump", "OutputSurfaceNormal"])];
42
- }
43
- export function makeMaterialField(options, catalogue) {
44
- const graph = { nodes: [], connections: [] };
45
- const node = (op, inputs, name) => {
46
- if (!catalogue.nodes[op])
47
- throw new Error("Recipe operation unavailable: " + op);
48
- graph.nodes.push(makeMaterialNode({ op, inputs, name }));
49
- return graph.nodes.length - 1;
50
- };
51
- const connect = (source, target, port, output = 0) => {
52
- graph.connections.push({ input_node: source, input_slot: output, output_node: target, output_slot: port, flags: 0 });
53
- };
54
- const selector = (op, port, label) => {
55
- const index = catalogue.nodes[op]?.inputs[port]?.values?.indexOf(label) ?? -1;
56
- if (index < 0)
57
- throw new Error("Recipe requires " + op + " option " + label);
58
- return index;
59
- };
60
- const size = typeof options.featureSize === "number" ? { x: options.featureSize, y: options.featureSize, z: options.featureSize } : options.featureSize;
61
- const coordinateUnit = options.coordinateSpace === "world" ? 1 / INTERNAL_UNITS_PER_MM : 1;
62
- const frequency = { x: coordinateUnit / size.x, y: coordinateUnit / size.y, z: coordinateUnit / size.z };
63
- const positionOp = options.coordinateSpace === "object" ? "InputPosition" : "InputWorldPosition";
64
- const normalOp = options.coordinateSpace === "object" ? "InputNormal" : "InputWorldNormal";
65
- let position = node(positionOp, {}, options.coordinateSpace + " coordinates");
66
- if (options.pattern === "woven" || options.pattern === "grid") {
67
- let normal = node(normalOp, {}, "Matching projection normal");
68
- if (options.rotation) {
69
- const rotatePosition = node("Mapping", { 1: 0, 2: options.rotation, 3: 1 }, "Rotated position");
70
- const rotateNormal = node("Mapping", { 1: 0, 2: options.rotation, 3: 1 }, "Rotated projection normal");
71
- connect(position, rotatePosition, 0);
72
- connect(normal, rotateNormal, 0);
73
- position = rotatePosition;
74
- normal = rotateNormal;
75
- }
76
- const projected = node("Projection", { 0: selector("Projection", 0, "Cubic"), 3: 1, 4: 0, 5: 0 }, "Physical face coordinates");
77
- connect(position, projected, 1);
78
- connect(normal, projected, 2);
79
- position = projected;
80
- }
81
- const mapping = node("Mapping", { 1: 0, 2: options.pattern === "woven" || options.pattern === "grid" ? 0 : options.rotation ?? 0, 3: frequency }, "Feature size in selected coordinates");
82
- connect(position, mapping, 0);
83
- const detail = options.detail ?? 4;
84
- let pattern;
85
- if (options.pattern === "grid") {
86
- const lines = (coordinates, label) => {
87
- const fraction = node("Math", { 0: selector("Math", 0, "Fraction"), 3: 0 }, "Grid cell coordinates");
88
- connect(coordinates, fraction, 1);
89
- const edges = [0, 1].map(axis => {
90
- const component = node("GetComponent", { 1: axis }, "Grid axis");
91
- connect(fraction, component, 0);
92
- const reverse = node("Math", { 0: selector("Math", 0, "Subtract"), 1: 1, 3: 0 }, "Opposite cell edge");
93
- connect(component, reverse, 2);
94
- const edge = node("Math", { 0: selector("Math", 0, "Min"), 3: 0 }, "Nearest cell edge");
95
- connect(component, edge, 1);
96
- connect(reverse, edge, 2);
97
- return edge;
98
- });
99
- const edge = node("Math", { 0: selector("Math", 0, "Min"), 3: 0 }, "Grid line distance");
100
- connect(edges[0], edge, 1);
101
- connect(edges[1], edge, 2);
102
- const coverage = node("Math", { 0: selector("Math", 0, "Divide"), 2: 0.04, 3: 1 }, "Grid line coverage");
103
- connect(edge, coverage, 1);
104
- const mask = node("Math", { 0: selector("Math", 0, "Subtract"), 1: 1, 3: 0 }, label);
105
- connect(coverage, mask, 2);
106
- return mask;
107
- };
108
- const major = lines(mapping, "Major grid lines");
109
- const minorCoordinates = node("Mapping", { 1: 0, 2: 0, 3: 10 }, "One-tenth-unit grid coordinates");
110
- connect(mapping, minorCoordinates, 0);
111
- const minor = lines(minorCoordinates, "Minor grid lines");
112
- const minorContrast = node("Math", { 0: selector("Math", 0, "Mulitiply"), 2: 0.35, 3: 0 }, "Lower-contrast minor grid");
113
- connect(minor, minorContrast, 1);
114
- pattern = node("Math", { 0: selector("Math", 0, "Max"), 3: 0 }, "Combined two-scale grid");
115
- connect(major, pattern, 1);
116
- connect(minorContrast, pattern, 2);
117
- }
118
- else if (options.pattern === "grain") {
119
- const warp = node("NoiseFBM", { 1: 0.16, 2: 3, 3: 0.35, 4: 1 }, "Slow grain meander");
120
- connect(mapping, warp, 0);
121
- const warped = node("Mapping", { 1: 0, 2: 0, 3: 1 }, "Warped grain coordinates");
122
- connect(mapping, warped, 0);
123
- connect(warp, warped, 1);
124
- pattern = node("NoiseFBM", { 1: 1, 2: detail, 3: 1, 4: 0 }, "Directional grain");
125
- connect(warped, pattern, 0);
126
- }
127
- else if (options.pattern === "woven") {
128
- const threads = [0, 1].map(axis => {
129
- const component = node("GetComponent", { 1: axis }, axis === 0 ? "Warp axis" : "Weft axis");
130
- connect(mapping, component, 0);
131
- const phase = node("Math", { 0: selector("Math", 0, "Mulitiply"), 2: Math.PI * 2, 3: 0 }, "Thread repeat phase");
132
- connect(component, phase, 1);
133
- const sine = node("Math", { 0: selector("Math", 0, "Sine"), 3: 0 }, "Rounded thread profile");
134
- connect(phase, sine, 1);
135
- const thread = node("MathLerp", { 0: 0.5, 1: 1 }, "Positive thread height");
136
- connect(sine, thread, 2);
137
- return thread;
138
- });
139
- pattern = node("MathLerp", { 2: 0.5 }, "Crossed warp and weft");
140
- connect(threads[0], pattern, 0);
141
- connect(threads[1], pattern, 1);
142
- }
143
- else if (options.pattern === "cells") {
144
- pattern = node("NoiseVoronoi", { 1: 1, 2: selector("NoiseVoronoi", 2, options.cellMode ?? "Distance"), 3: selector("NoiseVoronoi", 3, "Euclidean") }, "Cellular structure");
145
- connect(mapping, pattern, 0);
146
- }
147
- else {
148
- pattern = node("NoiseFBM", { 1: 1, 2: detail, 3: 1, 4: 0 }, "Irregular surface structure");
149
- connect(mapping, pattern, 0);
150
- }
151
- if (options.remap) {
152
- const shifted = node("Math", { 0: selector("Math", 0, "Subtract"), 2: options.remap.low, 3: 0 }, "Mask offset");
153
- connect(pattern, shifted, 1);
154
- const normalized = node("Math", { 0: selector("Math", 0, "Divide"), 2: options.remap.high - options.remap.low, 3: 1 }, "Clamped field contrast");
155
- connect(shifted, normalized, 1);
156
- pattern = normalized;
157
- }
158
- return { graph, source: { node: pattern, slot: 0 } };
159
- }
160
- export function makeMaterialRecipe(options, catalogue) {
161
- const { graph, source } = makeMaterialField(options, catalogue);
162
- const pattern = source.node;
163
- const node = (op, inputs, name) => {
164
- if (!catalogue.nodes[op])
165
- throw new Error("Recipe operation unavailable: " + op);
166
- graph.nodes.push(makeMaterialNode({ op, inputs, name }));
167
- return graph.nodes.length - 1;
168
- };
169
- const connect = (input, output, slot, sourceSlot = 0) => graph.connections.push({ input_node: input, input_slot: sourceSlot, output_node: output, output_slot: slot, flags: 0 });
170
- const colorMix = node("MathLerp", { 0: options.colorA, 1: options.colorB }, "Structure-correlated color");
171
- connect(pattern, colorMix, 2);
172
- const roughness = node("MathLerp", { 0: options.roughnessA, 1: options.roughnessB }, "Structure-correlated roughness");
173
- connect(pattern, roughness, 2);
174
- const metal = node("Constant", { 1: options.metalness ?? 0 }, "Metalness");
175
- const output = node("OutputMaterial", {}, "Editable surface material");
176
- connect(colorMix, output, 0);
177
- connect(roughness, output, 4);
178
- connect(metal, output, 3, 1);
179
- const strength = node("Constant", { 1: options.reliefScale, 3: 3 }, "Surface relief strength");
180
- if (options.relief === "height") {
181
- const height = node("OutputHeight", {}, "Height shading");
182
- connect(pattern, height, 0);
183
- connect(strength, height, 2, 1);
184
- }
185
- else {
186
- const bump = node("Bump", {}, "Procedural bump normal");
187
- connect(pattern, bump, 0);
188
- connect(strength, bump, 1, 1);
189
- const normal = node("OutputSurfaceNormal", {}, "Tangent normal output");
190
- connect(bump, normal, 0);
191
- }
192
- return graph;
193
- }