Skip to content

Commit 0adf88a

Browse files
committed
documentation for interfaces
1 parent 638e7ef commit 0adf88a

4 files changed

Lines changed: 181 additions & 2 deletions

File tree

src/types/ast.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// src/types/ast.ts
2+
3+
/**
4+
* Defines the available classifications for node instances within the engine.
5+
* Every visual node placed on the canvas must map to one of these predefined registry types.
6+
*/
17
export type NodeType =
28
| 'OUTPUT_FRAG'
39
| 'OUTPUT_VERT'
@@ -27,14 +33,34 @@ export type NodeType =
2733
| 'BEAM_ENDPOINT'
2834
;
2935

36+
/**
37+
* Specifies the supported GLSL data types used for mathematical evaluation and port connections.
38+
*/
3039
export type GLSLType = 'float' | 'vec2' | 'vec3' | 'vec4' | 'string';
3140

41+
/**
42+
* Represents an input or output connection point on a node instance.
43+
* * @property id - The specific identifier for the port (e.g., 'color', 'uv_coords').
44+
* @property type - The GLSL data type expected or emitted by this port.
45+
* @property value - An optional constant value used if the input port has no active connection.
46+
*/
3247
export interface NodePort {
3348
id: string;
3449
type: GLSLType;
3550
value?: any;
3651
}
3752

53+
/**
54+
* Represents an active instance of a node placed within the visual editor.
55+
* * @property id - The globally unique identifier for this specific node instance (e.g., 'noise-16843000').
56+
* @property type - The overarching structural classification of the node.
57+
* @property inputs - The array of available input ports and their current constant values.
58+
* @property outputs - The array of available output ports.
59+
* @property data - Optional arbitrary data required for specific node logic.
60+
* @property isUniform - Flag indicating if this node instance should be compiled into a GLSL uniform.
61+
* @property uniformName - The declared variable name used in the shader program if the node is a uniform.
62+
* @property position - The physical X/Y coordinates of the node on the React Flow canvas, used for layout restoration.
63+
*/
3864
export interface ShaderNode {
3965
id: string;
4066
type: NodeType;
@@ -46,6 +72,14 @@ export interface ShaderNode {
4672
position?: { x: number; y: number };
4773
}
4874

75+
/**
76+
* Represents a directed structural link (wire) between an output port of one node and an input port of another.
77+
* * @property id - The globally unique identifier for the connection wire.
78+
* @property sourceNodeId - The instance ID of the node emitting data.
79+
* @property sourcePortId - The specific port ID on the emitting node.
80+
* @property targetNodeId - The instance ID of the node receiving data.
81+
* @property targetPortId - The specific port ID on the receiving node.
82+
*/
4983
export interface ShaderConnection {
5084
id: string;
5185
sourceNodeId: string;
@@ -54,6 +88,12 @@ export interface ShaderConnection {
5488
targetPortId: string;
5589
}
5690

91+
/**
92+
* The root Abstract Syntax Tree (AST) structure encapsulating the entire visual logic.
93+
* This is the primary data structure passed to the Compilers and Evaluators.
94+
* * @property nodes - The complete collection of active node instances in the current workspace.
95+
* @property connections - The collection of wires linking the node instances together.
96+
*/
5797
export interface ShaderGraph {
5898
nodes: ShaderNode[];
5999
connections: ShaderConnection[];

src/types/context.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,108 @@ import type * as THREE from 'three';
55
import type { IWorkspaceExporter } from './export';
66
import type { ShaderGraph } from './ast';
77

8+
/**
9+
* Provides the core Three.js components required to initialize and render a 3D preview.
10+
* * @property scene - The Three.js scene graph object.
11+
* @property camera - The default perspective camera for the viewport.
12+
* @property material - The shader material instance that will be updated by the AST.
13+
*/
814
export interface RenderContext {
915
scene: THREE.Scene;
1016
camera: THREE.PerspectiveCamera;
1117
material: THREE.ShaderMaterial;
1218
}
1319

20+
/**
21+
* Defines the lifecycle and rendering logic for a specific visual context in the 3D canvas.
22+
*/
1423
export interface IPreviewStrategy {
24+
/**
25+
* Called once when the preview canvas is mounted or the context is switched.
26+
* * @param ctx - The rendering context containing the scene, camera, and material.
27+
* @param settings - The current configuration settings for this context.
28+
*/
1529
init: (ctx: RenderContext, settings: Record<string, any>) => void;
30+
31+
/**
32+
* Called every frame to animate and update the preview logic.
33+
* * @param time - The elapsed time in milliseconds.
34+
* @param settings - The current configuration settings for this context.
35+
* @param graph - Optional reference to the current AST for real-time mathematical evaluation.
36+
*/
1637
update: (time: number, settings: Record<string, any>, graph?: ShaderGraph) => void;
38+
39+
/**
40+
* Called when a configuration parameter is modified in the context's SettingsPanel.
41+
* * @param settings - The updated configuration settings object.
42+
*/
1743
onSettingsChange: (settings: Record<string, any>) => void;
44+
45+
/**
46+
* Called to safely dispose of geometry, materials, and helpers to prevent memory leaks.
47+
*/
1848
dispose: () => void;
1949
}
2050

51+
/**
52+
* Represents an isolated environment within the engine (e.g., Material, Trail, Beam).
53+
* Encapsulates context-specific logic, UI, and rendering rules to maintain extensibility.
54+
*/
2155
export interface IProjectContext {
56+
/** * @property id - Unique identifier for the context.
57+
*/
2258
id: string;
59+
60+
/** * @property name - Display name used in the user interface.
61+
*/
2362
name: string;
63+
64+
/** * @property requiresGlobalMaterial - Indicates if this context relies on the compiled shader from the global MATERIAL context.
65+
*/
2466
requiresGlobalMaterial?: boolean;
67+
68+
/**
69+
* Determines if the current settings require an orthographic camera projection.
70+
* * @param settings - The current configuration settings for the context.
71+
* @returns True if an orthographic camera should be used, false otherwise.
72+
*/
2573
isOrthographic?: (settings: Record<string, any>) => boolean;
74+
75+
/**
76+
* Generates the default workspace layout.
77+
* * @returns An array of initial React Flow nodes.
78+
*/
2679
getInitialNodes: () => Node[];
80+
81+
/**
82+
* Evaluates whether a specific AST node type is permitted within this context.
83+
* * @param nodeType - The string identifier of the node type to check.
84+
* @returns True if the node is permitted, false otherwise.
85+
*/
2786
isNodeAllowed: (nodeType: string) => boolean;
87+
88+
/** * @property SettingsPanel - React component rendering the specific configuration controls for this context.
89+
*/
2890
SettingsPanel: React.FC<{
2991
settings: Record<string, any>;
3092
onSettingChange: (key: string, value: any) => void;
3193
}>;
94+
95+
/**
96+
* Instantiates the Three.js rendering strategy for the 3D preview.
97+
* * @returns A new instance of the preview strategy.
98+
*/
3299
createPreviewStrategy: () => IPreviewStrategy;
100+
101+
/**
102+
* Provides the fallback configuration values for the context settings.
103+
* * @returns A dictionary of default settings.
104+
*/
33105
getDefaultSettings: () => Record<string, any>;
106+
107+
/**
108+
* Provides the exporter responsible for converting the AST into game-ready metadata.
109+
* * @returns The configured workspace exporter or null if exporting is not supported.
110+
*/
34111
getExporter: () => IWorkspaceExporter | null;
35112
}

src/types/export.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
// src/types/export.ts
22
import type { ShaderGraph } from './ast';
33

4+
/**
5+
* Defines the runtime origin for a shader uniform within the target game engine.
6+
*/
47
export type UniformSource = 'GAME_TIME' | 'CUSTOM' | 'TEXTURE' | 'LIGHTMAP';
58

9+
/**
10+
* Represents the metadata required by the game engine to bind a specific uniform.
11+
* * @property type - The GLSL data type (e.g., 'float', 'vec3').
12+
* @property source - The origin classification for the uniform data.
13+
*/
614
export interface UniformMeta {
715
type: string;
816
source: UniformSource;
917
}
1018

19+
/**
20+
* Defines the low-level graphics pipeline state required to render the material.
21+
*/
1122
export interface RenderState {
1223
blend_mode: 'OPAQUE' | 'TRANSLUCENT' | 'ADDITIVE' | 'MULTIPLY';
1324
cull_mode: 'BACK' | 'FRONT' | 'NONE';
@@ -16,28 +27,59 @@ export interface RenderState {
1627
alpha_cutoff: number;
1728
}
1829

30+
/**
31+
* The standard JSON schema expected by the target game engine's metadata parser.
32+
*/
1933
export interface CosmosMetadata {
2034
name: string;
2135
vertex_format: string;
2236
render_state: RenderState;
2337
uniforms: Record<string, UniformMeta>;
2438
}
2539

40+
/**
41+
* Configuration parameters provided to the metadata extraction process.
42+
*/
2643
export interface ExportConfig {
2744
name: string;
2845
isTranslucent: boolean;
2946
}
3047

48+
/**
49+
* Contract for services that parse a ShaderGraph into valid engine metadata.
50+
*/
3151
export interface IMetadataExtractor {
52+
/**
53+
* Extracts metadata from a shader graph based on the provided configuration.
54+
* * @param graph - The logical AST representing the visual shader.
55+
* @param config - Export configuration parameters.
56+
* @returns The generated Cosmos metadata schema.
57+
*/
3258
extract(graph: ShaderGraph, config: ExportConfig): CosmosMetadata;
3359
}
3460

61+
/**
62+
* Represents a discrete file generated during the export process.
63+
* * @property fileName - The target name of the file including extension.
64+
* @property fileContent - The raw string payload or binary blob.
65+
* @property mimeType - The MIME classification for the file payload.
66+
*/
3567
export interface ExportResult {
3668
fileName: string;
3769
fileContent: string | Blob;
3870
mimeType: string;
3971
}
4072

73+
/**
74+
* Contract for context-specific exporters that compile ASTs into downloadable game assets.
75+
*/
4176
export interface IWorkspaceExporter {
77+
/**
78+
* Compiles the AST into an array of downloadable files.
79+
* * @param graph - The logical AST representing the visual shader.
80+
* @param settings - The current configuration settings for the context.
81+
* @param globalSettings - Application-wide settings including namespace and project name.
82+
* @returns A promise resolving to an array of export results.
83+
*/
4284
export(graph: ShaderGraph, settings: Record<string, any>, globalSettings: { namespace: string; projectName: string }): Promise<ExportResult[]>;
4385
}

src/types/node-def.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,37 @@
22
import type { NodeType, GLSLType } from './ast';
33
import type { NodeStrategy } from './compiler';
44

5+
/**
6+
* Defines the user interface control rendered on a node for manipulating constant input values.
7+
* * @property id - Optional identifier for the control. Defaults to the input port ID if omitted.
8+
* @property label - The text label displayed next to the control in the UI.
9+
* @property type - The visual representation of the control (e.g., a slider, a color picker, or a dropdown).
10+
* @property min - The minimum allowed value (applicable to 'slider' and 'number' types).
11+
* @property max - The maximum allowed value (applicable to 'slider' and 'number' types).
12+
* @property step - The incremental step value for adjustments (applicable to 'slider' and 'number' types).
13+
* @property options - An array of available string choices (applicable only to 'select' types).
14+
*/
515
export interface NodeControl {
616
id?: string;
717
label: string;
8-
type: 'slider' | 'color-rgb' | 'none'| 'select' | 'number';
18+
type: 'slider' | 'color-rgb' | 'none' | 'select' | 'number';
919
min?: number;
1020
max?: number;
1121
step?: number;
1222
options?: string[];
1323
}
1424

15-
25+
/**
26+
* The structural blueprint used to register a mathematical or functional node within the engine.
27+
* Every node available in the editor must have a corresponding NodeDefinition.
28+
* * @property type - The unique AST identifier for the node (e.g., 'MATH_BINARY', 'TIME').
29+
* @property label - The human-readable display name rendered on the node's header.
30+
* @property color - The hexadecimal color code used to style the node's header background.
31+
* @property inputs - An array defining the input ports, their expected data types, default values, and optional UI controls.
32+
* @property outputs - An array defining the output ports and the mathematical data types they emit.
33+
* @property strategy - The compilation logic used to translate this node into GLSL strings and executable TypeScript math.
34+
* @property canHavePreview - Indicates if the node supports rendering an isolated 2D GLSL preview canvas within its body.
35+
*/
1636
export interface NodeDefinition {
1737
type: NodeType;
1838
label: string;

0 commit comments

Comments
 (0)