-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomNodeProgram.js
545 lines (438 loc) · 23.2 KB
/
customNodeProgram.js
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
function WebglCircle(size, color, data, colorIndexes, rawData) {
circleDataArray = assignQuadrant(getDataPercentage(data), colorIndexes);
this.size = size;
this.color = color;
this.data = circleDataArray[0];
this.rawData = rawData;
this.colorIndexes = circleDataArray[1];
}
function getDataPercentage(data) {
var arrayData = [];
var total = 0;
$.each(data,function() {
total += this;
});
for (i = 0; i<data.length; i++){
var result = (data[i]/total) * 360;
if (data[i] == 0) arrayData.push(data[i]);
else arrayData.push(result);
}
return arrayData;
}
function assignQuadrant(dataInPercentage, colorIndexes){
//console.log(maxSize);
//console.log(dataInPercentage);
//console.log(colorIndexes);
maxSize = dataInPercentage.length;
newDataArray = [];
newIndexArray = [];
remaining = 0;
totalAngles = 0;
prevTotalAngles = 0;
countData = 0;
if (dataInPercentage.length == 0){
return [[0],[0]];
}
else{
while(totalAngles + dataInPercentage[countData] <= 90){
totalAngles += dataInPercentage[countData];
newDataArray.push(dataInPercentage[countData]);
newIndexArray.push(colorIndexes[countData]);
countData++;
}
remaining = 90 - totalAngles;
if (remaining > 0){
newDataArray.push(remaining);
newIndexArray.push(colorIndexes[countData]);
dataInPercentage[countData] = dataInPercentage[countData] - remaining;
totalAngles += remaining;
}
while(totalAngles + dataInPercentage[countData] <= 180){
totalAngles += dataInPercentage[countData];
newDataArray.push(dataInPercentage[countData]);
newIndexArray.push(colorIndexes[countData]);
countData++;
}
if(totalAngles > 0) remaining = 180 - totalAngles;
else remaining = 90;
if (remaining > 0){
newDataArray.push(remaining);
newIndexArray.push(colorIndexes[countData]);
dataInPercentage[countData] = dataInPercentage[countData] - remaining;
totalAngles += remaining;
}
while(totalAngles + dataInPercentage[countData] <= 270){
totalAngles += dataInPercentage[countData];
newDataArray.push(dataInPercentage[countData]);
newIndexArray.push(colorIndexes[countData]);
countData++;
}
if(totalAngles > 0) remaining = 270 - totalAngles;
else remaining = 90;
if (remaining > 0){
newDataArray.push(remaining);
newIndexArray.push(colorIndexes[countData]);
dataInPercentage[countData] = dataInPercentage[countData] - remaining;
totalAngles += remaining;
}
while(totalAngles + dataInPercentage[countData] <= 360){
totalAngles += dataInPercentage[countData];
newDataArray.push(dataInPercentage[countData]);
newIndexArray.push(colorIndexes[countData]);
countData++;
}
return [newDataArray, newIndexArray];
}
}
function buildCircleNodeShader(angleNumbers, totalTypes) {
// For each primitive we need 4 attributes: x, y, color and size.
var ATTRIBUTES_PER_PRIMITIVE = 4 + (angleNumbers * 2), //angle numbers + color Indexes
//totalTypes are passed directly to the fragment shader
numberOfAngles = angleNumbers,
attributesToVertex = '',
anglesToArray = '',
remainingAngles = numberOfAngles,
countvec4 = 0,
currentIndexA = 0,
currentIndexC = 0,
elementsPerVec = [];
//console.log(ATTRIBUTES_PER_PRIMITIVE)
var first = true;
//console.log(ATTRIBUTES_PER_PRIMITIVE);
var firstTime = 0;
while(remainingAngles / 2 >= 1 || remainingAngles > 0){
countvec4 += 1;
var vecIndexes = 0;
var attrTag = '';
if (remainingAngles >= 2){
attrTag = 'vec4';
vecIndexes = 4;
elementsPerVec.push(vecIndexes);
}
else if(remainingAngles == 1){
attrTag = 'vec2';
vecIndexes = 2;
elementsPerVec.push(vecIndexes);
}
// else{
// attrTag += String(remainingAngles);
// vecIndexes = remainingAngles;
// }
attributesToVertex += 'attribute '+ attrTag +' a_anglesAndColors' + String(countvec4) + ';\n';
//attributesToVertex += 'attribute '+ attrTag +' a_colorIndex' + String(countvec4) + ';\n';
var isAngle = true;
for (i = 1; i <= vecIndexes; i++){
if (vecIndexes == 1){
anglesToArray += ' angles[' + String(currentIndexA) + '] = a_anglesAndColors' + String(countvec4) + ';\n';
anglesToArray += ' colorIndex[' + String(currentIndexC) + '] = a_colorIndex' + String(countvec4) + ';\n';
}
else{
if (isAngle){
anglesToArray += 'angles[' + String(currentIndexA) + '] = a_anglesAndColors' + String(countvec4) + '[' + String(i-1) + '];\n';
currentIndexA += 1;
isAngle = false;
}
else{
anglesToArray += 'colorIndex[' + String(currentIndexC) + '] = a_anglesAndColors' + String(countvec4) + '[' + String(i-1) + '];\n';
currentIndexC += 1;
isAngle = true;
}
}
}
remainingAngles = remainingAngles - 2;
}
attributesToVertex += 'const int numberOfAngles = ' + String(numberOfAngles) + ';\n';
// for (i = 0; i<numberOfAngles; i++){
// anglesToArray += ' angles[' + String(i) + '] = a_angle' + String(i+1) + ';\n';
// anglesToArray += ' colorIndex[' + String(i) + '] = a_colorIndex' + String(i+1) + ';\n';
// }
var nodesVS = [
'precision mediump float;',
'attribute vec2 a_vertexPos;',
// Pack clor and size into vector. First elemnt is color, second - size.
// Since it's floating point we can only use 24 bit to pack colors...
// thus alpha channel is dropped, and is always assumed to be 1.
'attribute vec2 a_customAttributes;',
String(attributesToVertex),
'uniform vec2 u_screenSize;',
'uniform mat4 u_transform;',
'varying float angles['+String(numberOfAngles)+'];',
'varying float colorIndex['+String(numberOfAngles)+'];',
'void main(void) {',
' gl_Position = u_transform * vec4(a_vertexPos/u_screenSize, 0, 1);',
' gl_PointSize = a_customAttributes[1] * u_transform[0][0];',
anglesToArray,
'}'].join('\n'),
nodesFS = [
'precision mediump float;',
'const int numberOfAngles = ' + String(numberOfAngles) + ';',
'varying float angles[numberOfAngles];',
'varying float colorIndex[numberOfAngles];',
'vec4 currentColor = vec4(1,0,0,1);',
//'varying vec2 vTexCoord;', //get the passing value from the vertex shader
'vec4 getColor(float col){',
'vec4 colorToUse;',
'float c = col;',
' colorToUse.b = mod(c, 256.0); c = floor(c/256.0);',
' colorToUse.g = mod(c, 256.0); c = floor(c/256.0);',
' colorToUse.r = mod(c, 256.0); c = floor(c/256.0); colorToUse /= 255.0;',
' colorToUse.a = 1.0;',
'return colorToUse;',
'}',
'void main(){',
'float prevAngle = radians(0.0);',
'float radQuad = radians(90.0);',
'float totalAngles = 0.0;',
'bool found = false;',
'bool hasRest = false;',
'float rad = 0.0;',
'float AngleToUse = 0.0;',
'float rest;',
'int prevAngleNumber = 0;',
'float prevTotal = 0.0;',
'if (gl_PointCoord.y <= 0.5 && gl_PointCoord.x <= 0.5){',
'prevAngle = radians(0.0);',
'for(int i = 0; i<numberOfAngles;i++){',
'totalAngles = totalAngles + angles[i];',
'if (totalAngles <= 90.0){',
'AngleToUse = angles[i];',
'}',
'else{',
'continue;',
'}',
'rad = radians(AngleToUse);',
'if (totalAngles == 90.0 && (tan(prevAngle) <= (gl_PointCoord.y - 0.5) / (gl_PointCoord.x - 0.5))){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'else if ((tan(rad + prevAngle) >= (gl_PointCoord.y - 0.5) / (gl_PointCoord.x - 0.5)) && (tan(prevAngle) <= (gl_PointCoord.y - 0.5) / (gl_PointCoord.x - 0.5))){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'prevAngle = prevAngle + rad;',
'}',
'}',
'else if (gl_PointCoord.y < 0.5 && gl_PointCoord.x > 0.5){',
'prevAngle = radians(0.0);',
'for(int i = 0; i<numberOfAngles;i++){',
'totalAngles = totalAngles + angles[i];',
'if (totalAngles > 90.0 && totalAngles <= 180.0){',
'AngleToUse = angles[i];',
'}',
'else{',
'continue;',
'}',
'rad = radians(AngleToUse);',
'if (totalAngles == 180.0 && tan(prevAngle) <= (- 2.0 * ( 0.5 - gl_PointCoord.x)) / (- 2.0 * (gl_PointCoord.y - 0.5))){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'else if (tan(rad + prevAngle) >= (- 2.0 * ( 0.5 - gl_PointCoord.x)) / (- 2.0 * (gl_PointCoord.y - 0.5)) && tan(prevAngle) <= (- 2.0 * ( 0.5 - gl_PointCoord.x)) / (- 2.0 * (gl_PointCoord.y - 0.5)) ){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'prevAngle = prevAngle + rad;',
'}',
'}',
'else if (gl_PointCoord.y >= 0.5 && gl_PointCoord.x >= 0.5){',
'prevAngle = radians(0.0);',
'for(int i = 0; i<numberOfAngles;i++){',
'totalAngles = totalAngles + angles[i];',
'if (totalAngles > 180.0 && totalAngles <= 270.0){',
'AngleToUse = angles[i];',
'}',
'else {',
'continue;',
'}',
'rad = radians(AngleToUse);',
'if (totalAngles == 270.0 && tan(prevAngle) <= (- 2.0 * ( 0.5 - gl_PointCoord.y)) / (- 2.0 * ( 0.5 - gl_PointCoord.x))){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'else if (tan(rad + prevAngle) >= (- 2.0 * ( 0.5 - gl_PointCoord.y)) / (- 2.0 * ( 0.5 - gl_PointCoord.x)) && tan(prevAngle) <= (- 2.0 * ( 0.5 - gl_PointCoord.y)) / (- 2.0 * ( 0.5 - gl_PointCoord.x)) ){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'prevAngle = prevAngle + rad;',
'}',
'}',
'else if (gl_PointCoord.y >= 0.5 && gl_PointCoord.x <= 0.5){',
'prevAngle = radians(0.0);',
'for(int i = 0; i<numberOfAngles;i++){',
'totalAngles = totalAngles + angles[i];',
'if (totalAngles > 270.0 && totalAngles <= 360.0){',
'AngleToUse = angles[i];',
'}',
'else{',
'continue;',
'}',
'rad = radians(AngleToUse);',
'if (AngleToUse != 0.0 && totalAngles == 360.0 && tan(prevAngle) <= (- 2.0 * (gl_PointCoord.x - 0.5)) / (- 2.0 * ( 0.5 - gl_PointCoord.y))){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'else if(AngleToUse == 0.0){',
'found = true;',
'continue;',
'}',
'else if (tan((rad + prevAngle)) >= (- 2.0 * (gl_PointCoord.x - 0.5)) / (- 2.0 * ( 0.5 - gl_PointCoord.y)) && tan((prevAngle)) <= (- 2.0 * (gl_PointCoord.x - 0.5)) / (- 2.0 * ( 0.5 - gl_PointCoord.y)) ){',
'float ind = float(colorIndex[i]);',
'vec4 colorToUse = getColor(ind);',
'gl_FragColor = colorToUse;',
'found = true;',
'}',
'prevAngle = prevAngle + rad;',
'}',
'}',
'if (found == false){',
'if ((gl_PointCoord.x - 0.5) * (gl_PointCoord.x - 0.5) + (gl_PointCoord.y - 0.5) * (gl_PointCoord.y - 0.5) < 0.25){',
'gl_FragColor = vec4(0, 0, 1, 1);',
'}',
'else{',
'gl_FragColor = vec4(0);',
'}',
'}',
'else if ((gl_PointCoord.x - 0.5) * (gl_PointCoord.x - 0.5) + (gl_PointCoord.y - 0.5) * (gl_PointCoord.y - 0.5) > 0.25){',
' gl_FragColor = vec4(0);',
'}',
'}'].join('\n');
var program,
gl,
buffer,
locations,
utils,
nodes = new Float32Array(64),
nodesCount = 0,
canvasWidth, canvasHeight, transform,
isCanvasDirty;
return {
/**
* Called by webgl renderer to load the shader into gl context.
*/
load : function (glContext) {
gl = glContext;
//alert(gl.getParameter(gl.MAX_VARYING_VECTORS));
//if (prevNodeProgram != null) gl.deleteProgram(prevNodeProgram);
webglUtils = Viva.Graph.webgl(glContext);
program = webglUtils.createProgram(nodesVS, nodesFS);
//prevNodeProgram = program;
locations = webglUtils.getLocations(program, ['a_vertexPos', 'a_customAttributes', 'u_screenSize', 'u_transform']);
gl.enableVertexAttribArray(locations.vertexPos);
//gl.enableVertexAttribArray(locations.customAttributes);
//console.log(locations.customAttributes);
var prevNodeIndex = 0;
for (o = 0; o <= countvec4; o++){
gl.enableVertexAttribArray(locations.customAttributes + o);
}
buffer = gl.createBuffer();
},
/**
* Called by webgl renderer to update node position in the buffer array
*
* @param nodeUI - data model for the rendered node (WebGLCircle in this case)
* @param pos - {x, y} coordinates of the node.
*/
position : function (nodeUI, pos) {
var idx = nodeUI.id;
//console.log(nodeUI.data);
var prevNodeIndex = 0;
nodes[idx * ATTRIBUTES_PER_PRIMITIVE] = pos.x;
nodes[idx * ATTRIBUTES_PER_PRIMITIVE + 1] = pos.y;
nodes[idx * ATTRIBUTES_PER_PRIMITIVE + 2] = nodeUI.color;
nodes[idx * ATTRIBUTES_PER_PRIMITIVE + 3] = nodeUI.size;
var countTimes = 0;
for (x = 1; x<= numberOfAngles; x++){
nodes[idx * ATTRIBUTES_PER_PRIMITIVE + 3 + x + prevNodeIndex] = nodeUI.data[x-1];
nodes[idx * ATTRIBUTES_PER_PRIMITIVE + 3 + x + 1 + prevNodeIndex] = nodeUI.colorIndexes[x-1];
prevNodeIndex += 1;
}
//console.log(nodeUI.colorIndexes);
// if(firstTime<1) console.log(nodes);
// firstTime += 1;
},
/**
* Request from webgl renderer to actually draw our stuff into the
* gl context. This is the core of our shader.
*/
render : function() {
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, nodes, gl.DYNAMIC_DRAW);
if (first){
first = false;
}
if (isCanvasDirty) {
isCanvasDirty = false;
gl.uniformMatrix4fv(locations.transform, false, transform);
gl.uniform2f(locations.screenSize, canvasWidth, canvasHeight);
}
//Since you are using GL_FLOAT as type, which has a size of 4 bytes, the offset is only allowed to be a multiple of 4.
gl.vertexAttribPointer(locations.vertexPos, 2, gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(locations.customAttributes, 2, gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, 2 * 4);
var prevNodeIndex = 0;
for (i = 1; i<= countvec4; i++){
gl.vertexAttribPointer(locations.customAttributes + i, elementsPerVec[i-1], gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, (4 + prevNodeIndex) * 4);
//gl.vertexAttribPointer(locations.customAttributes + i + 1 + prevNodeIndex, 1, gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, (4+i-1 + 1 + prevNodeIndex) * 4);
prevNodeIndex += elementsPerVec[i-1];
}
//gl.vertexAttribPointer(locations.dados, 3, gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, 5 * 4);
gl.finish();
gl.drawArrays(gl.POINTS, 0, nodesCount);
},
/**
* Called by webgl renderer when user scales/pans the canvas with nodes.
*/
updateTransform : function (newTransform) {
transform = newTransform;
isCanvasDirty = true;
},
/**
* Called by webgl renderer when user resizes the canvas with nodes.
*/
updateSize : function (newCanvasWidth, newCanvasHeight) {
canvasWidth = newCanvasWidth;
canvasHeight = newCanvasHeight;
isCanvasDirty = true;
},
/**
* Called by webgl renderer to notify us that the new node was created in the graph
*/
createNode : function (node) {
nodes = webglUtils.extendArray(nodes, nodesCount, ATTRIBUTES_PER_PRIMITIVE);
nodesCount += 1;
},
/**
* Called by webgl renderer to notify us that the node was removed from the graph
*/
removeNode : function (node) {
if (nodesCount > 0) { nodesCount -=1; }
if (node.id < nodesCount && nodesCount > 0) {
// we do not really delete anything from the buffer.
// Instead we swap deleted node with the "last" node in the
// buffer and decrease marker of the "last" node. Gives nice O(1)
// performance, but make code slightly harder than it could be:
webglUtils.copyArrayPart(nodes, node.id*ATTRIBUTES_PER_PRIMITIVE, nodesCount*ATTRIBUTES_PER_PRIMITIVE, ATTRIBUTES_PER_PRIMITIVE);
}
},
/**
* This method is called by webgl renderer when it changes parts of its
* buffers. We don't use it here, but it's needed by API (see the comment
* in the removeNode() method)
*/
replaceProperties : function(replacedNode, newNode) {},
};
}