@vim3d/core 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -1
- package/channels.d.ts +8 -0
- package/channels.js +74 -0
- package/compile.d.ts +2 -0
- package/compile.js +658 -0
- package/contexts.d.ts +55 -0
- package/contexts.js +310 -0
- package/esm/channels.js +68 -0
- package/esm/compile.js +655 -0
- package/esm/contexts.js +304 -0
- package/esm/evaluate.js +213 -0
- package/esm/expression.js +179 -0
- package/esm/graph.js +105 -0
- package/esm/index.js +3 -0
- package/esm/math.js +81 -0
- package/esm/model.js +165 -0
- package/esm/registry.js +93 -0
- package/esm/types.js +23 -0
- package/evaluate.d.ts +2 -0
- package/evaluate.js +216 -0
- package/expression.d.ts +65 -0
- package/expression.js +184 -0
- package/graph.d.ts +14 -0
- package/graph.js +110 -0
- package/index.d.ts +6 -0
- package/index.js +8 -1
- package/math.d.ts +25 -0
- package/math.js +101 -0
- package/model.d.ts +107 -0
- package/model.js +181 -0
- package/package.json +4 -1
- package/registry.d.ts +14 -0
- package/registry.js +98 -0
- package/types.d.ts +192 -0
- package/types.js +30 -0
package/compile.js
ADDED
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compileScene = compileScene;
|
|
4
|
+
const parser_1 = require("@vim3d/parser");
|
|
5
|
+
const channels_js_1 = require("./channels.js");
|
|
6
|
+
const contexts_js_1 = require("./contexts.js");
|
|
7
|
+
const expression_js_1 = require("./expression.js");
|
|
8
|
+
const graph_js_1 = require("./graph.js");
|
|
9
|
+
const registry_js_1 = require("./registry.js");
|
|
10
|
+
const model_js_1 = require("./model.js");
|
|
11
|
+
const types_js_1 = require("./types.js");
|
|
12
|
+
const aliases = {
|
|
13
|
+
s: 'select', m: 'translate', mv: 'translate', cam: 'camera', l: 'light',
|
|
14
|
+
zNear: 'znear', zFar: 'zfar', reg: 'register', reference: 'ref', disp: 'display',
|
|
15
|
+
};
|
|
16
|
+
const primitives = new Set(['cube', 'grid', 'sphere', 'ellipse', 'ellipseFace', 'circle', 'circleFace', 'ring']);
|
|
17
|
+
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']);
|
|
19
|
+
const isChannel = (node) => 'ports' in node;
|
|
20
|
+
const isObject = (node) => 'authored' in node;
|
|
21
|
+
const isExtension = (node) => 'definition' in node;
|
|
22
|
+
const isBuiltInPath = (path) => /^\/(?:obj|ch)\//.test(path);
|
|
23
|
+
const commandOf = (text) => Object.hasOwn(aliases, text) ? aliases[text] : text;
|
|
24
|
+
const location = ({ file, line }) => ({ file, line });
|
|
25
|
+
const prop = (name, value) => ({ name, type: typeof value, value });
|
|
26
|
+
function range(args, at, source) {
|
|
27
|
+
const start = args[at] === undefined ? 0 : (0, model_js_1.readFrame)(args[at], source);
|
|
28
|
+
const end = args[at + 1] === undefined ? Infinity : (0, model_js_1.readFrame)(args[at + 1], source);
|
|
29
|
+
if (start > end)
|
|
30
|
+
(0, types_js_1.fail)('invalid-range', 'Range ends before it starts', source);
|
|
31
|
+
return { start, end, source };
|
|
32
|
+
}
|
|
33
|
+
function addBinding(bindings, binding, target) {
|
|
34
|
+
if (bindings.some(b => b.start <= binding.end && binding.start <= b.end)) {
|
|
35
|
+
(0, types_js_1.fail)('overlapping-drivers', 'Overlapping drivers for ' + target, binding.source);
|
|
36
|
+
}
|
|
37
|
+
bindings.push(binding);
|
|
38
|
+
}
|
|
39
|
+
function coversTimeline(bindings) {
|
|
40
|
+
let next = 0;
|
|
41
|
+
for (const binding of [...bindings].sort((a, b) => a.start - b.start)) {
|
|
42
|
+
if (binding.start > next)
|
|
43
|
+
return false;
|
|
44
|
+
if (binding.end === Infinity)
|
|
45
|
+
return true;
|
|
46
|
+
next = binding.end + 1;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
function normalizePath(text, owner, source) {
|
|
51
|
+
const parts = text.startsWith('/') ? [] : owner.slice(1).split('/');
|
|
52
|
+
for (const segment of (text.startsWith('/') ? text.slice(1) : text).split('/')) {
|
|
53
|
+
if (segment === '.')
|
|
54
|
+
continue;
|
|
55
|
+
if (segment === '..' && parts.length)
|
|
56
|
+
parts.pop();
|
|
57
|
+
else if ((0, channels_js_1.validSegment)(segment))
|
|
58
|
+
parts.push(segment);
|
|
59
|
+
else
|
|
60
|
+
(0, types_js_1.fail)('invalid-path', 'Invalid path: ' + text, source);
|
|
61
|
+
}
|
|
62
|
+
return '/' + parts.join('/');
|
|
63
|
+
}
|
|
64
|
+
function compileScene(source, options = {}) {
|
|
65
|
+
try {
|
|
66
|
+
const fps = options.fps ?? 30;
|
|
67
|
+
const { frameCount } = options;
|
|
68
|
+
if (!Number.isFinite(fps) || fps <= 0)
|
|
69
|
+
(0, types_js_1.fail)('invalid-fps', 'FPS must be finite and positive');
|
|
70
|
+
if (frameCount !== undefined && (!Number.isSafeInteger(frameCount) || frameCount < 0)) {
|
|
71
|
+
(0, types_js_1.fail)('invalid-frame-count', 'Frame count must be a nonnegative safe integer');
|
|
72
|
+
}
|
|
73
|
+
const decoder = new TextDecoder();
|
|
74
|
+
const expanded = (0, parser_1.expand)(source.path, {
|
|
75
|
+
readFile: file => {
|
|
76
|
+
if (file === source.path)
|
|
77
|
+
return source.text;
|
|
78
|
+
const data = options.readFile?.(file);
|
|
79
|
+
return data instanceof Uint8Array ? decoder.decode(data) : data ?? null;
|
|
80
|
+
},
|
|
81
|
+
expandDirectives: true,
|
|
82
|
+
reservedCommands: ['expr', 'parent'],
|
|
83
|
+
preserveText: true,
|
|
84
|
+
});
|
|
85
|
+
if (expanded.problems.length)
|
|
86
|
+
return { diagnostics: expanded.problems.map(problem => ({
|
|
87
|
+
code: 'expansion', message: problem.message, source: location(problem),
|
|
88
|
+
})) };
|
|
89
|
+
return { value: compile(expanded.lines, location({ file: source.path, line: 0 }), fps, options) };
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
if (error instanceof types_js_1.EvaluationError)
|
|
93
|
+
return { diagnostics: [error.diagnostic] };
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function compile(lines, root, fps, options) {
|
|
98
|
+
const registry = (0, registry_js_1.createContextRegistry)(options.contexts);
|
|
99
|
+
const extensions = new contexts_js_1.Extensions(registry);
|
|
100
|
+
const objects = [];
|
|
101
|
+
const channels = [];
|
|
102
|
+
const objNames = new Map();
|
|
103
|
+
const chNames = new Map();
|
|
104
|
+
const counters = new Map();
|
|
105
|
+
const drivers = new Map();
|
|
106
|
+
const events = [];
|
|
107
|
+
const pending = [];
|
|
108
|
+
const timed = [];
|
|
109
|
+
let nextId = 0;
|
|
110
|
+
for (const [name, portNames] of Object.entries(options.liveChannels ?? {})) {
|
|
111
|
+
if (!(0, channels_js_1.validSegment)(name))
|
|
112
|
+
(0, types_js_1.fail)('invalid-live-channel', 'Invalid live channel name: ' + name, root);
|
|
113
|
+
if (!Array.isArray(portNames) || !portNames.length) {
|
|
114
|
+
(0, types_js_1.fail)('invalid-live-channel', 'Live channel must declare at least one port: ' + name, root);
|
|
115
|
+
}
|
|
116
|
+
const ports = new Map();
|
|
117
|
+
for (const portName of portNames) {
|
|
118
|
+
if (typeof portName !== 'string' || !(0, channels_js_1.validSegment)(portName)) {
|
|
119
|
+
(0, types_js_1.fail)('invalid-live-channel', 'Invalid live channel port name: ' + String(portName), root);
|
|
120
|
+
}
|
|
121
|
+
if (ports.has(portName)) {
|
|
122
|
+
(0, types_js_1.fail)('invalid-live-channel', 'Duplicate live channel port: /ch/' + name + '/' + portName, root);
|
|
123
|
+
}
|
|
124
|
+
ports.set(portName, { live: true, source: root });
|
|
125
|
+
}
|
|
126
|
+
const channel = { id: nextId++, name, source: root, live: true, ports };
|
|
127
|
+
channels.push(channel);
|
|
128
|
+
chNames.set(name, channel);
|
|
129
|
+
}
|
|
130
|
+
function nextName(type, names) {
|
|
131
|
+
let n = counters.get(type) ?? 0;
|
|
132
|
+
let name;
|
|
133
|
+
do
|
|
134
|
+
name = prefixes[type] + ++n;
|
|
135
|
+
while (names.has(name));
|
|
136
|
+
counters.set(type, n);
|
|
137
|
+
return name;
|
|
138
|
+
}
|
|
139
|
+
function addObject(kind, source, name = nextName(kind, objNames)) {
|
|
140
|
+
const object = { id: nextId++, name, kind, source,
|
|
141
|
+
authored: (0, model_js_1.defaults)(kind), pathBindings: [], aimBindings: [] };
|
|
142
|
+
objects.push(object);
|
|
143
|
+
objNames.set(name, object);
|
|
144
|
+
return object;
|
|
145
|
+
}
|
|
146
|
+
function lookup(name, context, source) {
|
|
147
|
+
const object = context === 'ch' || name.startsWith('/ch/') ? undefined : objNames.get(name.replace(/^\/obj\//, ''));
|
|
148
|
+
const channel = context === 'obj' || name.startsWith('/obj/') ? undefined : chNames.get(name.replace(/^\/ch\//, ''));
|
|
149
|
+
if (object && channel)
|
|
150
|
+
(0, types_js_1.fail)('ambiguous-name', 'Ambiguous name: ' + name, source);
|
|
151
|
+
const node = object ?? channel;
|
|
152
|
+
if (!node)
|
|
153
|
+
(0, types_js_1.fail)('missing-target', 'Unknown ' + (context === 'either' ? 'node' : context === 'obj' ? 'object' : 'channel') + ': ' + name, source);
|
|
154
|
+
return node;
|
|
155
|
+
}
|
|
156
|
+
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
157
|
+
const channelOutput = (path, source) => {
|
|
158
|
+
const parts = path.split('/');
|
|
159
|
+
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
160
|
+
(0, types_js_1.fail)('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
161
|
+
}
|
|
162
|
+
const node = lookup(parts[2], 'ch', source);
|
|
163
|
+
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
164
|
+
(0, types_js_1.fail)('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
165
|
+
}
|
|
166
|
+
const name = parts[3] ?? 'value';
|
|
167
|
+
if (!node.ports.has(name))
|
|
168
|
+
(0, types_js_1.fail)('missing-target', 'Unknown channel port: ' + path, source);
|
|
169
|
+
return (0, model_js_1.channelPath)(node) + '/' + name;
|
|
170
|
+
};
|
|
171
|
+
const scalar = (node, param, source) => {
|
|
172
|
+
if (!Object.hasOwn(node.authored.parameters, param)) {
|
|
173
|
+
(0, types_js_1.fail)('unsupported-parameter', 'Unsupported parameter: ' + (0, model_js_1.objectPath)(node) + '/' + param, source);
|
|
174
|
+
}
|
|
175
|
+
return (0, model_js_1.objectPath)(node) + '/' + param;
|
|
176
|
+
};
|
|
177
|
+
const objectScalar = (path, source) => {
|
|
178
|
+
const [, context, name, param, extra] = path.split('/');
|
|
179
|
+
if (context !== 'obj' || !name || !param || extra !== undefined)
|
|
180
|
+
(0, types_js_1.fail)('invalid-path', 'Expected an object parameter path: ' + path, source);
|
|
181
|
+
return scalar(getObject(name, source), param, source);
|
|
182
|
+
};
|
|
183
|
+
const extensionAddress = (path, source) => {
|
|
184
|
+
if (!path?.startsWith('/'))
|
|
185
|
+
(0, types_js_1.fail)('invalid-path', 'Expected an absolute path: ' + path, source);
|
|
186
|
+
return normalizePath(path, '/', source);
|
|
187
|
+
};
|
|
188
|
+
const extensionSource = (address, source) => {
|
|
189
|
+
if (address.startsWith('/ch/')) {
|
|
190
|
+
const path = channelOutput(address, source);
|
|
191
|
+
return { address: path, path, type: 'number', port: 'value', stage: 'ch' };
|
|
192
|
+
}
|
|
193
|
+
if (address.startsWith('/obj/')) {
|
|
194
|
+
const [, , name, member, ...extra] = address.split('/');
|
|
195
|
+
if (!member || extra.length)
|
|
196
|
+
(0, types_js_1.fail)('invalid-path', 'Expected an object output path: ' + address, source);
|
|
197
|
+
const object = getObject(name, source);
|
|
198
|
+
if (member === 'worldMatrix' || member === 'localMatrix' || member === 'snapshot') {
|
|
199
|
+
return { address, path: (0, model_js_1.worldStep)(object), port: member === 'worldMatrix' ? 'value' : member,
|
|
200
|
+
type: member === 'snapshot' ? 'object' : 'matrix4', stage: 'obj' };
|
|
201
|
+
}
|
|
202
|
+
const path = scalar(object, member, source);
|
|
203
|
+
return { address: path, path, type: 'number', port: 'value', stage: 'obj' };
|
|
204
|
+
}
|
|
205
|
+
return extensions.output(address, source);
|
|
206
|
+
};
|
|
207
|
+
let current = addObject('camera', root, 'Cam0');
|
|
208
|
+
let viewCamera;
|
|
209
|
+
let backgroundColor;
|
|
210
|
+
let display;
|
|
211
|
+
let curve;
|
|
212
|
+
for (const line of lines) {
|
|
213
|
+
const [word, ...args] = line.words;
|
|
214
|
+
const command = commandOf(word);
|
|
215
|
+
const source = location(line);
|
|
216
|
+
const numbers = (args) => args.map(value => (0, model_js_1.readNumber)(value, source));
|
|
217
|
+
if (curve && (word === 'point' || word === 'vertex' || word === 'v' || /^[+\-.\d]/.test(word))) {
|
|
218
|
+
const values = /^[+\-.\d]/.test(word) ? line.words : args;
|
|
219
|
+
(0, model_js_1.checkArity)('curve vertex', values, [3], source);
|
|
220
|
+
const [x, y, z] = numbers(values);
|
|
221
|
+
curve.points.push([x, y, z]);
|
|
222
|
+
}
|
|
223
|
+
else if (curve && command !== 'end') {
|
|
224
|
+
(0, types_js_1.fail)('invalid-curve', 'Expected a curve vertex or end', source);
|
|
225
|
+
}
|
|
226
|
+
else if (command === 'node') {
|
|
227
|
+
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
228
|
+
current = extensions.add(extensionAddress(args[0], source), args[1], source);
|
|
229
|
+
}
|
|
230
|
+
else if (command === 'begin') {
|
|
231
|
+
(0, model_js_1.checkArity)(command, args, [0, 1], source);
|
|
232
|
+
if (args.length && args[0] !== 'draw')
|
|
233
|
+
(0, types_js_1.fail)('invalid-curve', 'Only authored curve vertices are supported', source);
|
|
234
|
+
current = addObject('curve', source);
|
|
235
|
+
curve = { object: current, points: [] };
|
|
236
|
+
}
|
|
237
|
+
else if (command === 'end') {
|
|
238
|
+
(0, model_js_1.checkArity)(command, args, [0], source);
|
|
239
|
+
if (!curve)
|
|
240
|
+
(0, types_js_1.fail)('invalid-curve', 'end without begin', source);
|
|
241
|
+
curve.object.geometry = { primitive: 'curve', args: [], vertices: curve.points };
|
|
242
|
+
curve = undefined;
|
|
243
|
+
}
|
|
244
|
+
else if (command === 'add' && args[0] !== 'channel') {
|
|
245
|
+
const [type, ...params] = args;
|
|
246
|
+
const kind = type === 'cam' ? 'camera' : primitives.has(type) ? 'geo' : type;
|
|
247
|
+
if (!['geo', 'null', 'curve', 'light', 'camera'].includes(kind))
|
|
248
|
+
(0, types_js_1.fail)('invalid-object-kind', 'Unknown object type: ' + type, source);
|
|
249
|
+
const limits = { cube: [0], grid: [0, 1, 2, 3, 4, 5], sphere: [0, 1, 2, 3] }[type];
|
|
250
|
+
if (limits || !primitives.has(type))
|
|
251
|
+
(0, model_js_1.checkArity)(command, params, limits ?? [0], source);
|
|
252
|
+
const values = numbers(params);
|
|
253
|
+
current = addObject(kind, source);
|
|
254
|
+
if (primitives.has(type))
|
|
255
|
+
current.geometry = { primitive: type, args: values };
|
|
256
|
+
else if (kind === 'curve')
|
|
257
|
+
current.geometry = { primitive: 'curve', args: [], vertices: [] };
|
|
258
|
+
}
|
|
259
|
+
else if (command === 'load' || command === 'add') {
|
|
260
|
+
(0, model_js_1.checkArity)(command, args, command === 'load' ? [2] : [1], source);
|
|
261
|
+
if (args[0] !== 'channel')
|
|
262
|
+
(0, types_js_1.fail)('invalid-operands', 'Expected load channel FILE', source);
|
|
263
|
+
const asset = args[1];
|
|
264
|
+
const data = command === 'load' ? (0, channels_js_1.loadTable)(asset, options.readFile?.(asset) ?? null, source)
|
|
265
|
+
: { ports: new Map([['value', { source }]]) };
|
|
266
|
+
const channel = { id: nextId++, name: nextName('channel', chNames), source, ...data };
|
|
267
|
+
channels.push(channel);
|
|
268
|
+
chNames.set(channel.name, channel);
|
|
269
|
+
current = channel;
|
|
270
|
+
}
|
|
271
|
+
else if (command === 'select') {
|
|
272
|
+
(0, model_js_1.checkArity)(command, args, [1], source);
|
|
273
|
+
const address = args[0].startsWith('/') && !isBuiltInPath(args[0]) ? extensionAddress(args[0], source) : args[0];
|
|
274
|
+
current = extensions.nodes.get(address) ?? lookup(args[0], 'either', source);
|
|
275
|
+
}
|
|
276
|
+
else if (command === 'name') {
|
|
277
|
+
(0, model_js_1.checkArity)(command, args, [1], source);
|
|
278
|
+
const [name] = args;
|
|
279
|
+
if (!(0, channels_js_1.validSegment)(name))
|
|
280
|
+
(0, types_js_1.fail)('invalid-name', 'Invalid name: ' + name, source);
|
|
281
|
+
if (isExtension(current))
|
|
282
|
+
(0, types_js_1.fail)('invalid-subject', 'name requires an object or channel', source);
|
|
283
|
+
if (isChannel(current))
|
|
284
|
+
rename(chNames, current, name, source);
|
|
285
|
+
else
|
|
286
|
+
rename(objNames, current, name, source);
|
|
287
|
+
}
|
|
288
|
+
else if (command === 'view') {
|
|
289
|
+
if (args[0] === 'camera' || args[0] === 'cam') {
|
|
290
|
+
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
291
|
+
const camera = getObject(args[1], source);
|
|
292
|
+
if (camera.kind !== 'camera')
|
|
293
|
+
(0, types_js_1.fail)('invalid-target', 'Expected a camera: ' + args[1], source);
|
|
294
|
+
viewCamera = camera;
|
|
295
|
+
}
|
|
296
|
+
else if (args[0] === 'bgcolor') {
|
|
297
|
+
(0, model_js_1.checkArity)(command, args, [4], source);
|
|
298
|
+
const [r, g, b] = numbers(args.slice(1));
|
|
299
|
+
backgroundColor = [r, g, b];
|
|
300
|
+
}
|
|
301
|
+
else
|
|
302
|
+
(0, types_js_1.fail)('invalid-operands', 'Expected view camera NAME or view bgcolor R G B', source);
|
|
303
|
+
}
|
|
304
|
+
else if (command === 'display') {
|
|
305
|
+
(0, model_js_1.checkArity)(command, args, [1], source);
|
|
306
|
+
if (args[0] !== 'none')
|
|
307
|
+
(0, types_js_1.fail)('invalid-operands', 'Expected display none', source);
|
|
308
|
+
display = 'none';
|
|
309
|
+
}
|
|
310
|
+
else if (command === 'at') {
|
|
311
|
+
if (args.length < 2)
|
|
312
|
+
(0, types_js_1.fail)('invalid-operands', 'Expected at FRAME COMMAND', source);
|
|
313
|
+
const [frameText, eventCommand, ...values] = args;
|
|
314
|
+
const frame = (0, model_js_1.readFrame)(frameText, source);
|
|
315
|
+
if (commandOf(eventCommand) === 'select') {
|
|
316
|
+
(0, model_js_1.checkArity)('select', values, [1], source);
|
|
317
|
+
const selection = lookup(values[0], 'either', source);
|
|
318
|
+
if (isChannel(selection)) {
|
|
319
|
+
(0, types_js_1.fail)('event-missing-target', 'Object is not available at the event source: ' + values[0], source);
|
|
320
|
+
}
|
|
321
|
+
timed.push({ frame, source, selection });
|
|
322
|
+
}
|
|
323
|
+
else
|
|
324
|
+
timed.push({ frame, source, command: eventCommand, args: values, objectCount: nextId });
|
|
325
|
+
}
|
|
326
|
+
else if (deferred.has(command)) {
|
|
327
|
+
pending.push({ command, args, source, current, text: line.text });
|
|
328
|
+
}
|
|
329
|
+
else if (model_js_1.editCommands.has(command)) {
|
|
330
|
+
if (!isObject(current))
|
|
331
|
+
(0, types_js_1.fail)('invalid-subject', command + ' requires an object', source);
|
|
332
|
+
(0, model_js_1.propertyEdit)(command, args, current, source)(current.authored);
|
|
333
|
+
}
|
|
334
|
+
else
|
|
335
|
+
(0, types_js_1.fail)('unsupported-command', 'Unsupported scene command: ' + word, source);
|
|
336
|
+
}
|
|
337
|
+
if (curve)
|
|
338
|
+
(0, types_js_1.fail)('invalid-curve', 'Curve is missing end', curve.object.source);
|
|
339
|
+
for (const { command, args, source } of pending) {
|
|
340
|
+
if (command !== 'parent')
|
|
341
|
+
continue;
|
|
342
|
+
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
343
|
+
const child = getObject(args[0], source);
|
|
344
|
+
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
345
|
+
}
|
|
346
|
+
const graph = new graph_js_1.GraphBuilder();
|
|
347
|
+
const expressionPorts = new Map();
|
|
348
|
+
for (const channel of channels) {
|
|
349
|
+
for (const [name, port] of channel.ports)
|
|
350
|
+
expressionPorts.set((0, model_js_1.channelPath)(channel) + '/' + name, { node: channel, name, port });
|
|
351
|
+
}
|
|
352
|
+
const inputs = new Map();
|
|
353
|
+
const offTimeRefs = [];
|
|
354
|
+
const contextWires = [];
|
|
355
|
+
const depend = (path, on) => {
|
|
356
|
+
if (!inputs.has(path))
|
|
357
|
+
inputs.set(path, new Set());
|
|
358
|
+
inputs.get(path).add(on);
|
|
359
|
+
};
|
|
360
|
+
const bind = (text, path, source) => (0, expression_js_1.bindExpression)((0, expression_js_1.parseExpression)(text, source), (ref, name) => {
|
|
361
|
+
const address = normalizePath(ref, path.slice(0, path.lastIndexOf('/')), source);
|
|
362
|
+
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
363
|
+
if (!isBuiltInPath(address) && registry.get(address.split('/')[1])) {
|
|
364
|
+
const from = extensionSource(address, source);
|
|
365
|
+
if (!(0, registry_js_1.allowedSource)(targetStage, from.stage)) {
|
|
366
|
+
(0, types_js_1.fail)('extension-stage', 'Built-in nodes cannot read ' + address, source);
|
|
367
|
+
}
|
|
368
|
+
if (from.stage !== 'audio' || name !== 'ch') {
|
|
369
|
+
(0, types_js_1.fail)('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address, source);
|
|
370
|
+
}
|
|
371
|
+
if (from.type !== 'number')
|
|
372
|
+
(0, types_js_1.fail)('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
373
|
+
if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
|
|
374
|
+
contextWires.push({ to: path, input: from.address, from });
|
|
375
|
+
}
|
|
376
|
+
return from.address;
|
|
377
|
+
}
|
|
378
|
+
if (address.startsWith('/obj/')) {
|
|
379
|
+
if (path.startsWith('/ch/'))
|
|
380
|
+
(0, types_js_1.fail)('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address, source);
|
|
381
|
+
if (name !== 'ch')
|
|
382
|
+
(0, types_js_1.fail)('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
383
|
+
}
|
|
384
|
+
const input = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
|
|
385
|
+
if (name !== 'ch' && input.startsWith('/ch/'))
|
|
386
|
+
offTimeRefs.push({ path: input, source });
|
|
387
|
+
depend(path, input);
|
|
388
|
+
return input;
|
|
389
|
+
});
|
|
390
|
+
const driveScalar = (path, expression, source) => {
|
|
391
|
+
if (path.startsWith('/ch/')) {
|
|
392
|
+
const { port } = expressionPorts.get(path);
|
|
393
|
+
if (port.samples || port.expression || port.live)
|
|
394
|
+
(0, types_js_1.fail)('overlapping-drivers', 'Channel already has a driver: ' + path, source);
|
|
395
|
+
port.expression = expression;
|
|
396
|
+
port.source = source;
|
|
397
|
+
}
|
|
398
|
+
else
|
|
399
|
+
addDriver(path, { kind: 'expr', expression, ...range([], 0, source) });
|
|
400
|
+
};
|
|
401
|
+
for (const { command, args, source, current, text: lineText } of pending) {
|
|
402
|
+
if (command === 'parent')
|
|
403
|
+
continue;
|
|
404
|
+
const numberAt = (i, fallback) => args[i] === undefined ? fallback : (0, model_js_1.readNumber)(args[i], source);
|
|
405
|
+
if (command === 'set') {
|
|
406
|
+
const [target] = args;
|
|
407
|
+
if (!target)
|
|
408
|
+
(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 an extension node', source);
|
|
411
|
+
const address = target.startsWith('/') ? extensionAddress(target, source)
|
|
412
|
+
: current.path + '/' + target;
|
|
413
|
+
extensions.property(address, args.slice(1), lineText, source);
|
|
414
|
+
}
|
|
415
|
+
else if (command === 'connect') {
|
|
416
|
+
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
417
|
+
const to = extensionAddress(args[1], source);
|
|
418
|
+
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
419
|
+
if (isBuiltInPath(to)) {
|
|
420
|
+
if (from.stage === 'audio') {
|
|
421
|
+
if (from.type !== 'number')
|
|
422
|
+
(0, types_js_1.fail)('extension-port-type', 'Expected an audio scalar at ' + from.address, source);
|
|
423
|
+
const path = to.startsWith('/ch/') ? channelOutput(to, source) : objectScalar(to, source);
|
|
424
|
+
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
425
|
+
contextWires.push({ to: path, input: from.address, from });
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
const [, context, name, input, extra] = to.split('/');
|
|
429
|
+
if (context !== 'obj' || input !== 'geometry' || extra !== undefined || from.stage !== 'geo') {
|
|
430
|
+
(0, types_js_1.fail)('extension-stage', 'connect cannot drive this built-in input: ' + to, source);
|
|
431
|
+
}
|
|
432
|
+
const object = getObject(name, source);
|
|
433
|
+
if (object.kind !== 'geo')
|
|
434
|
+
(0, types_js_1.fail)('invalid-subject', 'A geometry connection requires a geometry object', source);
|
|
435
|
+
if (from.type !== 'object')
|
|
436
|
+
(0, types_js_1.fail)('extension-port-type', 'Expected object geometry at ' + to, source);
|
|
437
|
+
if (object.geometry || object.geometrySource)
|
|
438
|
+
(0, types_js_1.fail)('overlapping-drivers', 'Geometry already assigned: ' + to, source);
|
|
439
|
+
object.geometrySource = from.address;
|
|
440
|
+
contextWires.push({ to: (0, model_js_1.worldStep)(object), input, from });
|
|
441
|
+
}
|
|
442
|
+
else
|
|
443
|
+
extensions.input(to, from.type, { kind: 'wire', from }, source);
|
|
444
|
+
}
|
|
445
|
+
else if (command === 'expr') {
|
|
446
|
+
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
447
|
+
const [target, text] = args;
|
|
448
|
+
if (!target.startsWith('/'))
|
|
449
|
+
(0, types_js_1.fail)('invalid-path', 'Expression targets must be absolute', source);
|
|
450
|
+
const address = normalizePath(target, '/', source);
|
|
451
|
+
if (!isBuiltInPath(address) && (registry.get(address.split('/')[1]) || options.contexts?.length)) {
|
|
452
|
+
const references = new Map();
|
|
453
|
+
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
454
|
+
const expression = (0, expression_js_1.bindExpression)((0, expression_js_1.parseExpression)(text, source), (ref, name) => {
|
|
455
|
+
const from = extensionSource(normalizePath(ref, owner, source), source);
|
|
456
|
+
if (name !== 'ch' && !from.address.startsWith('/ch/')) {
|
|
457
|
+
(0, types_js_1.fail)('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
458
|
+
}
|
|
459
|
+
if (from.type !== 'number')
|
|
460
|
+
(0, types_js_1.fail)('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
461
|
+
if (name !== 'ch' && from.address.startsWith('/ch/'))
|
|
462
|
+
offTimeRefs.push({ path: from.address, source });
|
|
463
|
+
references.set(from.address, from);
|
|
464
|
+
return from.address;
|
|
465
|
+
});
|
|
466
|
+
extensions.input(address, 'number', { kind: 'expr', expression, references: [...references.values()], source }, source);
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
const path = address.startsWith('/ch/') ? channelOutput(address, source) : objectScalar(address, source);
|
|
470
|
+
driveScalar(path, bind(text, path, source), source);
|
|
471
|
+
}
|
|
472
|
+
else if (command === 'register' || command === 'ref') {
|
|
473
|
+
const register = command === 'register';
|
|
474
|
+
const end = register ? 3 : 4;
|
|
475
|
+
(0, model_js_1.checkArity)(command, args, [end, end + 1, end + 2, end + 3, end + 4, end + 6], source);
|
|
476
|
+
const targetIndex = register ? 0 : 2;
|
|
477
|
+
const path = scalar(getObject(args[targetIndex], source), args[targetIndex + 1], source);
|
|
478
|
+
if (register && !/^(?:[tr][xyz]|(?:diff|spec|amb)[rgba]|shine)$/.test(args[1])) {
|
|
479
|
+
(0, types_js_1.fail)('unsupported-parameter', 'register cannot write ' + path, source);
|
|
480
|
+
}
|
|
481
|
+
const input = register ? channelOutput(args[2].startsWith('/') ? args[2] : '/ch/' + args[2], source)
|
|
482
|
+
: scalar(getObject(args[0], source), args[1], source);
|
|
483
|
+
const common = { input, scale: numberAt(end + 1, 1), offset: numberAt(end + 2, 0), ...range(args, end + 4, source) };
|
|
484
|
+
const freq = numberAt(end, 1);
|
|
485
|
+
const shift = numberAt(end + 3, 0);
|
|
486
|
+
if (register && (freq !== 1 || shift !== 0 || args[1] === 'rx' || args[1] === 'ry' || args[1] === 'rz')) {
|
|
487
|
+
offTimeRefs.push({ path: input, source });
|
|
488
|
+
}
|
|
489
|
+
addDriver(path, register ? { kind: 'register', freq, shift, ...common } : { kind: 'ref', ...common });
|
|
490
|
+
depend(path, input);
|
|
491
|
+
}
|
|
492
|
+
else if (command === 'path') {
|
|
493
|
+
(0, model_js_1.checkArity)(command, args, [2, 5, 6, 7, 9], source);
|
|
494
|
+
const object = getObject(args[0], source);
|
|
495
|
+
const target = getObject(args[1], source);
|
|
496
|
+
if (!target.geometry?.vertices?.length)
|
|
497
|
+
(0, types_js_1.fail)('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
498
|
+
addBinding(object.pathBindings, {
|
|
499
|
+
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
500
|
+
freq: numberAt(5, 1), shift: numberAt(6, 0), ...range(args, 7, source),
|
|
501
|
+
}, (0, model_js_1.positionStep)(object));
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
(0, model_js_1.checkArity)(command, args, [2, 4], source);
|
|
505
|
+
const target = getObject(args[1], source);
|
|
506
|
+
if (command === 'children' && (args[0] !== 'lookat' || !isObject(current))) {
|
|
507
|
+
(0, types_js_1.fail)('invalid-subject', 'Expected children lookat TARGET with an object current', source);
|
|
508
|
+
}
|
|
509
|
+
const activeRange = range(args, 2, source);
|
|
510
|
+
const subjects = command === 'children' ? objects.filter(object => object.parent === current) : [getObject(args[0], source)];
|
|
511
|
+
for (const object of subjects)
|
|
512
|
+
addBinding(object.aimBindings, { target, ...activeRange }, (0, model_js_1.worldStep)(object));
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
function addDriver(path, driver) {
|
|
516
|
+
const bindings = drivers.get(path) ?? [];
|
|
517
|
+
addBinding(bindings, driver, path);
|
|
518
|
+
drivers.set(path, bindings);
|
|
519
|
+
}
|
|
520
|
+
function validateState(object, state, source) {
|
|
521
|
+
if (state.matrix && (object.aimBindings.length || object.pathBindings.length ||
|
|
522
|
+
[...model_js_1.translations, ...model_js_1.rotations, ...model_js_1.scales].some(p => drivers.has((0, model_js_1.objectPath)(object) + '/' + p)))) {
|
|
523
|
+
(0, types_js_1.fail)('invalid-trs', 'Drivers and constraints require a non-sheared, nonsingular TRS matrix', source);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
for (const object of objects)
|
|
527
|
+
validateState(object, object.authored, object.source);
|
|
528
|
+
const state = new Map(objects.map(object => [object, (0, model_js_1.copyState)(object.authored)]));
|
|
529
|
+
const lightChanges = new Map();
|
|
530
|
+
let selected = current;
|
|
531
|
+
for (const event of timed.sort((a, b) => a.frame - b.frame)) {
|
|
532
|
+
if ('selection' in event) {
|
|
533
|
+
selected = event.selection;
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
const { frame, args, source, objectCount } = event;
|
|
537
|
+
const command = commandOf(event.command);
|
|
538
|
+
if (!model_js_1.editCommands.has(command))
|
|
539
|
+
(0, types_js_1.fail)('forbidden-event-command', 'at cannot execute ' + event.command, source);
|
|
540
|
+
if (!isObject(selected) || selected.id >= objectCount)
|
|
541
|
+
(0, types_js_1.fail)('event-missing-target', 'No current object is available at the event source', source);
|
|
542
|
+
const edit = (0, model_js_1.propertyEdit)(command, args, selected, source);
|
|
543
|
+
edit(state.get(selected));
|
|
544
|
+
validateState(selected, state.get(selected), source);
|
|
545
|
+
events.push({ frame, object: selected, edit });
|
|
546
|
+
if (selected.kind === 'light' && command === 'light') {
|
|
547
|
+
if (!lightChanges.has(selected))
|
|
548
|
+
lightChanges.set(selected, new Map());
|
|
549
|
+
lightChanges.get(selected).set(frame, state.get(selected).light === 'spot');
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
for (const object of objects) {
|
|
553
|
+
if (object.kind !== 'light' || !object.aimBindings.length)
|
|
554
|
+
continue;
|
|
555
|
+
const changes = lightChanges.get(object) ?? new Map();
|
|
556
|
+
for (const binding of object.aimBindings) {
|
|
557
|
+
let spot = object.authored.light === 'spot';
|
|
558
|
+
for (const [frame, isSpot] of changes) {
|
|
559
|
+
if (frame > binding.start)
|
|
560
|
+
break;
|
|
561
|
+
spot = isSpot;
|
|
562
|
+
}
|
|
563
|
+
if (!spot || [...changes].some(([frame, isSpot]) => frame > binding.start && frame <= binding.end && !isSpot)) {
|
|
564
|
+
(0, types_js_1.fail)('invalid-lookat-subject', 'Only spotlights may use lookat', binding.source);
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
for (const [path, { node, name, port }] of expressionPorts) {
|
|
569
|
+
if (!port.samples && !port.expression && !port.live)
|
|
570
|
+
(0, types_js_1.fail)('missing-channel-driver', 'Missing driver for ' + path, port.source);
|
|
571
|
+
const nodeType = node.table ? 'source:table' : port.live ? 'source:live' : 'source:expression';
|
|
572
|
+
const nodeProps = node.table
|
|
573
|
+
? [prop('asset', node.table.asset), prop('sampling', node.table)]
|
|
574
|
+
: port.live ? [] : [prop('ast', port.expression)];
|
|
575
|
+
graph.add(path, nodeType, 'number', [prop('target', path), prop('port', name), ...nodeProps], port.source);
|
|
576
|
+
}
|
|
577
|
+
for (const object of objects) {
|
|
578
|
+
const path = (0, model_js_1.objectPath)(object);
|
|
579
|
+
const pos = (0, model_js_1.positionStep)(object), world = (0, model_js_1.worldStep)(object);
|
|
580
|
+
for (const param of Object.keys(object.authored.parameters)) {
|
|
581
|
+
const target = path + '/' + param;
|
|
582
|
+
const bindings = drivers.get(target) ?? [];
|
|
583
|
+
graph.add(target, 'parameter:scalar', 'number', [prop('target', target),
|
|
584
|
+
...bindings.map(binding => binding.kind === 'expr' ? prop('ast', binding.expression)
|
|
585
|
+
: prop('binding', { ...binding, end: Number.isFinite(binding.end) ? binding.end : null }))], bindings[0]?.source ?? object.source);
|
|
586
|
+
}
|
|
587
|
+
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
588
|
+
...object.pathBindings.map(binding => prop('path', { curve: (0, model_js_1.objectPath)(binding.curve),
|
|
589
|
+
freq: binding.freq, shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
590
|
+
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
591
|
+
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
592
|
+
...(object.parent ? [prop('parent', (0, model_js_1.objectPath)(object.parent))] : []),
|
|
593
|
+
...object.aimBindings.map(binding => prop('lookat', { target: (0, model_js_1.objectPath)(binding.target), start: binding.start,
|
|
594
|
+
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
595
|
+
model_js_1.translations.forEach(param => depend(pos, path + '/' + param));
|
|
596
|
+
[...model_js_1.rotations, ...model_js_1.scales].forEach(param => depend(world, path + '/' + param));
|
|
597
|
+
depend(world, pos);
|
|
598
|
+
if (object.parent) {
|
|
599
|
+
depend(world, (0, model_js_1.worldStep)(object.parent));
|
|
600
|
+
if (!coversTimeline(object.pathBindings))
|
|
601
|
+
depend(pos, (0, model_js_1.worldStep)(object.parent));
|
|
602
|
+
}
|
|
603
|
+
for (const binding of object.pathBindings)
|
|
604
|
+
depend(pos, (0, model_js_1.worldStep)(binding.curve));
|
|
605
|
+
for (const binding of object.aimBindings)
|
|
606
|
+
depend(world, (0, model_js_1.positionStep)(binding.target));
|
|
607
|
+
}
|
|
608
|
+
for (const [path, dependencies] of inputs)
|
|
609
|
+
for (const input of dependencies)
|
|
610
|
+
graph.depend(path, input);
|
|
611
|
+
extensions.addToGraph(graph);
|
|
612
|
+
for (const { to, input, from } of contextWires)
|
|
613
|
+
graph.wire(to, input, from.path, from.port);
|
|
614
|
+
const finished = graph.finish();
|
|
615
|
+
const livePorts = new Set();
|
|
616
|
+
for (const path of finished.order) {
|
|
617
|
+
const entry = expressionPorts.get(path);
|
|
618
|
+
if (!entry)
|
|
619
|
+
continue;
|
|
620
|
+
if (entry.port.live || [...(inputs.get(path) ?? [])].some(dep => livePorts.has(dep))) {
|
|
621
|
+
livePorts.add(path);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
for (const ref of offTimeRefs) {
|
|
625
|
+
if (livePorts.has(ref.path)) {
|
|
626
|
+
(0, types_js_1.fail)('off-time-live-channel', 'Live channel does not support off-time reads: ' + ref.path, ref.source);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const liveChannels = channels.filter(channel => channel.live).map(model_js_1.channelPath).sort(graph_js_1.comparePaths);
|
|
630
|
+
const audioChannels = new Set(contextWires.filter(({ to, from }) => to.startsWith('/ch/') && from.stage === 'audio').map(({ to }) => to));
|
|
631
|
+
for (const path of finished.order) {
|
|
632
|
+
if (path.startsWith('/ch/') && [...(inputs.get(path) ?? [])].some(input => audioChannels.has(input))) {
|
|
633
|
+
audioChannels.add(path);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
const scene = (0, types_js_1.freeze)({
|
|
637
|
+
fps,
|
|
638
|
+
...(options.frameCount !== undefined && { frameCount: options.frameCount }),
|
|
639
|
+
graph: finished.graph,
|
|
640
|
+
liveChannels,
|
|
641
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
642
|
+
...(viewCamera && { camera: (0, model_js_1.objectPath)(viewCamera) }),
|
|
643
|
+
...(backgroundColor && { backgroundColor }),
|
|
644
|
+
...(display && { display }),
|
|
645
|
+
} }),
|
|
646
|
+
});
|
|
647
|
+
objects.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.objectPath)(a), (0, model_js_1.objectPath)(b)));
|
|
648
|
+
channels.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.channelPath)(a), (0, model_js_1.channelPath)(b)));
|
|
649
|
+
model_js_1.plans.set(scene, { objects, channels, drivers, events, order: finished.order, extensions, audioChannels });
|
|
650
|
+
return scene;
|
|
651
|
+
}
|
|
652
|
+
function rename(names, node, name, source) {
|
|
653
|
+
if (names.has(name) && names.get(name) !== node)
|
|
654
|
+
(0, types_js_1.fail)('duplicate-name', 'Name is already used: ' + name, source);
|
|
655
|
+
names.delete(node.name);
|
|
656
|
+
node.name = name;
|
|
657
|
+
names.set(name, node);
|
|
658
|
+
}
|