sigma
Version:
A JavaScript library aimed at visualizing graphs of thousands of nodes and edges.
134 lines (133 loc) • 6.42 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("../../../utils");
var edge_arrowHead_vert_glsl_1 = __importDefault(require("../shaders/edge.arrowHead.vert.glsl.js"));
var edge_arrowHead_frag_glsl_1 = __importDefault(require("../shaders/edge.arrowHead.frag.glsl.js"));
var edge_1 = require("./common/edge");
var POINTS = 3, ATTRIBUTES = 9, STRIDE = POINTS * ATTRIBUTES;
var EdgeArrowHeadProgram = /** @class */ (function (_super) {
__extends(EdgeArrowHeadProgram, _super);
function EdgeArrowHeadProgram(gl) {
var _this = _super.call(this, gl, edge_arrowHead_vert_glsl_1.default, edge_arrowHead_frag_glsl_1.default, POINTS, ATTRIBUTES) || this;
// Locations
_this.positionLocation = gl.getAttribLocation(_this.program, "a_position");
_this.colorLocation = gl.getAttribLocation(_this.program, "a_color");
_this.normalLocation = gl.getAttribLocation(_this.program, "a_normal");
_this.radiusLocation = gl.getAttribLocation(_this.program, "a_radius");
_this.barycentricLocation = gl.getAttribLocation(_this.program, "a_barycentric");
// Uniform locations
var matrixLocation = gl.getUniformLocation(_this.program, "u_matrix");
if (matrixLocation === null)
throw new Error("EdgeArrowHeadProgram: error while getting matrixLocation");
_this.matrixLocation = matrixLocation;
var sqrtZoomRatioLocation = gl.getUniformLocation(_this.program, "u_sqrtZoomRatio");
if (sqrtZoomRatioLocation === null)
throw new Error("EdgeArrowHeadProgram: error while getting sqrtZoomRatioLocation");
_this.sqrtZoomRatioLocation = sqrtZoomRatioLocation;
var correctionRatioLocation = gl.getUniformLocation(_this.program, "u_correctionRatio");
if (correctionRatioLocation === null)
throw new Error("EdgeArrowHeadProgram: error while getting correctionRatioLocation");
_this.correctionRatioLocation = correctionRatioLocation;
_this.bind();
return _this;
}
EdgeArrowHeadProgram.prototype.bind = function () {
var gl = this.gl;
// Bindings
gl.enableVertexAttribArray(this.positionLocation);
gl.enableVertexAttribArray(this.normalLocation);
gl.enableVertexAttribArray(this.radiusLocation);
gl.enableVertexAttribArray(this.colorLocation);
gl.enableVertexAttribArray(this.barycentricLocation);
gl.vertexAttribPointer(this.positionLocation, 2, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(this.normalLocation, 2, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 8);
gl.vertexAttribPointer(this.radiusLocation, 1, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 16);
gl.vertexAttribPointer(this.colorLocation, 4, gl.UNSIGNED_BYTE, true, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 20);
// TODO: maybe we can optimize here by packing this in a bit mask
gl.vertexAttribPointer(this.barycentricLocation, 3, gl.FLOAT, false, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 24);
};
EdgeArrowHeadProgram.prototype.computeIndices = function () {
// nothing to do
};
EdgeArrowHeadProgram.prototype.process = function (sourceData, targetData, data, hidden, offset) {
if (hidden) {
for (var i_1 = offset * STRIDE, l = i_1 + STRIDE; i_1 < l; i_1++)
this.array[i_1] = 0;
return;
}
var thickness = data.size || 1, radius = targetData.size || 1, x1 = sourceData.x, y1 = sourceData.y, x2 = targetData.x, y2 = targetData.y, color = (0, utils_1.floatColor)(data.color);
// Computing normals
var dx = x2 - x1, dy = y2 - y1;
var len = dx * dx + dy * dy, n1 = 0, n2 = 0;
if (len) {
len = 1 / Math.sqrt(len);
n1 = -dy * len * thickness;
n2 = dx * len * thickness;
}
var i = POINTS * ATTRIBUTES * offset;
var array = this.array;
// First point
array[i++] = x2;
array[i++] = y2;
array[i++] = -n1;
array[i++] = -n2;
array[i++] = radius;
array[i++] = color;
array[i++] = 1;
array[i++] = 0;
array[i++] = 0;
// Second point
array[i++] = x2;
array[i++] = y2;
array[i++] = -n1;
array[i++] = -n2;
array[i++] = radius;
array[i++] = color;
array[i++] = 0;
array[i++] = 1;
array[i++] = 0;
// Third point
array[i++] = x2;
array[i++] = y2;
array[i++] = -n1;
array[i++] = -n2;
array[i++] = radius;
array[i++] = color;
array[i++] = 0;
array[i++] = 0;
array[i] = 1;
};
EdgeArrowHeadProgram.prototype.render = function (params) {
if (this.hasNothingToRender())
return;
var gl = this.gl;
var program = this.program;
gl.useProgram(program);
// Binding uniforms
gl.uniformMatrix3fv(this.matrixLocation, false, params.matrix);
gl.uniform1f(this.sqrtZoomRatioLocation, Math.sqrt(params.ratio));
gl.uniform1f(this.correctionRatioLocation, params.correctionRatio);
// Drawing:
gl.drawArrays(gl.TRIANGLES, 0, this.array.length / ATTRIBUTES);
};
return EdgeArrowHeadProgram;
}(edge_1.AbstractEdgeProgram));
exports.default = EdgeArrowHeadProgram;