sigma
Version:
A JavaScript library aimed at visualizing graphs of thousands of nodes and edges.
118 lines (117 loc) • 5.52 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 });
/**
* Sigma.js WebGL Renderer Triangle Edge Program
* ==============================================
*
* Program rendering directed edges as a single triangle.
* @module
*/
var utils_1 = require("../../../utils");
var edge_triangle_vert_glsl_1 = __importDefault(require("../shaders/edge.triangle.vert.glsl.js"));
var edge_triangle_frag_glsl_1 = __importDefault(require("../shaders/edge.triangle.frag.glsl.js"));
var edge_1 = require("./common/edge");
var POINTS = 3, ATTRIBUTES = 5, STRIDE = POINTS * ATTRIBUTES;
var EdgeTriangleProgram = /** @class */ (function (_super) {
__extends(EdgeTriangleProgram, _super);
function EdgeTriangleProgram(gl) {
var _this = _super.call(this, gl, edge_triangle_vert_glsl_1.default, edge_triangle_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");
var matrixLocation = gl.getUniformLocation(_this.program, "u_matrix");
if (matrixLocation === null)
throw new Error("EdgeTriangleProgram: error while getting matrixLocation");
_this.matrixLocation = matrixLocation;
var correctionRatioLocation = gl.getUniformLocation(_this.program, "u_correctionRatio");
if (correctionRatioLocation === null)
throw new Error("EdgeTriangleProgram: error while getting correctionRatioLocation");
_this.correctionRatioLocation = correctionRatioLocation;
var sqrtZoomRatioLocation = gl.getUniformLocation(_this.program, "u_sqrtZoomRatio");
if (sqrtZoomRatioLocation === null)
throw new Error("EdgeTriangleProgram: error while getting sqrtZoomRatioLocation");
_this.sqrtZoomRatioLocation = sqrtZoomRatioLocation;
_this.bind();
return _this;
}
EdgeTriangleProgram.prototype.bind = function () {
var gl = this.gl;
// Bindings
gl.enableVertexAttribArray(this.positionLocation);
gl.enableVertexAttribArray(this.normalLocation);
gl.enableVertexAttribArray(this.colorLocation);
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.colorLocation, 4, gl.UNSIGNED_BYTE, true, ATTRIBUTES * Float32Array.BYTES_PER_ELEMENT, 16);
};
EdgeTriangleProgram.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, 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++] = x1;
array[i++] = y1;
array[i++] = n1;
array[i++] = n2;
array[i++] = color;
array[i++] = x1;
array[i++] = y1;
array[i++] = -n1;
array[i++] = -n2;
array[i++] = color;
array[i++] = x2;
array[i++] = y2;
array[i++] = 0;
array[i++] = 0;
array[i] = color;
};
EdgeTriangleProgram.prototype.computeIndices = function () {
// nothing todo ?
};
EdgeTriangleProgram.prototype.render = function (params) {
if (this.hasNothingToRender())
return;
var gl = this.gl;
var program = this.program;
gl.useProgram(program);
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 EdgeTriangleProgram;
}(edge_1.AbstractEdgeProgram));
exports.default = EdgeTriangleProgram;