-
Notifications
You must be signed in to change notification settings - Fork 0
/
2d-rotative-shapes-dance.html
614 lines (510 loc) · 16.8 KB
/
2d-rotative-shapes-dance.html
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" type="image/x-icon" href="https://raw.githubusercontent.com/rubenandrebarreiro/rubenandrebarreiro.github.io/master/assets/images/javascript-logo-1.png"/>
<style>
body {
color: #000;
font-family:Tahoma;
font-size:13px;
font-weight: bold;
background-color: #000000;
background-image: url("imgs/JPGs/background.jpg");
}
#container {
position: absolute;
border-radius: 5px;
}
#workDescription {
background:#eeeeee;
position: absolute;
left: 8px; top: 578px;
padding:0;
margin:0;
overflow:hidden;
width: 1200px;
border-radius: 5px;
text-align: center;
}
#authorsInfo {
background:#eeeeee;
position: absolute;
left: 8px; top: 840px;
padding:0;
margin:0;
overflow:hidden;
width: 1200px;
border-radius: 5px;
text-align: center;
}
#instructions {
background:#eeeeee;
position: absolute;
left: 1220px; top: 24px;
padding:0;
margin:0;
overflow:hidden;
width: 640px;
border-radius: 5px;
text-align: center;
}
#settings {
background:#eeeeee;
position: absolute;
left: 1220px; top: 190px;
padding:0;
margin:0;
overflow:hidden;
width: 640px;
border-radius: 5px;
text-align: center;
}
a {
color: #35b0ab;
}
.color {
display: inline-block;
zoom:1; *display: inline;
width: 200px;
height: 40px;
border: 1px solid rgba(0, 0, 0, 0.5);
-webkit-box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, 0.3);
box-shadow: 1px 1px 2px 0px rgba(0, 0, 0, 0.3);
background-color: rgba(0, 0, 0, 1.0);
}
</style>
<script id="vertex-shader" type="x-shader/x-vertex">
precision mediump float;
attribute vec2 vPosition;
attribute vec4 vColor;
attribute vec3 vSizeFigureSpeed;
attribute vec2 vBlurDirection;
varying vec4 fColor;
varying vec3 fSizeFigureSpeed;
varying vec2 fBlurDirection;
uniform float time;
void main() {
gl_Position = vec4(vPosition, 0.0, 1.0); // The last argument must be 1.0, because it's the normalization coordinate
gl_PointSize = vSizeFigureSpeed[0];
fColor = vColor;
fSizeFigureSpeed = vSizeFigureSpeed;
fBlurDirection = vBlurDirection;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
varying vec3 fSizeFigureSpeed;
varying vec2 fBlurDirection;
uniform float time;
float getDist1D(float a, float b) {
float dist = (a - b);
return dist > 0.0 ? dist : dist * -1.0;
}
vec2 getPos(vec2 p) {
return vec2(2.0, -2.0) * p - vec2(1.0, -1.0);
}
// FIGURE 1 - SQUARE
vec4 square(vec2 posToDraw, vec4 color, bool blur) {
float a = sqrt(2.0)/2.0;
// Draw a Square
if(posToDraw.x > a || posToDraw.x < -a || posToDraw.y > a || posToDraw.y < -a) {
discard;
}
// Option of blur effect
if(blur) {
if(posToDraw.x < 0.0) {
posToDraw.x *= -1.0;
}
if(posToDraw.y < 0.0) {
posToDraw.y *= -1.0;
}
float maxFromCoord;
if(posToDraw.x > posToDraw.y) {
maxFromCoord = posToDraw.x;
}
else {
maxFromCoord = posToDraw.y;
}
return vec4(color.xyz, 1.0 - smoothstep(0.6, a, maxFromCoord));
}
else {
return color;
}
}
// FIGURE 2 - CIRCLE
vec4 circle(vec2 posToDraw, vec4 color, bool blur) {
float dist = distance(vec2(0.0, 0.0), posToDraw);
float radio = 1.0;
// Draw a Circle
if(dist > radio) {
discard;
}
// Option of blur effect
if(blur) {
return vec4(color.xyz, 1.0 - smoothstep(0.8, 1.0, dist));
}
else {
return color;
}
}
// FIGURE 3 - CROSS
vec4 cross(vec2 posToDraw, vec4 color, bool blur) {
float edgeToDiscard1 = 0.75;
float edgeToDiscard2 = 0.25;
bool xyEdgesCond1 = posToDraw.x > edgeToDiscard1 || posToDraw.x < -edgeToDiscard1 ||
posToDraw.y > edgeToDiscard1 || posToDraw.y < -edgeToDiscard1;
bool xyEdgesCond2 = posToDraw.x > edgeToDiscard2 && posToDraw.y > edgeToDiscard2;
bool xyEdgesCond3 = posToDraw.x < -edgeToDiscard2 && posToDraw.y > edgeToDiscard2;
bool xyEdgesCond4 = posToDraw.x < -edgeToDiscard2 && posToDraw.y < -edgeToDiscard2;
bool xyEdgesCond5 = posToDraw.x > edgeToDiscard2 && posToDraw.y < -edgeToDiscard2;
// Draw a Cross
if(xyEdgesCond1 || xyEdgesCond2 || xyEdgesCond3 || xyEdgesCond4 || xyEdgesCond5) {
discard;
}
// Option of blur effect
if(blur) {
float dist = distance(vec2(0.0, 0.0), posToDraw);
bool softEdge1 = posToDraw.x > 0.15 && posToDraw.y > 0.15;// && posToDraw.x < 0.5 && posToDraw.y < 0.5;
bool softEdge2 = posToDraw.x < -0.15 && posToDraw.y > 0.15;// && posToDraw.x > -0.5 && posToDraw.y < 0.5;
bool softEdge3 = posToDraw.x < -0.15 && posToDraw.y < -0.15;// && posToDraw.x > -0.5 && posToDraw.y > -0.5;
bool softEdge4 = posToDraw.x > 0.15 && posToDraw.y < -0.15;// && posToDraw.x < 0.5 && posToDraw.y > -0.5;
bool softEdge5 = posToDraw.x > 0.745 || posToDraw.x < -0.745 || posToDraw.y > 0.745 || posToDraw.y < -0.745;
float absX = (posToDraw.x > 0.0) ? posToDraw.x : posToDraw.x * (-1.0);
float absY = (posToDraw.y > 0.0) ? posToDraw.y : posToDraw.y * (-1.0);
float maxFromCoord = (absX > absY) ? absX : absY;
float minFromCoord = (absX < absY) ? absX : absY;
if(softEdge1 || softEdge2 || softEdge3 || softEdge4) {
return vec4(color.xyz, 1.0 - smoothstep(0.15, edgeToDiscard2, minFromCoord));
}
else if(softEdge5) {
return vec4(color.xyz, 1.0 - smoothstep(0.745, 0.75, maxFromCoord));
}
else {
return color;
}
}
else {
return color;
}
}
// FIGURE 4 - DONUT/TORUS (NORMAL)
vec4 donut1(vec2 posToDraw, vec4 color, bool blur) {
float dist = distance(vec2(0.0, 0.0), getPos(gl_PointCoord));
// Draw a Donut/Torus (Excluding the interior radio)
if(dist > 1.0 || dist < 0.5) {
discard;
}
bool firstEdgeOfRadio = getDist1D(0.5, dist) < getDist1D(1.0, dist);
// Option of blur effect
if(blur) {
if(firstEdgeOfRadio) {
return vec4(color.xyz, 1.0 - smoothstep(0.65, 0.5, dist));
}
else {
return vec4(color.xyz, 1.0 - smoothstep(0.85, 1.0, dist));
}
}
else {
return color;
}
}
// FIGURE 5 - HEART
vec4 heart(vec2 posToDraw, vec4 color, bool blur) {
float radio = pow(posToDraw.x, 2.0) + pow((5.0 * (posToDraw.y + 0.25)/4.0 - sqrt(abs(posToDraw.x))), 2.0);
if(0.5 < radio) {
discard;
}
// Option of blur effect
if(blur) {
bool drawBlur = (0.25 < radio);
if(drawBlur) {
return vec4(color.xyz, 1.0 - smoothstep(0.25, 0.5, radio));
}
else {
return color;
}
}
else {
return color;
}
}
// FIGURE 6 - FRAME
vec4 frame(vec2 posToDraw, vec4 color, bool blur) {
float a = sqrt(2.0)/2.0;
float b = 0.5 * a;
float c = 0.75 * a;
// Draw a Square
if(posToDraw.x > a || posToDraw.x < -a || posToDraw.y > a || posToDraw.y < -a) {
discard;
}
if(posToDraw.x < b && posToDraw.x > -b && posToDraw.y < b && posToDraw.y > -b) {
discard;
}
// Option of blur effect
if(blur) {
float absX = abs(posToDraw.x);
float absY = abs(posToDraw.y);
float maxFromCoord = (absX > absY)? absX : absY;
if(maxFromCoord > c) {
return vec4(color.xyz, 1.0 - smoothstep(c, a, maxFromCoord));
}
else {
return vec4(color.xyz, 1.0 - smoothstep(c, b, maxFromCoord));
}
}
else {
return color;
}
}
// FIGURE 7 - ASTROID
vec4 astroid(vec2 posToDraw, vec4 color) {
float a = 1.0;
float astroidEq1 = (pow(posToDraw.x, (2.0/3.0))) + (pow(posToDraw.y, (2.0/3.0))) - (pow(a, (2.0/3.0)));
float astroidEq2 = (pow(-posToDraw.x, (2.0/3.0))) + (pow(posToDraw.y, (2.0/3.0))) - (pow(a, (2.0/3.0)));
float astroidEq3 = (pow(posToDraw.x, (2.0/3.0))) + (pow(-posToDraw.y, (2.0/3.0))) - (pow(a, (2.0/3.0)));
float astroidEq4 = (pow(-posToDraw.x, (2.0/3.0))) + (pow(-posToDraw.y, (2.0/3.0))) - (pow(a, (2.0/3.0)));
// Draw an Astroid
if(astroidEq1 > 0.0 || astroidEq2 > 0.0 || astroidEq3 > 0.0 || astroidEq4 > 0.0) {
discard;
}
return color;
}
// FIGURE 8 - PAC-MAN
vec4 pacman(vec2 posToDraw, vec4 color) {
float dist = distance(vec2(0.0), posToDraw);
float theta = acos(posToDraw.x / dist);
float pi = 3.1415926535;
float angle1 = pi/4.0;
float angle2 = -pi/4.0;
if(dist > 1.0 || ((theta > -pi/6.0) && (theta < pi/6.0))) {
discard;
}
return color;
}
// FIGURE 9 - THREE LEAVED CLOVER
vec4 threeLeavedClover(vec2 posToDraw, vec4 color) {
float extRadio = 100.0 * (pow(posToDraw.x, 4.0) + (2.0 * pow(posToDraw.x, 2.0) * pow(posToDraw.y, 2.0)) + pow(posToDraw.y, 4.0) - pow(posToDraw.x, 3.0) + 3.0 * posToDraw.x * pow(posToDraw.y, 2.0));
if(0.0 < extRadio) {
discard;
}
else {
return color;
}
}
// FIGURE 10 - DONUT/TORUS (WITHOUT SOME EDGES)
vec4 donut2(vec2 posToDraw, vec4 color) {
float dist = distance(vec2(0.0, 0.0), posToDraw);
float theta = acos(posToDraw.x/dist);
float phi = 3.1415926/20.0;
float firstFactor = 0.0;
float secondFactor = 1.0;
for(int i = 0; i < 23; i++) {
if(secondFactor < 21.0 && secondFactor < 22.0) {
vec2 coodinatesInterval = vec2((firstFactor * phi), (secondFactor * phi));
// Excluding some angles using a theta = (PI/20)
if(dist > 1.0 || dist < 0.5 || (theta > coodinatesInterval.x && theta < coodinatesInterval.y && posToDraw.y > 0.0)) {
discard;
}
}
firstFactor += 2.0;
secondFactor += 2.0;
}
firstFactor = 1.0;
secondFactor = 2.0;
for(int i = 0; i < 23; i++) {
if(secondFactor < 21.0 && secondFactor < 22.0) {
vec2 coodinatesInterval = vec2((firstFactor * phi), (secondFactor * phi));
// Excluding some angles using a theta = (PI/20)
if(dist > 1.0 || dist < 0.5 || (theta > coodinatesInterval.x && theta < coodinatesInterval.y && posToDraw.y < 0.0)) {
discard;
}
}
firstFactor += 2.0;
secondFactor += 2.0;
}
return color;
}
mat2 rotate2d(float _angle) {
return mat2(cos(_angle), -sin(_angle), sin(_angle), cos(_angle));
}
void main() {
float theta = time * fBlurDirection.y * fSizeFigureSpeed.z;
vec2 rotatedPoint = rotate2d(theta) * getPos(gl_PointCoord);
bool blur = (fBlurDirection.x == 1.0) ? true : false;
// FIGURES WITH BLUR EFFECT
// Square
if(fSizeFigureSpeed.y <= 0.1) {
gl_FragColor = square(rotatedPoint, fColor, blur);
}
// Circle
else if(fSizeFigureSpeed.y <= 1.1) {
gl_FragColor = circle(rotatedPoint, fColor, blur);
}
// Cross
else if(fSizeFigureSpeed.y <= 2.1) {
gl_FragColor = cross(rotatedPoint, fColor, blur);
}
// Donut/Torus (Normal)
else if(fSizeFigureSpeed.y <= 3.1) {
gl_FragColor = donut1(rotatedPoint, fColor, blur);
}
// Heart
else if (fSizeFigureSpeed.y <= 4.1) {
gl_FragColor = heart(rotatedPoint, fColor, blur);
}
// Frame
else if (fSizeFigureSpeed.y <= 5.1) {
gl_FragColor = frame(rotatedPoint, fColor, blur);
}
// FIGURES WITHOUT BLUR EFFECT
// Astroid
else if (fSizeFigureSpeed.y <= 6.1) {
gl_FragColor = astroid(rotatedPoint, fColor);
}
// Pac-Man
else if (fSizeFigureSpeed.y <= 7.1) {
gl_FragColor = pacman(rotatedPoint, fColor);
}
// Three Leaved Clover
else if (fSizeFigureSpeed.y <= 8.1) {
gl_FragColor = threeLeavedClover(rotatedPoint, fColor);
}
// Donut/Torus (Without some Edges)
else if (fSizeFigureSpeed.y <= 9.1) {
gl_FragColor = donut2(rotatedPoint, fColor);
}
}
</script>
<script type="text/javascript" src="common/webgl-utils.js"></script>
<script type="text/javascript" src="common/initShaders.js"></script>
<script type="text/javascript" src="common/MV.js"></script>
<script type="text/javascript" src="2d-rotative-shapes-dance.js"></script>
</head>
<body>
<title>The Rotative Shapes' Dance</title>
<br>
<div id="container" style="display: inline-block; vertical-align: top">
<canvas id="gl-canvas" width="1200" height="540" style="border-radius: 5px;">
Oops... your browser doesn't support the HTML5 canvas element"
</canvas>
</div>
<br/>
<br/>
<div id="workDescription">
<h2>
<a href="https://www.fct.unl.pt/en/education/course/integrated-master-computer-science">
Computer Science and Engineering
<br/>
Integrated Master
</a>
</h2>
<h3>
@ <a href="https://www.fct.unl.pt/en"><i>FCT NOVA | FCT/UNL
<br/>
(Faculty of Sciences and Technology of New University of Lisbon)</i></a>
</h3>
<br/>
<h3>
<a href="http://www.unl.pt/guia/2018/fct/UNLGI_getUC?uc=8150">
Computer Graphics and Interfaces
</a>
<br/>
(2017/2018)
</h3>
<h4>
Lab Work 1 (Rotative Shapes' Dance)
</h4>
</div>
<div id="authorsInfo">
<h2>
Authors/Contributors
</h2>
<h3>
<u><i>Rúben André Barreiro</i></u>
</h3>
<h4>
no. 42648 (<a href="mailto:r.barreiro@campus.fct.unl.pt">r.barreiro@campus.fct.unl.pt</a>)
</h4>
</div>
<div id="instructions">
<h2>
Instructions
</h2>
<font size="2">
- Click anywhere in the canvas to create a <i><b>Rotative Shape</b></i>;
<br/>
- Choose the current <i>Shape's color</i>, by adjusting the <i>colors' factors</i>;
<br/>
- Choose the current <i>Geometric Shape</i> in use;
<br/>
- Choose the option to allow <i>Blur Edges</i>;
<br/>
- Choose the current direction (<i>Clock Direction</i> or the <i>opposite</i>) of the <i>Geometric Shape</i>;
<br/>
<br/>
</font>
</div>
<div id="settings">
<h2>
Settings/Adjustments
</h2>
<b>Colors' Factors Available:</b>
<br>
<dt><font color="red"><b>Red</b></font></dt><b>0%</b> <input id="redFactor" type="range" min="0.00" max="1.00" step="0.01" value="0.00"> <b>100%</b>
<br>
<dt><font color="green"><b>Green</b></font></dt><b>0%</b> <input id="greenFactor" type="range" min="0.00" max="1.00" step="0.01" value="0.00"> <b>100%</b>
<br>
<dt><font color="blue"><b>Blue</b></font> </dt><b>0%</b> <input id="blueFactor" type="range" min="0.00" max="1.00" step="0.01" value="0.00"> <b>100%</b>
<br>
<dt><b>Alpha</b> </dt><b>0%</b> <input id="alphaFactor" type="range" min="0.00" max="1.00" step="0.01" value="1.00"> <b>100%</b>
<br>
<br>
<b>Color Preview:</b>
<br>
<div id="colorPreview" class="color">
<!-- Color Preview Panel -->
</div>
<br>
<br>
<b>Geometric Figures/Shapes Available:</b>
<br>
<select id="figureInUse">
<option value = 0> Square </option>
<option value = 1> Circle </option>
<option value = 2> Cross </option>
<option value = 3> Donut/Torus </option>
<option value = 4> Heart </option>
<option value = 5> Frame </option>
<option value = 6> Astroid (No Blur Effect) </option>
<option value = 7> Pac-Man (No Blur Effect) </option>
<option value = 8> Three Leaved Clover (No Blur Effect) </option>
<option value = 9> Donut/Torus without some Edges (No Blur Effect) </option>
</select>
<br>
<br>
<b>Auto Generation of Points:</b>
<br>
<input type="button" id="autoButtonClick" value="Off">
<br>
<br>
<div id="blurOption">
<b>Blur Effect Option:</b>
<br>
<input type="radio" name="blurEffectRadios" value="true"> Yes
<br>
<input type="radio" name="blurEffectRadios" value="false" checked="checked"> No
<br>
<br>
</div>
<b>Rotation Direction:</b>
<br>
<input type="radio" name="rotationDirectionsRadios" value="clockDirection" checked="checked"> Clock Direction
<br>
<input type="radio" name="rotationDirectionsRadios" value="inverseClockDirection"> Inverse Clock Direction
<br/>
<br/>
</div>
</body>
</html>