-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment1.html
113 lines (87 loc) · 3.02 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!! 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='600' height='600'>
</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 - xy.x * xy.x - 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);
float s,u = 0.0;
// Fades the surrounding square in and out
s = sin((uTime));
if (z > 0.) {
//Periodically makes the outside square appear
s = sin((sin(x)/0.25*uTime));
//Responds if the user moves the mouse in from +y directions
if(uCursor.y > z){
s = tan((sin(x)/cos(uTime*1.5+200.0)));
}
//Responds if the user moves the mouse in from +x directions
if(uCursor.x > z){
s = tan((sin(x)/cos(uTime*1.123+200.0)));
}
//Responds if the user moves the mouse in from -x directions
if((-uCursor.x) > z){
s = tan((sin(x)/cos(uTime*1.123+200.0)));
}
//Responds if the user moves the mouse in from -y directions
if((-uCursor.y) > z){
s = tan((sin(x)/cos(uTime*1.5+200.0)));
}
//Used to change the "phase" of the sphere
float d = dot(vec3(x,y,z), vec3(1.,1.,1.));
if (d > 0.)
s += 0.6 * d;
}
//Changes color based on the direction from which the mouse enters the sphere
if(uCursor.x > z)
gl_FragColor = vec4(s * vec3(1.0, 5.0, 8.5), 1.);
else if((-uCursor.x) > z)
gl_FragColor = vec4(s * vec3(7.6, 2.0, 1.5), 1.);
else if(uCursor.y > z)
gl_FragColor = vec4(s * vec3(2.6, 2.0, 8.5), 1.);
else if((-uCursor.y) > z)
gl_FragColor = vec4(s * vec3(3.6, 9.0, 3.5), 1.);
else {gl_FragColor = vec4(s * vec3(0.0, 1.0, 0.5), 1.);}
}
</script>
<script>
start_gl("canvas1", getStringFromScript('vs'), getStringFromScript('fs'));
</script>