forked from jacomyal/sigma.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.ts
More file actions
210 lines (174 loc) · 6.44 KB
/
Copy pathedge.ts
File metadata and controls
210 lines (174 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* Sigma.js WebGL Renderer Edge Program
* =====================================
*
* Program rendering edges as thick lines using four points translated
* orthogonally from the source & target's centers by half thickness.
*
* Rendering two triangles by using only four points is made possible through
* the use of indices.
*
* This method should be faster than the 6 points / 2 triangles approach and
* should handle thickness better than with gl.LINES.
*
* This version of the shader balances geometry computation evenly between
* the CPU & GPU (normals are computed on the CPU side).
* @module
*/
import { floatColor, canUse32BitsIndices } from "../../../utils";
import { EdgeDisplayData, NodeDisplayData } from "../../../types";
import vertexShaderSource from "../shaders/edge.vert.glsl";
import fragmentShaderSource from "../shaders/edge.frag.glsl";
import { AbstractEdgeProgram } from "./common/edge";
import { RenderParams } from "./common/program";
const POINTS = 4,
ATTRIBUTES = 5,
STRIDE = POINTS * ATTRIBUTES;
export default class EdgeProgram extends AbstractEdgeProgram {
IndicesArray: Uint32ArrayConstructor | Uint16ArrayConstructor;
indicesArray: Uint32Array | Uint16Array;
indicesBuffer: WebGLBuffer;
indicesType: GLenum;
canUse32BitsIndices: boolean;
positionLocation: GLint;
colorLocation: GLint;
normalLocation: GLint;
matrixLocation: WebGLUniformLocation;
sqrtZoomRatioLocation: WebGLUniformLocation;
correctionRatioLocation: WebGLUniformLocation;
constructor(gl: WebGLRenderingContext) {
super(gl, vertexShaderSource, fragmentShaderSource, POINTS, ATTRIBUTES);
// Initializing indices buffer
const indicesBuffer = gl.createBuffer();
if (indicesBuffer === null) throw new Error("EdgeProgram: error while creating indicesBuffer");
this.indicesBuffer = indicesBuffer;
// Locations
this.positionLocation = gl.getAttribLocation(this.program, "a_position");
this.colorLocation = gl.getAttribLocation(this.program, "a_color");
this.normalLocation = gl.getAttribLocation(this.program, "a_normal");
const matrixLocation = gl.getUniformLocation(this.program, "u_matrix");
if (matrixLocation === null) throw new Error("EdgeProgram: error while getting matrixLocation");
this.matrixLocation = matrixLocation;
const correctionRatioLocation = gl.getUniformLocation(this.program, "u_correctionRatio");
if (correctionRatioLocation === null) throw new Error("EdgeProgram: error while getting correctionRatioLocation");
this.correctionRatioLocation = correctionRatioLocation;
const sqrtZoomRatioLocation = gl.getUniformLocation(this.program, "u_sqrtZoomRatio");
if (sqrtZoomRatioLocation === null) throw new Error("EdgeProgram: error while getting sqrtZoomRatioLocation");
this.sqrtZoomRatioLocation = sqrtZoomRatioLocation;
// Enabling the OES_element_index_uint extension
// NOTE: on older GPUs, this means that really large graphs won't
// have all their edges rendered. But it seems that the
// `OES_element_index_uint` is quite everywhere so we'll handle
// the potential issue if it really arises.
// NOTE: when using webgl2, the extension is enabled by default
this.canUse32BitsIndices = canUse32BitsIndices(gl);
this.IndicesArray = this.canUse32BitsIndices ? Uint32Array : Uint16Array;
this.indicesArray = new this.IndicesArray();
this.indicesType = this.canUse32BitsIndices ? gl.UNSIGNED_INT : gl.UNSIGNED_SHORT;
this.bind();
}
bind(): void {
const gl = this.gl;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indicesBuffer);
// Bindings
gl.enableVertexAttribArray(this.positionLocation);
gl.enableVertexAttribArray(this.normalLocation);
gl.enableVertexAttribArray(this.colorLocation);
gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(this.normalLocation, 2, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 8);
gl.vertexAttribPointer(
this.colorLocation,
4,
gl.UNSIGNED_BYTE,
true,
ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
16,
);
}
computeIndices(): void {
const l = this.array.length / ATTRIBUTES;
const size = l + l / 2;
const indices = new this.IndicesArray(size);
for (let i = 0, c = 0; i < l; i += 4) {
indices[c++] = i;
indices[c++] = i + 1;
indices[c++] = i + 2;
indices[c++] = i + 2;
indices[c++] = i + 1;
indices[c++] = i + 3;
}
this.indicesArray = indices;
}
bufferData(): void {
super.bufferData();
// Indices data
const gl = this.gl;
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, this.indicesArray, gl.STATIC_DRAW);
}
process(
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
hidden: boolean,
offset: number,
): void {
if (hidden) {
for (let i = offset * STRIDE, l = i + STRIDE; i < l; i++) this.array[i] = 0;
return;
}
const thickness = data.size || 1,
x1 = sourceData.x,
y1 = sourceData.y,
x2 = targetData.x,
y2 = targetData.y,
color = floatColor(data.color);
// Computing normals
const dx = x2 - x1,
dy = y2 - y1;
let len = dx * dx + dy * dy,
n1 = 0,
n2 = 0;
if (len) {
len = 1 / Math.sqrt(len);
n1 = -dy * len * thickness;
n2 = dx * len * thickness;
}
let i = POINTS * ATTRIBUTES * offset;
const array = this.array;
// First point
array[i++] = x1;
array[i++] = y1;
array[i++] = n1;
array[i++] = n2;
array[i++] = color;
// First point flipped
array[i++] = x1;
array[i++] = y1;
array[i++] = -n1;
array[i++] = -n2;
array[i++] = color;
// Second point
array[i++] = x2;
array[i++] = y2;
array[i++] = n1;
array[i++] = n2;
array[i++] = color;
// Second point flipped
array[i++] = x2;
array[i++] = y2;
array[i++] = -n1;
array[i++] = -n2;
array[i] = color;
}
render(params: RenderParams): void {
if (this.hasNothingToRender()) return;
const gl = this.gl;
const program = this.program;
gl.useProgram(program);
gl.uniformMatrix3fv(this.matrixLocation, false, params.matrix);
gl.uniform1f(this.sqrtZoomRatioLocation, Math.sqrt(params.ratio));
gl.uniform1f(this.correctionRatioLocation, params.correctionRatio);
// Drawing:
gl.drawElements(gl.TRIANGLES, this.indicesArray.length, this.indicesType, 0);
}
}