Elffuss BraidHub

kikocis/utopia public

262af8b3828a · 5.9 KiBRawPermalink
1// 方形节点的四层渲染 program —— 与圆形 createNodeBorderProgram 完全同构:
2// 状态环(0.1) → 钢灰描边(0.07) → 深色壳(0.3) → 微彩核心(fill)。
3// 圆形用欧氏距离 length(),方形改用切比雪夫距离 max(|x|,|y|),
4// 几何沿用 @sigma/node-square:6 顶点四边形、角点 ±45°/±135°、sqrt8 缩放
5//(等效半边长与圆形半径一致,hover/selected 状态环两种形状表现对齐)。
6
7import { NodeProgram } from "sigma/rendering";
8import type { ProgramInfo } from "sigma/rendering";
9import type { NodeDisplayData, RenderParams } from "sigma/types";
10import { floatColor } from "sigma/utils";
11
12const { UNSIGNED_BYTE, FLOAT } = WebGLRenderingContext;
13
14const UNIFORMS = ["u_sizeRatio", "u_correctionRatio", "u_cameraAngle", "u_matrix"] as const;
15
16const VERTEX_SHADER_SOURCE = /*glsl*/ `
17attribute vec2 a_position;
18attribute float a_size;
19attribute float a_angle;
20
21uniform mat3 u_matrix;
22uniform float u_sizeRatio;
23uniform float u_cameraAngle;
24uniform float u_correctionRatio;
25
26varying vec2 v_diffVector;
27varying float v_radius;
28
29#ifdef PICKING_MODE
30attribute vec4 a_id;
31varying vec4 v_color;
32#else
33attribute vec4 a_ringColor;
34attribute vec4 a_borderColor;
35attribute vec4 a_shellColor;
36attribute vec4 a_color;
37varying vec4 v_ringColor;
38varying vec4 v_borderColor;
39varying vec4 v_shellColor;
40varying vec4 v_fillColor;
41#endif
42
43const float sqrt_8 = sqrt(8.0);
44const float sqrt_2 = sqrt(2.0);
45
46void main() {
47 float size = a_size * u_correctionRatio / u_sizeRatio * sqrt_8;
48 // 屏幕上保持轴对齐(跟随相机旋转),切比雪夫度量用未旋转的本地坐标
49 float angle = a_angle + u_cameraAngle;
50 vec2 diffVector = size * vec2(cos(angle), sin(angle));
51 vec2 position = a_position + diffVector;
52 gl_Position = vec4((u_matrix * vec3(position, 1)).xy, 0, 1);
53
54 v_diffVector = size * vec2(cos(a_angle), sin(a_angle));
55 v_radius = size / sqrt_2; // 半边长,与圆形程序的半径等值
56
57 #ifdef PICKING_MODE
58 v_color = a_id;
59 #else
60 v_ringColor = a_ringColor;
61 v_borderColor = a_borderColor;
62 v_shellColor = a_shellColor;
63 v_fillColor = a_color;
64 #endif
65}
66`;
67
68const FRAGMENT_SHADER_SOURCE = /*glsl*/ `
69precision highp float;
70
71varying vec2 v_diffVector;
72varying float v_radius;
73
74#ifdef PICKING_MODE
75varying vec4 v_color;
76#else
77varying vec4 v_ringColor;
78varying vec4 v_borderColor;
79varying vec4 v_shellColor;
80varying vec4 v_fillColor;
81#endif
82
83uniform float u_correctionRatio;
84
85const float bias = 255.0 / 254.0;
86const vec4 transparent = vec4(0.0, 0.0, 0.0, 0.0);
87
88void main(void) {
89 float dist = max(abs(v_diffVector.x), abs(v_diffVector.y));
90
91 #ifdef PICKING_MODE
92 if (dist > v_radius)
93 gl_FragColor = transparent;
94 else {
95 gl_FragColor = v_color;
96 gl_FragColor.a *= bias;
97 }
98 #else
99 float aaBorder = 2.0 * u_correctionRatio;
100 // 层厚比例与圆形配置一致:0.1 / 0.07 / 0.3 / 剩余为核心
101 float r0 = v_radius;
102 float r1 = r0 - v_radius * 0.1;
103 float r2 = r1 - v_radius * 0.07;
104 float r3 = r2 - v_radius * 0.3;
105
106 vec4 c1 = v_ringColor; c1.a *= bias;
107 vec4 c2 = v_borderColor; c2.a *= bias;
108 vec4 c3 = v_shellColor; c3.a *= bias;
109 vec4 c4 = v_fillColor; c4.a *= bias;
110
111 if (dist > r0) {
112 gl_FragColor = transparent;
113 } else if (dist > r0 - aaBorder) {
114 gl_FragColor = mix(c1, transparent, (dist - r0 + aaBorder) / aaBorder);
115 } else if (dist > r1) {
116 gl_FragColor = c1;
117 } else if (dist > r1 - aaBorder) {
118 gl_FragColor = mix(c2, c1, (dist - r1 + aaBorder) / aaBorder);
119 } else if (dist > r2) {
120 gl_FragColor = c2;
121 } else if (dist > r2 - aaBorder) {
122 gl_FragColor = mix(c3, c2, (dist - r2 + aaBorder) / aaBorder);
123 } else if (dist > r3) {
124 gl_FragColor = c3;
125 } else if (dist > r3 - aaBorder) {
126 gl_FragColor = mix(c4, c3, (dist - r3 + aaBorder) / aaBorder);
127 } else {
128 gl_FragColor = c4;
129 }
130 #endif
131}
132`;
133
134export class NodeSquareShellProgram extends NodeProgram<(typeof UNIFORMS)[number]> {
135 getDefinition() {
136 return {
137 VERTICES: 6,
138 VERTEX_SHADER_SOURCE,
139 FRAGMENT_SHADER_SOURCE,
140 METHOD: WebGLRenderingContext.TRIANGLES,
141 UNIFORMS,
142 ATTRIBUTES: [
143 { name: "a_position", size: 2, type: FLOAT },
144 { name: "a_id", size: 4, type: UNSIGNED_BYTE, normalized: true },
145 { name: "a_size", size: 1, type: FLOAT },
146 { name: "a_ringColor", size: 4, type: UNSIGNED_BYTE, normalized: true },
147 { name: "a_borderColor", size: 4, type: UNSIGNED_BYTE, normalized: true },
148 { name: "a_shellColor", size: 4, type: UNSIGNED_BYTE, normalized: true },
149 { name: "a_color", size: 4, type: UNSIGNED_BYTE, normalized: true },
150 ],
151 CONSTANT_ATTRIBUTES: [{ name: "a_angle", size: 1, type: FLOAT }],
152 CONSTANT_DATA: [
153 [Math.PI / 4],
154 [(3 * Math.PI) / 4],
155 [-Math.PI / 4],
156 [(3 * Math.PI) / 4],
157 [-Math.PI / 4],
158 [(-3 * Math.PI) / 4],
159 ],
160 };
161 }
162
163 processVisibleItem(nodeIndex: number, startIndex: number, data: NodeDisplayData): void {
164 const array = this.array;
165 const d = data as NodeDisplayData & {
166 ringColor?: string;
167 borderColor?: string;
168 shellColor?: string;
169 };
170 array[startIndex++] = data.x;
171 array[startIndex++] = data.y;
172 array[startIndex++] = nodeIndex;
173 array[startIndex++] = data.size;
174 array[startIndex++] = floatColor(d.ringColor || "rgba(0,0,0,0)");
175 array[startIndex++] = floatColor(d.borderColor || "rgba(0,0,0,0)");
176 array[startIndex++] = floatColor(d.shellColor || "rgba(0,0,0,0)");
177 array[startIndex++] = floatColor(data.color);
178 }
179
180 setUniforms(params: RenderParams, { gl, uniformLocations }: ProgramInfo): void {
181 const { u_sizeRatio, u_correctionRatio, u_cameraAngle, u_matrix } = uniformLocations;
182 gl.uniform1f(u_sizeRatio, params.sizeRatio);
183 gl.uniform1f(u_correctionRatio, params.correctionRatio);
184 gl.uniform1f(u_cameraAngle, params.cameraAngle);
185 gl.uniformMatrix3fv(u_matrix, false, params.matrix);
186 }
187}