@vim3d/core 0.2.0 → 0.2.1
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 +6 -2
- package/compile.js +195 -23
- package/contexts.d.ts +55 -0
- package/contexts.js +310 -0
- package/esm/compile.js +195 -23
- package/esm/contexts.js +304 -0
- package/esm/evaluate.js +56 -7
- package/esm/graph.js +27 -5
- package/esm/index.js +1 -0
- package/esm/registry.js +93 -0
- package/evaluate.d.ts +2 -2
- package/evaluate.js +56 -7
- package/graph.d.ts +3 -0
- package/graph.js +27 -5
- package/index.d.ts +3 -1
- package/index.js +4 -1
- package/model.d.ts +6 -0
- package/package.json +2 -2
- package/registry.d.ts +14 -0
- package/registry.js +98 -0
- package/types.d.ts +72 -3
package/esm/contexts.js
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import { validSegment } from './channels.js';
|
|
2
|
+
import { comparePaths } from './graph.js';
|
|
3
|
+
import { evaluateExpression } from './expression.js';
|
|
4
|
+
import { allowedSource } from './registry.js';
|
|
5
|
+
import { EvaluationError, fail, freeze, } from './types.js';
|
|
6
|
+
const nodeType = /^[a-zA-Z][a-zA-Z0-9_-]*:[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
7
|
+
const propTypes = new Set(['string', 'number', 'boolean', 'object']);
|
|
8
|
+
const portTypes = new Set([...propTypes, 'vector3', 'matrix4']);
|
|
9
|
+
function isRecord(value) {
|
|
10
|
+
return value !== null && typeof value === 'object' &&
|
|
11
|
+
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
12
|
+
}
|
|
13
|
+
function matches(value, type) {
|
|
14
|
+
if (type === 'object')
|
|
15
|
+
return isRecord(value) || Array.isArray(value);
|
|
16
|
+
if (type === 'number')
|
|
17
|
+
return typeof value === 'number' && Number.isFinite(value);
|
|
18
|
+
if (type === 'vector3' || type === 'matrix4') {
|
|
19
|
+
return Array.isArray(value) && value.length === (type === 'vector3' ? 3 : 16) &&
|
|
20
|
+
Array.from(value).every(item => typeof item === 'number' && Number.isFinite(item));
|
|
21
|
+
}
|
|
22
|
+
return typeof value === type;
|
|
23
|
+
}
|
|
24
|
+
function copyValue(value, invalid, seen = new Set()) {
|
|
25
|
+
if (value === null)
|
|
26
|
+
return null;
|
|
27
|
+
if (typeof value === 'string' || typeof value === 'boolean' ||
|
|
28
|
+
(typeof value === 'number' && Number.isFinite(value)))
|
|
29
|
+
return value;
|
|
30
|
+
if ((!Array.isArray(value) && !isRecord(value)) || seen.has(value))
|
|
31
|
+
return invalid();
|
|
32
|
+
seen.add(value);
|
|
33
|
+
const copy = Array.isArray(value) ? Array.from(value, item => copyValue(item, invalid, seen))
|
|
34
|
+
: Object.fromEntries(Object.entries(value).map(([key, item]) => [key, copyValue(item, invalid, seen)]));
|
|
35
|
+
seen.delete(value);
|
|
36
|
+
return freeze(copy);
|
|
37
|
+
}
|
|
38
|
+
function definitions(entries, types, kind, owner) {
|
|
39
|
+
if (!Array.isArray(entries))
|
|
40
|
+
fail('invalid-extension-definition', 'Expected ' + kind + ' definitions for ' + owner);
|
|
41
|
+
const map = new Map();
|
|
42
|
+
for (const entry of entries) {
|
|
43
|
+
if (!entry || typeof entry.name !== 'string' || !validSegment(entry.name) || !types.has(entry.type) || map.has(entry.name)) {
|
|
44
|
+
fail('invalid-extension-definition', 'Invalid or duplicate ' + kind + ' on ' + owner + ': ' + entry?.name);
|
|
45
|
+
}
|
|
46
|
+
map.set(entry.name, { ...entry });
|
|
47
|
+
}
|
|
48
|
+
return map;
|
|
49
|
+
}
|
|
50
|
+
export class Extensions {
|
|
51
|
+
registry;
|
|
52
|
+
types = new Map();
|
|
53
|
+
nodes = new Map();
|
|
54
|
+
constructor(registry) {
|
|
55
|
+
this.registry = registry;
|
|
56
|
+
for (const context of registry.contexts) {
|
|
57
|
+
const { name, stage, nodeTypes } = context;
|
|
58
|
+
if (stage === 'obj' || stage === 'ch' || stage === 'reserved')
|
|
59
|
+
continue;
|
|
60
|
+
const types = new Map();
|
|
61
|
+
for (const definition of nodeTypes) {
|
|
62
|
+
const { type, evaluate } = definition;
|
|
63
|
+
if (!nodeType.test(type) || typeof evaluate !== 'function')
|
|
64
|
+
fail('invalid-extension-definition', 'Invalid node type: ' + type);
|
|
65
|
+
if (types.has(type))
|
|
66
|
+
fail('duplicate-extension-type', 'Duplicate node type in /' + name + ': ' + type);
|
|
67
|
+
const owner = '/' + name + ' ' + type;
|
|
68
|
+
const inputs = definitions(definition.inputs, portTypes, 'input', owner);
|
|
69
|
+
const outputs = definitions(definition.outputs, portTypes, 'output', owner);
|
|
70
|
+
const props = definitions(definition.props, propTypes, 'prop', owner);
|
|
71
|
+
for (const [key, prop] of props) {
|
|
72
|
+
const invalid = () => fail('invalid-extension-definition', 'Invalid prop definition on ' + owner + ': ' + key);
|
|
73
|
+
if (prop.required !== undefined && typeof prop.required !== 'boolean')
|
|
74
|
+
invalid();
|
|
75
|
+
if (prop.default !== undefined) {
|
|
76
|
+
if (!matches(prop.default, prop.type))
|
|
77
|
+
invalid();
|
|
78
|
+
props.set(key, { ...prop, default: copyValue(prop.default, invalid) });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
types.set(type, { type, inputs, outputs, props, evaluate });
|
|
82
|
+
}
|
|
83
|
+
this.types.set(name, types);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
add(path, type, source) {
|
|
87
|
+
const [root, context, name, ...extra] = path.split('/');
|
|
88
|
+
if (root || !context || !validSegment(name) || extra.length)
|
|
89
|
+
fail('invalid-path', 'Expected an extension node path: ' + path, source);
|
|
90
|
+
const registered = this.registry.get(context);
|
|
91
|
+
if (!registered)
|
|
92
|
+
fail('unknown-extension-context', 'Unknown extension context: /' + context, source);
|
|
93
|
+
const definition = this.types.get(context)?.get(type);
|
|
94
|
+
if (!definition)
|
|
95
|
+
fail('unknown-extension-type', 'Unknown node type in /' + context + ': ' + type, source);
|
|
96
|
+
if (this.nodes.has(path))
|
|
97
|
+
fail('duplicate-name', 'Name is already used: ' + path, source);
|
|
98
|
+
const props = new Map();
|
|
99
|
+
for (const [key, def] of definition.props)
|
|
100
|
+
if (def.default !== undefined)
|
|
101
|
+
props.set(key, def.default);
|
|
102
|
+
const node = { path, context, stage: registered.stage, definition, source, props, drivers: new Map() };
|
|
103
|
+
this.nodes.set(path, node);
|
|
104
|
+
return node;
|
|
105
|
+
}
|
|
106
|
+
property(address, args, text, source) {
|
|
107
|
+
const { node, key } = this.member(address, source);
|
|
108
|
+
const def = node.definition.props.get(key);
|
|
109
|
+
if (!def)
|
|
110
|
+
fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
|
|
111
|
+
const invalid = () => fail('invalid-extension-prop', 'Expected ' + def.type + ' for ' + address, source);
|
|
112
|
+
let value = args[0];
|
|
113
|
+
if (def.type === 'object') {
|
|
114
|
+
const raw = text?.trimEnd().match(/^[ \t:]*(?:set|"set")[ \t]+(?:"[^"]*"|[^ \t]+)[ \t]+(.*)$/)?.[1];
|
|
115
|
+
const literal = raw?.match(/^(?:"(?:\\.|[^"\\])*"|[^"#])*/)?.[0];
|
|
116
|
+
try {
|
|
117
|
+
value = JSON.parse(literal);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
invalid();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
if (args.length !== 1)
|
|
125
|
+
fail('invalid-operands', 'Expected one value for ' + address, source);
|
|
126
|
+
if (def.type === 'number')
|
|
127
|
+
value = args[0].trim() === '' ? NaN : Number(args[0]);
|
|
128
|
+
if (def.type === 'boolean')
|
|
129
|
+
value = args[0] === 'true' ? true : args[0] === 'false' ? false : undefined;
|
|
130
|
+
}
|
|
131
|
+
if (!matches(value, def.type))
|
|
132
|
+
invalid();
|
|
133
|
+
node.props.set(key, copyValue(value, invalid));
|
|
134
|
+
}
|
|
135
|
+
input(address, type, driver, source) {
|
|
136
|
+
const { node, key } = this.member(address, source);
|
|
137
|
+
const port = node.definition.inputs.get(key);
|
|
138
|
+
if (!port)
|
|
139
|
+
fail('unknown-extension-port', 'Unknown input port: ' + address, source);
|
|
140
|
+
if (port.type !== type)
|
|
141
|
+
fail('extension-port-type', 'Expected ' + port.type + ' at ' + address + ', got ' + type, source);
|
|
142
|
+
for (const from of driver.kind === 'wire' ? [driver.from] : driver.references) {
|
|
143
|
+
if (!allowedSource(node.stage, from.stage))
|
|
144
|
+
fail('extension-stage', address + ' cannot read ' + from.address, source);
|
|
145
|
+
}
|
|
146
|
+
if (node.drivers.has(key))
|
|
147
|
+
fail('overlapping-drivers', 'Input already has a driver: ' + address, source);
|
|
148
|
+
node.drivers.set(key, driver);
|
|
149
|
+
}
|
|
150
|
+
output(address, source) {
|
|
151
|
+
const { node, key } = this.member(address, source);
|
|
152
|
+
if (node.stage === 'output')
|
|
153
|
+
fail('extension-stage', 'Cannot read an output-stage node: ' + node.path, source);
|
|
154
|
+
const port = node.definition.outputs.get(key);
|
|
155
|
+
if (!port)
|
|
156
|
+
fail('unknown-extension-port', 'Unknown output port: ' + address, source);
|
|
157
|
+
return { address, path: node.path, port: key, type: port.type, stage: node.stage };
|
|
158
|
+
}
|
|
159
|
+
member(address, source) {
|
|
160
|
+
const [root, context, name, key, ...extra] = address.split('/');
|
|
161
|
+
if (root || !context || !name || !key || extra.length)
|
|
162
|
+
fail('invalid-path', 'Expected an extension member path: ' + address, source);
|
|
163
|
+
if (!this.registry.get(context))
|
|
164
|
+
fail('unknown-extension-context', 'Unknown extension context: /' + context, source);
|
|
165
|
+
const path = '/' + context + '/' + name;
|
|
166
|
+
const node = this.nodes.get(path);
|
|
167
|
+
if (!node)
|
|
168
|
+
fail('missing-target', 'Unknown extension node: ' + path, source);
|
|
169
|
+
return { node, key };
|
|
170
|
+
}
|
|
171
|
+
addToGraph(graph) {
|
|
172
|
+
for (const node of this.nodes.values()) {
|
|
173
|
+
const { definition, path, context, source } = node;
|
|
174
|
+
for (const [name, prop] of definition.props) {
|
|
175
|
+
if (prop.required && !node.props.has(name))
|
|
176
|
+
fail('missing-extension-prop', 'Missing required prop: ' + path + '/' + name, source);
|
|
177
|
+
}
|
|
178
|
+
const props = [...node.props.keys()].sort(comparePaths).map(name => ({
|
|
179
|
+
name, type: definition.props.get(name).type, value: node.props.get(name),
|
|
180
|
+
}));
|
|
181
|
+
for (const input of [...node.drivers.keys()].sort(comparePaths)) {
|
|
182
|
+
const driver = node.drivers.get(input);
|
|
183
|
+
if (driver.kind === 'expr') {
|
|
184
|
+
props.push({ name: 'expr/' + input, type: 'object', value: driver.expression });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
graph.addNode(path, { context, type: definition.type, props,
|
|
188
|
+
inputs: [...definition.inputs.values()], outputs: [...definition.outputs.values()] }, source);
|
|
189
|
+
}
|
|
190
|
+
for (const { path, drivers } of this.nodes.values()) {
|
|
191
|
+
for (const [input, driver] of drivers) {
|
|
192
|
+
for (const from of driver.kind === 'wire' ? [driver.from] : driver.references) {
|
|
193
|
+
graph.expose(from.path, { name: from.port, type: from.type });
|
|
194
|
+
graph.wire(path, input, from.path, from.port);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
export function checkAudioFrame(path, sampleFrame, frame) {
|
|
201
|
+
if (Math.abs(sampleFrame - frame) > 1e-9) {
|
|
202
|
+
fail('off-time-audio-reference', 'Audio features are available only at the evaluated frame: ' + path);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
export function createContextEvaluation(extensions, order, frame, time, fps, frameCount, options, readBuiltIn) {
|
|
206
|
+
const used = new Set([...extensions.nodes.values()].map(node => node.context));
|
|
207
|
+
const contextNames = extensions.registry.contexts.filter(({ stage, name }) => stage === 'evaluate' || stage === 'output' || used.has(name)).map(({ name }) => name).sort(comparePaths);
|
|
208
|
+
let base;
|
|
209
|
+
let objects = new Map();
|
|
210
|
+
const values = new Map();
|
|
211
|
+
const snapshots = new Map();
|
|
212
|
+
const sorted = [...extensions.nodes.keys()].sort(comparePaths);
|
|
213
|
+
const read = (path, sampleFrame) => {
|
|
214
|
+
if (path.startsWith('/ch/'))
|
|
215
|
+
return readBuiltIn(path, sampleFrame);
|
|
216
|
+
if (path.startsWith('/obj/')) {
|
|
217
|
+
const end = path.lastIndexOf('/');
|
|
218
|
+
const object = objects.get(path.slice(0, end));
|
|
219
|
+
const key = path.slice(end + 1);
|
|
220
|
+
if (key === 'snapshot')
|
|
221
|
+
return copyValue(object, () => fail('invalid-extension-input', 'Invalid object snapshot: ' + path));
|
|
222
|
+
if (key === 'worldMatrix' || key === 'localMatrix')
|
|
223
|
+
return object[key];
|
|
224
|
+
return object.parameters[key];
|
|
225
|
+
}
|
|
226
|
+
if (path.startsWith('/audio/'))
|
|
227
|
+
checkAudioFrame(path, sampleFrame, frame);
|
|
228
|
+
return values.get(path);
|
|
229
|
+
};
|
|
230
|
+
const snapshot = () => {
|
|
231
|
+
if (!contextNames.length)
|
|
232
|
+
return base;
|
|
233
|
+
const contexts = Object.fromEntries(contextNames.map(name => [name, []]));
|
|
234
|
+
for (const path of sorted) {
|
|
235
|
+
const node = snapshots.get(path);
|
|
236
|
+
if (node)
|
|
237
|
+
contexts[node.context].push(node);
|
|
238
|
+
}
|
|
239
|
+
return freeze({ ...base, contexts });
|
|
240
|
+
};
|
|
241
|
+
function run(stage) {
|
|
242
|
+
const inputSnapshot = stage === 'output' ? snapshot() : undefined;
|
|
243
|
+
for (const path of order) {
|
|
244
|
+
const node = extensions.nodes.get(path);
|
|
245
|
+
if (!node || node.stage !== stage)
|
|
246
|
+
continue;
|
|
247
|
+
const { definition, source, context } = node;
|
|
248
|
+
const inputs = freeze(Object.fromEntries([...node.drivers].map(([key, driver]) => [
|
|
249
|
+
key, driver.kind === 'wire' ? read(driver.from.address, frame) : evaluateExpression(driver.expression, {
|
|
250
|
+
frame, fps, frameCount, source: driver.source, read: (address, sampleFrame) => read(address, sampleFrame),
|
|
251
|
+
}),
|
|
252
|
+
])));
|
|
253
|
+
const props = freeze(Object.fromEntries(node.props));
|
|
254
|
+
const evaluate = definition.evaluate;
|
|
255
|
+
const audio = options.audio;
|
|
256
|
+
const name = path.slice('/audio/'.length);
|
|
257
|
+
const data = stage === 'audio' && audio
|
|
258
|
+
? Object.hasOwn(audio, path) ? audio[path] : Object.hasOwn(audio, name) ? audio[name] : undefined
|
|
259
|
+
: undefined;
|
|
260
|
+
const ctx = freeze({ path, frame, time, fps, inputs, props, ...(inputSnapshot && { snapshot: inputSnapshot }) });
|
|
261
|
+
let result;
|
|
262
|
+
try {
|
|
263
|
+
result = stage === 'audio' ? evaluate(ctx, data) : evaluate(ctx);
|
|
264
|
+
}
|
|
265
|
+
catch (error) {
|
|
266
|
+
if (error instanceof EvaluationError || (stage !== 'evaluate' && stage !== 'output'))
|
|
267
|
+
throw error;
|
|
268
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
269
|
+
fail('plugin-error', `${path}: ${message}`, source);
|
|
270
|
+
}
|
|
271
|
+
const returned = result === undefined ? {} : result;
|
|
272
|
+
if (!isRecord(returned))
|
|
273
|
+
fail('invalid-extension-output', 'Expected an output record from ' + path, source);
|
|
274
|
+
for (const name of Object.keys(returned)) {
|
|
275
|
+
if (!definition.outputs.has(name))
|
|
276
|
+
fail('invalid-extension-output', 'Undeclared output: ' + path + '/' + name, source);
|
|
277
|
+
}
|
|
278
|
+
const outputs = Object.fromEntries([...definition.outputs].map(([name, { type }]) => {
|
|
279
|
+
const address = path + '/' + name;
|
|
280
|
+
const value = Object.hasOwn(returned, name) ? returned[name] : undefined;
|
|
281
|
+
const invalid = () => fail('invalid-extension-output', 'Expected ' + type + ' output at ' + address, source);
|
|
282
|
+
if (type === 'number' && typeof value === 'number' && !Number.isFinite(value)) {
|
|
283
|
+
fail('non-finite', address + ' must be finite', source);
|
|
284
|
+
}
|
|
285
|
+
if (!matches(value, type))
|
|
286
|
+
invalid();
|
|
287
|
+
const output = copyValue(value, invalid);
|
|
288
|
+
values.set(address, output);
|
|
289
|
+
return [name, output];
|
|
290
|
+
}));
|
|
291
|
+
snapshots.set(path, freeze({ path, context, type: definition.type, stage, props, inputs, outputs }));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
run, read,
|
|
296
|
+
finish(snapshotBase) {
|
|
297
|
+
base = snapshotBase;
|
|
298
|
+
objects = new Map(base.objects.map(object => [object.path, object]));
|
|
299
|
+
run('evaluate');
|
|
300
|
+
run('output');
|
|
301
|
+
return snapshot();
|
|
302
|
+
},
|
|
303
|
+
};
|
|
304
|
+
}
|
package/esm/evaluate.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import { sample } from './channels.js';
|
|
2
|
+
import { checkAudioFrame, createContextEvaluation } from './contexts.js';
|
|
2
3
|
import { evaluateExpression } from './expression.js';
|
|
3
4
|
import { add, aim, column, euler, identity, inverseDirection, matrix, multiply, normalize, point, scale, sub, worldUp, wrap } from './math.js';
|
|
4
5
|
import { active, channelPath, copyState, isColor, objectPath, plans, positionStep, rotations, scales, translations, vector, worldStep } from './model.js';
|
|
5
6
|
import { EvaluationError, fail, finite, freeze } from './types.js';
|
|
6
|
-
export function evaluateFrame(scene, frame) {
|
|
7
|
+
export function evaluateFrame(scene, frame, options = {}) {
|
|
7
8
|
try {
|
|
8
9
|
if (!Number.isSafeInteger(frame) || frame < 0 || (scene.frameCount !== undefined && frame >= scene.frameCount)) {
|
|
9
10
|
fail('invalid-frame', 'Frame is outside the scene: ' + frame);
|
|
10
11
|
}
|
|
11
|
-
return { value: evaluate(scene, frame) };
|
|
12
|
+
return { value: evaluate(scene, frame, options) };
|
|
12
13
|
}
|
|
13
14
|
catch (error) {
|
|
14
15
|
if (error instanceof EvaluationError)
|
|
@@ -16,10 +17,39 @@ export function evaluateFrame(scene, frame) {
|
|
|
16
17
|
throw error;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
function
|
|
20
|
+
function isLiveSample(input) {
|
|
21
|
+
return typeof input === 'object' && input !== null && 'values' in input
|
|
22
|
+
&& typeof input.values === 'object' && input.values !== null;
|
|
23
|
+
}
|
|
24
|
+
function readLivePort(options, channel, portName, path, source) {
|
|
25
|
+
const input = options?.liveChannels?.[channel.name] ?? options?.liveChannels?.[channelPath(channel)];
|
|
26
|
+
if (!input || typeof input !== 'object') {
|
|
27
|
+
fail('missing-live-channel', 'Missing live channel input for ' + path, source);
|
|
28
|
+
}
|
|
29
|
+
let values;
|
|
30
|
+
if (isLiveSample(input)) {
|
|
31
|
+
if (input.timestamp !== undefined) {
|
|
32
|
+
finite(input.timestamp, 'Live channel timestamp for ' + channelPath(channel), source);
|
|
33
|
+
}
|
|
34
|
+
values = input.values;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
values = input;
|
|
38
|
+
}
|
|
39
|
+
const value = values[portName];
|
|
40
|
+
if (value === undefined)
|
|
41
|
+
fail('missing-live-channel', 'Missing live channel value for ' + path, source);
|
|
42
|
+
if (typeof value !== 'number')
|
|
43
|
+
fail('non-finite', path + ' must be finite', source);
|
|
44
|
+
return finite(value, path, source);
|
|
45
|
+
}
|
|
46
|
+
function evaluate(scene, frame, options) {
|
|
20
47
|
const plan = plans.get(scene);
|
|
21
48
|
if (!plan)
|
|
22
49
|
fail('invalid-scene', 'Scene was not created by compileScene');
|
|
50
|
+
if (options?.deterministic && scene.liveChannels.length) {
|
|
51
|
+
fail('non-deterministic-live-channel', 'Deterministic evaluation cannot run a scene with live channels');
|
|
52
|
+
}
|
|
23
53
|
const { fps, frameCount } = scene;
|
|
24
54
|
const time = finite(frame / fps, 'Scene time');
|
|
25
55
|
const state = new Map(plan.objects.map(object => [object, copyState(object.authored)]));
|
|
@@ -41,28 +71,45 @@ function evaluate(scene, frame) {
|
|
|
41
71
|
const outputs = new Map();
|
|
42
72
|
for (const channel of plan.channels) {
|
|
43
73
|
for (const [name, port] of channel.ports) {
|
|
44
|
-
outputs.set(channelPath(channel) + '/' + name, { channel, port, cache: new Map() });
|
|
74
|
+
outputs.set(channelPath(channel) + '/' + name, { channel, name, port, cache: new Map() });
|
|
45
75
|
}
|
|
46
76
|
}
|
|
77
|
+
const contexts = createContextEvaluation(plan.extensions, plan.order, frame, time, fps, frameCount, options, read);
|
|
47
78
|
const expr = (ast, q, source) => evaluateExpression(ast, { frame: q, fps, frameCount, source, read });
|
|
48
79
|
function read(path, q) {
|
|
80
|
+
if (path.startsWith('/audio/'))
|
|
81
|
+
return contexts.read(path, q);
|
|
49
82
|
if (path.startsWith('/obj/')) {
|
|
50
83
|
const { object, name } = parameters.get(path);
|
|
51
84
|
return state.get(object).parameters[name];
|
|
52
85
|
}
|
|
53
|
-
|
|
86
|
+
if (plan.audioChannels.has(path)) {
|
|
87
|
+
checkAudioFrame(path, q, frame);
|
|
88
|
+
q = frame;
|
|
89
|
+
}
|
|
90
|
+
const { channel, name, port, cache } = outputs.get(path);
|
|
54
91
|
const value = cache.get(q);
|
|
55
92
|
if (value !== undefined)
|
|
56
93
|
return value;
|
|
57
|
-
|
|
94
|
+
let result;
|
|
95
|
+
if (port.live) {
|
|
96
|
+
if (q !== frame)
|
|
97
|
+
fail('off-time-live-channel', 'Live channel does not support off-time reads: ' + path, port.source);
|
|
98
|
+
result = readLivePort(options, channel, name, path, port.source);
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
result = channel.table ? sample(channel.table, port, q, fps) : expr(port.expression, q, port.source);
|
|
102
|
+
}
|
|
58
103
|
cache.set(q, result);
|
|
59
104
|
return result;
|
|
60
105
|
}
|
|
61
106
|
const channels = Object.create(null);
|
|
107
|
+
contexts.run('audio');
|
|
62
108
|
for (const path of plan.order) {
|
|
63
109
|
if (outputs.has(path))
|
|
64
110
|
channels[path] = read(path, frame);
|
|
65
111
|
}
|
|
112
|
+
contexts.run('geo');
|
|
66
113
|
for (const path of plan.order) {
|
|
67
114
|
const target = parameters.get(path);
|
|
68
115
|
if (!target)
|
|
@@ -144,6 +191,7 @@ function evaluate(scene, frame) {
|
|
|
144
191
|
path: objectPath(object), kind: object.kind, ...(object.parent && { parent: objectPath(object.parent) }),
|
|
145
192
|
localMatrix, worldMatrix, parameters: p, visible: authored.visible,
|
|
146
193
|
...(object.geometry && { geometry: object.geometry }),
|
|
194
|
+
...(object.geometrySource && { proceduralGeometry: contexts.read(object.geometrySource, frame) }),
|
|
147
195
|
...(Object.hasOwn(p, 'diffr') && { material: {
|
|
148
196
|
diffuse: [p.diffr, p.diffg, p.diffb, p.diffa], ambient: [p.ambr, p.ambg, p.ambb, p.amba],
|
|
149
197
|
specular: [p.specr, p.specg, p.specb, p.speca], ...(p.shine !== undefined && { shininess: p.shine }),
|
|
@@ -159,6 +207,7 @@ function evaluate(scene, frame) {
|
|
|
159
207
|
};
|
|
160
208
|
return snapshot;
|
|
161
209
|
});
|
|
162
|
-
|
|
210
|
+
const snapshot = freeze({ frame, time, fps, objects, channels, cameras: objects.filter(o => o.kind === 'camera'),
|
|
163
211
|
lights: objects.filter(o => o.kind === 'light') });
|
|
212
|
+
return contexts.finish(snapshot);
|
|
164
213
|
}
|
package/esm/graph.js
CHANGED
|
@@ -15,12 +15,25 @@ export class GraphBuilder {
|
|
|
15
15
|
steps = new Map();
|
|
16
16
|
add(path, type, portType, props, source) {
|
|
17
17
|
const context = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
18
|
-
this.
|
|
19
|
-
|
|
18
|
+
this.addNode(path, { context, type, props, inputs: [], outputs: [{ name: 'value', type: portType }] }, source);
|
|
19
|
+
}
|
|
20
|
+
addNode(path, node, source) {
|
|
21
|
+
this.steps.set(path, { node: { ...node, name: graphName(path) }, dependencies: new Set(), wires: [], source });
|
|
20
22
|
}
|
|
21
23
|
depend(to, on) {
|
|
22
24
|
this.steps.get(to).dependencies.add(on);
|
|
23
25
|
}
|
|
26
|
+
wire(to, input, from, output) {
|
|
27
|
+
const step = this.steps.get(to);
|
|
28
|
+
step.dependencies.add(from);
|
|
29
|
+
step.wires.push({ path: from, output, input });
|
|
30
|
+
}
|
|
31
|
+
expose(path, port) {
|
|
32
|
+
const step = this.steps.get(path);
|
|
33
|
+
if (!step.node.outputs.some(output => output.name === port.name)) {
|
|
34
|
+
step.node = { ...step.node, outputs: [...step.node.outputs, port] };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
24
37
|
finish() {
|
|
25
38
|
const paths = [...this.steps.keys()].sort(comparePaths);
|
|
26
39
|
const counts = new Map();
|
|
@@ -69,14 +82,23 @@ export class GraphBuilder {
|
|
|
69
82
|
}
|
|
70
83
|
const edges = [];
|
|
71
84
|
const nodes = paths.map(path => {
|
|
72
|
-
const { node, dependencies } = this.steps.get(path);
|
|
73
|
-
const
|
|
85
|
+
const { node, dependencies, wires } = this.steps.get(path);
|
|
86
|
+
const typedInputs = wires.sort((a, b) => comparePaths(a.input, b.input) || comparePaths(a.path, b.path) || comparePaths(a.output, b.output)).map(({ path: from, output, input }) => {
|
|
87
|
+
const source = this.steps.get(from).node;
|
|
88
|
+
edges.push({ src: { node: source.name, port: output }, dst: { node: node.name, port: input } });
|
|
89
|
+
return { name: input, type: source.outputs.find(port => port.name === output).type };
|
|
90
|
+
});
|
|
91
|
+
if (node.context !== 'obj' && node.context !== 'ch') {
|
|
92
|
+
return node;
|
|
93
|
+
}
|
|
94
|
+
const wired = new Set(wires.map(wire => wire.path));
|
|
95
|
+
const inputs = [...dependencies].filter(dep => !wired.has(dep)).sort(comparePaths).map(dep => {
|
|
74
96
|
const source = this.steps.get(dep).node;
|
|
75
97
|
const output = source.outputs[0];
|
|
76
98
|
edges.push({ src: { node: source.name, port: output.name }, dst: { node: node.name, port: source.name } });
|
|
77
99
|
return { name: source.name, type: output.type };
|
|
78
100
|
});
|
|
79
|
-
return { ...node, inputs };
|
|
101
|
+
return { ...node, inputs: [...inputs, ...typedInputs] };
|
|
80
102
|
});
|
|
81
103
|
return { order, graph: { name: 'scene', nodes, edges } };
|
|
82
104
|
}
|
package/esm/index.js
CHANGED
package/esm/registry.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { fail, finite, freeze, } from './types.js';
|
|
2
|
+
const ports = (type, ...names) => names.map(name => ({ name, type }));
|
|
3
|
+
const numeric = (defaults) => Object.entries(defaults)
|
|
4
|
+
.map(([name, value]) => ({ name, type: 'number', default: value }));
|
|
5
|
+
const audioSource = (type) => ({
|
|
6
|
+
type: 'audio:' + type,
|
|
7
|
+
inputs: [],
|
|
8
|
+
outputs: [...ports('object', 'signal'), ...ports('number', 'rms', 'peak')],
|
|
9
|
+
props: type === 'file' ? [{ name: 'asset', type: 'string', required: true }, ...numeric({ gain: 1 })]
|
|
10
|
+
: [{ name: 'device', type: 'string' }, ...numeric({ gain: 1 })],
|
|
11
|
+
evaluate: ({ path, props }, audio) => {
|
|
12
|
+
const gain = Math.abs(props.gain);
|
|
13
|
+
const rms = finite((audio?.rms ?? 0) * gain, path + '/rms');
|
|
14
|
+
const peak = finite((audio?.peak ?? 0) * gain, path + '/peak');
|
|
15
|
+
return { signal: { rms, peak }, rms, peak };
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
const audio = [
|
|
19
|
+
audioSource('file'), audioSource('mic'),
|
|
20
|
+
{
|
|
21
|
+
type: 'audio:fft',
|
|
22
|
+
inputs: ports('object', 'signal'),
|
|
23
|
+
outputs: [...ports('number', 'bass', 'mid', 'high', 'centroid', 'flux'), ...ports('object', 'spectrum')],
|
|
24
|
+
props: numeric({ fftSize: 2048, smoothing: 0.8 }),
|
|
25
|
+
evaluate: (_, data) => ({
|
|
26
|
+
bass: data?.bass ?? 0, mid: data?.mid ?? 0, high: data?.high ?? 0, centroid: data?.centroid ?? 0,
|
|
27
|
+
flux: data?.flux ?? 0, spectrum: data?.spectrum ?? [],
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
type: 'audio:envelope',
|
|
32
|
+
inputs: ports('number', 'value'), outputs: ports('number', 'value'),
|
|
33
|
+
props: numeric({ attack: 0.01, release: 0.1 }),
|
|
34
|
+
evaluate: ({ inputs }, data) => ({ value: data?.rms ?? inputs.value ?? 0 }),
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
const geo = [
|
|
38
|
+
{
|
|
39
|
+
type: 'geo:box', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ width: 1, height: 1, depth: 1 }),
|
|
40
|
+
evaluate: ({ props }) => ({ geometry: { primitive: 'box', args: [props.width, props.height, props.depth] } }),
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
type: 'geo:sphere', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ radius: 1, segments: 32 }),
|
|
44
|
+
evaluate: ({ props }) => ({ geometry: { primitive: 'sphere', args: [props.radius, props.radius, props.segments] } }),
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: 'geo:transform',
|
|
48
|
+
inputs: [...ports('object', 'geometry'), ...ports('number', 'tx', 'ty', 'tz', 'sx', 'sy', 'sz')],
|
|
49
|
+
outputs: ports('object', 'geometry'), props: [],
|
|
50
|
+
evaluate: ({ inputs }) => ({ geometry: {
|
|
51
|
+
kind: 'transform', geometry: inputs.geometry ?? null,
|
|
52
|
+
translate: [inputs.tx ?? 0, inputs.ty ?? 0, inputs.tz ?? 0], scale: [inputs.sx ?? 1, inputs.sy ?? 1, inputs.sz ?? 1],
|
|
53
|
+
} }),
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
type: 'geo:merge', inputs: ports('object', 'a', 'b'), outputs: ports('object', 'geometry'), props: [],
|
|
57
|
+
evaluate: ({ inputs }) => ({ geometry: { kind: 'merge', geometries: [inputs.a, inputs.b].filter(value => value !== undefined) } }),
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
export const BUILTIN_CONTEXTS = freeze([
|
|
61
|
+
{ name: 'audio', stage: 'audio', nodeTypes: audio },
|
|
62
|
+
{ name: 'ch', stage: 'ch', nodeTypes: [] },
|
|
63
|
+
{ name: 'geo', stage: 'geo', nodeTypes: geo },
|
|
64
|
+
{ name: 'obj', stage: 'obj', nodeTypes: [] },
|
|
65
|
+
{ name: 'sim', stage: 'reserved', nodeTypes: [] },
|
|
66
|
+
]);
|
|
67
|
+
export function createContextRegistry(extensions = []) {
|
|
68
|
+
const contexts = new Map(BUILTIN_CONTEXTS.map(context => [context.name, context]));
|
|
69
|
+
for (const context of extensions) {
|
|
70
|
+
if (typeof context.name !== 'string' || !/^[a-z][a-z0-9_-]*$/.test(context.name) ||
|
|
71
|
+
BUILTIN_CONTEXTS.some(({ name }) => name === context.name)) {
|
|
72
|
+
fail('invalid-extension-context', 'Invalid or reserved extension context: ' + context.name);
|
|
73
|
+
}
|
|
74
|
+
if (contexts.has(context.name))
|
|
75
|
+
fail('duplicate-extension-context', 'Duplicate extension context: ' + context.name);
|
|
76
|
+
if ((context.stage !== 'evaluate' && context.stage !== 'output') || !Array.isArray(context.nodeTypes)) {
|
|
77
|
+
fail('invalid-extension-definition', 'Invalid stage or node types for /' + context.name);
|
|
78
|
+
}
|
|
79
|
+
contexts.set(context.name, Object.freeze({ ...context, nodeTypes: Object.freeze([...context.nodeTypes]) }));
|
|
80
|
+
}
|
|
81
|
+
return Object.freeze({ contexts: Object.freeze([...contexts.values()]), get: (name) => contexts.get(name) });
|
|
82
|
+
}
|
|
83
|
+
export function allowedSource(target, source) {
|
|
84
|
+
switch (target) {
|
|
85
|
+
case 'audio': return source === 'audio';
|
|
86
|
+
case 'ch': return source === 'audio' || source === 'ch';
|
|
87
|
+
case 'geo': return source === 'audio' || source === 'ch' || source === 'geo';
|
|
88
|
+
case 'obj': return source === 'audio' || source === 'ch' || source === 'geo' || source === 'obj';
|
|
89
|
+
case 'evaluate':
|
|
90
|
+
case 'output': return source !== 'output' && source !== 'reserved';
|
|
91
|
+
case 'reserved': return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
package/evaluate.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { type CompiledScene, type FrameSnapshot, type Result } from './types.js';
|
|
2
|
-
export declare function evaluateFrame(scene: CompiledScene, frame: number): Result<FrameSnapshot>;
|
|
1
|
+
import { type CompiledScene, type EvaluateFrameOptions, type FrameSnapshot, type Result } from './types.js';
|
|
2
|
+
export declare function evaluateFrame(scene: CompiledScene, frame: number, options?: EvaluateFrameOptions): Result<FrameSnapshot>;
|