Skip to content

Instantly share code, notes, and snippets.

@noerw
Last active October 19, 2018 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noerw/56b70408745b323c3d56aeb112f0e1df to your computer and use it in GitHub Desktop.
Save noerw/56b70408745b323c3d56aeb112f0e1df to your computer and use it in GitHub Desktop.
animating a glowing colorful circle (first shader, for glslViewer)
#define ROTATION_PERIOD 0.22
#define BEAM_RADIUS 0.72
#define BEAM_WIDTH 1.2
#define BEAM_PERIOD 0.1667
#define RIPPLE_COUNT 30.0
#define RIPPLE_INTENSITY 0.06
#define RIPPLE_PERIOD 0.5
#define FILL_INTENSITY 0.3 // !!!
#define FILL_PERIOD 0.2
#define TAU 6.28318
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
vec3 color(vec2 uv) {
//get the color
float xCol = (uv.x - (u_time * ROTATION_PERIOD)) * 3.0;
xCol = mod(xCol, 3.0);
vec3 horColour = vec3(0.25, 0.25, 0.25);
if (xCol < 1.0) {
horColour.r += 1.0 - xCol;
horColour.g += xCol;
} else if (xCol < 2.0) {
xCol -= 1.0;
horColour.g += 1.0 - xCol;
horColour.b += xCol;
} else {
xCol -= 2.0;
horColour.b += 1.0 - xCol;
horColour.r += xCol;
}
return horColour;
}
vec3 color2 (vec2 st) {
float offset = 0.2;
float scale = 0.2;
return vec3(
offset + scale*cos( st.x - u_time),
offset + scale*sin( st.y - u_time),
offset + scale*cos( st.y - u_time) + scale*sin( st.x - u_time )
);
}
vec3 color21 (vec2 st) {
float offset = 0.5;
float scale = 0.5;
float nudge = 0.6;
return vec3(
offset + scale*cos( st.x - u_time),
offset + scale*sin( st.y - u_time * (1.0 + nudge)),
offset + scale*cos( st.y - u_time) + scale*sin( st.x - u_time*(0.9 - nudge) )
);
}
vec3 color3 (vec2 uv) {
float offset = 0.25;
float scale = 0.5;
return vec3(
offset + scale * cos(TAU * uv.x - u_time) * scale * sin(TAU * uv.y - u_time),
offset + scale * sin(TAU * uv.x - u_time) * scale * cos(TAU * uv.x - u_time),
offset + scale * cos(TAU * uv.x - u_time) * scale * sin(TAU * uv.x - u_time)
);
}
vec3 color4 (vec2 uv) {
float offset = 0.3;
float scale = 0.2;
return vec3(
offset + scale * sin(TAU * uv.x - u_time) * cos(TAU * uv.y - u_time),
offset + scale * cos(TAU * uv.x - u_time) * sin(TAU * uv.y - u_time),
offset + scale * cos(TAU - u_time)
);
}
vec3 color5 (vec2 uv) {
float offset = .6;
float scale = .4;
float period = 1.0;
float phase = period * TAU * uv.x - u_time / (2.0 * ROTATION_PERIOD);
return vec3(
offset + scale * sin(phase),
offset + scale * sin(phase + 1./3. * TAU),
offset + scale * sin(phase + 2./3. * TAU)
);
}
vec3 color6 (vec2 uv) {
float offset = .22;
float scale = .15;
float period = 1.0; // TODO: make them changeable!
float phase = period * TAU * uv.x - u_time / (4.0 * ROTATION_PERIOD);
return vec3(
offset + scale * sin(phase) * sin(u_time),
offset + scale * sin(1./3. * TAU + phase) * 2. * cos(1./2. - u_time),
offset + scale * sin(2./3. * TAU + phase) * 2. * cos(u_time)
);
}
float squarewave(float phase, float crossperiod) {
float crossfade = sin(phase * crossperiod);
crossfade += sin(phase * crossperiod * 3.0) / 3.0;
crossfade += sin(phase * crossperiod * 5.0) / 5.0;
crossfade += sin(phase * crossperiod * 7.0) / 7.0;
crossfade += sin(phase * crossperiod * 9.0) / 9.0;
crossfade += sin(phase * crossperiod * 11.0) / 11.0;
crossfade += sin(phase * crossperiod * 13.0) / 13.0;
crossfade += sin(phase * crossperiod * 15.0) / 15.0;
crossfade += sin(phase * crossperiod * 17.0) / 17.0;
crossfade += sin(phase * crossperiod * 19.0) / 19.0;
crossfade += 1.0;
crossfade /= 2.0;
return crossfade;
}
vec4 circle(vec2 p) {
float a = atan(p.x, p.y);
float r = length(p) - (BEAM_RADIUS / 2.0) + 0.1*pow(squarewave(u_time, 0.1), 2.0);
vec2 uv = vec2(a / TAU, r);
// draw color beam
float beambeam = 0.0;// 0.3 * cos(u_time+TAU*2.0*uv.x);
// float beam = BEAM_WIDTH * abs(1.0 / (50.0 * cos(u_time*BEAM_PERIOD) * uv.y));
float beam = BEAM_WIDTH * abs(1.0 / (50.0 * cos(beambeam + u_time*BEAM_PERIOD) * uv.y));
// ripples
float ripplecount = clamp(floor(5.0 + 1.5*RIPPLE_COUNT*cos(15.0 + u_time*RIPPLE_PERIOD)), 0.0, RIPPLE_COUNT);
beam += RIPPLE_INTENSITY * cos(uv.x*TAU*ripplecount - (1.5*u_time/ROTATION_PERIOD)) * abs(1.0 / (30.0 * uv.y));
// inside / outside color
beam += FILL_INTENSITY * (0.7+0.5*cos(uv.x*10.0*TAU*0.15*0.0)) * abs(1.0 / max(50.0 * cos(TAU/2.0 + u_time*FILL_PERIOD) * uv.y, 1.0));
// return vec4(beam * color(uv), 1.0); // first version, not linear
// return vec4(beam * color5(uv), 1.0); // rotating rainbow, linear!
// return vec4(beam * color6(uv), 1.0); // rotating and changing color subset!
// return vec4(beam * color3(uv), 1.0); // 2color rotating
// return vec4(beam * color4(uv), 1.0); // changing 3 color, weird scheme
// return vec2c4(beam * color2(p), 1.0); // spherical changing color
// return vec4(beam * color21(p), 1.0); // spherical changing more!
// getting magical: crossfading between colorschemes!
vec3 col = mix(color5(uv), color21(p), squarewave(u_time, 0.2));
return vec4(beam * col, 1.0);
}
void main() {
vec2 p = gl_FragCoord.xy;
p /= u_resolution;
p.y -= 0.75 * u_mouse.y / u_resolution.y;
// p.y -= 0.15 + 0.15 * (u_mouse.y / u_resolution.y - 1.9);
// p.y /= 0.5 * (u_mouse.y / u_resolution.y) + 0.5;
p -= 0.5;
p /= (u_resolution.y / u_resolution);
gl_FragColor = circle(p) * min(1.0, u_time * 0.2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment