All files / core/domain/renderers/webgl edgesFast.js

0% Statements 0/39
0% Branches 0/13
0% Functions 0/3
0% Lines 0/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                           
import floatColor from "../../utils/misc/floatColor";
import loadProgram from "../../utils/webgl/loadProgram";
import loadShader from "../../utils/webgl/loadShader";
 
/**
 * This edge renderer will display edges as lines with the gl.LINES display
 * mode. Since this mode does not support well thickness, edges are all drawn
 * with the same thickness (3px), independantly of the edge attributes or the
 * zooming ratio.
 */
export default {
  POINTS: 2,
  ATTRIBUTES: 3,
  addEdge(edge, source, target, data, i, prefix, settings) {
    const x1 = source[`${prefix}x`];
    const y1 = source[`${prefix}y`];
    const x2 = target[`${prefix}x`];
    const y2 = target[`${prefix}y`];
    let { color } = edge;
 
    if (!color)
      switch (settings("edgeColor")) {
        case "source":
          color = source.color || settings("defaultNodeColor");
          break;
        case "target":
          color = target.color || settings("defaultNodeColor");
          break;
        default:
          color = settings("defaultEdgeColor");
          break;
      }
 
    // Normalize color:
    color = floatColor(color);
 
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = color;
 
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = color;
  },
  render(gl, program, data, params) {
    // Define attributes:
    const colorLocation = gl.getAttribLocation(program, "a_color");
    const positionLocation = gl.getAttribLocation(program, "a_position");
    const resolutionLocation = gl.getUniformLocation(program, "u_resolution");
    const matrixLocation = gl.getUniformLocation(program, "u_matrix");
 
    const buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, data, gl.DYNAMIC_DRAW);
    gl.uniform2f(resolutionLocation, params.width, params.height);
    gl.uniformMatrix3fv(matrixLocation, false, params.matrix);
    gl.enableVertexAttribArray(positionLocation);
    gl.enableVertexAttribArray(colorLocation);
 
    gl.vertexAttribPointer(
      positionLocation,
      2,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      0
    );
    gl.vertexAttribPointer(
      colorLocation,
      1,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      8
    );
 
    gl.lineWidth(3);
    gl.drawArrays(
      gl.LINES,
      params.start || 0,
      params.count || data.length / this.ATTRIBUTES
    );
  },
  initProgram(gl) {
    const vertexShader = loadShader(
      gl,
      [
        "attribute vec2 a_position;",
        "attribute float a_color;",
 
        "uniform vec2 u_resolution;",
        "uniform mat3 u_matrix;",
 
        "varying vec4 color;",
 
        "void main() {",
        // Scale from [[-1 1] [-1 1]] to the container:
        "gl_Position = vec4(",
        "((u_matrix * vec3(a_position, 1)).xy /",
        "u_resolution * 2.0 - 1.0) * vec2(1, -1),",
        "0,",
        "1",
        ");",
 
        // Extract the color:
        "float c = a_color;",
        "color.b = mod(c, 256.0); c = floor(c / 256.0);",
        "color.g = mod(c, 256.0); c = floor(c / 256.0);",
        "color.r = mod(c, 256.0); c = floor(c / 256.0); color /= 255.0;",
        "color.a = 1.0;",
        "}"
      ].join("\n"),
      gl.VERTEX_SHADER
    );
 
    const fragmentShader = loadShader(
      gl,
      [
        "precision mediump float;",
 
        "varying vec4 color;",
 
        "void main(void) {",
        "gl_FragColor = color;",
        "}"
      ].join("\n"),
      gl.FRAGMENT_SHADER
    );
 
    const program = loadProgram(gl, [vertexShader, fragmentShader]);
    return program;
  }
};