|
| 1 | +import { Attributes } from "graphology-types"; |
| 2 | +import { NodeProgram, ProgramInfo } from "sigma/rendering"; |
| 3 | +import { NodeDisplayData, RenderParams } from "sigma/types"; |
| 4 | +import { floatColor } from "sigma/utils"; |
| 5 | + |
| 6 | +import FRAGMENT_SHADER_SOURCE from "./shader-frag"; |
| 7 | +import VERTEX_SHADER_SOURCE from "./shader-vert"; |
| 8 | +import { drawSquareNodeHover, drawSquareNodeLabel } from "./utils"; |
| 9 | + |
| 10 | +const { UNSIGNED_BYTE, FLOAT } = WebGLRenderingContext; |
| 11 | + |
| 12 | +const UNIFORMS = ["u_sizeRatio", "u_correctionRatio", "u_cameraAngle", "u_matrix"] as const; |
| 13 | + |
| 14 | +const PI = Math.PI; |
| 15 | + |
| 16 | +export class NodeDiamondProgram< |
| 17 | + N extends Attributes = Attributes, |
| 18 | + E extends Attributes = Attributes, |
| 19 | + G extends Attributes = Attributes, |
| 20 | +> extends NodeProgram<(typeof UNIFORMS)[number], N, E, G> { |
| 21 | + override drawHover = drawSquareNodeHover; |
| 22 | + override drawLabel = drawSquareNodeLabel; |
| 23 | + |
| 24 | + getDefinition() { |
| 25 | + return { |
| 26 | + VERTICES: 6, |
| 27 | + VERTEX_SHADER_SOURCE: VERTEX_SHADER_SOURCE, |
| 28 | + FRAGMENT_SHADER_SOURCE: FRAGMENT_SHADER_SOURCE, |
| 29 | + METHOD: WebGLRenderingContext.TRIANGLES, |
| 30 | + UNIFORMS, |
| 31 | + ATTRIBUTES: [ |
| 32 | + { name: "a_position", size: 2, type: FLOAT }, |
| 33 | + { name: "a_size", size: 1, type: FLOAT }, |
| 34 | + { name: "a_color", size: 4, type: UNSIGNED_BYTE, normalized: true }, |
| 35 | + { name: "a_id", size: 4, type: UNSIGNED_BYTE, normalized: true }, |
| 36 | + ], |
| 37 | + CONSTANT_ATTRIBUTES: [{ name: "a_angle", size: 1, type: FLOAT }], |
| 38 | + CONSTANT_DATA: [[PI / 4], [(3 * PI) / 4], [-PI / 4], [(3 * PI) / 4], [-PI / 4], [(-3 * PI) / 4]], |
| 39 | + }; |
| 40 | + } |
| 41 | + |
| 42 | + processVisibleItem(nodeIndex: number, startIndex: number, data: NodeDisplayData) { |
| 43 | + const array = this.array; |
| 44 | + const color = floatColor(data.color); |
| 45 | + |
| 46 | + array[startIndex++] = data.x; |
| 47 | + array[startIndex++] = data.y; |
| 48 | + array[startIndex++] = data.size; |
| 49 | + array[startIndex++] = color; |
| 50 | + array[startIndex++] = nodeIndex; |
| 51 | + } |
| 52 | + |
| 53 | + setUniforms(params: RenderParams, { gl, uniformLocations }: ProgramInfo): void { |
| 54 | + const { u_sizeRatio, u_correctionRatio, u_cameraAngle, u_matrix } = uniformLocations; |
| 55 | + |
| 56 | + gl.uniform1f(u_sizeRatio, params.sizeRatio); |
| 57 | + gl.uniform1f(u_cameraAngle, params.cameraAngle); |
| 58 | + gl.uniform1f(u_correctionRatio, params.correctionRatio); |
| 59 | + gl.uniformMatrix3fv(u_matrix, false, params.matrix); |
| 60 | + } |
| 61 | +} |
0 commit comments