@xifu/shader-graph-glsl 0.1.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 ADDED
@@ -0,0 +1,86 @@
1
+ # Shader Graph GLSL
2
+
3
+ 一个 Unity `Shader Graph` 高仿,纯 GLSL ES 3.00 实现,渲染器无关。
4
+
5
+ ## 📦 双包架构
6
+
7
+ ```
8
+ @xifu/shader-graph-glsl/runtime — 轻量运行时引擎 (零外部依赖,纯 WebGL2)
9
+ @xifu/shader-graph-glsl/editor — 可视化编辑器 (基于 Rete.js + React + Three.js)
10
+ ```
11
+
12
+ | 包 | 描述 | 依赖 |
13
+ |---|---|---|
14
+ | `@xifu/shader-graph-glsl/runtime` | 独立 WebGL2 渲染引擎,加载 ShaderConfig 渲染 | 无 |
15
+ | `@xifu/shader-graph-glsl/editor` | 可视化节点编辑器,编译产出 ShaderConfig | React, Three.js |
16
+
17
+ ## 🚀 快速开始
18
+
19
+ ### 运行时最小用例
20
+
21
+ ```ts
22
+ import { ShaderGraphRuntime } from '@xifu/shader-graph-glsl/runtime'
23
+
24
+ const runtime = new ShaderGraphRuntime(canvas)
25
+ const program = runtime.load(shaderConfig) // ShaderConfig 由编辑器编译产出
26
+
27
+ runtime.uniforms(program)
28
+ .set('uColor', [1, 0, 0, 1])
29
+ .commit()
30
+
31
+ runtime.draw({
32
+ geometry: { attributes: { aPosition: { data, size: 3 } }, vertexCount: 3 },
33
+ program,
34
+ uniforms: {},
35
+ })
36
+ ```
37
+
38
+ ### 编辑器最小用例
39
+
40
+ ```ts
41
+ import { ShaderGraphEditor } from '@xifu/shader-graph-glsl/editor'
42
+
43
+ const editor = new ShaderGraphEditor(document.getElementById('editor')!)
44
+ await editor.createGraph()
45
+
46
+ // 编译导出
47
+ const config = await editor.compile()
48
+ ```
49
+
50
+ ## 📖 文档
51
+
52
+ - [API 文档](https://a876691666.github.io/shader-graph-glsl/#/api)
53
+ - [使用用例](https://a876691666.github.io/shader-graph-glsl/#/examples)
54
+ - Three.js 集成
55
+ - PixiJS 集成
56
+ - 原生 WebGL2
57
+ - PlayCanvas 集成
58
+
59
+ ## 🖼️ 截图
60
+
61
+ <div style="display: grid; grid: repeat(2, 180px) / auto-flow 290px;">
62
+ <img width="280" alt="fresnelOutline" src="./screenshots/fresnelOutline.png">
63
+ <img width="280" alt="dissolve" src="./screenshots/dissolve.png">
64
+ <img width="280" alt="procedural" src="./screenshots/procedural.png">
65
+ <img width="280" alt="subgraph" src="./screenshots/subgraph.png">
66
+ <img width="280" alt="subgraph" src="./screenshots/previewNumber.png">
67
+ <img width="280" alt="subgraph" src="./screenshots/flowmap.png">
68
+ </div>
69
+
70
+ ## 🔧 开发
71
+
72
+ ```bash
73
+ # 安装依赖
74
+ pnpm install
75
+
76
+ # 启动编辑器 dev server
77
+ pnpm dev
78
+
79
+ # 启动文档站点
80
+ pnpm dev:docs
81
+
82
+ # 构建文档站点
83
+ pnpm build:docs
84
+ ```
85
+
86
+ MIT 仅供学习交流使用
@@ -0,0 +1,110 @@
1
+ /**
2
+ * ShaderConfig — 结构化着色器配置导出格式
3
+ *
4
+ * 这是运行时引擎与编辑器之间的契约类型。
5
+ * 编辑器编译导出的 ShaderConfig 可由运行时引擎直接加载渲染。
6
+ */
7
+ /** Uniform 元数据 (用于运行时更新) */
8
+ interface UniformMeta {
9
+ /** GLSL 中的变量名 */
10
+ name: string;
11
+ /** GLSL 类型 */
12
+ type: UniformDataType;
13
+ /** 默认值 */
14
+ default?: UniformValue;
15
+ /** 编辑器上下文键 (用于编辑器映射) */
16
+ contextKey?: string;
17
+ }
18
+ type UniformDataType = 'float' | 'int' | 'bool' | 'vec2' | 'vec3' | 'vec4' | 'mat2' | 'mat3' | 'mat4' | 'sampler2D';
19
+ type UniformValue = number | [number, number] | [number, number, number] | [number, number, number, number] | number[];
20
+ /** 纹理绑定配置 */
21
+ interface TextureBinding {
22
+ /** uniform 变量名 */
23
+ name: string;
24
+ /** 纹理 URL 或 asset ID */
25
+ url?: string;
26
+ /** 纹理 asset ID (编辑器资源引用) */
27
+ assetId?: string;
28
+ /** 颜色空间 */
29
+ colorSpace?: 'sRGB' | 'Linear';
30
+ /** 类型 (default / normal) */
31
+ type?: 'default' | 'normal';
32
+ }
33
+ /** 参数配置 (编辑器暴露的可调参数) */
34
+ interface ParameterConfig {
35
+ name: string;
36
+ type: UniformDataType;
37
+ default: UniformValue;
38
+ }
39
+ /** 子图配置 */
40
+ interface SubGraphConfig {
41
+ /** 子图 ID (与主图中引用的 asset.id 对应) */
42
+ id: string;
43
+ /** 子图名称 */
44
+ name?: string;
45
+ /** 子图的完整着色器配置 */
46
+ config: ShaderConfig;
47
+ }
48
+ /** 导出着色器配置 v1 */
49
+ interface ShaderConfig {
50
+ /** 配置版本 */
51
+ version: 1;
52
+ /** 唯一标识 */
53
+ id: string;
54
+ /** 可读名称 */
55
+ name?: string;
56
+ /** 完整顶点着色器 GLSL ES 3.00 源码 */
57
+ vertCode: string;
58
+ /** 完整片段着色器 GLSL ES 3.00 源码 */
59
+ fragCode: string;
60
+ /** 所有 uniform 元数据 */
61
+ uniforms: UniformMeta[];
62
+ /** 纹理绑定信息 */
63
+ textures: TextureBinding[];
64
+ /** 暴露的参数 (可在运行时覆盖) */
65
+ parameters: ParameterConfig[];
66
+ /** 渲染状态 */
67
+ renderState?: {
68
+ /** 混合模式 */
69
+ blending?: 'opaque' | 'transparent' | 'additive' | 'multiply';
70
+ /** 面剔除 */
71
+ cullMode?: 'front' | 'back' | 'none';
72
+ /** 深度写入 */
73
+ depthWrite?: boolean;
74
+ /** 深度测试 */
75
+ depthTest?: 'never' | 'less' | 'equal' | 'lequal' | 'greater' | 'notequal' | 'gequal' | 'always';
76
+ /** 精度 */
77
+ precision?: 'highp' | 'mediump' | 'lowp';
78
+ };
79
+ /** 引用的子图 (key = asset.id) */
80
+ subGraphs?: Record<string, SubGraphConfig>;
81
+ }
82
+ /** 编译选项 (编辑器 → 编译器) */
83
+ interface ShaderCompileOptions {
84
+ /** 模板类型 */
85
+ template: 'unlit' | 'lit' | 'subgraph';
86
+ /** precision */
87
+ precision?: 'single' | 'float';
88
+ /** 表面类型 */
89
+ surfaceType?: 'opaque' | 'transparent';
90
+ /** 渲染面 */
91
+ renderFace?: 'front' | 'back' | 'both';
92
+ /** 深度写入 */
93
+ depthWrite?: 'auto' | 'enable' | 'disable';
94
+ /** 深度测试 */
95
+ depthTest?: string;
96
+ /** alpha 裁剪 */
97
+ alphaClipping?: boolean;
98
+ /** 投射阴影 */
99
+ castShadows?: boolean;
100
+ /** 混合模式 */
101
+ blendingMode?: 'alpha' | 'additive' | 'multiply' | 'premultiply';
102
+ /** 允许材质覆盖 */
103
+ allowMaterialOverride?: boolean;
104
+ }
105
+ /** Uniform 运行时值集合 */
106
+ interface UniformValues {
107
+ [name: string]: UniformValue | WebGLTexture | null;
108
+ }
109
+
110
+ export type { ParameterConfig as P, ShaderConfig as S, TextureBinding as T, UniformMeta as U, UniformValue as a, UniformValues as b, ShaderCompileOptions as c, SubGraphConfig as d, UniformDataType as e };