#extension GL_OES_standard_derivatives : enable precision highp float; uniform float time; uniform vec2 mouse; uniform vec2 resolution; #define PI 3.1415926535897932384626433832795 vec3 shade1(float r, vec2 center, vec2 pos, vec3 lights[2]) { return vec3(255,0,0); } vec3 shade2(float r, vec2 center, vec2 pos, vec3 lights[2]) { float dist = sqrt(pos.x*pos.x + pos.y*pos.y); if (dist < r) return vec3(1,0,0); else return vec3(0,0,0); } vec3 shade3(float r, vec2 center, vec2 pos, vec3 lights[2]) { float z = sqrt(r * r - pos.x * pos.x - pos.y * pos.y); // z coordinate of the fragment (given x, y) if (z < r) return vec3(z/r,0,0); else return vec3(0,0,0); } vec3 shade4(float r, vec2 center, vec2 pos, vec3 lights[2]) { float z = sqrt(r * r - pos.x * pos.x - pos.y * pos.y); // z coordinate of the fragment (given x, y) vec3 n = normalize(vec3(pos.x, pos.y, z)); // surface normal float val = acos(dot(n, vec3(0,0,1))); return vec3(val,val,0); } vec3 shade5(float r, vec2 center, vec2 pos, vec3 lights[2]) { float z = sqrt(r * r - pos.x * pos.x - pos.y * pos.y); // z coordinate of the fragment (given x, y) vec3 n = normalize(vec3(pos.x, pos.y, z)); // surface normal float val0 = max(dot(n, lights[0]), 0.0); vec3 c0 = vec3(val0, val0, val0); float val1 = max(dot(n, lights[1]), 0.0); vec3 c1 = vec3(val1, val1, val1); return c0+c1; } vec3 shade6(float r, vec2 center, vec2 pos, vec3 lights[2]) { float z = sqrt(r * r - pos.x * pos.x - pos.y * pos.y); // z coordinate of the fragment (given x, y) vec3 n = normalize(vec3(pos.x, pos.y, z)); // surface normal float val0 = max(dot(n, lights[0]), 0.0); vec3 c0 = vec3(val0, val0, 0.0); float val1 = max(dot(n, lights[1]), 0.0); vec3 c1 = vec3(0.0, val1, val1); float dist = sqrt(pos.x*pos.x + pos.y*pos.y); float mod1 = 0.5*sin(lights[0].x*4.0*dist/r*PI); float mod2 = 0.5*cos(4.0*dist/r*PI+PI*0.5); return mod1*c0+mod2*c1; } void main( void ) { // sphere definition: vec2 center = resolution.xy / 2.0; // center of screen as the center of the sphere; iResolution: viewport resolution (in pixels) float r = resolution.y / 3.0; // r: radius of the sphere // fragCoord: screen space coordinates (based on the viewport), see https://computergraphics.stackexchange.com/questions/5724/glsl-can-someone-explain-why-gl-fragcoord-xy-screensize-is-performed-and-for vec2 pos = gl_FragCoord.xy - center; // light vector: vec3 lights[2]; // two lights // the lights will move over time; the positions of the lights: lights[0] = normalize(vec3(sin(time), sin(time), cos(time))); lights[1] = normalize(vec3(-sin(time), cos(time), sin(time))); vec3 c = shade6(r, center, pos, lights); // calculate the color for each fragment gl_FragColor = vec4(c, 1.0); }