forked from polyrhythmatic/computer-graphics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment1.html
executable file
·94 lines (66 loc) · 2.2 KB
/
assignment1.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
<!! THIS IS THE index.html THAT WE ENDED UP WITH BY THE END OF CLASS>
<script src=lib1.js></script>
<body bgcolor=black>
<center>
<canvas id='canvas1' width='800' height='800'>
</canvas>
</center>
</body>
<!!=================================================================
VERTEX SHADER: runs once per triangle vertex.
------------------------------------------------------------------->
<script id='vs' type='other'>
attribute vec3 aPosition;
varying vec3 vPosition;
void main() {
gl_Position = vec4(aPosition, 1.0);
vPosition = aPosition;
}
</script>
<!!=================================================================
FRAGMENT SHADER: runs once per pixel fragment.
Your assignment is to write your own interesting version of the
fragment shader program. Try to have fun with it -- use "uTime"
to create animated patterns, and "uCursor" to make it responsive
to a user's mouse gestures.
DO NOT just hand in a variation of what I did in class! I expect
you to create something original.
.------------------------------------------------------------------>
<script id='fs' type='other'>
precision mediump float;
uniform float uTime;
uniform vec3 uCursor;
varying vec3 vPosition;
// COMPUTE THE Z FOR A SPHERE OF RADIUS r.
float computeZ(vec2 xy, float r) {
float zz = r * r - (2.5-sin(uTime*sin(1.5*uTime))) * xy.x * xy.x - (2.5-sin(2.*uTime*sin(0.5*uTime)))*xy.y * xy.y;
if (zz < 0.)
return -1.;
else
return sqrt(zz);
}
void main() {
float x = vPosition.x;
float y = vPosition.y;
float z = computeZ(vPosition.xy, 1.0);
// BACKGROUND SHADE IS JUST BLACK (ZERO).
float s = 0.0;
// IF WE ARE IN THE SPHERE, THEN SHADE IT.
if (z > 0.) {
// START WITH DARK SHADE.
s = 0.2;
//animating the light
float d = dot(vec3(x,y,z), vec3(3.*sin(uTime),sin(.5*uTime),0.8+.2*sin(5.*uTime)));
// float d = 1.0;
if (d > 0.){
s += 0.8 * d;
}
s *= 0.5 + 0.5 * y*x*tan(5.*uTime) * z*x*tan(2.*uTime)*y*tan(3.*uTime*sin(20.*uTime));
}
// multiply shade by gradient
gl_FragColor = vec4(s * vec3(.5+.5*(.5*y + .5*x), 0., .5-.5*(.5*y + .5*x)), 1.);
}
</script>
<script>
start_gl("canvas1", getStringFromScript('vs'), getStringFromScript('fs'));
</script>