@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.
@@ -0,0 +1,304 @@
1
+ import { validSegment } from './channels.js';
2
+ import { comparePaths } from './graph.js';
3
+ import { evaluateExpression } from './expression.js';
4
+ import { allowedSource } from './registry.js';
5
+ import { EvaluationError, fail, freeze, } from './types.js';
6
+ const nodeType = /^[a-zA-Z][a-zA-Z0-9_-]*:[a-zA-Z][a-zA-Z0-9_-]*$/;
7
+ const propTypes = new Set(['string', 'number', 'boolean', 'object']);
8
+ const portTypes = new Set([...propTypes, 'vector3', 'matrix4']);
9
+ function isRecord(value) {
10
+ return value !== null && typeof value === 'object' &&
11
+ (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
12
+ }
13
+ function matches(value, type) {
14
+ if (type === 'object')
15
+ return isRecord(value) || Array.isArray(value);
16
+ if (type === 'number')
17
+ return typeof value === 'number' && Number.isFinite(value);
18
+ if (type === 'vector3' || type === 'matrix4') {
19
+ return Array.isArray(value) && value.length === (type === 'vector3' ? 3 : 16) &&
20
+ Array.from(value).every(item => typeof item === 'number' && Number.isFinite(item));
21
+ }
22
+ return typeof value === type;
23
+ }
24
+ function copyValue(value, invalid, seen = new Set()) {
25
+ if (value === null)
26
+ return null;
27
+ if (typeof value === 'string' || typeof value === 'boolean' ||
28
+ (typeof value === 'number' && Number.isFinite(value)))
29
+ return value;
30
+ if ((!Array.isArray(value) && !isRecord(value)) || seen.has(value))
31
+ return invalid();
32
+ seen.add(value);
33
+ const copy = Array.isArray(value) ? Array.from(value, item => copyValue(item, invalid, seen))
34
+ : Object.fromEntries(Object.entries(value).map(([key, item]) => [key, copyValue(item, invalid, seen)]));
35
+ seen.delete(value);
36
+ return freeze(copy);
37
+ }
38
+ function definitions(entries, types, kind, owner) {
39
+ if (!Array.isArray(entries))
40
+ fail('invalid-extension-definition', 'Expected ' + kind + ' definitions for ' + owner);
41
+ const map = new Map();
42
+ for (const entry of entries) {
43
+ if (!entry || typeof entry.name !== 'string' || !validSegment(entry.name) || !types.has(entry.type) || map.has(entry.name)) {
44
+ fail('invalid-extension-definition', 'Invalid or duplicate ' + kind + ' on ' + owner + ': ' + entry?.name);
45
+ }
46
+ map.set(entry.name, { ...entry });
47
+ }
48
+ return map;
49
+ }
50
+ export class Extensions {
51
+ registry;
52
+ types = new Map();
53
+ nodes = new Map();
54
+ constructor(registry) {
55
+ this.registry = registry;
56
+ for (const context of registry.contexts) {
57
+ const { name, stage, nodeTypes } = context;
58
+ if (stage === 'obj' || stage === 'ch' || stage === 'reserved')
59
+ continue;
60
+ const types = new Map();
61
+ for (const definition of nodeTypes) {
62
+ const { type, evaluate } = definition;
63
+ if (!nodeType.test(type) || typeof evaluate !== 'function')
64
+ fail('invalid-extension-definition', 'Invalid node type: ' + type);
65
+ if (types.has(type))
66
+ fail('duplicate-extension-type', 'Duplicate node type in /' + name + ': ' + type);
67
+ const owner = '/' + name + ' ' + type;
68
+ const inputs = definitions(definition.inputs, portTypes, 'input', owner);
69
+ const outputs = definitions(definition.outputs, portTypes, 'output', owner);
70
+ const props = definitions(definition.props, propTypes, 'prop', owner);
71
+ for (const [key, prop] of props) {
72
+ const invalid = () => fail('invalid-extension-definition', 'Invalid prop definition on ' + owner + ': ' + key);
73
+ if (prop.required !== undefined && typeof prop.required !== 'boolean')
74
+ invalid();
75
+ if (prop.default !== undefined) {
76
+ if (!matches(prop.default, prop.type))
77
+ invalid();
78
+ props.set(key, { ...prop, default: copyValue(prop.default, invalid) });
79
+ }
80
+ }
81
+ types.set(type, { type, inputs, outputs, props, evaluate });
82
+ }
83
+ this.types.set(name, types);
84
+ }
85
+ }
86
+ add(path, type, source) {
87
+ const [root, context, name, ...extra] = path.split('/');
88
+ if (root || !context || !validSegment(name) || extra.length)
89
+ fail('invalid-path', 'Expected an extension node path: ' + path, source);
90
+ const registered = this.registry.get(context);
91
+ if (!registered)
92
+ fail('unknown-extension-context', 'Unknown extension context: /' + context, source);
93
+ const definition = this.types.get(context)?.get(type);
94
+ if (!definition)
95
+ fail('unknown-extension-type', 'Unknown node type in /' + context + ': ' + type, source);
96
+ if (this.nodes.has(path))
97
+ fail('duplicate-name', 'Name is already used: ' + path, source);
98
+ const props = new Map();
99
+ for (const [key, def] of definition.props)
100
+ if (def.default !== undefined)
101
+ props.set(key, def.default);
102
+ const node = { path, context, stage: registered.stage, definition, source, props, drivers: new Map() };
103
+ this.nodes.set(path, node);
104
+ return node;
105
+ }
106
+ property(address, args, text, source) {
107
+ const { node, key } = this.member(address, source);
108
+ const def = node.definition.props.get(key);
109
+ if (!def)
110
+ fail('unknown-extension-prop', 'Unknown prop: ' + address, source);
111
+ const invalid = () => fail('invalid-extension-prop', 'Expected ' + def.type + ' for ' + address, source);
112
+ let value = args[0];
113
+ if (def.type === 'object') {
114
+ const raw = text?.trimEnd().match(/^[ \t:]*(?:set|"set")[ \t]+(?:"[^"]*"|[^ \t]+)[ \t]+(.*)$/)?.[1];
115
+ const literal = raw?.match(/^(?:"(?:\\.|[^"\\])*"|[^"#])*/)?.[0];
116
+ try {
117
+ value = JSON.parse(literal);
118
+ }
119
+ catch {
120
+ invalid();
121
+ }
122
+ }
123
+ else {
124
+ if (args.length !== 1)
125
+ fail('invalid-operands', 'Expected one value for ' + address, source);
126
+ if (def.type === 'number')
127
+ value = args[0].trim() === '' ? NaN : Number(args[0]);
128
+ if (def.type === 'boolean')
129
+ value = args[0] === 'true' ? true : args[0] === 'false' ? false : undefined;
130
+ }
131
+ if (!matches(value, def.type))
132
+ invalid();
133
+ node.props.set(key, copyValue(value, invalid));
134
+ }
135
+ input(address, type, driver, source) {
136
+ const { node, key } = this.member(address, source);
137
+ const port = node.definition.inputs.get(key);
138
+ if (!port)
139
+ fail('unknown-extension-port', 'Unknown input port: ' + address, source);
140
+ if (port.type !== type)
141
+ fail('extension-port-type', 'Expected ' + port.type + ' at ' + address + ', got ' + type, source);
142
+ for (const from of driver.kind === 'wire' ? [driver.from] : driver.references) {
143
+ if (!allowedSource(node.stage, from.stage))
144
+ fail('extension-stage', address + ' cannot read ' + from.address, source);
145
+ }
146
+ if (node.drivers.has(key))
147
+ fail('overlapping-drivers', 'Input already has a driver: ' + address, source);
148
+ node.drivers.set(key, driver);
149
+ }
150
+ output(address, source) {
151
+ const { node, key } = this.member(address, source);
152
+ if (node.stage === 'output')
153
+ fail('extension-stage', 'Cannot read an output-stage node: ' + node.path, source);
154
+ const port = node.definition.outputs.get(key);
155
+ if (!port)
156
+ fail('unknown-extension-port', 'Unknown output port: ' + address, source);
157
+ return { address, path: node.path, port: key, type: port.type, stage: node.stage };
158
+ }
159
+ member(address, source) {
160
+ const [root, context, name, key, ...extra] = address.split('/');
161
+ if (root || !context || !name || !key || extra.length)
162
+ fail('invalid-path', 'Expected an extension member path: ' + address, source);
163
+ if (!this.registry.get(context))
164
+ fail('unknown-extension-context', 'Unknown extension context: /' + context, source);
165
+ const path = '/' + context + '/' + name;
166
+ const node = this.nodes.get(path);
167
+ if (!node)
168
+ fail('missing-target', 'Unknown extension node: ' + path, source);
169
+ return { node, key };
170
+ }
171
+ addToGraph(graph) {
172
+ for (const node of this.nodes.values()) {
173
+ const { definition, path, context, source } = node;
174
+ for (const [name, prop] of definition.props) {
175
+ if (prop.required && !node.props.has(name))
176
+ fail('missing-extension-prop', 'Missing required prop: ' + path + '/' + name, source);
177
+ }
178
+ const props = [...node.props.keys()].sort(comparePaths).map(name => ({
179
+ name, type: definition.props.get(name).type, value: node.props.get(name),
180
+ }));
181
+ for (const input of [...node.drivers.keys()].sort(comparePaths)) {
182
+ const driver = node.drivers.get(input);
183
+ if (driver.kind === 'expr') {
184
+ props.push({ name: 'expr/' + input, type: 'object', value: driver.expression });
185
+ }
186
+ }
187
+ graph.addNode(path, { context, type: definition.type, props,
188
+ inputs: [...definition.inputs.values()], outputs: [...definition.outputs.values()] }, source);
189
+ }
190
+ for (const { path, drivers } of this.nodes.values()) {
191
+ for (const [input, driver] of drivers) {
192
+ for (const from of driver.kind === 'wire' ? [driver.from] : driver.references) {
193
+ graph.expose(from.path, { name: from.port, type: from.type });
194
+ graph.wire(path, input, from.path, from.port);
195
+ }
196
+ }
197
+ }
198
+ }
199
+ }
200
+ export function checkAudioFrame(path, sampleFrame, frame) {
201
+ if (Math.abs(sampleFrame - frame) > 1e-9) {
202
+ fail('off-time-audio-reference', 'Audio features are available only at the evaluated frame: ' + path);
203
+ }
204
+ }
205
+ export function createContextEvaluation(extensions, order, frame, time, fps, frameCount, options, readBuiltIn) {
206
+ const used = new Set([...extensions.nodes.values()].map(node => node.context));
207
+ const contextNames = extensions.registry.contexts.filter(({ stage, name }) => stage === 'evaluate' || stage === 'output' || used.has(name)).map(({ name }) => name).sort(comparePaths);
208
+ let base;
209
+ let objects = new Map();
210
+ const values = new Map();
211
+ const snapshots = new Map();
212
+ const sorted = [...extensions.nodes.keys()].sort(comparePaths);
213
+ const read = (path, sampleFrame) => {
214
+ if (path.startsWith('/ch/'))
215
+ return readBuiltIn(path, sampleFrame);
216
+ if (path.startsWith('/obj/')) {
217
+ const end = path.lastIndexOf('/');
218
+ const object = objects.get(path.slice(0, end));
219
+ const key = path.slice(end + 1);
220
+ if (key === 'snapshot')
221
+ return copyValue(object, () => fail('invalid-extension-input', 'Invalid object snapshot: ' + path));
222
+ if (key === 'worldMatrix' || key === 'localMatrix')
223
+ return object[key];
224
+ return object.parameters[key];
225
+ }
226
+ if (path.startsWith('/audio/'))
227
+ checkAudioFrame(path, sampleFrame, frame);
228
+ return values.get(path);
229
+ };
230
+ const snapshot = () => {
231
+ if (!contextNames.length)
232
+ return base;
233
+ const contexts = Object.fromEntries(contextNames.map(name => [name, []]));
234
+ for (const path of sorted) {
235
+ const node = snapshots.get(path);
236
+ if (node)
237
+ contexts[node.context].push(node);
238
+ }
239
+ return freeze({ ...base, contexts });
240
+ };
241
+ function run(stage) {
242
+ const inputSnapshot = stage === 'output' ? snapshot() : undefined;
243
+ for (const path of order) {
244
+ const node = extensions.nodes.get(path);
245
+ if (!node || node.stage !== stage)
246
+ continue;
247
+ const { definition, source, context } = node;
248
+ const inputs = freeze(Object.fromEntries([...node.drivers].map(([key, driver]) => [
249
+ key, driver.kind === 'wire' ? read(driver.from.address, frame) : evaluateExpression(driver.expression, {
250
+ frame, fps, frameCount, source: driver.source, read: (address, sampleFrame) => read(address, sampleFrame),
251
+ }),
252
+ ])));
253
+ const props = freeze(Object.fromEntries(node.props));
254
+ const evaluate = definition.evaluate;
255
+ const audio = options.audio;
256
+ const name = path.slice('/audio/'.length);
257
+ const data = stage === 'audio' && audio
258
+ ? Object.hasOwn(audio, path) ? audio[path] : Object.hasOwn(audio, name) ? audio[name] : undefined
259
+ : undefined;
260
+ const ctx = freeze({ path, frame, time, fps, inputs, props, ...(inputSnapshot && { snapshot: inputSnapshot }) });
261
+ let result;
262
+ try {
263
+ result = stage === 'audio' ? evaluate(ctx, data) : evaluate(ctx);
264
+ }
265
+ catch (error) {
266
+ if (error instanceof EvaluationError || (stage !== 'evaluate' && stage !== 'output'))
267
+ throw error;
268
+ const message = error instanceof Error ? error.message : String(error);
269
+ fail('plugin-error', `${path}: ${message}`, source);
270
+ }
271
+ const returned = result === undefined ? {} : result;
272
+ if (!isRecord(returned))
273
+ fail('invalid-extension-output', 'Expected an output record from ' + path, source);
274
+ for (const name of Object.keys(returned)) {
275
+ if (!definition.outputs.has(name))
276
+ fail('invalid-extension-output', 'Undeclared output: ' + path + '/' + name, source);
277
+ }
278
+ const outputs = Object.fromEntries([...definition.outputs].map(([name, { type }]) => {
279
+ const address = path + '/' + name;
280
+ const value = Object.hasOwn(returned, name) ? returned[name] : undefined;
281
+ const invalid = () => fail('invalid-extension-output', 'Expected ' + type + ' output at ' + address, source);
282
+ if (type === 'number' && typeof value === 'number' && !Number.isFinite(value)) {
283
+ fail('non-finite', address + ' must be finite', source);
284
+ }
285
+ if (!matches(value, type))
286
+ invalid();
287
+ const output = copyValue(value, invalid);
288
+ values.set(address, output);
289
+ return [name, output];
290
+ }));
291
+ snapshots.set(path, freeze({ path, context, type: definition.type, stage, props, inputs, outputs }));
292
+ }
293
+ }
294
+ return {
295
+ run, read,
296
+ finish(snapshotBase) {
297
+ base = snapshotBase;
298
+ objects = new Map(base.objects.map(object => [object.path, object]));
299
+ run('evaluate');
300
+ run('output');
301
+ return snapshot();
302
+ },
303
+ };
304
+ }
@@ -0,0 +1,213 @@
1
+ import { sample } from './channels.js';
2
+ import { checkAudioFrame, createContextEvaluation } from './contexts.js';
3
+ import { evaluateExpression } from './expression.js';
4
+ import { add, aim, column, euler, identity, inverseDirection, matrix, multiply, normalize, point, scale, sub, worldUp, wrap } from './math.js';
5
+ import { active, channelPath, copyState, isColor, objectPath, plans, positionStep, rotations, scales, translations, vector, worldStep } from './model.js';
6
+ import { EvaluationError, fail, finite, freeze } from './types.js';
7
+ export function evaluateFrame(scene, frame, options = {}) {
8
+ try {
9
+ if (!Number.isSafeInteger(frame) || frame < 0 || (scene.frameCount !== undefined && frame >= scene.frameCount)) {
10
+ fail('invalid-frame', 'Frame is outside the scene: ' + frame);
11
+ }
12
+ return { value: evaluate(scene, frame, options) };
13
+ }
14
+ catch (error) {
15
+ if (error instanceof EvaluationError)
16
+ return { diagnostics: [error.diagnostic] };
17
+ throw error;
18
+ }
19
+ }
20
+ function isLiveSample(input) {
21
+ return typeof input === 'object' && input !== null && 'values' in input
22
+ && typeof input.values === 'object' && input.values !== null;
23
+ }
24
+ function readLivePort(options, channel, portName, path, source) {
25
+ const input = options?.liveChannels?.[channel.name] ?? options?.liveChannels?.[channelPath(channel)];
26
+ if (!input || typeof input !== 'object') {
27
+ fail('missing-live-channel', 'Missing live channel input for ' + path, source);
28
+ }
29
+ let values;
30
+ if (isLiveSample(input)) {
31
+ if (input.timestamp !== undefined) {
32
+ finite(input.timestamp, 'Live channel timestamp for ' + channelPath(channel), source);
33
+ }
34
+ values = input.values;
35
+ }
36
+ else {
37
+ values = input;
38
+ }
39
+ const value = values[portName];
40
+ if (value === undefined)
41
+ fail('missing-live-channel', 'Missing live channel value for ' + path, source);
42
+ if (typeof value !== 'number')
43
+ fail('non-finite', path + ' must be finite', source);
44
+ return finite(value, path, source);
45
+ }
46
+ function evaluate(scene, frame, options) {
47
+ const plan = plans.get(scene);
48
+ if (!plan)
49
+ fail('invalid-scene', 'Scene was not created by compileScene');
50
+ if (options?.deterministic && scene.liveChannels.length) {
51
+ fail('non-deterministic-live-channel', 'Deterministic evaluation cannot run a scene with live channels');
52
+ }
53
+ const { fps, frameCount } = scene;
54
+ const time = finite(frame / fps, 'Scene time');
55
+ const state = new Map(plan.objects.map(object => [object, copyState(object.authored)]));
56
+ for (const event of plan.events) {
57
+ if (event.frame > frame)
58
+ break;
59
+ event.edit(state.get(event.object));
60
+ }
61
+ const parameters = new Map();
62
+ const positions = new Map();
63
+ const worlds = new Map();
64
+ for (const object of plan.objects) {
65
+ const path = objectPath(object);
66
+ for (const name of Object.keys(object.authored.parameters))
67
+ parameters.set(path + '/' + name, { object, name });
68
+ positions.set(positionStep(object), object);
69
+ worlds.set(worldStep(object), object);
70
+ }
71
+ const outputs = new Map();
72
+ for (const channel of plan.channels) {
73
+ for (const [name, port] of channel.ports) {
74
+ outputs.set(channelPath(channel) + '/' + name, { channel, name, port, cache: new Map() });
75
+ }
76
+ }
77
+ const contexts = createContextEvaluation(plan.extensions, plan.order, frame, time, fps, frameCount, options, read);
78
+ const expr = (ast, q, source) => evaluateExpression(ast, { frame: q, fps, frameCount, source, read });
79
+ function read(path, q) {
80
+ if (path.startsWith('/audio/'))
81
+ return contexts.read(path, q);
82
+ if (path.startsWith('/obj/')) {
83
+ const { object, name } = parameters.get(path);
84
+ return state.get(object).parameters[name];
85
+ }
86
+ if (plan.audioChannels.has(path)) {
87
+ checkAudioFrame(path, q, frame);
88
+ q = frame;
89
+ }
90
+ const { channel, name, port, cache } = outputs.get(path);
91
+ const value = cache.get(q);
92
+ if (value !== undefined)
93
+ return value;
94
+ let result;
95
+ if (port.live) {
96
+ if (q !== frame)
97
+ fail('off-time-live-channel', 'Live channel does not support off-time reads: ' + path, port.source);
98
+ result = readLivePort(options, channel, name, path, port.source);
99
+ }
100
+ else {
101
+ result = channel.table ? sample(channel.table, port, q, fps) : expr(port.expression, q, port.source);
102
+ }
103
+ cache.set(q, result);
104
+ return result;
105
+ }
106
+ const channels = Object.create(null);
107
+ contexts.run('audio');
108
+ for (const path of plan.order) {
109
+ if (outputs.has(path))
110
+ channels[path] = read(path, frame);
111
+ }
112
+ contexts.run('geo');
113
+ for (const path of plan.order) {
114
+ const target = parameters.get(path);
115
+ if (!target)
116
+ continue;
117
+ const { object, name } = target;
118
+ const values = state.get(object).parameters;
119
+ const binding = plan.drivers.get(path)?.find(range => active(frame, range));
120
+ let value = values[name];
121
+ if (binding?.kind === 'expr')
122
+ value = expr(binding.expression, frame, binding.source);
123
+ else if (binding?.kind === 'ref')
124
+ value = binding.scale * read(binding.input, frame) + binding.offset;
125
+ else if (binding?.kind === 'register') {
126
+ const at = (f) => read(binding.input, finite(binding.freq * f + binding.shift, 'Registration sample frame', binding.source));
127
+ if (name === 'rx' || name === 'ry' || name === 'rz') {
128
+ if (frame !== binding.start)
129
+ value += binding.scale * (at(binding.start) - at(frame));
130
+ }
131
+ else
132
+ value = binding.scale * at(frame) + binding.offset;
133
+ }
134
+ finite(value, path, binding?.source ?? object.source);
135
+ values[name] = isColor(name) ? Math.min(1, Math.max(0, value)) : value;
136
+ }
137
+ const worldPositions = new Map();
138
+ const localMatrices = new Map();
139
+ const worldMatrices = new Map();
140
+ for (const path of plan.order) {
141
+ const positioned = positions.get(path);
142
+ if (positioned) {
143
+ const authored = state.get(positioned);
144
+ const binding = positioned.pathBindings.find(range => active(frame, range));
145
+ let worldPosition;
146
+ if (binding) {
147
+ const vertices = binding.curve.geometry.vertices;
148
+ const index = wrap(Math.floor(finite(binding.freq * frame + binding.shift, 'Path vertex index', binding.source)), vertices.length);
149
+ worldPosition = add(point(worldMatrices.get(binding.curve), vertices[index]), binding.offset);
150
+ }
151
+ else {
152
+ const parent = positioned.parent ? worldMatrices.get(positioned.parent) : identity;
153
+ worldPosition = point(parent, vector(authored.parameters, translations));
154
+ }
155
+ for (const coordinate of worldPosition)
156
+ finite(coordinate, 'Final position', binding?.source ?? positioned.source);
157
+ worldPositions.set(positioned, worldPosition);
158
+ }
159
+ const object = worlds.get(path);
160
+ if (!object)
161
+ continue;
162
+ const authored = state.get(object);
163
+ const parent = object.parent ? worldMatrices.get(object.parent) : identity;
164
+ const sizes = vector(authored.parameters, scales);
165
+ const binding = object.aimBindings.find(range => active(frame, range));
166
+ let axes = euler(vector(authored.parameters, rotations));
167
+ if (binding) {
168
+ if (object.kind === 'light' && authored.light !== 'spot') {
169
+ fail('invalid-lookat-subject', 'Only spotlights may use lookat', binding.source);
170
+ }
171
+ const up = object.kind === 'camera' ? authored.up : worldUp;
172
+ const forward = object.kind === 'camera' || object.kind === 'light' ? -1 : 1;
173
+ axes = aim(parent, worldPositions.get(object), worldPositions.get(binding.target), up, forward, sizes, binding.source) ?? axes;
174
+ }
175
+ const pathBinding = object.pathBindings.find(range => active(frame, range));
176
+ const localPosition = pathBinding
177
+ ? inverseDirection(parent, sub(worldPositions.get(object), column(parent, 3)), pathBinding.source)
178
+ : vector(authored.parameters, translations);
179
+ const local = authored.matrix ?? matrix(localPosition, axes, sizes);
180
+ const world = multiply(parent, local);
181
+ for (const component of [...local, ...world])
182
+ finite(component, 'Final matrix component', binding?.source ?? object.source);
183
+ localMatrices.set(object, local);
184
+ worldMatrices.set(object, world);
185
+ }
186
+ const objects = plan.objects.map(object => {
187
+ const authored = state.get(object);
188
+ const p = authored.parameters;
189
+ const localMatrix = localMatrices.get(object), worldMatrix = worldMatrices.get(object);
190
+ const snapshot = {
191
+ path: objectPath(object), kind: object.kind, ...(object.parent && { parent: objectPath(object.parent) }),
192
+ localMatrix, worldMatrix, parameters: p, visible: authored.visible,
193
+ ...(object.geometry && { geometry: object.geometry }),
194
+ ...(object.geometrySource && { proceduralGeometry: contexts.read(object.geometrySource, frame) }),
195
+ ...(Object.hasOwn(p, 'diffr') && { material: {
196
+ diffuse: [p.diffr, p.diffg, p.diffb, p.diffa], ambient: [p.ambr, p.ambg, p.ambb, p.amba],
197
+ specular: [p.specr, p.specg, p.specb, p.speca], ...(p.shine !== undefined && { shininess: p.shine }),
198
+ } }),
199
+ ...(object.kind === 'camera' && { camera: {
200
+ position: column(worldMatrix, 3), direction: normalize(scale(column(worldMatrix, 2), -1)),
201
+ up: normalize(column(worldMatrix, 1)), fovy: p.fovy, znear: p.znear, zfar: p.zfar,
202
+ } }),
203
+ ...(object.kind === 'light' && { light: {
204
+ kind: authored.light, enabled: authored.enabled, position: column(worldMatrix, 3),
205
+ direction: normalize(scale(column(worldMatrix, 2), -1)),
206
+ } }),
207
+ };
208
+ return snapshot;
209
+ });
210
+ const snapshot = freeze({ frame, time, fps, objects, channels, cameras: objects.filter(o => o.kind === 'camera'),
211
+ lights: objects.filter(o => o.kind === 'light') });
212
+ return contexts.finish(snapshot);
213
+ }