forked from jacomyal/sigma.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.fast.ts
60 lines (49 loc) · 1.66 KB
/
node.fast.ts
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
/**
* Sigma.js WebGL Renderer Node Program
* =====================================
*
* Simple program rendering nodes using GL_POINTS. This is faster than the
* three triangle option but has some quirks and is not supported equally by
* every GPU.
* @module
*/
import { NodeDisplayData } from "../../../types";
import { floatColor } from "../../../utils";
import vertexShaderSource from "../shaders/node.fast.vert.glsl";
import fragmentShaderSource from "../shaders/node.fast.frag.glsl";
import { AbstractNodeProgram } from "./common/node";
import { RenderParams } from "./common/program";
const POINTS = 1,
ATTRIBUTES = 4;
export default class NodeFastProgram extends AbstractNodeProgram {
constructor(gl: WebGLRenderingContext) {
super(gl, vertexShaderSource, fragmentShaderSource, POINTS, ATTRIBUTES);
this.bind();
}
process(data: NodeDisplayData, hidden: boolean, offset: number): void {
const array = this.array;
let i = offset * POINTS * ATTRIBUTES;
if (hidden) {
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
array[i++] = 0;
return;
}
const color = floatColor(data.color);
array[i++] = data.x;
array[i++] = data.y;
array[i++] = data.size;
array[i] = color;
}
render(params: RenderParams): void {
if (this.hasNothingToRender()) return;
const gl = this.gl;
const program = this.program;
gl.useProgram(program);
gl.uniform1f(this.ratioLocation, 1 / Math.sqrt(params.ratio));
gl.uniform1f(this.scaleLocation, params.scalingRatio);
gl.uniformMatrix3fv(this.matrixLocation, false, params.matrix);
gl.drawArrays(gl.POINTS, 0, this.array.length / ATTRIBUTES);
}
}