@vim3d/core 0.2.0 → 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/README.md +6 -2
- 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 +322 -28
- package/contexts.d.ts +57 -0
- package/contexts.js +330 -0
- package/esm/ch-operators.js +170 -0
- package/esm/channels.js +11 -4
- package/esm/compile.js +323 -29
- package/esm/contexts.js +323 -0
- package/esm/evaluate.js +99 -11
- package/esm/expression.js +34 -8
- package/esm/graph.js +27 -5
- package/esm/index.js +1 -0
- package/esm/ramp.js +36 -0
- package/esm/ranges.js +9 -0
- package/esm/registry.js +96 -0
- package/evaluate.d.ts +2 -2
- package/evaluate.js +98 -10
- package/expression.d.ts +11 -2
- package/expression.js +34 -8
- 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 +15 -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 +22 -0
- package/registry.js +101 -0
- package/types.d.ts +79 -3
package/README.md
CHANGED
|
@@ -30,5 +30,9 @@ evaluated scalar values and final local and world transforms; a renderer
|
|
|
30
30
|
does not run expressions or constraints again.
|
|
31
31
|
|
|
32
32
|
For a procedural channel, use `add channel` and drive its `value` port
|
|
33
|
-
with `expr`. For
|
|
34
|
-
|
|
33
|
+
with `expr`. For live browser sources (such as Web Audio), declare
|
|
34
|
+
`liveChannels` in `compileScene` options and supply per-frame samples in
|
|
35
|
+
`evaluateFrame(scene, frame, { liveChannels })`; off-time reads (`chf`,
|
|
36
|
+
`cht`, or non-unit `register`) and `{ deterministic: true }` reject live
|
|
37
|
+
graphs. For authored path vertices, use `begin draw`, one XYZ triplet per
|
|
38
|
+
line, and `end`.
|
|
@@ -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);
|