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