@vim3d/core 0.2.1 → 0.2.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/ch-operators.d.ts +18 -0
- package/ch-operators.js +174 -0
- package/channels.d.ts +2 -0
- package/channels.js +14 -5
- package/compile.js +145 -23
- package/contexts.d.ts +5 -3
- package/contexts.js +37 -17
- package/esm/ch-operators.js +170 -0
- package/esm/channels.js +11 -4
- package/esm/compile.js +147 -25
- package/esm/contexts.js +37 -18
- package/esm/evaluate.js +45 -6
- package/esm/expression.js +34 -8
- package/esm/graph.js +1 -1
- package/esm/ramp.js +36 -0
- package/esm/ranges.js +9 -0
- package/esm/registry.js +6 -3
- package/evaluate.js +44 -5
- package/expression.d.ts +11 -2
- package/expression.js +34 -8
- package/graph.js +1 -1
- package/index.d.ts +1 -1
- package/model.d.ts +9 -1
- package/package.json +2 -2
- package/ramp.d.ts +8 -0
- package/ramp.js +40 -0
- package/ranges.d.ts +2 -0
- package/ranges.js +12 -0
- package/registry.d.ts +8 -0
- package/registry.js +7 -4
- package/types.d.ts +7 -0
package/contexts.js
CHANGED
|
@@ -1,16 +1,28 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Extensions = void 0;
|
|
4
|
+
exports.jsonProp = jsonProp;
|
|
4
5
|
exports.checkAudioFrame = checkAudioFrame;
|
|
5
6
|
exports.createContextEvaluation = createContextEvaluation;
|
|
6
7
|
const channels_js_1 = require("./channels.js");
|
|
7
8
|
const graph_js_1 = require("./graph.js");
|
|
8
9
|
const expression_js_1 = require("./expression.js");
|
|
9
10
|
const registry_js_1 = require("./registry.js");
|
|
11
|
+
const ranges_js_1 = require("./ranges.js");
|
|
10
12
|
const types_js_1 = require("./types.js");
|
|
11
13
|
const nodeType = /^[a-zA-Z][a-zA-Z0-9_-]*:[a-zA-Z][a-zA-Z0-9_-]*$/;
|
|
12
14
|
const propTypes = new Set(['string', 'number', 'boolean', 'object']);
|
|
13
15
|
const portTypes = new Set([...propTypes, 'vector3', 'matrix4']);
|
|
16
|
+
function jsonProp(text, invalid) {
|
|
17
|
+
const raw = text?.trimEnd().match(/^[ \t:]*(?:set|"set")[ \t]+(?:"[^"]*"|[^ \t]+)[ \t]+(.*)$/)?.[1];
|
|
18
|
+
const literal = raw?.match(/^(?:"(?:\\.|[^"\\])*"|[^"#])*/)?.[0];
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(literal);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return invalid();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
14
26
|
function isRecord(value) {
|
|
15
27
|
return value !== null && typeof value === 'object' &&
|
|
16
28
|
(Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
@@ -116,14 +128,7 @@ class Extensions {
|
|
|
116
128
|
const invalid = () => (0, types_js_1.fail)('invalid-extension-prop', 'Expected ' + def.type + ' for ' + address, source);
|
|
117
129
|
let value = args[0];
|
|
118
130
|
if (def.type === 'object') {
|
|
119
|
-
|
|
120
|
-
const literal = raw?.match(/^(?:"(?:\\.|[^"\\])*"|[^"#])*/)?.[0];
|
|
121
|
-
try {
|
|
122
|
-
value = JSON.parse(literal);
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
invalid();
|
|
126
|
-
}
|
|
131
|
+
value = jsonProp(text, invalid);
|
|
127
132
|
}
|
|
128
133
|
else {
|
|
129
134
|
if (args.length !== 1)
|
|
@@ -161,6 +166,13 @@ class Extensions {
|
|
|
161
166
|
(0, types_js_1.fail)('unknown-extension-port', 'Unknown output port: ' + address, source);
|
|
162
167
|
return { address, path: node.path, port: key, type: port.type, stage: node.stage };
|
|
163
168
|
}
|
|
169
|
+
numericPort(address, source) {
|
|
170
|
+
const { node, key } = this.member(address, source);
|
|
171
|
+
if (node.definition.inputs.get(key)?.type !== 'number' && node.definition.outputs.get(key)?.type !== 'number') {
|
|
172
|
+
(0, types_js_1.fail)('extension-port-type', 'Expected a numeric port at ' + address, source);
|
|
173
|
+
}
|
|
174
|
+
return address;
|
|
175
|
+
}
|
|
164
176
|
member(address, source) {
|
|
165
177
|
const [root, context, name, key, ...extra] = address.split('/');
|
|
166
178
|
if (root || !context || !name || !key || extra.length)
|
|
@@ -208,7 +220,7 @@ function checkAudioFrame(path, sampleFrame, frame) {
|
|
|
208
220
|
(0, types_js_1.fail)('off-time-audio-reference', 'Audio features are available only at the evaluated frame: ' + path);
|
|
209
221
|
}
|
|
210
222
|
}
|
|
211
|
-
function createContextEvaluation(extensions, order, frame, time, fps, frameCount, options, readBuiltIn) {
|
|
223
|
+
function createContextEvaluation(extensions, order, frame, time, fps, frameCount, options, readBuiltIn, ranges) {
|
|
212
224
|
const used = new Set([...extensions.nodes.values()].map(node => node.context));
|
|
213
225
|
const contextNames = extensions.registry.contexts.filter(({ stage, name }) => stage === 'evaluate' || stage === 'output' || used.has(name)).map(({ name }) => name).sort(graph_js_1.comparePaths);
|
|
214
226
|
let base;
|
|
@@ -216,9 +228,9 @@ function createContextEvaluation(extensions, order, frame, time, fps, frameCount
|
|
|
216
228
|
const values = new Map();
|
|
217
229
|
const snapshots = new Map();
|
|
218
230
|
const sorted = [...extensions.nodes.keys()].sort(graph_js_1.comparePaths);
|
|
219
|
-
const read = (path, sampleFrame) => {
|
|
231
|
+
const read = (path, sampleFrame, rampPosition) => {
|
|
220
232
|
if (path.startsWith('/ch/'))
|
|
221
|
-
return readBuiltIn(path, sampleFrame);
|
|
233
|
+
return readBuiltIn(path, sampleFrame, rampPosition);
|
|
222
234
|
if (path.startsWith('/obj/')) {
|
|
223
235
|
const end = path.lastIndexOf('/');
|
|
224
236
|
const object = objects.get(path.slice(0, end));
|
|
@@ -251,11 +263,19 @@ function createContextEvaluation(extensions, order, frame, time, fps, frameCount
|
|
|
251
263
|
if (!node || node.stage !== stage)
|
|
252
264
|
continue;
|
|
253
265
|
const { definition, source, context } = node;
|
|
254
|
-
const inputs =
|
|
255
|
-
|
|
256
|
-
frame, fps, frameCount, source: driver.source, read: (address, sampleFrame) => read(address, sampleFrame),
|
|
257
|
-
})
|
|
258
|
-
|
|
266
|
+
const inputs = Object.fromEntries([...node.drivers].map(([key, driver]) => {
|
|
267
|
+
const value = driver.kind === 'wire' ? read(driver.from.address, frame) : (0, expression_js_1.evaluateExpression)(driver.expression, {
|
|
268
|
+
frame, fps, frameCount, source: driver.source, read: (address, sampleFrame, u) => read(address, sampleFrame, u),
|
|
269
|
+
});
|
|
270
|
+
return [key, typeof value === 'number' ? (0, ranges_js_1.applyRange)(ranges, path + '/' + key, value) : value];
|
|
271
|
+
}));
|
|
272
|
+
if (ranges && stage === 'geo' && definition.type === 'geo:transform') {
|
|
273
|
+
for (const [key, fallback] of Object.entries(registry_js_1.geoTransformDefaults)) {
|
|
274
|
+
const address = path + '/' + key;
|
|
275
|
+
if (ranges[address] && !Object.hasOwn(inputs, key))
|
|
276
|
+
inputs[key] = (0, ranges_js_1.applyRange)(ranges, address, fallback);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
259
279
|
const props = (0, types_js_1.freeze)(Object.fromEntries(node.props));
|
|
260
280
|
const evaluate = definition.evaluate;
|
|
261
281
|
const audio = options.audio;
|
|
@@ -290,7 +310,7 @@ function createContextEvaluation(extensions, order, frame, time, fps, frameCount
|
|
|
290
310
|
}
|
|
291
311
|
if (!matches(value, type))
|
|
292
312
|
invalid();
|
|
293
|
-
const output = copyValue(value, invalid);
|
|
313
|
+
const output = typeof value === 'number' ? (0, ranges_js_1.applyRange)(ranges, address, value) : copyValue(value, invalid);
|
|
294
314
|
values.set(address, output);
|
|
295
315
|
return [name, output];
|
|
296
316
|
}));
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { wrap } from './math.js';
|
|
2
|
+
import { fail, finite } from './types.js';
|
|
3
|
+
const numberProp = (name, value) => ({ name, type: 'number', ...(value !== undefined && { default: value }) });
|
|
4
|
+
const stringProp = (name, value) => ({ name, type: 'string', default: value });
|
|
5
|
+
const scalar = [{ name: 'value', type: 'number' }];
|
|
6
|
+
function operator(definition) {
|
|
7
|
+
return {
|
|
8
|
+
...definition, inputs: scalar, outputs: scalar,
|
|
9
|
+
evaluate: ({ inputs, props, frame, fps }) => {
|
|
10
|
+
const value = (inputs.value ?? 0);
|
|
11
|
+
const source = { file: '<channel operator>', line: 1 };
|
|
12
|
+
const invalid = (message) => fail('invalid-channel-operator', message, source);
|
|
13
|
+
const configured = { ...Object.fromEntries(definition.props.map(p => [p.name, p.default])), ...props };
|
|
14
|
+
definition.validate(configured, invalid);
|
|
15
|
+
return { value: definition.sample(configured, frame, fps, source, {
|
|
16
|
+
read: () => value, integrate: () => [value, 0], end: Infinity,
|
|
17
|
+
}) };
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
function interpolateFrames(frame, at) {
|
|
22
|
+
const i = Math.floor(frame);
|
|
23
|
+
const t = frame - i;
|
|
24
|
+
const a = at(i);
|
|
25
|
+
return t ? (1 - t) * a + t * at(i + 1) : a;
|
|
26
|
+
}
|
|
27
|
+
function springStep(stiffness, damping, mass, dt) {
|
|
28
|
+
const k = stiffness / mass, decay = damping / mass / 2;
|
|
29
|
+
const discriminant = k - decay * decay;
|
|
30
|
+
const rate = Math.sqrt(Math.abs(discriminant));
|
|
31
|
+
const E = Math.exp(-decay * dt);
|
|
32
|
+
let c, s;
|
|
33
|
+
if (discriminant > 0) {
|
|
34
|
+
c = E * Math.cos(rate * dt);
|
|
35
|
+
s = E * Math.sin(rate * dt) / rate;
|
|
36
|
+
}
|
|
37
|
+
else if (discriminant < 0 && rate * dt > 1e-4) {
|
|
38
|
+
const slow = Math.exp((-k / (decay + rate)) * dt);
|
|
39
|
+
const fast = Math.exp(-(decay + rate) * dt);
|
|
40
|
+
c = (slow + fast) / 2;
|
|
41
|
+
s = (slow - fast) / (2 * rate);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
c = E * Math.cosh(rate * dt);
|
|
45
|
+
s = rate === 0 ? E * dt : E * Math.sinh(rate * dt) / rate;
|
|
46
|
+
}
|
|
47
|
+
return ([position, velocity], target) => {
|
|
48
|
+
const offset = position - target;
|
|
49
|
+
return [target + (c + decay * s) * offset + s * velocity, (c - decay * s) * velocity - k * s * offset];
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const clampFrame = (frame, end) => Math.max(0, Math.min(end, frame));
|
|
53
|
+
const filterRadius = (props, fps) => Math.floor(Number(props.width) * fps / 2);
|
|
54
|
+
export const channelOperators = [
|
|
55
|
+
operator({
|
|
56
|
+
type: 'ch:limit',
|
|
57
|
+
props: [numberProp('min'), numberProp('max'), stringProp('type', 'clamp'), numberProp('quantize')],
|
|
58
|
+
validate(props, invalid) {
|
|
59
|
+
const { min = -Infinity, max = Infinity, type, quantize } = props;
|
|
60
|
+
if (min > max)
|
|
61
|
+
invalid('Expected min <= max');
|
|
62
|
+
if (type !== 'clamp' && type !== 'loop' && type !== 'zigzag')
|
|
63
|
+
invalid('Expected clamp, loop or zigzag');
|
|
64
|
+
if (type !== 'clamp' && (!Number.isFinite(min) || !Number.isFinite(max) || min >= max)) {
|
|
65
|
+
invalid('Loop and zigzag require finite min < max');
|
|
66
|
+
}
|
|
67
|
+
if (quantize !== undefined && Number(quantize) <= 0)
|
|
68
|
+
invalid('Quantize must be positive');
|
|
69
|
+
},
|
|
70
|
+
sample(props, frame, _fps, source, { read }) {
|
|
71
|
+
const { min = -Infinity, max = Infinity, quantize } = props;
|
|
72
|
+
const width = max - min;
|
|
73
|
+
let value = read(frame);
|
|
74
|
+
if (props.type === 'loop')
|
|
75
|
+
value = min + wrap(value - min, width);
|
|
76
|
+
if (props.type === 'zigzag') {
|
|
77
|
+
const offset = wrap(value - min, 2 * width);
|
|
78
|
+
value = min + Math.min(offset, 2 * width - offset);
|
|
79
|
+
}
|
|
80
|
+
if (quantize !== undefined)
|
|
81
|
+
value = Math.round(Math.max(min, Math.min(max, value)) / quantize) * quantize;
|
|
82
|
+
return Math.max(min, Math.min(max, finite(value, 'Limit value', source)));
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
operator({
|
|
86
|
+
type: 'ch:lag',
|
|
87
|
+
props: [numberProp('lagUp', 0.1), numberProp('lagDown', 0.1)],
|
|
88
|
+
usesHistory: props => props.lagUp !== 0 || props.lagDown !== 0,
|
|
89
|
+
validate(props, invalid) {
|
|
90
|
+
if (Number(props.lagUp) < 0 || Number(props.lagDown) < 0)
|
|
91
|
+
invalid('Lag times must be nonnegative');
|
|
92
|
+
},
|
|
93
|
+
sample(props, frame, fps, _source, { end, integrate, read }) {
|
|
94
|
+
const { lagUp, lagDown } = props;
|
|
95
|
+
if (!lagUp && !lagDown)
|
|
96
|
+
return read(frame);
|
|
97
|
+
const alpha = (tau) => tau > 0 ? -Math.expm1(-1 / fps / tau) : 1;
|
|
98
|
+
const up = alpha(lagUp), down = alpha(lagDown);
|
|
99
|
+
return interpolateFrames(clampFrame(frame, end), i => integrate(i, ([previous], input) => {
|
|
100
|
+
const weight = input > previous ? up : down;
|
|
101
|
+
return [(1 - weight) * previous + weight * input, 0];
|
|
102
|
+
})[0]);
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
operator({
|
|
106
|
+
type: 'ch:spring',
|
|
107
|
+
props: [numberProp('stiffness', 120), numberProp('damping', 12), numberProp('mass', 1)],
|
|
108
|
+
usesHistory: () => true,
|
|
109
|
+
validate(props, invalid) {
|
|
110
|
+
if (Number(props.stiffness) <= 0 || Number(props.mass) <= 0 || Number(props.damping) < 0) {
|
|
111
|
+
invalid('Stiffness and mass must be positive; damping must be nonnegative');
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
sample(props, frame, fps, _source, { end, integrate }) {
|
|
115
|
+
const { stiffness, damping, mass } = props;
|
|
116
|
+
const step = springStep(stiffness, damping, mass, 1 / fps);
|
|
117
|
+
return interpolateFrames(clampFrame(frame, end), i => integrate(i, step)[0]);
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
operator({
|
|
121
|
+
type: 'ch:filter',
|
|
122
|
+
props: [numberProp('width', 0.2), stringProp('type', 'gaussian')],
|
|
123
|
+
usesHistory: (props, fps) => filterRadius(props, fps) !== 0,
|
|
124
|
+
validate(props, invalid) {
|
|
125
|
+
if (Number(props.width) < 0 || (props.type !== 'box' && props.type !== 'gaussian')) {
|
|
126
|
+
invalid('Expected a nonnegative width and type box or gaussian');
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
sample(props, frame, fps, source, { end, read }) {
|
|
130
|
+
const radius = filterRadius(props, fps);
|
|
131
|
+
if (radius === 0)
|
|
132
|
+
return read(frame);
|
|
133
|
+
if (!Number.isSafeInteger(radius))
|
|
134
|
+
fail('invalid-channel-operator', 'Filter window is too wide', source);
|
|
135
|
+
return interpolateFrames(frame, i => {
|
|
136
|
+
if (!Number.isSafeInteger(i - radius) || !Number.isSafeInteger(i + radius)) {
|
|
137
|
+
fail('invalid-channel-operator', 'Filter sample frame is out of range', source);
|
|
138
|
+
}
|
|
139
|
+
let average = 0, total = 0;
|
|
140
|
+
for (let offset = -radius; offset <= radius; offset++) {
|
|
141
|
+
const weight = props.type === 'box' ? 1 : Math.exp(-2 * (offset / radius) ** 2);
|
|
142
|
+
const nextTotal = total + weight;
|
|
143
|
+
average = average * (total / nextTotal) + read(clampFrame(i + offset, end)) * (weight / nextTotal);
|
|
144
|
+
total = nextTotal;
|
|
145
|
+
}
|
|
146
|
+
return average;
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
}),
|
|
150
|
+
];
|
|
151
|
+
export function createChannelSampler(read, frameCount, source, getHistory = () => []) {
|
|
152
|
+
let states;
|
|
153
|
+
return {
|
|
154
|
+
read, end: frameCount === undefined ? Infinity : frameCount - 1,
|
|
155
|
+
integrate(frame, step) {
|
|
156
|
+
if (!Number.isSafeInteger(frame))
|
|
157
|
+
fail('invalid-frame', 'Operator sample frame is out of range', source);
|
|
158
|
+
states ??= getHistory();
|
|
159
|
+
if (!states.length)
|
|
160
|
+
states.push([read(0), 0]);
|
|
161
|
+
for (let i = states.length; i <= frame; i++) {
|
|
162
|
+
const next = step(states[i - 1], read(i));
|
|
163
|
+
finite(next[0], 'Operator position', source);
|
|
164
|
+
finite(next[1], 'Operator velocity', source);
|
|
165
|
+
states.push(next);
|
|
166
|
+
}
|
|
167
|
+
return states[frame];
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
package/esm/channels.js
CHANGED
|
@@ -2,6 +2,8 @@ import { wrap } from './math.js';
|
|
|
2
2
|
import { readNumber } from './model.js';
|
|
3
3
|
import { fail, finite } from './types.js';
|
|
4
4
|
export const validSegment = (text) => !!text && text !== '.' && text !== '..' && !/[\/\x00-\x1f\x7f]/.test(text);
|
|
5
|
+
export const boundaryMode = (value) => value === 'hold' || value === 'cycle' || value === 'clamp' || value === 'wrap';
|
|
6
|
+
export const cyclic = (mode) => mode === 'wrap' || mode === 'cycle';
|
|
5
7
|
export function loadTable(asset, data, source) {
|
|
6
8
|
if (data == null)
|
|
7
9
|
fail('missing-asset', 'Cannot read channel asset: ' + asset, source);
|
|
@@ -32,8 +34,13 @@ export function loadTable(asset, data, source) {
|
|
|
32
34
|
}
|
|
33
35
|
if (!raw || !Number.isFinite(raw.sampleRate) || raw.sampleRate <= 0)
|
|
34
36
|
invalid('expected a positive sampleRate');
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
for (const mode of [raw.boundary, raw.extend]) {
|
|
38
|
+
if (mode !== undefined && !boundaryMode(mode))
|
|
39
|
+
invalid('invalid boundary mode');
|
|
40
|
+
}
|
|
41
|
+
if (raw.boundary !== undefined && raw.extend !== undefined && cyclic(raw.boundary) !== cyclic(raw.extend)) {
|
|
42
|
+
invalid('conflicting boundary and extend modes');
|
|
43
|
+
}
|
|
37
44
|
if (raw.interpolation !== undefined && raw.interpolation !== 'step' && raw.interpolation !== 'linear')
|
|
38
45
|
invalid('invalid interpolation');
|
|
39
46
|
if (!raw.channels || typeof raw.channels !== 'object' || Array.isArray(raw.channels))
|
|
@@ -51,7 +58,7 @@ export function loadTable(asset, data, source) {
|
|
|
51
58
|
ports.set(name, { samples, source });
|
|
52
59
|
}
|
|
53
60
|
Object.assign(table, { sampleRate: raw.sampleRate,
|
|
54
|
-
interpolation: raw.interpolation ?? 'step', boundary: raw.boundary ?? 'wrap' });
|
|
61
|
+
interpolation: raw.interpolation ?? 'step', boundary: raw.boundary ?? raw.extend ?? 'wrap' });
|
|
55
62
|
}
|
|
56
63
|
else
|
|
57
64
|
invalid('expected a .chan or .json asset');
|
|
@@ -61,7 +68,7 @@ export function sample(table, port, frame, fps) {
|
|
|
61
68
|
const rows = port.samples;
|
|
62
69
|
const r = finite(table.sampleRate === undefined ? frame : frame * table.sampleRate / fps, 'Row coordinate', port.source);
|
|
63
70
|
const i = Math.floor(r);
|
|
64
|
-
const index = (j) => table.boundary
|
|
71
|
+
const index = (j) => cyclic(table.boundary) ? wrap(j, rows.length) : Math.max(0, Math.min(rows.length - 1, j));
|
|
65
72
|
const value = rows[index(i)];
|
|
66
73
|
return table.interpolation === 'step' ? value
|
|
67
74
|
: finite(value + (rows[index(i + 1)] - value) * (r - i), 'Interpolated sample', port.source);
|
package/esm/compile.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { expand } from '@vim3d/parser';
|
|
2
|
-
import { loadTable, validSegment } from './channels.js';
|
|
3
|
-
import {
|
|
2
|
+
import { boundaryMode, cyclic, loadTable, validSegment } from './channels.js';
|
|
3
|
+
import { channelOperators } from './ch-operators.js';
|
|
4
|
+
import { Extensions, jsonProp } from './contexts.js';
|
|
4
5
|
import { bindExpression, parseExpression } from './expression.js';
|
|
5
6
|
import { comparePaths, GraphBuilder } from './graph.js';
|
|
6
7
|
import { allowedSource, createContextRegistry } from './registry.js';
|
|
8
|
+
import { rampStops } from './ramp.js';
|
|
7
9
|
import { channelPath, checkArity, copyState, defaults, editCommands, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
8
10
|
import { EvaluationError, fail, freeze } from './types.js';
|
|
9
11
|
const aliases = {
|
|
@@ -12,7 +14,7 @@ const aliases = {
|
|
|
12
14
|
};
|
|
13
15
|
const primitives = new Set(['cube', 'grid', 'sphere', 'ellipse', 'ellipseFace', 'circle', 'circleFace', 'ring']);
|
|
14
16
|
const prefixes = { geo: 'geo', curve: 'curve', null: 'null', camera: 'cam', light: 'light', channel: 'ch' };
|
|
15
|
-
const deferred = new Set(['parent', 'expr', 'register', 'ref', 'path', 'lookat', 'children', 'connect', 'set']);
|
|
17
|
+
const deferred = new Set(['parent', 'expr', 'register', 'ref', 'path', 'lookat', 'children', 'connect', 'set', 'range']);
|
|
16
18
|
const isChannel = (node) => 'ports' in node;
|
|
17
19
|
const isObject = (node) => 'authored' in node;
|
|
18
20
|
const isExtension = (node) => 'definition' in node;
|
|
@@ -76,7 +78,7 @@ export function compileScene(source, options = {}) {
|
|
|
76
78
|
return data instanceof Uint8Array ? decoder.decode(data) : data ?? null;
|
|
77
79
|
},
|
|
78
80
|
expandDirectives: true,
|
|
79
|
-
reservedCommands: ['expr', 'parent'],
|
|
81
|
+
reservedCommands: ['expr', 'parent', 'range'],
|
|
80
82
|
preserveText: true,
|
|
81
83
|
});
|
|
82
84
|
if (expanded.problems.length)
|
|
@@ -151,12 +153,14 @@ function compile(lines, root, fps, options) {
|
|
|
151
153
|
return node;
|
|
152
154
|
}
|
|
153
155
|
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
154
|
-
const channelOutput = (path, source) => {
|
|
156
|
+
const channelOutput = (path, source, ramp = false) => {
|
|
155
157
|
const parts = path.split('/');
|
|
156
158
|
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
157
159
|
fail('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
158
160
|
}
|
|
159
161
|
const node = lookup(parts[2], 'ch', source);
|
|
162
|
+
if (ramp && !node.ramp)
|
|
163
|
+
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
|
|
160
164
|
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
161
165
|
fail('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
162
166
|
}
|
|
@@ -165,6 +169,14 @@ function compile(lines, root, fps, options) {
|
|
|
165
169
|
fail('missing-target', 'Unknown channel port: ' + path, source);
|
|
166
170
|
return channelPath(node) + '/' + name;
|
|
167
171
|
};
|
|
172
|
+
const channelInput = (path, source) => {
|
|
173
|
+
const explicit = /^\/ch\/([^/]+)\/input\/value$/.exec(path);
|
|
174
|
+
if (!explicit)
|
|
175
|
+
return channelOutput(path, source);
|
|
176
|
+
if (!chNames.get(explicit[1])?.operator)
|
|
177
|
+
fail('missing-target', 'Unknown operator input: ' + path, source);
|
|
178
|
+
return channelOutput('/ch/' + explicit[1] + '/value', source);
|
|
179
|
+
};
|
|
168
180
|
const scalar = (node, param, source) => {
|
|
169
181
|
if (!Object.hasOwn(node.authored.parameters, param)) {
|
|
170
182
|
fail('unsupported-parameter', 'Unsupported parameter: ' + objectPath(node) + '/' + param, source);
|
|
@@ -222,7 +234,25 @@ function compile(lines, root, fps, options) {
|
|
|
222
234
|
}
|
|
223
235
|
else if (command === 'node') {
|
|
224
236
|
checkArity(command, args, [2], source);
|
|
225
|
-
|
|
237
|
+
const path = extensionAddress(args[0], source);
|
|
238
|
+
const definition = path.startsWith('/ch/') ? channelOperators.find(operator => operator.type === args[1]) : undefined;
|
|
239
|
+
if (path.startsWith('/ch/') && (args[1] === 'ch:ramp' || definition)) {
|
|
240
|
+
const [, , name, ...extra] = path.split('/');
|
|
241
|
+
if (!validSegment(name) || extra.length)
|
|
242
|
+
fail('invalid-path', 'Expected a channel node path: ' + path, source);
|
|
243
|
+
if (chNames.has(name))
|
|
244
|
+
fail('duplicate-name', 'Name is already used: ' + path, source);
|
|
245
|
+
const channel = { id: nextId++, name, source, ports: new Map([['value', { source }]]) };
|
|
246
|
+
if (definition)
|
|
247
|
+
channel.operator = { definition, props: Object.assign(Object.create(null), Object.fromEntries(definition.props.flatMap(p => p.default === undefined ? [] : [[p.name, p.default]]))) };
|
|
248
|
+
else
|
|
249
|
+
channel.ramp = [];
|
|
250
|
+
chNames.set(name, channel);
|
|
251
|
+
channels.push(channel);
|
|
252
|
+
current = channel;
|
|
253
|
+
}
|
|
254
|
+
else
|
|
255
|
+
current = extensions.add(path, args[1], source);
|
|
226
256
|
}
|
|
227
257
|
else if (command === 'begin') {
|
|
228
258
|
checkArity(command, args, [0, 1], source);
|
|
@@ -341,6 +371,7 @@ function compile(lines, root, fps, options) {
|
|
|
341
371
|
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
342
372
|
}
|
|
343
373
|
const graph = new GraphBuilder();
|
|
374
|
+
const ranges = Object.create(null);
|
|
344
375
|
const expressionPorts = new Map();
|
|
345
376
|
for (const channel of channels) {
|
|
346
377
|
for (const [name, port] of channel.ports)
|
|
@@ -356,6 +387,8 @@ function compile(lines, root, fps, options) {
|
|
|
356
387
|
};
|
|
357
388
|
const bind = (text, path, source) => bindExpression(parseExpression(text, source), (ref, name) => {
|
|
358
389
|
const address = normalizePath(ref, path.slice(0, path.lastIndexOf('/')), source);
|
|
390
|
+
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
391
|
+
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
|
|
359
392
|
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
360
393
|
if (!isBuiltInPath(address) && registry.get(address.split('/')[1])) {
|
|
361
394
|
const from = extensionSource(address, source);
|
|
@@ -378,16 +411,16 @@ function compile(lines, root, fps, options) {
|
|
|
378
411
|
if (name !== 'ch')
|
|
379
412
|
fail('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
380
413
|
}
|
|
381
|
-
const input = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
|
|
382
|
-
if (name
|
|
414
|
+
const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
|
|
415
|
+
if (name === 'chf' || name === 'cht')
|
|
383
416
|
offTimeRefs.push({ path: input, source });
|
|
384
417
|
depend(path, input);
|
|
385
418
|
return input;
|
|
386
419
|
});
|
|
387
420
|
const driveScalar = (path, expression, source) => {
|
|
388
421
|
if (path.startsWith('/ch/')) {
|
|
389
|
-
const { port } = expressionPorts.get(path);
|
|
390
|
-
if (port.samples || port.expression || port.live)
|
|
422
|
+
const { node, port } = expressionPorts.get(path);
|
|
423
|
+
if (node.ramp || port.samples || port.expression || port.live)
|
|
391
424
|
fail('overlapping-drivers', 'Channel already has a driver: ' + path, source);
|
|
392
425
|
port.expression = expression;
|
|
393
426
|
port.source = source;
|
|
@@ -399,27 +432,87 @@ function compile(lines, root, fps, options) {
|
|
|
399
432
|
if (command === 'parent')
|
|
400
433
|
continue;
|
|
401
434
|
const numberAt = (i, fallback) => args[i] === undefined ? fallback : readNumber(args[i], source);
|
|
402
|
-
if (command === '
|
|
435
|
+
if (command === 'range') {
|
|
436
|
+
checkArity(command, args, [3, 4], source);
|
|
437
|
+
const address = extensionAddress(args[0], source);
|
|
438
|
+
let path;
|
|
439
|
+
if (address.startsWith('/ch/')) {
|
|
440
|
+
if (address.endsWith('/input/value')) {
|
|
441
|
+
channelInput(address, source);
|
|
442
|
+
path = address;
|
|
443
|
+
}
|
|
444
|
+
else
|
|
445
|
+
path = channelOutput(address, source);
|
|
446
|
+
}
|
|
447
|
+
else if (address.startsWith('/obj/'))
|
|
448
|
+
path = objectScalar(address, source);
|
|
449
|
+
else
|
|
450
|
+
path = extensions.numericPort(address, source);
|
|
451
|
+
const min = readNumber(args[1], source), max = readNumber(args[2], source);
|
|
452
|
+
const mode = args[3] ?? 'clamp';
|
|
453
|
+
if (min > max || (mode !== 'clamp' && mode !== 'error')) {
|
|
454
|
+
fail('invalid-range', 'Expected MIN <= MAX and mode clamp or error', source);
|
|
455
|
+
}
|
|
456
|
+
if (ranges[path])
|
|
457
|
+
fail('overlapping-ranges', 'Range already declared for ' + path, source);
|
|
458
|
+
ranges[path] = { min, max, mode, source };
|
|
459
|
+
}
|
|
460
|
+
else if (command === 'set') {
|
|
403
461
|
const [target] = args;
|
|
404
462
|
if (!target)
|
|
405
463
|
fail('invalid-operands', 'Expected set KEY VALUE', source);
|
|
406
|
-
if (!target.startsWith('/') && !isExtension(current))
|
|
407
|
-
fail('invalid-subject', 'set requires
|
|
464
|
+
if (!target.startsWith('/') && !isExtension(current) && !isChannel(current)) {
|
|
465
|
+
fail('invalid-subject', 'set requires a context node', source);
|
|
466
|
+
}
|
|
408
467
|
const address = target.startsWith('/') ? extensionAddress(target, source)
|
|
409
|
-
: current.path + '/' + target;
|
|
410
|
-
|
|
468
|
+
: (isChannel(current) ? channelPath(current) : current.path) + '/' + target;
|
|
469
|
+
if (address.startsWith('/ch/')) {
|
|
470
|
+
const [, , name, key, ...extra] = address.split('/');
|
|
471
|
+
const node = chNames.get(name);
|
|
472
|
+
if (!node || extra.length)
|
|
473
|
+
fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
|
|
474
|
+
if (key === 'extend' || key === 'boundary') {
|
|
475
|
+
checkArity(command, args, [2], source);
|
|
476
|
+
const mode = args[1];
|
|
477
|
+
if (node.live || !boundaryMode(mode) || (!node.table && cyclic(mode) && options.frameCount === undefined)) {
|
|
478
|
+
fail('invalid-channel-extend', 'Invalid extend mode for ' + address, source);
|
|
479
|
+
}
|
|
480
|
+
if (node.table)
|
|
481
|
+
node.table.boundary = mode;
|
|
482
|
+
else
|
|
483
|
+
node.extend = mode;
|
|
484
|
+
}
|
|
485
|
+
else if (node.ramp && key === 'stops') {
|
|
486
|
+
node.ramp = rampStops(jsonProp(lineText, () => fail('invalid-ramp', 'Expected JSON ramp stops', source)), source);
|
|
487
|
+
}
|
|
488
|
+
else {
|
|
489
|
+
const spec = node.operator?.definition.props.find(p => p.name === key);
|
|
490
|
+
if (!spec)
|
|
491
|
+
fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
|
|
492
|
+
checkArity(command, args, [2], source);
|
|
493
|
+
node.operator.props[key] = spec.type === 'number' ? readNumber(args[1], source) : args[1];
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
else
|
|
497
|
+
extensions.property(address, args.slice(1), lineText, source);
|
|
411
498
|
}
|
|
412
499
|
else if (command === 'connect') {
|
|
413
500
|
checkArity(command, args, [2], source);
|
|
414
501
|
const to = extensionAddress(args[1], source);
|
|
415
502
|
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
416
503
|
if (isBuiltInPath(to)) {
|
|
417
|
-
if (from.stage === 'audio') {
|
|
504
|
+
if (from.stage === 'audio' || from.stage === 'ch') {
|
|
418
505
|
if (from.type !== 'number')
|
|
419
|
-
fail('extension-port-type', 'Expected
|
|
420
|
-
const path = to.startsWith('/ch/') ?
|
|
506
|
+
fail('extension-port-type', 'Expected a numeric output at ' + from.address, source);
|
|
507
|
+
const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
|
|
508
|
+
const operator = expressionPorts.get(path)?.node.operator;
|
|
509
|
+
if (from.stage === 'ch' && to.startsWith('/ch/') && !operator) {
|
|
510
|
+
fail('extension-stage', 'Expected a channel operator input: ' + to, source);
|
|
511
|
+
}
|
|
421
512
|
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
422
|
-
|
|
513
|
+
if (from.stage === 'ch')
|
|
514
|
+
depend(path, from.path);
|
|
515
|
+
contextWires.push({ to: path, input: operator ? 'value' : from.address, from });
|
|
423
516
|
continue;
|
|
424
517
|
}
|
|
425
518
|
const [, context, name, input, extra] = to.split('/');
|
|
@@ -449,13 +542,16 @@ function compile(lines, root, fps, options) {
|
|
|
449
542
|
const references = new Map();
|
|
450
543
|
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
451
544
|
const expression = bindExpression(parseExpression(text, source), (ref, name) => {
|
|
452
|
-
const
|
|
545
|
+
const address = normalizePath(ref, owner, source);
|
|
546
|
+
if (name === 'chramp')
|
|
547
|
+
channelOutput(address, source, true);
|
|
548
|
+
const from = extensionSource(address, source);
|
|
453
549
|
if (name !== 'ch' && !from.address.startsWith('/ch/')) {
|
|
454
550
|
fail('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
455
551
|
}
|
|
456
552
|
if (from.type !== 'number')
|
|
457
553
|
fail('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
458
|
-
if (name
|
|
554
|
+
if (name === 'chf' || name === 'cht')
|
|
459
555
|
offTimeRefs.push({ path: from.address, source });
|
|
460
556
|
references.set(from.address, from);
|
|
461
557
|
return from.address;
|
|
@@ -463,7 +559,7 @@ function compile(lines, root, fps, options) {
|
|
|
463
559
|
extensions.input(address, 'number', { kind: 'expr', expression, references: [...references.values()], source }, source);
|
|
464
560
|
continue;
|
|
465
561
|
}
|
|
466
|
-
const path = address.startsWith('/ch/') ?
|
|
562
|
+
const path = address.startsWith('/ch/') ? channelInput(address, source) : objectScalar(address, source);
|
|
467
563
|
driveScalar(path, bind(text, path, source), source);
|
|
468
564
|
}
|
|
469
565
|
else if (command === 'register' || command === 'ref') {
|
|
@@ -516,8 +612,11 @@ function compile(lines, root, fps, options) {
|
|
|
516
612
|
}
|
|
517
613
|
function validateState(object, state, source) {
|
|
518
614
|
if (state.matrix && (object.aimBindings.length || object.pathBindings.length ||
|
|
519
|
-
[...
|
|
520
|
-
|
|
615
|
+
[...rotations, ...scales].some(p => {
|
|
616
|
+
const target = objectPath(object) + '/' + p;
|
|
617
|
+
return drivers.has(target) || ranges[target];
|
|
618
|
+
}))) {
|
|
619
|
+
fail('invalid-trs', 'Rotation and scale drivers and ranges, and constraints, require a non-sheared, nonsingular TRS matrix', source);
|
|
521
620
|
}
|
|
522
621
|
}
|
|
523
622
|
for (const object of objects)
|
|
@@ -563,13 +662,28 @@ function compile(lines, root, fps, options) {
|
|
|
563
662
|
}
|
|
564
663
|
}
|
|
565
664
|
for (const [path, { node, name, port }] of expressionPorts) {
|
|
665
|
+
const extend = node.extend ? [prop('extend', node.extend)] : [];
|
|
666
|
+
if (node.ramp) {
|
|
667
|
+
if (!node.ramp.length)
|
|
668
|
+
fail('invalid-ramp', 'Missing ramp stops for ' + channelPath(node), port.source);
|
|
669
|
+
graph.add(path, 'ch:ramp', 'number', [prop('target', path), prop('stops', node.ramp), ...extend], port.source);
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
566
672
|
if (!port.samples && !port.expression && !port.live)
|
|
567
673
|
fail('missing-channel-driver', 'Missing driver for ' + path, port.source);
|
|
674
|
+
if (node.operator) {
|
|
675
|
+
const { definition, props } = node.operator;
|
|
676
|
+
definition.validate(props, message => fail('invalid-channel-operator', channelPath(node) + ': ' + message, node.source));
|
|
677
|
+
graph.addNode(path, { context: 'ch', type: definition.type, inputs: definition.inputs, outputs: definition.outputs,
|
|
678
|
+
props: [prop('target', path), prop('expr/value', port.expression), ...Object.entries(props).map(([key, value]) => prop(key, value)), ...extend],
|
|
679
|
+
}, node.source);
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
568
682
|
const nodeType = node.table ? 'source:table' : port.live ? 'source:live' : 'source:expression';
|
|
569
683
|
const nodeProps = node.table
|
|
570
684
|
? [prop('asset', node.table.asset), prop('sampling', node.table)]
|
|
571
685
|
: port.live ? [] : [prop('ast', port.expression)];
|
|
572
|
-
graph.add(path, nodeType, 'number', [prop('target', path), prop('port', name), ...nodeProps], port.source);
|
|
686
|
+
graph.add(path, nodeType, 'number', [prop('target', path), prop('port', name), ...nodeProps, ...extend], port.source);
|
|
573
687
|
}
|
|
574
688
|
for (const object of objects) {
|
|
575
689
|
const path = objectPath(object);
|
|
@@ -630,10 +744,18 @@ function compile(lines, root, fps, options) {
|
|
|
630
744
|
audioChannels.add(path);
|
|
631
745
|
}
|
|
632
746
|
}
|
|
747
|
+
for (const [path, { node }] of expressionPorts) {
|
|
748
|
+
if (!livePorts.has(path) && !audioChannels.has(path))
|
|
749
|
+
continue;
|
|
750
|
+
if (node.extend || node.operator?.definition.usesHistory?.(node.operator.props, fps)) {
|
|
751
|
+
fail('off-time-channel-operator', 'Channel requires unavailable off-frame input: ' + path, node.source);
|
|
752
|
+
}
|
|
753
|
+
}
|
|
633
754
|
const scene = freeze({
|
|
634
755
|
fps,
|
|
635
756
|
...(options.frameCount !== undefined && { frameCount: options.frameCount }),
|
|
636
757
|
graph: finished.graph,
|
|
758
|
+
...(Object.keys(ranges).length && { ranges }),
|
|
637
759
|
liveChannels,
|
|
638
760
|
...((viewCamera || backgroundColor || display) && { view: {
|
|
639
761
|
...(viewCamera && { camera: objectPath(viewCamera) }),
|