forked from jacomyal/sigma.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedge.fast.ts
More file actions
114 lines (93 loc) · 2.84 KB
/
Copy pathedge.fast.ts
File metadata and controls
114 lines (93 loc) · 2.84 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
/**
* Sigma.js WebGL Renderer Fast Edge Program
* ==========================================
*
* Program rendering edges using GL_LINES which is presumably very fast but
* won't render thickness correctly on some GPUs and has some quirks.
* @module
*/
import { EdgeDisplayData, NodeDisplayData } from "../../../types";
import { floatColor } from "../../../utils";
import vertexShaderSource from "../shaders/edge.fast.vert.glsl";
import fragmentShaderSource from "../shaders/edge.fast.frag.glsl";
import { AbstractEdgeProgram } from "./common/edge";
import { RenderParams } from "./common/program";
const POINTS = 2,
ATTRIBUTES = 3;
export default class EdgeFastProgram extends AbstractEdgeProgram {
positionLocation: GLint;
colorLocation: GLint;
matrixLocation: WebGLUniformLocation;
constructor(gl: WebGLRenderingContext) {
super(gl, vertexShaderSource, fragmentShaderSource, POINTS, ATTRIBUTES);
// Locations:
this.positionLocation = gl.getAttribLocation(this.program, "a_position");
this.colorLocation = gl.getAttribLocation(this.program, "a_color");
// Uniform locations:
const matrixLocation = gl.getUniformLocation(this.program, "u_matrix");
if (matrixLocation === null) throw new Error("EdgeFastProgram: error while getting matrixLocation");
this.matrixLocation = matrixLocation;
this.bind();
}
bind(): void {
const gl = this.gl;
// Bindings
gl.enableVertexAttribArray(this.positionLocation);
gl.enableVertexAttribArray(this.colorLocation);
gl.vertexAttribPointer(
this.positionLocation,
2,
gl.FLOAT,
false,
this.attributes * Float32Array.BYTES_PER_ELEMENT,
0,
);
gl.vertexAttribPointer(
this.colorLocation,
4,
gl.UNSIGNED_BYTE,
true,
this.attributes * Float32Array.BYTES_PER_ELEMENT,
8,
);
}
computeIndices(): void {
//nothing to do
}
process(
sourceData: NodeDisplayData,
targetData: NodeDisplayData,
data: EdgeDisplayData,
hidden: boolean,
offset: number,
): void {
const array = this.array;
let i = 0;
if (hidden) {
for (let l = i + POINTS * ATTRIBUTES; i < l; i++) array[i] = 0;
return;
}
const x1 = sourceData.x,
y1 = sourceData.y,
x2 = targetData.x,
y2 = targetData.y,
color = floatColor(data.color);
i = POINTS * ATTRIBUTES * offset;
// First point
array[i++] = x1;
array[i++] = y1;
array[i++] = color;
// Second point
array[i++] = x2;
array[i++] = y2;
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.drawArrays(gl.LINES, 0, this.array.length / ATTRIBUTES);
}
}