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

0% Statements 0/92
0% Branches 0/15
0% Functions 0/3
0% Lines 0/92

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 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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import floatColor from "../../utils/misc/floatColor";
import loadProgram from "../../utils/webgl/loadProgram";
import loadShader from "../../utils/webgl/loadShader";
import rotation from "../../utils/matrices/rotation";
 
/**
 * This edge renderer will display edges as lines going from the source node
 * to the target node. To deal with edge thicknesses, the lines are made of
 * two triangles forming rectangles, with the gl.TRIANGLES drawing mode.
 *
 * It is expensive, since drawing a single edge requires 6 points, each
 * having 7 attributes (source position, target position, thickness, color
 * and a flag indicating which vertice of the rectangle it is).
 */
export default {
  POINTS: 6,
  ATTRIBUTES: 7,
  addEdge(edge, source, target, data, i, prefix, settings) {
    const w = (edge[`${prefix}size`] || 1) / 2;
    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++] = x2;
    data[i++] = y2;
    data[i++] = w;
    data[i++] = 0.0;
    data[i++] = color;
 
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = w;
    data[i++] = 1.0;
    data[i++] = color;
 
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = w;
    data[i++] = 0.0;
    data[i++] = color;
 
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = w;
    data[i++] = 0.0;
    data[i++] = color;
 
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = w;
    data[i++] = 1.0;
    data[i++] = color;
 
    data[i++] = x1;
    data[i++] = y1;
    data[i++] = x2;
    data[i++] = y2;
    data[i++] = w;
    data[i++] = 0.0;
    data[i++] = color;
  },
  render(gl, program, data, params) {
    // Define attributes:
    const colorLocation = gl.getAttribLocation(program, "a_color");
    const positionLocation1 = gl.getAttribLocation(program, "a_position1");
    const positionLocation2 = gl.getAttribLocation(program, "a_position2");
    const thicknessLocation = gl.getAttribLocation(program, "a_thickness");
    const minusLocation = gl.getAttribLocation(program, "a_minus");
    const resolutionLocation = gl.getUniformLocation(program, "u_resolution");
    const matrixLocation = gl.getUniformLocation(program, "u_matrix");
    const matrixHalfPiLocation = gl.getUniformLocation(
      program,
      "u_matrixHalfPi"
    );
    const matrixHalfPiMinusLocation = gl.getUniformLocation(
      program,
      "u_matrixHalfPiMinus"
    );
    const ratioLocation = gl.getUniformLocation(program, "u_ratio");
    const scaleLocation = gl.getUniformLocation(program, "u_scale");
 
    const buffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
    gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
 
    gl.uniform2f(resolutionLocation, params.width, params.height);
    gl.uniform1f(
      ratioLocation,
      params.ratio / params.ratio ** params.settings("edgesPowRatio")
    );
    gl.uniform1f(scaleLocation, params.scalingRatio);
    gl.uniformMatrix3fv(matrixLocation, false, params.matrix);
    gl.uniformMatrix2fv(
      matrixHalfPiLocation,
      false,
      rotation(Math.PI / 2, true)
    );
    gl.uniformMatrix2fv(
      matrixHalfPiMinusLocation,
      false,
      rotation(-Math.PI / 2, true)
    );
 
    gl.enableVertexAttribArray(colorLocation);
    gl.enableVertexAttribArray(positionLocation1);
    gl.enableVertexAttribArray(positionLocation2);
    gl.enableVertexAttribArray(thicknessLocation);
    gl.enableVertexAttribArray(minusLocation);
 
    gl.vertexAttribPointer(
      positionLocation1,
      2,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      0
    );
    gl.vertexAttribPointer(
      positionLocation2,
      2,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      8
    );
    gl.vertexAttribPointer(
      thicknessLocation,
      1,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      16
    );
    gl.vertexAttribPointer(
      minusLocation,
      1,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      20
    );
    gl.vertexAttribPointer(
      colorLocation,
      1,
      gl.FLOAT,
      false,
      this.ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT,
      24
    );
 
    gl.drawArrays(
      gl.TRIANGLES,
      params.start || 0,
      params.count || data.length / this.ATTRIBUTES
    );
  },
  initProgram(gl) {
    const vertexShader = loadShader(
      gl,
      [
        "attribute vec2 a_position1;",
        "attribute vec2 a_position2;",
        "attribute float a_thickness;",
        "attribute float a_minus;",
        "attribute float a_color;",
 
        "uniform vec2 u_resolution;",
        "uniform float u_ratio;",
        "uniform float u_scale;",
        "uniform mat3 u_matrix;",
        "uniform mat2 u_matrixHalfPi;",
        "uniform mat2 u_matrixHalfPiMinus;",
 
        "varying vec4 color;",
 
        "void main() {",
        // Find the good point:
        "vec2 position = a_thickness * u_ratio *",
        "normalize(a_position2 - a_position1);",
 
        "mat2 matrix = a_minus * u_matrixHalfPiMinus +",
        "(1.0 - a_minus) * u_matrixHalfPi;",
 
        "position = matrix * position + a_position1;",
 
        // Scale from [[-1 1] [-1 1]] to the container:
        "gl_Position = vec4(",
        "((u_matrix * vec3(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;
  }
};