@vim3d/core 0.2.3 → 0.2.4
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 +7 -0
- package/compile.d.ts +6 -1
- package/compile.js +55 -51
- package/contexts.d.ts +5 -1
- package/contexts.js +45 -0
- package/esm/compile.js +57 -54
- package/esm/contexts.js +42 -0
- package/esm/evaluate.js +8 -2
- package/esm/index.js +1 -0
- package/esm/model.js +2 -1
- package/esm/storage.js +575 -0
- package/evaluate.js +8 -2
- package/index.d.ts +2 -0
- package/index.js +6 -1
- package/model.d.ts +20 -4
- package/model.js +4 -2
- package/package.json +3 -2
- package/storage.d.ts +36 -0
- package/storage.js +581 -0
package/README.md
CHANGED
|
@@ -29,6 +29,13 @@ exposes scalar, position, and transform dependencies. Frame snapshots hold
|
|
|
29
29
|
evaluated scalar values and final local and world transforms; a renderer
|
|
30
30
|
does not run expressions or constraints again.
|
|
31
31
|
|
|
32
|
+
Use `serializeStorageGraph(toStorageGraph(scene.value))` after successful
|
|
33
|
+
compilation to store the scene in the [`@fbp/spec` format](../../../docs/SPEC-2.md#2-storing-the-graph).
|
|
34
|
+
Use `fromStorageGraph(JSON.parse(text))` to load it back into a compiled scene
|
|
35
|
+
without rereading source or assets. The saved graph routes cross-context
|
|
36
|
+
data through subnet boundary ports and excludes view settings.
|
|
37
|
+
`hashStorageGraph` computes the SHA-256 digest of the canonical storage JSON.
|
|
38
|
+
|
|
32
39
|
For a procedural channel, use `add channel` and drive its `value` port
|
|
33
40
|
with `expr`. For live browser sources (such as Web Audio), declare
|
|
34
41
|
`liveChannels` in `compileScene` options and supply per-frame samples in
|
package/compile.d.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type Plan } from './model.js';
|
|
2
|
+
import { type CompiledScene, type CompileOptions, type Result, type SceneSource, type SourceLocation } from './types.js';
|
|
2
3
|
export declare function compileScene(source: SceneSource, options?: CompileOptions): Result<CompiledScene>;
|
|
4
|
+
export declare function finishScene(metadata: Pick<CompiledScene, 'fps' | 'frameCount' | 'ranges' | 'view'>, plan: Omit<Plan, 'order' | 'audioChannels'>, offTimeRefs?: readonly {
|
|
5
|
+
path: string;
|
|
6
|
+
source: SourceLocation;
|
|
7
|
+
}[]): CompiledScene;
|
package/compile.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.compileScene = compileScene;
|
|
4
|
+
exports.finishScene = finishScene;
|
|
4
5
|
const parser_1 = require("@vim3d/parser");
|
|
5
6
|
const channels_js_1 = require("./channels.js");
|
|
6
7
|
const ch_operators_js_1 = require("./ch-operators.js");
|
|
@@ -156,14 +157,12 @@ function compile(lines, root, fps, options) {
|
|
|
156
157
|
return node;
|
|
157
158
|
}
|
|
158
159
|
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
159
|
-
const channelOutput = (path, source
|
|
160
|
+
const channelOutput = (path, source) => {
|
|
160
161
|
const parts = path.split('/');
|
|
161
162
|
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
162
163
|
(0, types_js_1.fail)('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
163
164
|
}
|
|
164
165
|
const node = lookup(parts[2], 'ch', source);
|
|
165
|
-
if (ramp && !node.ramp)
|
|
166
|
-
(0, types_js_1.fail)('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
|
|
167
166
|
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
168
167
|
(0, types_js_1.fail)('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
169
168
|
}
|
|
@@ -378,7 +377,6 @@ function compile(lines, root, fps, options) {
|
|
|
378
377
|
const child = getObject(args[0], source);
|
|
379
378
|
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
380
379
|
}
|
|
381
|
-
const graph = new graph_js_1.GraphBuilder();
|
|
382
380
|
const ranges = Object.create(null);
|
|
383
381
|
const expressionPorts = new Map();
|
|
384
382
|
for (const channel of channels) {
|
|
@@ -398,28 +396,22 @@ function compile(lines, root, fps, options) {
|
|
|
398
396
|
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
399
397
|
(0, types_js_1.fail)('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
|
|
400
398
|
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
if (!(
|
|
404
|
-
(0, types_js_1.fail)('
|
|
399
|
+
const reject = (code, message) => (0, types_js_1.fail)(code, message, source);
|
|
400
|
+
if (!isBuiltInPath(address)) {
|
|
401
|
+
if (!registry.get(address.split('/')[1])) {
|
|
402
|
+
(0, types_js_1.fail)('invalid-path', 'Expected an object parameter path: ' + address, source);
|
|
405
403
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
}
|
|
409
|
-
if (from.type !== 'number')
|
|
410
|
-
(0, types_js_1.fail)('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
404
|
+
const from = extensionSource(address, source);
|
|
405
|
+
(0, contexts_js_1.validateExpressionSource)(targetStage, from, name, false, reject);
|
|
411
406
|
if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
|
|
412
407
|
contextWires.push({ to: path, input: from.address, from });
|
|
413
408
|
}
|
|
414
409
|
return from.address;
|
|
415
410
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
(0, types_js_1.fail)('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
421
|
-
}
|
|
422
|
-
const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
|
|
411
|
+
const stage = address.startsWith('/ch/') ? 'ch' : 'obj';
|
|
412
|
+
const channel = stage === 'ch' && channelOutput(address, source);
|
|
413
|
+
(0, contexts_js_1.validateExpressionSource)(targetStage, { stage, address, type: 'number' }, name, !!chNames.get(address.split('/')[2])?.ramp, reject);
|
|
414
|
+
const input = channel || objectScalar(address, source);
|
|
423
415
|
if (name === 'chf' || name === 'cht')
|
|
424
416
|
offTimeRefs.push({ path: input, source });
|
|
425
417
|
depend(path, input);
|
|
@@ -508,15 +500,14 @@ function compile(lines, root, fps, options) {
|
|
|
508
500
|
(0, model_js_1.checkArity)(command, args, [2], source);
|
|
509
501
|
const to = extensionAddress(args[1], source);
|
|
510
502
|
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
503
|
+
const reject = (code, message) => (0, types_js_1.fail)(code, message, source);
|
|
511
504
|
if (isBuiltInPath(to)) {
|
|
512
505
|
if (from.stage === 'audio' || from.stage === 'ch') {
|
|
513
506
|
if (from.type !== 'number')
|
|
514
507
|
(0, types_js_1.fail)('extension-port-type', 'Expected a numeric output at ' + from.address, source);
|
|
515
508
|
const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
|
|
516
509
|
const operator = expressionPorts.get(path)?.node.operator;
|
|
517
|
-
|
|
518
|
-
(0, types_js_1.fail)('extension-stage', 'Expected a channel operator input: ' + to, source);
|
|
519
|
-
}
|
|
510
|
+
(0, contexts_js_1.validateScalarConnectionSource)(to, !!operator, from, reject);
|
|
520
511
|
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
521
512
|
if (from.stage === 'ch')
|
|
522
513
|
depend(path, from.path);
|
|
@@ -530,8 +521,7 @@ function compile(lines, root, fps, options) {
|
|
|
530
521
|
const object = getObject(name, source);
|
|
531
522
|
if (object.kind !== 'geo')
|
|
532
523
|
(0, types_js_1.fail)('invalid-subject', 'A geometry connection requires a geometry object', source);
|
|
533
|
-
|
|
534
|
-
(0, types_js_1.fail)('extension-port-type', 'Expected object geometry at ' + to, source);
|
|
524
|
+
(0, contexts_js_1.validateGeometrySource)(to, from, reject);
|
|
535
525
|
if (object.geometry || object.geometrySource)
|
|
536
526
|
(0, types_js_1.fail)('overlapping-drivers', 'Geometry already assigned: ' + to, source);
|
|
537
527
|
object.geometrySource = from.address;
|
|
@@ -551,14 +541,10 @@ function compile(lines, root, fps, options) {
|
|
|
551
541
|
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
552
542
|
const expression = (0, expression_js_1.bindExpression)((0, expression_js_1.parseExpression)(text, source), (ref, name) => {
|
|
553
543
|
const address = normalizePath(ref, owner, source);
|
|
554
|
-
if (name === 'chramp')
|
|
555
|
-
channelOutput(address, source
|
|
544
|
+
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
545
|
+
channelOutput(address, source);
|
|
556
546
|
const from = extensionSource(address, source);
|
|
557
|
-
|
|
558
|
-
(0, types_js_1.fail)('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
559
|
-
}
|
|
560
|
-
if (from.type !== 'number')
|
|
561
|
-
(0, types_js_1.fail)('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
547
|
+
(0, contexts_js_1.validateExpressionSource)('extension', { ...from, address: name === 'chramp' ? address : from.address }, name, !!chNames.get(address.split('/')[2])?.ramp, (code, message) => (0, types_js_1.fail)(code, message, source));
|
|
562
548
|
if (name === 'chf' || name === 'cht')
|
|
563
549
|
offTimeRefs.push({ path: from.address, source });
|
|
564
550
|
references.set(from.address, from);
|
|
@@ -596,10 +582,13 @@ function compile(lines, root, fps, options) {
|
|
|
596
582
|
const target = getObject(args[1], source);
|
|
597
583
|
if (!target.geometry?.vertices?.length)
|
|
598
584
|
(0, types_js_1.fail)('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
585
|
+
const pos = (0, model_js_1.positionStep)(object);
|
|
599
586
|
addBinding(object.pathBindings, {
|
|
600
587
|
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
601
|
-
|
|
602
|
-
|
|
588
|
+
...(args[5] === undefined || (0, model_js_1.isNumberLiteral)(args[5]) ? { freq: numberAt(5, 1) }
|
|
589
|
+
: { position: bind(args[5], pos, source) }),
|
|
590
|
+
shift: numberAt(6, 0), ...range(args, 7, source),
|
|
591
|
+
}, pos);
|
|
603
592
|
}
|
|
604
593
|
else {
|
|
605
594
|
(0, model_js_1.checkArity)(command, args, [2, 4], source);
|
|
@@ -646,7 +635,7 @@ function compile(lines, root, fps, options) {
|
|
|
646
635
|
const edit = (0, model_js_1.propertyEdit)(command, args, selected, source);
|
|
647
636
|
edit(state.get(selected));
|
|
648
637
|
validateState(selected, state.get(selected), source);
|
|
649
|
-
events.push({ frame, object: selected, edit });
|
|
638
|
+
events.push({ frame, object: selected, command, args, source, edit });
|
|
650
639
|
if (selected.kind === 'light' && command === 'light') {
|
|
651
640
|
if (!lightChanges.has(selected))
|
|
652
641
|
lightChanges.set(selected, new Map());
|
|
@@ -669,6 +658,24 @@ function compile(lines, root, fps, options) {
|
|
|
669
658
|
}
|
|
670
659
|
}
|
|
671
660
|
}
|
|
661
|
+
return finishScene({
|
|
662
|
+
fps, frameCount: options.frameCount, ranges,
|
|
663
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
664
|
+
...(viewCamera && { camera: (0, model_js_1.objectPath)(viewCamera) }),
|
|
665
|
+
...(backgroundColor && { backgroundColor }),
|
|
666
|
+
...(display && { display }),
|
|
667
|
+
} }),
|
|
668
|
+
}, { objects, channels, drivers, events, extensions, inputs, contextWires }, offTimeRefs);
|
|
669
|
+
}
|
|
670
|
+
function finishScene(metadata, plan, offTimeRefs = []) {
|
|
671
|
+
const { fps, frameCount, ranges, view } = metadata;
|
|
672
|
+
const { objects, channels, drivers, events, extensions, inputs, contextWires } = plan;
|
|
673
|
+
const graph = new graph_js_1.GraphBuilder();
|
|
674
|
+
const expressionPorts = new Map();
|
|
675
|
+
for (const channel of channels) {
|
|
676
|
+
for (const [name, port] of channel.ports)
|
|
677
|
+
expressionPorts.set((0, model_js_1.channelPath)(channel) + '/' + name, { node: channel, name, port });
|
|
678
|
+
}
|
|
672
679
|
for (const [path, { node, name, port }] of expressionPorts) {
|
|
673
680
|
const extend = node.extend ? [prop('extend', node.extend)] : [];
|
|
674
681
|
if (node.ramp) {
|
|
@@ -705,24 +712,25 @@ function compile(lines, root, fps, options) {
|
|
|
705
712
|
}
|
|
706
713
|
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
707
714
|
...object.pathBindings.map(binding => prop('path', { curve: (0, model_js_1.objectPath)(binding.curve),
|
|
708
|
-
|
|
715
|
+
...(binding.position ? { position: binding.position } : { freq: binding.freq }),
|
|
716
|
+
shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
709
717
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
710
718
|
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
711
719
|
...(object.parent ? [prop('parent', (0, model_js_1.objectPath)(object.parent))] : []),
|
|
712
720
|
...object.aimBindings.map(binding => prop('lookat', { target: (0, model_js_1.objectPath)(binding.target), start: binding.start,
|
|
713
721
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
714
|
-
model_js_1.translations.forEach(param => depend(pos, path + '/' + param));
|
|
715
|
-
[...model_js_1.rotations, ...model_js_1.scales].forEach(param => depend(world, path + '/' + param));
|
|
716
|
-
depend(world, pos);
|
|
722
|
+
model_js_1.translations.forEach(param => graph.depend(pos, path + '/' + param));
|
|
723
|
+
[...model_js_1.rotations, ...model_js_1.scales].forEach(param => graph.depend(world, path + '/' + param));
|
|
724
|
+
graph.depend(world, pos);
|
|
717
725
|
if (object.parent) {
|
|
718
|
-
depend(world, (0, model_js_1.worldStep)(object.parent));
|
|
726
|
+
graph.depend(world, (0, model_js_1.worldStep)(object.parent));
|
|
719
727
|
if (!coversTimeline(object.pathBindings))
|
|
720
|
-
depend(pos, (0, model_js_1.worldStep)(object.parent));
|
|
728
|
+
graph.depend(pos, (0, model_js_1.worldStep)(object.parent));
|
|
721
729
|
}
|
|
722
730
|
for (const binding of object.pathBindings)
|
|
723
|
-
depend(pos, (0, model_js_1.worldStep)(binding.curve));
|
|
731
|
+
graph.depend(pos, (0, model_js_1.worldStep)(binding.curve));
|
|
724
732
|
for (const binding of object.aimBindings)
|
|
725
|
-
depend(world, (0, model_js_1.positionStep)(binding.target));
|
|
733
|
+
graph.depend(world, (0, model_js_1.positionStep)(binding.target));
|
|
726
734
|
}
|
|
727
735
|
for (const [path, dependencies] of inputs)
|
|
728
736
|
for (const input of dependencies)
|
|
@@ -761,19 +769,15 @@ function compile(lines, root, fps, options) {
|
|
|
761
769
|
}
|
|
762
770
|
const scene = (0, types_js_1.freeze)({
|
|
763
771
|
fps,
|
|
764
|
-
...(
|
|
772
|
+
...(frameCount !== undefined && { frameCount }),
|
|
765
773
|
graph: finished.graph,
|
|
766
|
-
...(Object.keys(ranges).length && { ranges }),
|
|
774
|
+
...(ranges && Object.keys(ranges).length && { ranges }),
|
|
767
775
|
liveChannels,
|
|
768
|
-
...(
|
|
769
|
-
...(viewCamera && { camera: (0, model_js_1.objectPath)(viewCamera) }),
|
|
770
|
-
...(backgroundColor && { backgroundColor }),
|
|
771
|
-
...(display && { display }),
|
|
772
|
-
} }),
|
|
776
|
+
...(view && { view }),
|
|
773
777
|
});
|
|
774
778
|
objects.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.objectPath)(a), (0, model_js_1.objectPath)(b)));
|
|
775
779
|
channels.sort((a, b) => (0, graph_js_1.comparePaths)((0, model_js_1.channelPath)(a), (0, model_js_1.channelPath)(b)));
|
|
776
|
-
model_js_1.plans.set(scene, {
|
|
780
|
+
model_js_1.plans.set(scene, { ...plan, order: finished.order, audioChannels });
|
|
777
781
|
return scene;
|
|
778
782
|
}
|
|
779
783
|
function rename(names, node, name, source) {
|
package/contexts.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type GraphBuilder } from './graph.js';
|
|
2
|
-
import { type Expression } from './expression.js';
|
|
2
|
+
import { type Expression, type ReferenceFunction } from './expression.js';
|
|
3
3
|
import { type ContextRegistry, type ContextStage } from './registry.js';
|
|
4
4
|
import { type AudioFrameData, type ContextValue, type ContextValues, type EvaluateFrameOptions, type ExtensionNodeType, type ExtensionPropDefinition, type FrameSnapshot, type GraphPort, type ParameterRange, type SourceLocation } from './types.js';
|
|
5
5
|
export declare function jsonProp(text: string, invalid: () => never): unknown;
|
|
@@ -17,6 +17,10 @@ export interface ExtensionSource {
|
|
|
17
17
|
type: GraphPort['type'];
|
|
18
18
|
stage: ContextStage;
|
|
19
19
|
}
|
|
20
|
+
type RejectSource = (code: string, message: string) => never;
|
|
21
|
+
export declare function validateExpressionSource(target: 'ch' | 'obj' | 'extension', from: Pick<ExtensionSource, 'address' | 'stage' | 'type'>, name: ReferenceFunction, ramp: boolean, reject: RejectSource): void;
|
|
22
|
+
export declare function validateGeometrySource(target: string, from: ExtensionSource, reject: RejectSource): void;
|
|
23
|
+
export declare function validateScalarConnectionSource(target: string, operator: boolean, from: ExtensionSource, reject: RejectSource): void;
|
|
20
24
|
type InputDriver = {
|
|
21
25
|
kind: 'wire';
|
|
22
26
|
from: ExtensionSource;
|
package/contexts.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Extensions = void 0;
|
|
4
4
|
exports.jsonProp = jsonProp;
|
|
5
|
+
exports.validateExpressionSource = validateExpressionSource;
|
|
6
|
+
exports.validateGeometrySource = validateGeometrySource;
|
|
7
|
+
exports.validateScalarConnectionSource = validateScalarConnectionSource;
|
|
5
8
|
exports.checkAudioFrame = checkAudioFrame;
|
|
6
9
|
exports.createContextEvaluation = createContextEvaluation;
|
|
7
10
|
const channels_js_1 = require("./channels.js");
|
|
@@ -64,6 +67,48 @@ function definitions(entries, types, kind, owner) {
|
|
|
64
67
|
}
|
|
65
68
|
return map;
|
|
66
69
|
}
|
|
70
|
+
function validateExpressionSource(target, from, name, ramp, reject) {
|
|
71
|
+
const { stage, address, type } = from;
|
|
72
|
+
if (name === 'chramp' && (stage !== 'ch' || !ramp)) {
|
|
73
|
+
reject('invalid-ramp', 'chramp requires a ch:ramp node: ' + address);
|
|
74
|
+
}
|
|
75
|
+
if (target === 'ch' || target === 'obj') {
|
|
76
|
+
if (stage === 'obj') {
|
|
77
|
+
if (target === 'ch')
|
|
78
|
+
reject('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address);
|
|
79
|
+
if (name !== 'ch')
|
|
80
|
+
reject('off-time-object-reference', name + ' requires a channel: ' + address);
|
|
81
|
+
}
|
|
82
|
+
else if (stage !== 'ch') {
|
|
83
|
+
if (!(0, registry_js_1.allowedSource)(target, stage))
|
|
84
|
+
reject('extension-stage', 'Built-in nodes cannot read ' + address);
|
|
85
|
+
if (stage !== 'audio' || name !== 'ch') {
|
|
86
|
+
reject('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else if (name !== 'ch' && stage !== 'ch') {
|
|
91
|
+
reject('off-time-extension-reference', name + ' requires a channel: ' + address);
|
|
92
|
+
}
|
|
93
|
+
if (type !== 'number')
|
|
94
|
+
reject('extension-port-type', 'Expected a number at ' + address);
|
|
95
|
+
}
|
|
96
|
+
function validateGeometrySource(target, from, reject) {
|
|
97
|
+
if (from.stage !== 'geo')
|
|
98
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
99
|
+
if (from.type !== 'object')
|
|
100
|
+
reject('extension-port-type', 'Expected object geometry at ' + target);
|
|
101
|
+
}
|
|
102
|
+
function validateScalarConnectionSource(target, operator, from, reject) {
|
|
103
|
+
if (from.stage !== 'audio' && from.stage !== 'ch') {
|
|
104
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
105
|
+
}
|
|
106
|
+
if (from.type !== 'number')
|
|
107
|
+
reject('extension-port-type', 'Expected a numeric output at ' + from.address);
|
|
108
|
+
if (from.stage === 'ch' && target.startsWith('/ch/') && !operator) {
|
|
109
|
+
reject('extension-stage', 'Expected a channel operator input: ' + target);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
67
112
|
class Extensions {
|
|
68
113
|
registry;
|
|
69
114
|
types = new Map();
|
package/esm/compile.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { expand } from '@vim3d/parser';
|
|
2
2
|
import { boundaryMode, cyclic, loadTable, validSegment } from './channels.js';
|
|
3
3
|
import { channelOperators } from './ch-operators.js';
|
|
4
|
-
import { Extensions, jsonProp } from './contexts.js';
|
|
4
|
+
import { Extensions, jsonProp, validateExpressionSource, validateGeometrySource, validateScalarConnectionSource, } from './contexts.js';
|
|
5
5
|
import { bindExpression, parseExpression } from './expression.js';
|
|
6
6
|
import { comparePaths, GraphBuilder } from './graph.js';
|
|
7
|
-
import {
|
|
7
|
+
import { createContextRegistry } from './registry.js';
|
|
8
8
|
import { rampStops } from './ramp.js';
|
|
9
|
-
import { channelPath, checkArity, copyState, defaults, editCommands, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
9
|
+
import { channelPath, checkArity, copyState, defaults, editCommands, isNumberLiteral, objectPath, plans, positionStep, propertyEdit, readFrame, readNumber, rotations, scales, translations, worldStep } from './model.js';
|
|
10
10
|
import { EvaluationError, fail, freeze } from './types.js';
|
|
11
11
|
const aliases = {
|
|
12
12
|
s: 'select', m: 'translate', mv: 'translate', cam: 'camera', l: 'light',
|
|
@@ -153,14 +153,12 @@ function compile(lines, root, fps, options) {
|
|
|
153
153
|
return node;
|
|
154
154
|
}
|
|
155
155
|
const getObject = (name, source) => lookup(name, 'obj', source);
|
|
156
|
-
const channelOutput = (path, source
|
|
156
|
+
const channelOutput = (path, source) => {
|
|
157
157
|
const parts = path.split('/');
|
|
158
158
|
if (parts[0] || parts[1] !== 'ch' || (parts.length !== 3 && parts.length !== 4)) {
|
|
159
159
|
fail('invalid-path', 'Expected a channel output path: ' + path, source);
|
|
160
160
|
}
|
|
161
161
|
const node = lookup(parts[2], 'ch', source);
|
|
162
|
-
if (ramp && !node.ramp)
|
|
163
|
-
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + path, source);
|
|
164
162
|
if (parts.length === 3 && (node.ports.size !== 1 || !node.ports.has('value'))) {
|
|
165
163
|
fail('ambiguous-channel', 'Channel shorthand requires a sole value port: ' + path, source);
|
|
166
164
|
}
|
|
@@ -375,7 +373,6 @@ function compile(lines, root, fps, options) {
|
|
|
375
373
|
const child = getObject(args[0], source);
|
|
376
374
|
child.parent = args[1] === '/obj' ? undefined : getObject(args[1], source);
|
|
377
375
|
}
|
|
378
|
-
const graph = new GraphBuilder();
|
|
379
376
|
const ranges = Object.create(null);
|
|
380
377
|
const expressionPorts = new Map();
|
|
381
378
|
for (const channel of channels) {
|
|
@@ -395,28 +392,22 @@ function compile(lines, root, fps, options) {
|
|
|
395
392
|
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
396
393
|
fail('invalid-ramp', 'chramp requires a ch:ramp node: ' + address, source);
|
|
397
394
|
const targetStage = path.startsWith('/ch/') ? 'ch' : 'obj';
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
if (!
|
|
401
|
-
fail('
|
|
395
|
+
const reject = (code, message) => fail(code, message, source);
|
|
396
|
+
if (!isBuiltInPath(address)) {
|
|
397
|
+
if (!registry.get(address.split('/')[1])) {
|
|
398
|
+
fail('invalid-path', 'Expected an object parameter path: ' + address, source);
|
|
402
399
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
}
|
|
406
|
-
if (from.type !== 'number')
|
|
407
|
-
fail('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
400
|
+
const from = extensionSource(address, source);
|
|
401
|
+
validateExpressionSource(targetStage, from, name, false, reject);
|
|
408
402
|
if (!contextWires.some(wire => wire.to === path && wire.from.address === from.address)) {
|
|
409
403
|
contextWires.push({ to: path, input: from.address, from });
|
|
410
404
|
}
|
|
411
405
|
return from.address;
|
|
412
406
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
fail('off-time-object-reference', name + ' requires a channel: ' + address, source);
|
|
418
|
-
}
|
|
419
|
-
const input = address.startsWith('/ch/') ? channelOutput(address, source, name === 'chramp') : objectScalar(address, source);
|
|
407
|
+
const stage = address.startsWith('/ch/') ? 'ch' : 'obj';
|
|
408
|
+
const channel = stage === 'ch' && channelOutput(address, source);
|
|
409
|
+
validateExpressionSource(targetStage, { stage, address, type: 'number' }, name, !!chNames.get(address.split('/')[2])?.ramp, reject);
|
|
410
|
+
const input = channel || objectScalar(address, source);
|
|
420
411
|
if (name === 'chf' || name === 'cht')
|
|
421
412
|
offTimeRefs.push({ path: input, source });
|
|
422
413
|
depend(path, input);
|
|
@@ -505,15 +496,14 @@ function compile(lines, root, fps, options) {
|
|
|
505
496
|
checkArity(command, args, [2], source);
|
|
506
497
|
const to = extensionAddress(args[1], source);
|
|
507
498
|
const from = extensionSource(extensionAddress(args[0], source), source);
|
|
499
|
+
const reject = (code, message) => fail(code, message, source);
|
|
508
500
|
if (isBuiltInPath(to)) {
|
|
509
501
|
if (from.stage === 'audio' || from.stage === 'ch') {
|
|
510
502
|
if (from.type !== 'number')
|
|
511
503
|
fail('extension-port-type', 'Expected a numeric output at ' + from.address, source);
|
|
512
504
|
const path = to.startsWith('/ch/') ? channelInput(to, source) : objectScalar(to, source);
|
|
513
505
|
const operator = expressionPorts.get(path)?.node.operator;
|
|
514
|
-
|
|
515
|
-
fail('extension-stage', 'Expected a channel operator input: ' + to, source);
|
|
516
|
-
}
|
|
506
|
+
validateScalarConnectionSource(to, !!operator, from, reject);
|
|
517
507
|
driveScalar(path, { kind: 'reference', name: 'ch', path: from.address }, source);
|
|
518
508
|
if (from.stage === 'ch')
|
|
519
509
|
depend(path, from.path);
|
|
@@ -527,8 +517,7 @@ function compile(lines, root, fps, options) {
|
|
|
527
517
|
const object = getObject(name, source);
|
|
528
518
|
if (object.kind !== 'geo')
|
|
529
519
|
fail('invalid-subject', 'A geometry connection requires a geometry object', source);
|
|
530
|
-
|
|
531
|
-
fail('extension-port-type', 'Expected object geometry at ' + to, source);
|
|
520
|
+
validateGeometrySource(to, from, reject);
|
|
532
521
|
if (object.geometry || object.geometrySource)
|
|
533
522
|
fail('overlapping-drivers', 'Geometry already assigned: ' + to, source);
|
|
534
523
|
object.geometrySource = from.address;
|
|
@@ -548,14 +537,10 @@ function compile(lines, root, fps, options) {
|
|
|
548
537
|
const owner = address.slice(0, address.lastIndexOf('/'));
|
|
549
538
|
const expression = bindExpression(parseExpression(text, source), (ref, name) => {
|
|
550
539
|
const address = normalizePath(ref, owner, source);
|
|
551
|
-
if (name === 'chramp')
|
|
552
|
-
channelOutput(address, source
|
|
540
|
+
if (name === 'chramp' && !address.startsWith('/ch/'))
|
|
541
|
+
channelOutput(address, source);
|
|
553
542
|
const from = extensionSource(address, source);
|
|
554
|
-
|
|
555
|
-
fail('off-time-extension-reference', name + ' requires a channel: ' + from.address, source);
|
|
556
|
-
}
|
|
557
|
-
if (from.type !== 'number')
|
|
558
|
-
fail('extension-port-type', 'Expected a number at ' + from.address, source);
|
|
543
|
+
validateExpressionSource('extension', { ...from, address: name === 'chramp' ? address : from.address }, name, !!chNames.get(address.split('/')[2])?.ramp, (code, message) => fail(code, message, source));
|
|
559
544
|
if (name === 'chf' || name === 'cht')
|
|
560
545
|
offTimeRefs.push({ path: from.address, source });
|
|
561
546
|
references.set(from.address, from);
|
|
@@ -593,10 +578,13 @@ function compile(lines, root, fps, options) {
|
|
|
593
578
|
const target = getObject(args[1], source);
|
|
594
579
|
if (!target.geometry?.vertices?.length)
|
|
595
580
|
fail('invalid-path-curve', 'Path requires a nonempty curve', source);
|
|
581
|
+
const pos = positionStep(object);
|
|
596
582
|
addBinding(object.pathBindings, {
|
|
597
583
|
curve: target, offset: [numberAt(2, 0), numberAt(3, 0), numberAt(4, 0)],
|
|
598
|
-
|
|
599
|
-
|
|
584
|
+
...(args[5] === undefined || isNumberLiteral(args[5]) ? { freq: numberAt(5, 1) }
|
|
585
|
+
: { position: bind(args[5], pos, source) }),
|
|
586
|
+
shift: numberAt(6, 0), ...range(args, 7, source),
|
|
587
|
+
}, pos);
|
|
600
588
|
}
|
|
601
589
|
else {
|
|
602
590
|
checkArity(command, args, [2, 4], source);
|
|
@@ -643,7 +631,7 @@ function compile(lines, root, fps, options) {
|
|
|
643
631
|
const edit = propertyEdit(command, args, selected, source);
|
|
644
632
|
edit(state.get(selected));
|
|
645
633
|
validateState(selected, state.get(selected), source);
|
|
646
|
-
events.push({ frame, object: selected, edit });
|
|
634
|
+
events.push({ frame, object: selected, command, args, source, edit });
|
|
647
635
|
if (selected.kind === 'light' && command === 'light') {
|
|
648
636
|
if (!lightChanges.has(selected))
|
|
649
637
|
lightChanges.set(selected, new Map());
|
|
@@ -666,6 +654,24 @@ function compile(lines, root, fps, options) {
|
|
|
666
654
|
}
|
|
667
655
|
}
|
|
668
656
|
}
|
|
657
|
+
return finishScene({
|
|
658
|
+
fps, frameCount: options.frameCount, ranges,
|
|
659
|
+
...((viewCamera || backgroundColor || display) && { view: {
|
|
660
|
+
...(viewCamera && { camera: objectPath(viewCamera) }),
|
|
661
|
+
...(backgroundColor && { backgroundColor }),
|
|
662
|
+
...(display && { display }),
|
|
663
|
+
} }),
|
|
664
|
+
}, { objects, channels, drivers, events, extensions, inputs, contextWires }, offTimeRefs);
|
|
665
|
+
}
|
|
666
|
+
export function finishScene(metadata, plan, offTimeRefs = []) {
|
|
667
|
+
const { fps, frameCount, ranges, view } = metadata;
|
|
668
|
+
const { objects, channels, drivers, events, extensions, inputs, contextWires } = plan;
|
|
669
|
+
const graph = new GraphBuilder();
|
|
670
|
+
const expressionPorts = new Map();
|
|
671
|
+
for (const channel of channels) {
|
|
672
|
+
for (const [name, port] of channel.ports)
|
|
673
|
+
expressionPorts.set(channelPath(channel) + '/' + name, { node: channel, name, port });
|
|
674
|
+
}
|
|
669
675
|
for (const [path, { node, name, port }] of expressionPorts) {
|
|
670
676
|
const extend = node.extend ? [prop('extend', node.extend)] : [];
|
|
671
677
|
if (node.ramp) {
|
|
@@ -702,24 +708,25 @@ function compile(lines, root, fps, options) {
|
|
|
702
708
|
}
|
|
703
709
|
graph.add(pos, 'transform:position', 'vector3', [prop('target', path),
|
|
704
710
|
...object.pathBindings.map(binding => prop('path', { curve: objectPath(binding.curve),
|
|
705
|
-
|
|
711
|
+
...(binding.position ? { position: binding.position } : { freq: binding.freq }),
|
|
712
|
+
shift: binding.shift, offset: binding.offset, start: binding.start,
|
|
706
713
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.pathBindings[0]?.source ?? object.source);
|
|
707
714
|
graph.add(world, 'transform:world', 'matrix4', [prop('target', path),
|
|
708
715
|
...(object.parent ? [prop('parent', objectPath(object.parent))] : []),
|
|
709
716
|
...object.aimBindings.map(binding => prop('lookat', { target: objectPath(binding.target), start: binding.start,
|
|
710
717
|
end: Number.isFinite(binding.end) ? binding.end : null }))], object.aimBindings[0]?.source ?? object.source);
|
|
711
|
-
translations.forEach(param => depend(pos, path + '/' + param));
|
|
712
|
-
[...rotations, ...scales].forEach(param => depend(world, path + '/' + param));
|
|
713
|
-
depend(world, pos);
|
|
718
|
+
translations.forEach(param => graph.depend(pos, path + '/' + param));
|
|
719
|
+
[...rotations, ...scales].forEach(param => graph.depend(world, path + '/' + param));
|
|
720
|
+
graph.depend(world, pos);
|
|
714
721
|
if (object.parent) {
|
|
715
|
-
depend(world, worldStep(object.parent));
|
|
722
|
+
graph.depend(world, worldStep(object.parent));
|
|
716
723
|
if (!coversTimeline(object.pathBindings))
|
|
717
|
-
depend(pos, worldStep(object.parent));
|
|
724
|
+
graph.depend(pos, worldStep(object.parent));
|
|
718
725
|
}
|
|
719
726
|
for (const binding of object.pathBindings)
|
|
720
|
-
depend(pos, worldStep(binding.curve));
|
|
727
|
+
graph.depend(pos, worldStep(binding.curve));
|
|
721
728
|
for (const binding of object.aimBindings)
|
|
722
|
-
depend(world, positionStep(binding.target));
|
|
729
|
+
graph.depend(world, positionStep(binding.target));
|
|
723
730
|
}
|
|
724
731
|
for (const [path, dependencies] of inputs)
|
|
725
732
|
for (const input of dependencies)
|
|
@@ -758,19 +765,15 @@ function compile(lines, root, fps, options) {
|
|
|
758
765
|
}
|
|
759
766
|
const scene = freeze({
|
|
760
767
|
fps,
|
|
761
|
-
...(
|
|
768
|
+
...(frameCount !== undefined && { frameCount }),
|
|
762
769
|
graph: finished.graph,
|
|
763
|
-
...(Object.keys(ranges).length && { ranges }),
|
|
770
|
+
...(ranges && Object.keys(ranges).length && { ranges }),
|
|
764
771
|
liveChannels,
|
|
765
|
-
...(
|
|
766
|
-
...(viewCamera && { camera: objectPath(viewCamera) }),
|
|
767
|
-
...(backgroundColor && { backgroundColor }),
|
|
768
|
-
...(display && { display }),
|
|
769
|
-
} }),
|
|
772
|
+
...(view && { view }),
|
|
770
773
|
});
|
|
771
774
|
objects.sort((a, b) => comparePaths(objectPath(a), objectPath(b)));
|
|
772
775
|
channels.sort((a, b) => comparePaths(channelPath(a), channelPath(b)));
|
|
773
|
-
plans.set(scene, {
|
|
776
|
+
plans.set(scene, { ...plan, order: finished.order, audioChannels });
|
|
774
777
|
return scene;
|
|
775
778
|
}
|
|
776
779
|
function rename(names, node, name, source) {
|
package/esm/contexts.js
CHANGED
|
@@ -58,6 +58,48 @@ function definitions(entries, types, kind, owner) {
|
|
|
58
58
|
}
|
|
59
59
|
return map;
|
|
60
60
|
}
|
|
61
|
+
export function validateExpressionSource(target, from, name, ramp, reject) {
|
|
62
|
+
const { stage, address, type } = from;
|
|
63
|
+
if (name === 'chramp' && (stage !== 'ch' || !ramp)) {
|
|
64
|
+
reject('invalid-ramp', 'chramp requires a ch:ramp node: ' + address);
|
|
65
|
+
}
|
|
66
|
+
if (target === 'ch' || target === 'obj') {
|
|
67
|
+
if (stage === 'obj') {
|
|
68
|
+
if (target === 'ch')
|
|
69
|
+
reject('channel-object-reference', 'Channel expressions cannot read object parameters: ' + address);
|
|
70
|
+
if (name !== 'ch')
|
|
71
|
+
reject('off-time-object-reference', name + ' requires a channel: ' + address);
|
|
72
|
+
}
|
|
73
|
+
else if (stage !== 'ch') {
|
|
74
|
+
if (!allowedSource(target, stage))
|
|
75
|
+
reject('extension-stage', 'Built-in nodes cannot read ' + address);
|
|
76
|
+
if (stage !== 'audio' || name !== 'ch') {
|
|
77
|
+
reject('off-time-extension-reference', 'Only ch() can read built-in audio scalars: ' + address);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else if (name !== 'ch' && stage !== 'ch') {
|
|
82
|
+
reject('off-time-extension-reference', name + ' requires a channel: ' + address);
|
|
83
|
+
}
|
|
84
|
+
if (type !== 'number')
|
|
85
|
+
reject('extension-port-type', 'Expected a number at ' + address);
|
|
86
|
+
}
|
|
87
|
+
export function validateGeometrySource(target, from, reject) {
|
|
88
|
+
if (from.stage !== 'geo')
|
|
89
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
90
|
+
if (from.type !== 'object')
|
|
91
|
+
reject('extension-port-type', 'Expected object geometry at ' + target);
|
|
92
|
+
}
|
|
93
|
+
export function validateScalarConnectionSource(target, operator, from, reject) {
|
|
94
|
+
if (from.stage !== 'audio' && from.stage !== 'ch') {
|
|
95
|
+
reject('extension-stage', 'connect cannot drive this built-in input: ' + target);
|
|
96
|
+
}
|
|
97
|
+
if (from.type !== 'number')
|
|
98
|
+
reject('extension-port-type', 'Expected a numeric output at ' + from.address);
|
|
99
|
+
if (from.stage === 'ch' && target.startsWith('/ch/') && !operator) {
|
|
100
|
+
reject('extension-stage', 'Expected a channel operator input: ' + target);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
61
103
|
export class Extensions {
|
|
62
104
|
registry;
|
|
63
105
|
types = new Map();
|
package/esm/evaluate.js
CHANGED
|
@@ -182,8 +182,14 @@ function evaluate(scene, frame, options) {
|
|
|
182
182
|
let worldPosition;
|
|
183
183
|
if (binding) {
|
|
184
184
|
const vertices = binding.curve.geometry.vertices;
|
|
185
|
-
const
|
|
186
|
-
|
|
185
|
+
const sample = binding.position ? expr(binding.position, frame, binding.source) : binding.freq * frame;
|
|
186
|
+
const coordinate = finite(sample + binding.shift, 'Path vertex index', binding.source);
|
|
187
|
+
const left = Math.floor(coordinate);
|
|
188
|
+
const index = wrap(left, vertices.length);
|
|
189
|
+
const weight = coordinate - left;
|
|
190
|
+
const vertex = !weight || vertices.length === 1 ? vertices[index]
|
|
191
|
+
: add(scale(vertices[index], 1 - weight), scale(vertices[(index + 1) % vertices.length], weight));
|
|
192
|
+
worldPosition = add(point(worldMatrices.get(binding.curve), vertex), binding.offset);
|
|
187
193
|
}
|
|
188
194
|
else {
|
|
189
195
|
const parent = positioned.parent ? worldMatrices.get(positioned.parent) : identity;
|