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