* Defines UI for links in webgl renderer.
()
| 11 | * Defines UI for links in webgl renderer. |
| 12 | */ |
| 13 | function webglLinkProgram() { |
| 14 | var ATTRIBUTES_PER_PRIMITIVE = 6, // primitive is Line with two points. Each has x,y and color = 3 * 2 attributes. |
| 15 | BYTES_PER_LINK = 2 * (2 * Float32Array.BYTES_PER_ELEMENT + Uint32Array.BYTES_PER_ELEMENT), // two nodes * (x, y + color) |
| 16 | linksFS = [ |
| 17 | 'precision mediump float;', |
| 18 | 'varying vec4 color;', |
| 19 | 'void main(void) {', |
| 20 | ' gl_FragColor = color;', |
| 21 | '}' |
| 22 | ].join('\n'), |
| 23 | |
| 24 | linksVS = [ |
| 25 | 'attribute vec2 a_vertexPos;', |
| 26 | 'attribute vec4 a_color;', |
| 27 | |
| 28 | 'uniform vec2 u_screenSize;', |
| 29 | 'uniform mat4 u_transform;', |
| 30 | |
| 31 | 'varying vec4 color;', |
| 32 | |
| 33 | 'void main(void) {', |
| 34 | ' gl_Position = u_transform * vec4(a_vertexPos/u_screenSize, 0.0, 1.0);', |
| 35 | ' color = a_color.abgr;', |
| 36 | '}' |
| 37 | ].join('\n'), |
| 38 | |
| 39 | program, |
| 40 | gl, |
| 41 | buffer, |
| 42 | utils, |
| 43 | locations, |
| 44 | linksCount = 0, |
| 45 | frontLinkId, // used to track z-index of links. |
| 46 | storage = new ArrayBuffer(16 * BYTES_PER_LINK), |
| 47 | positions = new Float32Array(storage), |
| 48 | colors = new Uint32Array(storage), |
| 49 | width, |
| 50 | height, |
| 51 | transform, |
| 52 | sizeDirty, |
| 53 | |
| 54 | ensureEnoughStorage = function () { |
| 55 | // TODO: this is a duplicate of webglNodeProgram code. Extract it to webgl.js |
| 56 | if ((linksCount+1)*BYTES_PER_LINK > storage.byteLength) { |
| 57 | // Every time we run out of space create new array twice bigger. |
| 58 | // TODO: it seems buffer size is limited. Consider using multiple arrays for huge graphs |
| 59 | var extendedStorage = new ArrayBuffer(storage.byteLength * 2), |
| 60 | extendedPositions = new Float32Array(extendedStorage), |
| 61 | extendedColors = new Uint32Array(extendedStorage); |
| 62 | |
| 63 | extendedColors.set(colors); // should be enough to copy just one view. |
| 64 | positions = extendedPositions; |
| 65 | colors = extendedColors; |
| 66 | storage = extendedStorage; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | return { |
no test coverage detected