Skip to content

Instantly share code, notes, and snippets.

@akshayloke
Created September 23, 2016 21:35
Show Gist options
  • Save akshayloke/9af1feb944688abde44e392b35daab42 to your computer and use it in GitHub Desktop.
Save akshayloke/9af1feb944688abde44e392b35daab42 to your computer and use it in GitHub Desktop.
Cleaner implementation of the Spiral fragment shader
// Author: Akshay S. Loke
// Title: Spiral
#ifdef GL_ES
precision mediump float;
#endif
#define USE_SMOOTH
#define M_PI 3.14159265358979
#define M_2PI 6.28318530717958
#define RADIUS 0.2
#define NUMCURLS 3
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
void main() {
vec2 center = u_resolution * 0.5;
vec2 normCenter = vec2(0.0);
vec2 uv = gl_FragCoord.xy;
vec2 normUV = gl_FragCoord.xy / u_resolution.xy;
normUV = normUV * 2.0 - vec2(1.0);
float dist = length(normUV - normCenter);
float theta = atan(normUV.y, normUV.x) + M_PI;
float normTheta = theta / M_2PI;
/*float val0 = step(0.0, dist) - step(normTheta * RADIUS, dist);
float val1 = step(normTheta * RADIUS, dist) - step((normTheta + 1.0) * RADIUS, dist);
float val = (val0 * normTheta + val1 * (normTheta + 1.0)) * 0.5;
float smoothVal0 = smoothstep(0.0, 0.0, dist) - smoothstep(normTheta * RADIUS - 0.01, normTheta * RADIUS, dist);
float smoothVal1 = smoothstep(normTheta * RADIUS - 0.01, normTheta * RADIUS, dist) - smoothstep((normTheta + 1.0) * RADIUS - 0.01, (normTheta + 1.0) * RADIUS, dist);
float smoothVal = (smoothVal0 * normTheta + smoothVal1 * (normTheta + 1.0)) * 0.5;
*/
float val = 0.0;
float numCurlsFloat = float(NUMCURLS);
for (int i=-1; i<NUMCURLS-1; i++) {
float index = float(i);
float normThetaPre = (normTheta + index);
float ringPre = normThetaPre * RADIUS;
if (i == -1) {
ringPre = 0.0;
}
float normThetaPost = (normTheta + index + 1.0);
float ringPost = normThetaPost * RADIUS;
#ifdef USE_SMOOTH
float ringVal = smoothstep(ringPre-0.01, ringPre, dist) - smoothstep(ringPost-0.01, ringPost, dist);
#else
float ringVal = step(ringPre, dist) - step(ringPost, dist);
#endif
val += (ringVal * normThetaPost);
}
val /= numCurlsFloat;
gl_FragColor = vec4(vec3(val), 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment