@vim3d/core 0.2.0 → 0.2.2

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/ramp.js ADDED
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rampStops = rampStops;
4
+ exports.sampleRamp = sampleRamp;
5
+ const types_js_1 = require("./types.js");
6
+ function rampStops(value, source) {
7
+ const invalid = () => (0, types_js_1.fail)('invalid-ramp', 'Expected distinct ramp stops with finite values and positions in [0, 1]', source);
8
+ if (!Array.isArray(value) || !value.length)
9
+ return invalid();
10
+ const stops = value.map(stop => {
11
+ if (!stop || typeof stop !== 'object' || Array.isArray(stop))
12
+ return invalid();
13
+ const { pos, value, interp = 'linear' } = stop;
14
+ if (!Number.isFinite(pos) || pos < 0 || pos > 1 || !Number.isFinite(value) ||
15
+ (interp !== 'linear' && interp !== 'smooth' && interp !== 'constant'))
16
+ return invalid();
17
+ return { pos, value, interp };
18
+ }).sort((a, b) => a.pos - b.pos);
19
+ if (stops.some((stop, i) => i > 0 && stop.pos === stops[i - 1].pos))
20
+ return invalid();
21
+ return stops;
22
+ }
23
+ function sampleRamp(stops, u) {
24
+ const x = Math.max(0, Math.min(1, u));
25
+ if (x <= stops[0].pos)
26
+ return stops[0].value;
27
+ for (let i = 1; i < stops.length; i++) {
28
+ const left = stops[i - 1], right = stops[i];
29
+ if (x === right.pos)
30
+ return right.value;
31
+ if (x > right.pos)
32
+ continue;
33
+ if (left.interp === 'constant')
34
+ return left.value;
35
+ const t = (x - left.pos) / (right.pos - left.pos);
36
+ const w = left.interp === 'smooth' ? t * t * (3 - 2 * t) : t;
37
+ return left.value * (1 - w) + right.value * w;
38
+ }
39
+ return stops[stops.length - 1].value;
40
+ }
package/ranges.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { type ParameterRange } from './types.js';
2
+ export declare function applyRange(ranges: Readonly<Record<string, ParameterRange>> | undefined, path: string, value: number): number;
package/ranges.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.applyRange = applyRange;
4
+ const types_js_1 = require("./types.js");
5
+ function applyRange(ranges, path, value) {
6
+ const range = ranges?.[path];
7
+ if (!range || (value >= range.min && value <= range.max))
8
+ return value;
9
+ if (range.mode === 'error')
10
+ (0, types_js_1.fail)('range-violation', `${path} is ${value}, outside [${range.min}, ${range.max}]`, range.source);
11
+ return Math.max(range.min, Math.min(range.max, value));
12
+ }
package/registry.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { type ExtensionContextDefinition, type ExtensionNodeType } from './types.js';
2
+ export declare const geoTransformDefaults: {
3
+ tx: number;
4
+ ty: number;
5
+ tz: number;
6
+ sx: number;
7
+ sy: number;
8
+ sz: number;
9
+ };
10
+ export type ContextStage = 'audio' | 'ch' | 'geo' | 'obj' | 'evaluate' | 'output' | 'reserved';
11
+ export interface ContextDefinition {
12
+ readonly name: string;
13
+ readonly stage: ContextStage;
14
+ readonly nodeTypes: readonly ExtensionNodeType[];
15
+ }
16
+ export declare const BUILTIN_CONTEXTS: readonly ContextDefinition[];
17
+ export interface ContextRegistry {
18
+ readonly contexts: readonly ContextDefinition[];
19
+ readonly get: (name: string) => ContextDefinition | undefined;
20
+ }
21
+ export declare function createContextRegistry(extensions?: readonly ExtensionContextDefinition[]): ContextRegistry;
22
+ export declare function allowedSource(target: ContextStage, source: ContextStage): boolean;
package/registry.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BUILTIN_CONTEXTS = exports.geoTransformDefaults = void 0;
4
+ exports.createContextRegistry = createContextRegistry;
5
+ exports.allowedSource = allowedSource;
6
+ const ch_operators_js_1 = require("./ch-operators.js");
7
+ const types_js_1 = require("./types.js");
8
+ const ports = (type, ...names) => names.map(name => ({ name, type }));
9
+ const numeric = (defaults) => Object.entries(defaults)
10
+ .map(([name, value]) => ({ name, type: 'number', default: value }));
11
+ const audioSource = (type) => ({
12
+ type: 'audio:' + type,
13
+ inputs: [],
14
+ outputs: [...ports('object', 'signal'), ...ports('number', 'rms', 'peak')],
15
+ props: type === 'file' ? [{ name: 'asset', type: 'string', required: true }, ...numeric({ gain: 1 })]
16
+ : [{ name: 'device', type: 'string' }, ...numeric({ gain: 1 })],
17
+ evaluate: ({ path, props }, audio) => {
18
+ const gain = Math.abs(props.gain);
19
+ const rms = (0, types_js_1.finite)((audio?.rms ?? 0) * gain, path + '/rms');
20
+ const peak = (0, types_js_1.finite)((audio?.peak ?? 0) * gain, path + '/peak');
21
+ return { signal: { rms, peak }, rms, peak };
22
+ },
23
+ });
24
+ const audio = [
25
+ audioSource('file'), audioSource('mic'),
26
+ {
27
+ type: 'audio:fft',
28
+ inputs: ports('object', 'signal'),
29
+ outputs: [...ports('number', 'bass', 'mid', 'high', 'centroid', 'flux'), ...ports('object', 'spectrum')],
30
+ props: numeric({ fftSize: 2048, smoothing: 0.8 }),
31
+ evaluate: (_, data) => ({
32
+ bass: data?.bass ?? 0, mid: data?.mid ?? 0, high: data?.high ?? 0, centroid: data?.centroid ?? 0,
33
+ flux: data?.flux ?? 0, spectrum: data?.spectrum ?? [],
34
+ }),
35
+ },
36
+ {
37
+ type: 'audio:envelope',
38
+ inputs: ports('number', 'value'), outputs: ports('number', 'value'),
39
+ props: numeric({ attack: 0.01, release: 0.1 }),
40
+ evaluate: ({ inputs }, data) => ({ value: data?.rms ?? inputs.value ?? 0 }),
41
+ },
42
+ ];
43
+ exports.geoTransformDefaults = (0, types_js_1.freeze)({ tx: 0, ty: 0, tz: 0, sx: 1, sy: 1, sz: 1 });
44
+ const geo = [
45
+ {
46
+ type: 'geo:box', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ width: 1, height: 1, depth: 1 }),
47
+ evaluate: ({ props }) => ({ geometry: { primitive: 'box', args: [props.width, props.height, props.depth] } }),
48
+ },
49
+ {
50
+ type: 'geo:sphere', inputs: [], outputs: ports('object', 'geometry'), props: numeric({ radius: 1, segments: 32 }),
51
+ evaluate: ({ props }) => ({ geometry: { primitive: 'sphere', args: [props.radius, props.radius, props.segments] } }),
52
+ },
53
+ {
54
+ type: 'geo:transform',
55
+ inputs: [...ports('object', 'geometry'), ...ports('number', ...Object.keys(exports.geoTransformDefaults))],
56
+ outputs: ports('object', 'geometry'), props: [],
57
+ evaluate: ({ inputs }) => ({ geometry: {
58
+ kind: 'transform', geometry: inputs.geometry ?? null,
59
+ translate: [inputs.tx ?? exports.geoTransformDefaults.tx, inputs.ty ?? exports.geoTransformDefaults.ty, inputs.tz ?? exports.geoTransformDefaults.tz],
60
+ scale: [inputs.sx ?? exports.geoTransformDefaults.sx, inputs.sy ?? exports.geoTransformDefaults.sy, inputs.sz ?? exports.geoTransformDefaults.sz],
61
+ } }),
62
+ },
63
+ {
64
+ type: 'geo:merge', inputs: ports('object', 'a', 'b'), outputs: ports('object', 'geometry'), props: [],
65
+ evaluate: ({ inputs }) => ({ geometry: { kind: 'merge', geometries: [inputs.a, inputs.b].filter(value => value !== undefined) } }),
66
+ },
67
+ ];
68
+ exports.BUILTIN_CONTEXTS = (0, types_js_1.freeze)([
69
+ { name: 'audio', stage: 'audio', nodeTypes: audio },
70
+ { name: 'ch', stage: 'ch', nodeTypes: ch_operators_js_1.channelOperators },
71
+ { name: 'geo', stage: 'geo', nodeTypes: geo },
72
+ { name: 'obj', stage: 'obj', nodeTypes: [] },
73
+ { name: 'sim', stage: 'reserved', nodeTypes: [] },
74
+ ]);
75
+ function createContextRegistry(extensions = []) {
76
+ const contexts = new Map(exports.BUILTIN_CONTEXTS.map(context => [context.name, context]));
77
+ for (const context of extensions) {
78
+ if (typeof context.name !== 'string' || !/^[a-z][a-z0-9_-]*$/.test(context.name) ||
79
+ exports.BUILTIN_CONTEXTS.some(({ name }) => name === context.name)) {
80
+ (0, types_js_1.fail)('invalid-extension-context', 'Invalid or reserved extension context: ' + context.name);
81
+ }
82
+ if (contexts.has(context.name))
83
+ (0, types_js_1.fail)('duplicate-extension-context', 'Duplicate extension context: ' + context.name);
84
+ if ((context.stage !== 'evaluate' && context.stage !== 'output') || !Array.isArray(context.nodeTypes)) {
85
+ (0, types_js_1.fail)('invalid-extension-definition', 'Invalid stage or node types for /' + context.name);
86
+ }
87
+ contexts.set(context.name, Object.freeze({ ...context, nodeTypes: Object.freeze([...context.nodeTypes]) }));
88
+ }
89
+ return Object.freeze({ contexts: Object.freeze([...contexts.values()]), get: (name) => contexts.get(name) });
90
+ }
91
+ function allowedSource(target, source) {
92
+ switch (target) {
93
+ case 'audio': return source === 'audio';
94
+ case 'ch': return source === 'audio' || source === 'ch';
95
+ case 'geo': return source === 'audio' || source === 'ch' || source === 'geo';
96
+ case 'obj': return source === 'audio' || source === 'ch' || source === 'geo' || source === 'obj';
97
+ case 'evaluate':
98
+ case 'output': return source !== 'output' && source !== 'reserved';
99
+ case 'reserved': return false;
100
+ }
101
+ }
package/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ContextStage } from './registry.js';
1
2
  export interface SourceLocation {
2
3
  readonly file: string;
3
4
  readonly line: number;
@@ -17,23 +18,31 @@ export interface SceneSource {
17
18
  readonly path: string;
18
19
  readonly text: string;
19
20
  }
21
+ export interface LiveChannelSample {
22
+ readonly timestamp?: number;
23
+ readonly values: Readonly<Record<string, number>>;
24
+ }
25
+ export type LiveChannelInput = Readonly<Record<string, number>> | LiveChannelSample;
20
26
  export interface CompileOptions {
21
27
  readonly readFile?: (path: string) => string | Uint8Array | null;
22
28
  readonly fps?: number;
23
29
  readonly frameCount?: number;
30
+ readonly liveChannels?: Readonly<Record<string, readonly string[]>>;
31
+ readonly contexts?: readonly ExtensionContextDefinition[];
24
32
  }
33
+ export type GraphPortType = 'number' | 'vector3' | 'matrix4' | 'boolean' | 'string' | 'object';
25
34
  export interface GraphPort {
26
35
  readonly name: string;
27
- readonly type: 'number' | 'vector3' | 'matrix4';
36
+ readonly type: GraphPortType;
28
37
  }
29
38
  export interface GraphProp {
30
39
  readonly name: string;
31
- readonly type: 'string' | 'number' | 'object';
40
+ readonly type: 'string' | 'number' | 'boolean' | 'object';
32
41
  readonly value: unknown;
33
42
  }
34
43
  export interface GraphNode {
35
44
  readonly name: string;
36
- readonly context: 'obj' | 'ch';
45
+ readonly context: string;
37
46
  readonly type: string;
38
47
  readonly inputs: readonly GraphPort[];
39
48
  readonly outputs: readonly GraphPort[];
@@ -56,12 +65,20 @@ export interface CompiledScene {
56
65
  readonly fps: number;
57
66
  readonly frameCount?: number;
58
67
  readonly graph: EvaluationGraph;
68
+ readonly ranges?: Readonly<Record<string, ParameterRange>>;
69
+ readonly liveChannels: readonly string[];
59
70
  readonly view?: {
60
71
  readonly camera?: string;
61
72
  readonly backgroundColor?: Vec3;
62
73
  readonly display?: 'none';
63
74
  };
64
75
  }
76
+ export interface ParameterRange {
77
+ readonly min: number;
78
+ readonly max: number;
79
+ readonly mode: 'clamp' | 'error';
80
+ readonly source: SourceLocation;
81
+ }
65
82
  export type Vec3 = readonly [number, number, number];
66
83
  export type Matrix4 = readonly number[];
67
84
  export type ObjectKind = 'geo' | 'curve' | 'null' | 'camera' | 'light';
@@ -101,6 +118,7 @@ export interface ObjectSnapshot {
101
118
  readonly parameters: Readonly<Record<string, number>>;
102
119
  readonly visible: boolean;
103
120
  readonly geometry?: GeometrySnapshot;
121
+ readonly proceduralGeometry?: ContextValue;
104
122
  readonly material?: MaterialSnapshot;
105
123
  readonly camera?: CameraSnapshot;
106
124
  readonly light?: LightSnapshot;
@@ -113,6 +131,64 @@ export interface FrameSnapshot {
113
131
  readonly channels: ChannelSnapshot;
114
132
  readonly cameras: readonly ObjectSnapshot[];
115
133
  readonly lights: readonly ObjectSnapshot[];
134
+ readonly contexts?: Readonly<Record<string, readonly ContextNodeSnapshot[]>>;
135
+ }
136
+ export type ExtensionContextStage = 'evaluate' | 'output';
137
+ export type ContextValue = string | number | boolean | null | readonly ContextValue[] | {
138
+ readonly [key: string]: ContextValue;
139
+ };
140
+ export type ContextValues = Readonly<Record<string, ContextValue>>;
141
+ export interface ExtensionPropDefinition {
142
+ readonly name: string;
143
+ readonly type: GraphProp['type'];
144
+ readonly required?: boolean;
145
+ readonly default?: ContextValue;
146
+ }
147
+ export interface ExtensionNodeEvalContext {
148
+ readonly path: string;
149
+ readonly frame: number;
150
+ readonly time: number;
151
+ readonly fps: number;
152
+ readonly props: ContextValues;
153
+ readonly inputs: ContextValues;
154
+ readonly snapshot?: FrameSnapshot;
155
+ }
156
+ export interface AudioFrameData {
157
+ readonly rms?: number;
158
+ readonly peak?: number;
159
+ readonly bass?: number;
160
+ readonly mid?: number;
161
+ readonly high?: number;
162
+ readonly centroid?: number;
163
+ readonly flux?: number;
164
+ readonly spectrum?: readonly number[];
165
+ }
166
+ export interface EvaluateFrameOptions {
167
+ readonly audio?: Readonly<Record<string, AudioFrameData>>;
168
+ readonly liveChannels?: Readonly<Record<string, LiveChannelInput>>;
169
+ readonly deterministic?: boolean;
170
+ }
171
+ export type EvaluateOptions = EvaluateFrameOptions;
172
+ export interface ExtensionNodeType {
173
+ readonly type: string;
174
+ readonly inputs: readonly GraphPort[];
175
+ readonly outputs: readonly GraphPort[];
176
+ readonly props: readonly ExtensionPropDefinition[];
177
+ readonly evaluate: (ctx: ExtensionNodeEvalContext) => ContextValues | void;
178
+ }
179
+ export interface ExtensionContextDefinition {
180
+ readonly name: string;
181
+ readonly stage: ExtensionContextStage;
182
+ readonly nodeTypes: readonly ExtensionNodeType[];
183
+ }
184
+ export interface ContextNodeSnapshot {
185
+ readonly path: string;
186
+ readonly context: string;
187
+ readonly type: string;
188
+ readonly stage: ContextStage;
189
+ readonly props: ContextValues;
190
+ readonly inputs: ContextValues;
191
+ readonly outputs: ContextValues;
116
192
  }
117
193
  export declare class EvaluationError extends Error {
118
194
  readonly diagnostic: Diagnostic;