Skip to content

Instantly share code, notes, and snippets.

@akshayloke
Last active August 11, 2016 04:49
Show Gist options
  • Save akshayloke/b3d79241dee2c04492ba292c06f4e844 to your computer and use it in GitHub Desktop.
Save akshayloke/b3d79241dee2c04492ba292c06f4e844 to your computer and use it in GitHub Desktop.
Spiral.frag shader using editor.thebookofshaders.com
// Author:
// Title:
#ifdef GL_ES
precision mediump float;
#endif
#define M_PI 3.14159265358979
#define M_2PI 6.28318530717958
#define RADIUS 100.0
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 center = u_resolution.xy * 0.5;
vec2 normCenter = vec2(0.5);
vec2 uv = gl_FragCoord.xy;
vec2 normUV = gl_FragCoord.xy / u_resolution.xy;
vec2 centerToUV = normUV - normCenter;
float lengthCenterToUV = length(centerToUV);
//atan gives value between -pi and pi.. so first adding pi.. which makes the value between 0 and 2pi
//then scaling it back to 0 and 1 by dividing by 2pi
float theta = (atan(centerToUV.y, centerToUV.x) - M_PI) / M_2PI * -1.;
//if theta is 0 that means atan value is -pi
//if theta is 1 that means atan value is pi
//if theta is 0.5 that means atan value is 0
float mult = 0.1;
float col = 0.;
for (int i=0; i<4; i++) {
float index = float(i);
float ringPre = (theta + index - 1.) * mult;
if (i == 0) {
ringPre = 0.;
}
float ringPost = (theta + index) * mult;
float stepVal = step(ringPre, lengthCenterToUV) - step(ringPost, lengthCenterToUV);
col += (stepVal * (theta + index) * 0.25);
}
vec3 color = vec3(col);
float timeFactor = cos(u_time * 0.8) * 0.5 + 0.5;
col += (timeFactor);
if (col > 1.0)
col = (col - 1.0);
color = vec3(col);
//color lerp
float rate = col;
vec3 color1 = vec3(1., 0., 1.);
vec3 color2 = vec3(1., 1., 0.);
vec3 color3 = vec3(0., 1., 0.6);
vec3 color4 = vec3(0., 1., 1.);
vec3 color5 = vec3(0.6, 0., 1.);
vec3 color6 = vec3(1., 0., 1.);
float t0, t1;
vec3 c0, c1;
if (rate < 0.5) {
t0 = 0.0; t1 = 0.5;
c0 = color1; c1 = color2;
}
else if (rate < 0.6) {
t0 = 0.5; t1 = 0.6;
c0 = color2; c1 = color3;
}
else if (rate < 0.7) {
t0 = 0.6; t1 = 0.7;
c0 = color3; c1 = color4;
}
else if (rate < 0.8) {
t0 = 0.7; t1 = 0.8;
c0 = color4; c1 = color5;
}
else {
t0 = 0.8; t1 = 1.0;
c0 = color5; c1 = color6;
}
float param = (rate - t0) / (t1 - t0);
color = mix(c0, c1, param);
//circle only
if (lengthCenterToUV > 0.278)
color *= 0.0;
gl_FragColor = vec4(color, 1.0);
}
@akshayloke
Copy link
Author

akshayloke commented Aug 11, 2016

Rev 5:
download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment