@vim3d/core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -1
- package/channels.d.ts +8 -0
- package/channels.js +74 -0
- package/compile.d.ts +2 -0
- package/compile.js +486 -0
- package/esm/channels.js +68 -0
- package/esm/compile.js +483 -0
- package/esm/evaluate.js +164 -0
- package/esm/expression.js +179 -0
- package/esm/graph.js +83 -0
- package/esm/index.js +2 -0
- package/esm/math.js +81 -0
- package/esm/model.js +165 -0
- package/esm/types.js +23 -0
- package/evaluate.d.ts +2 -0
- package/evaluate.js +167 -0
- package/expression.d.ts +65 -0
- package/expression.js +184 -0
- package/graph.d.ts +11 -0
- package/graph.js +88 -0
- package/index.d.ts +4 -0
- package/index.js +5 -1
- package/math.d.ts +25 -0
- package/math.js +101 -0
- package/model.d.ts +101 -0
- package/model.js +181 -0
- package/package.json +4 -1
- package/types.d.ts +123 -0
- package/types.js +30 -0
package/esm/channels.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { wrap } from './math.js';
|
|
2
|
+
import { readNumber } from './model.js';
|
|
3
|
+
import { fail, finite } from './types.js';
|
|
4
|
+
export const validSegment = (text) => !!text && text !== '.' && text !== '..' && !/[\/\x00-\x1f\x7f]/.test(text);
|
|
5
|
+
export function loadTable(asset, data, source) {
|
|
6
|
+
if (data == null)
|
|
7
|
+
fail('missing-asset', 'Cannot read channel asset: ' + asset, source);
|
|
8
|
+
const text = typeof data === 'string' ? data : new TextDecoder().decode(data);
|
|
9
|
+
const table = { asset, interpolation: 'step', boundary: 'wrap' };
|
|
10
|
+
const ports = new Map();
|
|
11
|
+
const invalid = (message) => fail('invalid-channel-table', asset + ': ' + message, source);
|
|
12
|
+
if (asset.endsWith('.chan')) {
|
|
13
|
+
const rows = text.trim().split(/\r?\n/).filter(row => row.trim());
|
|
14
|
+
if (!rows.length)
|
|
15
|
+
invalid('expected a nonempty table');
|
|
16
|
+
const samples = rows.map(row => {
|
|
17
|
+
const fields = row.trim().split(/[ \t]+/);
|
|
18
|
+
if (fields.length !== 2)
|
|
19
|
+
invalid('expected two numeric fields per row');
|
|
20
|
+
readNumber(fields[0], source);
|
|
21
|
+
return readNumber(fields[1], source);
|
|
22
|
+
});
|
|
23
|
+
ports.set('value', { samples, source });
|
|
24
|
+
}
|
|
25
|
+
else if (asset.endsWith('.json')) {
|
|
26
|
+
let raw;
|
|
27
|
+
try {
|
|
28
|
+
raw = JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
invalid('invalid JSON');
|
|
32
|
+
}
|
|
33
|
+
if (!raw || !Number.isFinite(raw.sampleRate) || raw.sampleRate <= 0)
|
|
34
|
+
invalid('expected a positive sampleRate');
|
|
35
|
+
if (raw.boundary !== undefined && raw.boundary !== 'wrap' && raw.boundary !== 'clamp')
|
|
36
|
+
invalid('invalid boundary mode');
|
|
37
|
+
if (raw.interpolation !== undefined && raw.interpolation !== 'step' && raw.interpolation !== 'linear')
|
|
38
|
+
invalid('invalid interpolation');
|
|
39
|
+
if (!raw.channels || typeof raw.channels !== 'object' || Array.isArray(raw.channels))
|
|
40
|
+
invalid('expected named channels');
|
|
41
|
+
const tracks = Object.entries(raw.channels);
|
|
42
|
+
if (!tracks.length)
|
|
43
|
+
invalid('expected at least one channel');
|
|
44
|
+
let length;
|
|
45
|
+
for (const [name, samples] of tracks) {
|
|
46
|
+
if (!validSegment(name) || !Array.isArray(samples) || !samples.length ||
|
|
47
|
+
!samples.every(Number.isFinite) || (length !== undefined && length !== samples.length)) {
|
|
48
|
+
invalid('expected named, finite, nonempty tracks of equal length');
|
|
49
|
+
}
|
|
50
|
+
length = samples.length;
|
|
51
|
+
ports.set(name, { samples, source });
|
|
52
|
+
}
|
|
53
|
+
Object.assign(table, { sampleRate: raw.sampleRate,
|
|
54
|
+
interpolation: raw.interpolation ?? 'step', boundary: raw.boundary ?? 'wrap' });
|
|
55
|
+
}
|
|
56
|
+
else
|
|
57
|
+
invalid('expected a .chan or .json asset');
|
|
58
|
+
return { table, ports };
|
|
59
|
+
}
|
|
60
|
+
export function sample(table, port, frame, fps) {
|
|
61
|
+
const rows = port.samples;
|
|
62
|
+
const r = finite(table.sampleRate === undefined ? frame : frame * table.sampleRate / fps, 'Row coordinate', port.source);
|
|
63
|
+
const i = Math.floor(r);
|
|
64
|
+
const index = (j) => table.boundary === 'wrap' ? wrap(j, rows.length) : Math.max(0, Math.min(rows.length - 1, j));
|
|
65
|
+
const value = rows[index(i)];
|
|
66
|
+
return table.interpolation === 'step' ? value
|
|
67
|
+
: finite(value + (rows[index(i + 1)] - value) * (r - i), 'Interpolated sample', port.source);
|
|
68
|
+
}
|
package/esm/compile.js
ADDED
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
import { expand } from '@vim3d/parser';
|
|
2
|
+
import { loadTable, validSegment } from './channels.js';
|
|
3
|
+
import { bindExpression, parseExpression } from './expression.js';
|
|
4
|
+
import { comparePaths, GraphBuilder } from './graph.js';
|
|
5
|
+
import { channelPath, checkArity, copyState, defaults, editCommands, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
6
|
+
import { EvaluationError, fail, freeze } from './types.js';
|
|
7
|
+
const aliases = {
|
|
8
|
+
s: 'select', m: 'translate', mv: 'translate', cam: 'camera', l: 'light',
|
|
9
|
+
zNear: 'znear', zFar: 'zfar', reg: 'register', reference: 'ref', disp: 'display',
|
|
10
|
+
};
|
|
11
|
+
const primitives = new Set(['cube', 'grid', 'sphere', 'ellipse', 'ellipseFace', 'circle', 'circleFace', 'ring']);
|
|
12
|
+
const prefixes = { geo: 'geo', curve: 'curve', null: 'null', camera: 'cam', light: 'light', channel: 'ch' };
|
|
13
|
+
const deferred = new Set(['parent', 'expr', 'register', 'ref', 'path', 'lookat', 'children']);
|
|
14
|
+
const isChannel = (node) => 'ports' in node;
|
|
15
|
+
const commandOf = (text) => Object.hasOwn(aliases, text) ? aliases[text] : text;
|
|
16
|
+
const location = ({ file, line }) => ({ file, line });
|
|
17
|
+
const prop = (name, value) => ({ name, type: typeof value, value });
|
|
18
|
+
function range(args, at, source) {
|
|
19
|
+
const start = args[at] === undefined ? 0 : readFrame(args[at], source);
|
|
20
|
+
const end = args[at + 1] === undefined ? Infinity : readFrame(args[at + 1], source);
|
|
21
|
+
if (start > end)
|
|
22
|
+
fail('invalid-range', 'Range ends before it starts', source);
|
|
23
|
+
return { start, end, source };
|
|
24
|
+
}
|
|
25
|
+
function addBinding(bindings, binding, target) {
|
|
26
|
+
if (bindings.some(b => b.start <= binding.end && binding.start <= b.end)) {
|
|
27
|
+
fail('overlapping-drivers', 'Overlapping drivers for ' + target, binding.source);
|
|
28
|
+
}
|
|
29
|
+
bindings.push(binding);
|
|
30
|
+
}
|
|
31
|
+
function coversTimeline(bindings) {
|
|
32
|
+
let next = 0;
|
|
33
|
+
for (const binding of [...bindings].sort((a, b) => a.start - b.start)) {
|
|
34
|
+
if (binding.start > next)
|
|
35
|
+
return false;
|
|
36
|
+
if (binding.end === Infinity)
|
|
37
|
+
return true;
|
|
38
|
+
next = binding.end + 1;
|
|
39
|
+
}
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
function normalizePath(text, owner, source) {
|
|
43
|
+
const parts = text.startsWith('/') ? [] : owner.slice(1).split('/');
|
|
44
|
+
for (const segment of (text.startsWith('/') ? text.slice(1) : text).split('/')) {
|
|
45
|
+
if (segment === '.')
|
|
46
|
+
continue;
|
|
47
|
+
if (segment === '..' && parts.length)
|
|
48
|
+
parts.pop();
|
|
49
|
+
else if (validSegment(segment))
|
|
50
|
+
parts.push(segment);
|
|
51
|
+
else
|
|
52
|
+
fail('invalid-path', 'Invalid path: ' + text, source);
|
|
53
|
+
}
|
|
54
|
+
return '/' + parts.join('/');
|
|
55
|
+
}
|
|
56
|
+
export function compileScene(source, options = {}) {
|
|
57
|
+
try {
|
|
58
|
+
const fps = options.fps ?? 30;
|
|
59
|
+
const { frameCount } = options;
|
|
60
|
+
if (!Number.isFinite(fps) || fps <= 0)
|
|
61
|
+
fail('invalid-fps', 'FPS must be finite and positive');
|
|
62
|
+
if (frameCount !== undefined && (!Number.isSafeInteger(frameCount) || frameCount < 0)) {
|
|
63
|
+
fail('invalid-frame-count', 'Frame count must be a nonnegative safe integer');
|
|
64
|
+
}
|
|
65
|
+
const decoder = new TextDecoder();
|
|
66
|
+
const expanded = expand(source.path, {
|
|
67
|
+
readFile: file => {
|
|
68
|
+
if (file === source.path)
|
|
69
|
+
return source.text;
|
|
70
|
+
const data = options.readFile?.(file);
|
|
71
|
+
return data instanceof Uint8Array ? decoder.decode(data) : data ?? null;
|
|
72
|
+
},
|
|
73
|
+
expandDirectives: true,
|
|
74
|
+
reservedCommands: ['expr', 'parent'],
|
|
75
|
+
});
|
|
76
|
+
if (expanded.problems.length)
|
|
77
|
+
return { diagnostics: expanded.problems.map(problem => ({
|
|
78
|
+
code: 'expansion', message: problem.message, source: location(problem),
|
|
79
|
+
})) };
|
|
80
|
+
return { value: compile(expanded.lines, location({ file: source.path, line: 0 }), fps, options) };
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (error instanceof EvaluationError)
|
|
84
|
+
return { diagnostics: [error.diagnostic] };
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function compile(lines, root, fps, options) {
|
|
89
|
+
const objects = [];
|
|
90
|
+
const channels = [];
|
|
91
|
+
const objNames = new Map();
|
|
92
|
+
const chNames = new Map();
|
|
93
|
+
const counters = new Map();
|
|
94
|
+
const drivers = new Map();
|
|
95
|
+
const events = [];
|
|
96
|
+
const pending = [];
|
|
97
|
+
const timed = [];
|
|
98
|
+
let nextId = 0;
|
|
99
|
+
function nextName(type, names) {
|
|
100
|
+
let n = counters.get(type) ?? 0;
|
|
101
|
+
let name;
|
|
102
|
+
do
|
|
103
|
+
name = prefixes[type] + ++n;
|
|
104
|
+
while (names.has(name));
|
|
105
|
+
counters.set(type, n);
|
|
106
|
+
return name;
|
|
107
|
+
}
|
|
108
|
+
function addObject(kind, source, name = nextName(kind, objNames)) {
|
|
109
|
+
const object = { id: nextId++, name, kind, source,
|
|
110
|
+
authored: defaults(kind), pathBindings: [], aimBindings: [] };
|
|
111
|
+
objects.push(object);
|
|
112
|
+
objNames.set(name, object);
|
|
113
|
+
return object;
|
|
114
|
+
}
|
|
115
|
+
function lookup(name, context, source) {
|
|
116
|
+
const object = context === 'ch' || name.startsWith('/ch/') ? undefined : objNames.get(name.replace(/^\/obj\//, ''));
|
|
117
|
+
const channel = context === 'obj' || name.startsWith('/obj/') ? undefined : chNames.get(name.replace(/^\/ch\//, ''));
|
|
118
|
+
if (object && channel)
|
|
119
|
+
fail('ambiguous-name', 'Ambiguous name: ' + name, source);
|
|
120
|
+
const node = object ?? channel;
|
|
121
|
+
if (!node)
|
|
122
|
+
fail('missing-target', 'Unknown ' + (context === 'either' ? 'node' : context === 'obj' ? 'object' : 'channel') + ': ' + name, source);
|
|
123
|
+
return node;
|
|
124
|
+
}
|
|
125
|
+
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
126
|
+
const channelOutput = (path, source) => {
|
|
127
|
+
const parts = path.split('/');
|
|
128
|
+
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
129
|
+
fail('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
130
|
+
}
|
|
131
|
+
const node = lookup(parts[2], 'ch', source);
|
|
132
|
+
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
133
|
+
fail('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
134
|
+
}
|
|
135
|
+
const name = parts[3] ?? 'value';
|
|
136
|
+
if (!node.ports.has(name))
|
|
137
|
+
fail('missing-target', 'Unknown channel port: ' + path, source);
|
|
138
|
+
return channelPath(node) + '/' + name;
|
|
139
|
+
};
|
|
140
|
+
const scalar = (node, param, source) => {
|
|
141
|
+
if (!Object.hasOwn(node.authored.parameters, param)) {
|
|
142
|
+
fail('unsupported-parameter', 'Unsupported parameter: ' + objectPath(node) + '/' + param, source);
|
|
143
|
+
}
|
|
144
|
+
return objectPath(node) + '/' + param;
|
|
145
|
+
};
|
|
146
|
+
const objectScalar = (path, source) => {
|
|
147
|
+
const [, context, name, param, extra] = path.split('/');
|
|
148
|
+
if (context !== 'obj' || !name || !param || extra !== undefined)
|
|
149
|
+
fail('invalid-path', 'Expected an object parameter path: ' + path, source);
|
|
150
|
+
return scalar(getObject(name, source), param, source);
|
|
151
|
+
};
|
|
152
|
+
let current = addObject('camera', root, 'Cam0');
|
|
153
|
+
let viewCamera;
|
|
154
|
+
let backgroundColor;
|
|
155
|
+
let display;
|
|
156
|
+
let curve;
|
|
157
|
+
for (const line of lines) {
|
|
158
|
+
const [word, ...args] = line.words;
|
|
159
|
+
const command = commandOf(word);
|
|
160
|
+
const source = location(line);
|
|
161
|
+
const numbers = (args) => args.map(value => readNumber(value, source));
|
|
162
|
+
if (curve && (word === 'point' || word === 'vertex' || word === 'v' || /^[+\-.\d]/.test(word))) {
|
|
163
|
+
const values = /^[+\-.\d]/.test(word) ? line.words : args;
|
|
164
|
+
checkArity('curve vertex', values, [3], source);
|
|
165
|
+
const [x, y, z] = numbers(values);
|
|
166
|
+
curve.points.push([x, y, z]);
|
|
167
|
+
}
|
|
168
|
+
else if (curve && command !== 'end') {
|
|
169
|
+
fail('invalid-curve', 'Expected a curve vertex or end', source);
|
|
170
|
+
}
|
|
171
|
+
else if (command === 'begin') {
|
|
172
|
+
checkArity(command, args, [0, 1], source);
|
|
173
|
+
if (args.length && args[0] !== 'draw')
|
|
174
|
+
fail('invalid-curve', 'Only authored curve vertices are supported', source);
|
|
175
|
+
current = addObject('curve', source);
|
|
176
|
+
curve = { object: current, points: [] };
|
|
177
|
+
}
|
|
178
|
+
else if (command === 'end') {
|
|
179
|
+
checkArity(command, args, [0], source);
|
|
180
|
+
if (!curve)
|
|
181
|
+
fail('invalid-curve', 'end without begin', source);
|
|
182
|
+
curve.object.geometry = { primitive: 'curve', args: [], vertices: curve.points };
|
|
183
|
+
curve = undefined;
|
|
184
|
+
}
|
|
185
|
+
else if (command === 'add' && args[0] !== 'channel') {
|
|
186
|
+
const [type, ...params] = args;
|
|
187
|
+
const kind = type === 'cam' ? 'camera' : primitives.has(type) ? 'geo' : type;
|
|
188
|
+
if (!['geo', 'null', 'curve', 'light', 'camera'].includes(kind))
|
|
189
|
+
fail('invalid-object-kind', 'Unknown object type: ' + type, source);
|
|
190
|
+
const limits = { cube: [0], grid: [0, 1, 2, 3, 4, 5], sphere: [0, 1, 2, 3] }[type];
|
|
191
|
+
if (limits || !primitives.has(type))
|
|
192
|
+
checkArity(command, params, limits ?? [0], source);
|
|
193
|
+
const values = numbers(params);
|
|
194
|
+
current = addObject(kind, source);
|
|
195
|
+
if (primitives.has(type))
|
|
196
|
+
current.geometry = { primitive: type, args: values };
|
|
197
|
+
else if (kind === 'curve')
|
|
198
|
+
current.geometry = { primitive: 'curve', args: [], vertices: [] };
|
|
199
|
+
}
|
|
200
|
+
else if (command === 'load' || command === 'add') {
|
|
201
|
+
checkArity(command, args, command === 'load' ? [2] : [1], source);
|
|
202
|
+
if (args[0] !== 'channel')
|
|
203
|
+
fail('invalid-operands', 'Expected load channel FILE', source);
|
|
204
|
+
const asset = args[1];
|
|
205
|
+
const data = command === 'load' ? loadTable(asset, options.readFile?.(asset) ?? null, source)
|
|
206
|
+
: { ports: new Map([['value', { source }]]) };
|
|
207
|
+
const channel = { id: nextId++, name: nextName('channel', chNames), source, ...data };
|
|
208
|
+
channels.push(channel);
|
|
209
|
+
chNames.set(channel.name, channel);
|
|
210
|
+
current = channel;
|
|
211
|
+
}
|
|
212
|
+
else if (command === 'select') {
|
|
213
|
+
checkArity(command, args, [1], source);
|
|
214
|
+
current = lookup(args[0], 'either', source);
|
|
215
|
+
}
|
|
216
|
+
else if (command === 'name') {
|
|
217
|
+
checkArity(command, args, [1], source);
|
|
218
|
+
const [name] = args;
|
|
219
|
+
if (!validSegment(name))
|
|
220
|
+
fail('invalid-name', 'Invalid name: ' + name, source);
|
|
221
|
+
if (isChannel(current))
|
|
222
|
+
rename(chNames, current, name, source);
|
|
223
|
+
else
|
|
224
|
+
rename(objNames, current, name, source);
|
|
225
|
+
}
|
|
226
|
+
else if (command === 'view') {
|
|
227
|
+
if (args[0] === 'camera' || args[0] === 'cam') {
|
|
228
|
+
checkArity(command, args, [2], source);
|
|
229
|
+
const camera = getObject(args[1], source);
|
|
230
|
+
if (camera.kind !== 'camera')
|
|
231
|
+
fail('invalid-target', 'Expected a camera: ' + args[1], source);
|
|
232
|
+
viewCamera = camera;
|
|
233
|
+
}
|
|
234
|
+
else if (args[0] === 'bgcolor') {
|
|
235
|
+
checkArity(command, args, [4], source);
|
|
236
|
+
const [r, g, b] = numbers(args.slice(1));
|
|
237
|
+
backgroundColor = [r, g, b];
|
|
238
|
+
}
|
|
239
|
+
else
|
|
240
|
+
fail('invalid-operands', 'Expected view camera NAME or view bgcolor R G B', source);
|
|
241
|
+
}
|
|
242
|
+
else if (command === 'display') {
|
|
243
|
+
checkArity(command, args, [1], source);
|
|
244
|
+
if (args[0] !== 'none')
|
|
245
|
+
fail('invalid-operands', 'Expected display none', source);
|
|
246
|
+
display = 'none';
|
|
247
|
+
}
|
|
248
|
+
else if (command === 'at') {
|
|
249
|
+
if (args.length < 2)
|
|
250
|
+
fail('invalid-operands', 'Expected at FRAME COMMAND', source);
|
|
251
|
+
const [frameText, eventCommand, ...values] = args;
|
|
252
|
+
const frame = readFrame(frameText, source);
|
|
253
|
+
if (commandOf(eventCommand) === 'select') {
|
|
254
|
+
checkArity('select', values, [1], source);
|
|
255
|
+
const selection = lookup(values[0], 'either', source);
|
|
256
|
+
if (isChannel(selection)) {
|
|
257
|
+
fail('event-missing-target', 'Object is not available at the event source: ' + values[0], source);
|
|
258
|
+
}
|
|
259
|
+
timed.push({ frame, source, selection });
|
|
260
|
+
}
|
|
261
|
+
else
|
|
262
|
+
timed.push({ frame, source, command: eventCommand, args: values, objectCount: nextId });
|
|
263
|
+
}
|
|
264
|
+
else if (deferred.has(command)) {
|
|
265
|
+
pending.push({ command, args, source, current });
|
|
266
|
+
}
|
|
267
|
+
else if (editCommands.has(command)) {
|
|
268
|
+
if (isChannel(current))
|
|
269
|
+
fail('invalid-subject', command + ' requires an object', source);
|
|
270
|
+
propertyEdit(command, args, current, source)(current.authored);
|
|
271
|
+
}
|
|
272
|
+
else
|
|
273
|
+
fail('unsupported-command', 'Unsupported scene command: ' + word, source);
|
|
274
|
+
}
|
|
275
|
+
if (curve)
|
|
276
|
+
fail('invalid-curve', 'Curve is missing end', curve.object.source);
|
|
277
|
+
for (const { command, args, source } of pending) {
|
|
278
|
+
if (command !== 'parent')
|
|
279
|
+
continue;
|
|
280
|
+
checkArity(command, args, [2], source);
|
|
281
|
+
const child = getObject(args[0], source);
|
|
282
|
+
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
283
|
+
}
|
|
284
|
+
const graph = new GraphBuilder();
|
|
285
|
+
const expressionPorts = new Map();
|
|
286
|
+
for (const channel of channels) {
|
|
287
|
+
for (const [name, port] of channel.ports)
|
|
288
|
+
expressionPorts.set(channelPath(channel) + '/' + name, { node: channel, name, port });
|
|
289
|
+
}
|
|
290
|
+
const inputs = new Map();
|
|
291
|
+
const depend = (path, on) => {
|
|
292
|
+
if (!inputs.has(path))
|
|
293
|
+
inputs.set(path, new Set());
|
|
294
|
+
inputs.get(path).add(on);
|
|
295
|
+
};
|
|
296
|
+
const bind = (text, path, source) => bindExpression(parseExpression(text, source), (ref, name) => {
|
|
297
|
+
const address = normalizePath(ref, path.slice(0, path.lastIndexOf('/')), source);
|
|
298
|
+
if (address.startsWith('/obj/')) {
|
|
299
|
+
if (path.startsWith('/ch/'))
|
|
300
|
+
fail('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address, source);
|
|
301
|
+
if (name !== 'ch')
|
|
302
|
+
fail('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
303
|
+
}
|
|
304
|
+
const input = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
|
|
305
|
+
depend(path, input);
|
|
306
|
+
return input;
|
|
307
|
+
});
|
|
308
|
+
for (const { command, args, source, current } of pending) {
|
|
309
|
+
if (command === 'parent')
|
|
310
|
+
continue;
|
|
311
|
+
const numberAt = (i, fallback) => args[i] === undefined ? fallback : readNumber(args[i], source);
|
|
312
|
+
if (command === 'expr') {
|
|
313
|
+
checkArity(command, args, [2], source);
|
|
314
|
+
const [target, text] = args;
|
|
315
|
+
if (!target.startsWith('/'))
|
|
316
|
+
fail('invalid-path', 'Expression targets must be absolute', source);
|
|
317
|
+
const address = normalizePath(target, '/', source);
|
|
318
|
+
const path = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
|
|
319
|
+
const expression = bind(text, path, source);
|
|
320
|
+
if (path.startsWith('/ch/')) {
|
|
321
|
+
const { port } = expressionPorts.get(path);
|
|
322
|
+
if (port.samples || port.expression)
|
|
323
|
+
fail('overlapping-drivers', 'Channel already has a driver: ' + path, source);
|
|
324
|
+
port.expression = expression;
|
|
325
|
+
port.source = source;
|
|
326
|
+
}
|
|
327
|
+
else
|
|
328
|
+
addDriver(path, { kind: 'expr', expression, ...range([], 0, source) });
|
|
329
|
+
}
|
|
330
|
+
else if (command === 'register' || command === 'ref') {
|
|
331
|
+
const register = command === 'register';
|
|
332
|
+
const end = register ? 3 : 4;
|
|
333
|
+
checkArity(command, args, [end, end + 1, end + 2, end + 3, end + 4, end + 6], source);
|
|
334
|
+
const targetIndex = register ? 0 : 2;
|
|
335
|
+
const path = scalar(getObject(args[targetIndex], source), args[targetIndex + 1], source);
|
|
336
|
+
if (register && !/^(?:[tr][xyz]|(?:diff|spec|amb)[rgba]|shine)$/.test(args[1])) {
|
|
337
|
+
fail('unsupported-parameter', 'register cannot write ' + path, source);
|
|
338
|
+
}
|
|
339
|
+
const input = register ? channelOutput(args[2].startsWith('/') ? args[2] : '/ch/' + args[2], source)
|
|
340
|
+
: scalar(getObject(args[0], source), args[1], source);
|
|
341
|
+
const common = { input, scale: numberAt(end + 1, 1), offset: numberAt(end + 2, 0), ...range(args, end + 4, source) };
|
|
342
|
+
const freq = numberAt(end, 1);
|
|
343
|
+
const shift = numberAt(end + 3, 0);
|
|
344
|
+
addDriver(path, register ? { kind: 'register', freq, shift, ...common } : { kind: 'ref', ...common });
|
|
345
|
+
depend(path, input);
|
|
346
|
+
}
|
|
347
|
+
else if (command === 'path') {
|
|
348
|
+
checkArity(command, args, [2, 5, 6, 7, 9], source);
|
|
349
|
+
const object = getObject(args[0], source);
|
|
350
|
+
const target = getObject(args[1], source);
|
|
351
|
+
if (!target.geometry?.vertices?.length)
|
|
352
|
+
fail('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
353
|
+
addBinding(object.pathBindings, {
|
|
354
|
+
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
355
|
+
freq: numberAt(5, 1), shift: numberAt(6, 0), ...range(args, 7, source),
|
|
356
|
+
}, positionStep(object));
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
checkArity(command, args, [2, 4], source);
|
|
360
|
+
const target = getObject(args[1], source);
|
|
361
|
+
if (command === 'children' && (args[0] !== 'lookat' || isChannel(current))) {
|
|
362
|
+
fail('invalid-subject', 'Expected children lookat TARGET with an object current', source);
|
|
363
|
+
}
|
|
364
|
+
const activeRange = range(args, 2, source);
|
|
365
|
+
const subjects = command === 'children' ? objects.filter(object => object.parent === current) : [getObject(args[0], source)];
|
|
366
|
+
for (const object of subjects)
|
|
367
|
+
addBinding(object.aimBindings, { target, ...activeRange }, worldStep(object));
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
function addDriver(path, driver) {
|
|
371
|
+
const bindings = drivers.get(path) ?? [];
|
|
372
|
+
addBinding(bindings, driver, path);
|
|
373
|
+
drivers.set(path, bindings);
|
|
374
|
+
}
|
|
375
|
+
function validateState(object, state, source) {
|
|
376
|
+
if (state.matrix && (object.aimBindings.length || object.pathBindings.length ||
|
|
377
|
+
[...translations, ...rotations, ...scales].some(p => drivers.has(objectPath(object) + '/' + p)))) {
|
|
378
|
+
fail('invalid-trs', 'Drivers and constraints require a non-sheared, nonsingular TRS matrix', source);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
for (const object of objects)
|
|
382
|
+
validateState(object, object.authored, object.source);
|
|
383
|
+
const state = new Map(objects.map(object => [object, copyState(object.authored)]));
|
|
384
|
+
const lightChanges = new Map();
|
|
385
|
+
let selected = current;
|
|
386
|
+
for (const event of timed.sort((a, b) => a.frame - b.frame)) {
|
|
387
|
+
if ('selection' in event) {
|
|
388
|
+
selected = event.selection;
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const { frame, args, source, objectCount } = event;
|
|
392
|
+
const command = commandOf(event.command);
|
|
393
|
+
if (!editCommands.has(command))
|
|
394
|
+
fail('forbidden-event-command', 'at cannot execute ' + event.command, source);
|
|
395
|
+
if (isChannel(selected) || selected.id >= objectCount)
|
|
396
|
+
fail('event-missing-target', 'No current object is available at the event source', source);
|
|
397
|
+
const edit = propertyEdit(command, args, selected, source);
|
|
398
|
+
edit(state.get(selected));
|
|
399
|
+
validateState(selected, state.get(selected), source);
|
|
400
|
+
events.push({ frame, object: selected, edit });
|
|
401
|
+
if (selected.kind === 'light' && command === 'light') {
|
|
402
|
+
if (!lightChanges.has(selected))
|
|
403
|
+
lightChanges.set(selected, new Map());
|
|
404
|
+
lightChanges.get(selected).set(frame, state.get(selected).light === 'spot');
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
for (const object of objects) {
|
|
408
|
+
if (object.kind !== 'light' || !object.aimBindings.length)
|
|
409
|
+
continue;
|
|
410
|
+
const changes = lightChanges.get(object) ?? new Map();
|
|
411
|
+
for (const binding of object.aimBindings) {
|
|
412
|
+
let spot = object.authored.light === 'spot';
|
|
413
|
+
for (const [frame, isSpot] of changes) {
|
|
414
|
+
if (frame > binding.start)
|
|
415
|
+
break;
|
|
416
|
+
spot = isSpot;
|
|
417
|
+
}
|
|
418
|
+
if (!spot || [...changes].some(([frame, isSpot]) => frame > binding.start && frame <= binding.end && !isSpot)) {
|
|
419
|
+
fail('invalid-lookat-subject', 'Only spotlights may use lookat', binding.source);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
for (const [path, { node, name, port }] of expressionPorts) {
|
|
424
|
+
if (!port.samples && !port.expression)
|
|
425
|
+
fail('missing-channel-driver', 'Missing driver for ' + path, port.source);
|
|
426
|
+
graph.add(path, node.table ? 'source:table' : 'source:expression', 'number', [prop('target', path), prop('port', name), ...(node.table ? [prop('asset', node.table.asset),
|
|
427
|
+
prop('sampling', node.table)] : [prop('ast', port.expression)])], port.source);
|
|
428
|
+
}
|
|
429
|
+
for (const object of objects) {
|
|
430
|
+
const path = objectPath(object);
|
|
431
|
+
const pos = positionStep(object), world = worldStep(object);
|
|
432
|
+
for (const param of Object.keys(object.authored.parameters)) {
|
|
433
|
+
const target = path + '/' + param;
|
|
434
|
+
const bindings = drivers.get(target) ?? [];
|
|
435
|
+
graph.add(target, 'parameter:scalar', 'number', [prop('target', target),
|
|
436
|
+
...bindings.map(binding => binding.kind === 'expr' ? prop('ast', binding.expression)
|
|
437
|
+
: prop('binding', { ...binding, end: Number.isFinite(binding.end) ? binding.end : null }))], bindings[0]?.source ?? object.source);
|
|
438
|
+
}
|
|
439
|
+
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
440
|
+
...object.pathBindings.map(binding => prop('path', { curve: objectPath(binding.curve),
|
|
441
|
+
freq: binding.freq, shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
442
|
+
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
443
|
+
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
444
|
+
...(object.parent ? [prop('parent', objectPath(object.parent))] : []),
|
|
445
|
+
...object.aimBindings.map(binding => prop('lookat', { target: objectPath(binding.target), start: binding.start,
|
|
446
|
+
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
447
|
+
translations.forEach(param => depend(pos, path + '/' + param));
|
|
448
|
+
[...rotations, ...scales].forEach(param => depend(world, path + '/' + param));
|
|
449
|
+
depend(world, pos);
|
|
450
|
+
if (object.parent) {
|
|
451
|
+
depend(world, worldStep(object.parent));
|
|
452
|
+
if (!coversTimeline(object.pathBindings))
|
|
453
|
+
depend(pos, worldStep(object.parent));
|
|
454
|
+
}
|
|
455
|
+
for (const binding of object.pathBindings)
|
|
456
|
+
depend(pos, worldStep(binding.curve));
|
|
457
|
+
for (const binding of object.aimBindings)
|
|
458
|
+
depend(world, positionStep(binding.target));
|
|
459
|
+
}
|
|
460
|
+
for (const [path, dependencies] of inputs)
|
|
461
|
+
for (const input of dependencies)
|
|
462
|
+
graph.depend(path, input);
|
|
463
|
+
const finished = graph.finish();
|
|
464
|
+
const scene = freeze({ fps, ...(options.frameCount !== undefined && { frameCount: options.frameCount }),
|
|
465
|
+
graph: finished.graph,
|
|
466
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
467
|
+
...(viewCamera && { camera: objectPath(viewCamera) }),
|
|
468
|
+
...(backgroundColor && { backgroundColor }),
|
|
469
|
+
...(display && { display }),
|
|
470
|
+
} }),
|
|
471
|
+
});
|
|
472
|
+
objects.sort((a, b) => comparePaths(objectPath(a), objectPath(b)));
|
|
473
|
+
channels.sort((a, b) => comparePaths(channelPath(a), channelPath(b)));
|
|
474
|
+
plans.set(scene, { objects, channels, drivers, events, order: finished.order });
|
|
475
|
+
return scene;
|
|
476
|
+
}
|
|
477
|
+
function rename(names, node, name, source) {
|
|
478
|
+
if (names.has(name) && names.get(name) !== node)
|
|
479
|
+
fail('duplicate-name', 'Name is already used: ' + name, source);
|
|
480
|
+
names.delete(node.name);
|
|
481
|
+
node.name = name;
|
|
482
|
+
names.set(name, node);
|
|
483
|
+
}
|