* Defines simple UI for nodes in webgl renderer. Each node is rendered as square. Color and size can be changed.
()
| 13 | * Defines simple UI for nodes in webgl renderer. Each node is rendered as square. Color and size can be changed. |
| 14 | */ |
| 15 | function webglNodeProgram() { |
| 16 | var ATTRIBUTES_PER_PRIMITIVE = 4; // Primitive is point, x, y, size, color |
| 17 | // x, y, z - floats, color = uint. |
| 18 | var BYTES_PER_NODE = 3 * Float32Array.BYTES_PER_ELEMENT + Uint32Array.BYTES_PER_ELEMENT; |
| 19 | var nodesFS = [ |
| 20 | 'precision mediump float;', |
| 21 | 'varying vec4 color;', |
| 22 | |
| 23 | 'void main(void) {', |
| 24 | ' gl_FragColor = color;', |
| 25 | '}' |
| 26 | ].join('\n'); |
| 27 | var nodesVS = [ |
| 28 | 'attribute vec3 a_vertexPos;', |
| 29 | 'attribute vec4 a_color;', |
| 30 | 'uniform vec2 u_screenSize;', |
| 31 | 'uniform mat4 u_transform;', |
| 32 | 'varying vec4 color;', |
| 33 | |
| 34 | 'void main(void) {', |
| 35 | ' gl_Position = u_transform * vec4(a_vertexPos.xy/u_screenSize, 0, 1);', |
| 36 | ' gl_PointSize = a_vertexPos.z * u_transform[0][0];', |
| 37 | ' color = a_color.abgr;', |
| 38 | '}' |
| 39 | ].join('\n'); |
| 40 | |
| 41 | var program; |
| 42 | var gl; |
| 43 | var buffer; |
| 44 | var locations; |
| 45 | var utils; |
| 46 | var storage = new ArrayBuffer(16 * BYTES_PER_NODE); |
| 47 | var positions = new Float32Array(storage); |
| 48 | var colors = new Uint32Array(storage); |
| 49 | var nodesCount = 0; |
| 50 | var width; |
| 51 | var height; |
| 52 | var transform; |
| 53 | var sizeDirty; |
| 54 | |
| 55 | return { |
| 56 | load: load, |
| 57 | |
| 58 | /** |
| 59 | * Updates position of node in the buffer of nodes. |
| 60 | * |
| 61 | * @param idx - index of current node. |
| 62 | * @param pos - new position of the node. |
| 63 | */ |
| 64 | position: position, |
| 65 | |
| 66 | updateTransform: updateTransform, |
| 67 | |
| 68 | updateSize: updateSize, |
| 69 | |
| 70 | removeNode: removeNode, |
| 71 | |
| 72 | createNode: createNode, |