-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphWithoutViva.js
448 lines (354 loc) · 18.3 KB
/
graphWithoutViva.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
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];
this.angleNumbers = circleDataArray[0].length;
}
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 constructPie(webglCircle) {
angleNumbers = webglCircle.angleNumbers;
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');
var 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');
//Define VERTEX SHADER and FRAGMENT SHADER
vs = nodesVS;
fs = nodesFS;
var program = createProgram(vs,fs);
gl.useProgram(program);
program.vertexPos = gl.getAttribLocation(program,'a_vertexPos');
program.customAttributes = gl.getAttribLocation(program,'a_customAttributes');
program.screenSize = gl.getUniformLocation(program,'u_screenSize');
program.transform = gl.getUniformLocation(program,'u_transform');
var nodes = new Float32Array(4 + angleNumbers*2);
nodes[0] = 0;
nodes[1] = 0;
nodes[2] = webglCircle.color;
nodes[3] = webglCircle.size;
var countTimes = 0;
var prevNodeIndex = 0;
for (x = 1; x<= numberOfAngles; x++){
nodes[3 + x + prevNodeIndex] = webglCircle.data[x-1];
nodes[3 + x + 1 + prevNodeIndex] = webglCircle.colorIndexes[x-1];
prevNodeIndex += 1;
}
gl.enableVertexAttribArray(program.vertexPos);
for (o = 0; o <= countvec4; o++){
gl.enableVertexAttribArray(program.customAttributes + o);
}
buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, nodes, gl.DYNAMIC_DRAW);
gl.uniformMatrix4fv(program.transform, false, [0.9349198761484702, 0, 0, 0, 0, 0.9349198761484702, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
gl.uniform2f(program.screenSize, c.width, c.height);
gl.vertexAttribPointer(program.vertexPos, 2, gl.FLOAT, false, ATTRIBUTES_PER_PRIMITIVE * Float32Array.BYTES_PER_ELEMENT, 0);
gl.vertexAttribPointer(program.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(program.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.finish();
gl.drawArrays(gl.POINTS, 0, 1);
}