Skip to content

Instantly share code, notes, and snippets.

@caiwan
Last active August 29, 2015 14:20
Show Gist options
  • Save caiwan/b4139f08330a0e4588c1 to your computer and use it in GitHub Desktop.
Save caiwan/b4139f08330a0e4588c1 to your computer and use it in GitHub Desktop.
Farbrausch-like loaderbar in a single shader
namespace {
//works!
///@return 0 if everything succeeds 1 otherwise (or throw an exception)
int initLoader(){
this->loader.s_loader= new FWrender::Shader();
this->loader.s_loader->createFromMemory( /* create(&vss, &fss) */
"#version 330 core\n"
"layout(location = 0) in vec3 "SHADER_ATTRIB_VERTEX";"
"void main(){"
"gl_Position = vec4("SHADER_ATTRIB_VERTEX",1.);"
"}",
#if 0 /*old loaderbar*/
"#version 330 core\n"
"uniform float transition;\n"
"uniform vec2 "SHADER_UNIFORM_RESOLUTION";\n"
"void main(void)\n"
"{\n"
"vec2 uv = gl_FragCoord.xy / "SHADER_UNIFORM_RESOLUTION".xy; uv -=.5;\n"
//"vec3 color = vec3(1.,0.,0.);"
"vec3 color;\n"
"if (abs(uv.y) < 0.05 && (uv.x+.5)<transition) color = vec3(1.);\n"
"gl_FragColor = vec4(color,1.0);\n"
"}\n"
#else /*new loaderbar*/
"#version 330 core\n"
"uniform float transition;\n"
"uniform vec2 "SHADER_UNIFORM_RESOLUTION";\n"
"#define LIM .001\n"
"#define THICK .1\n"
"#define BORDER .01\n"
"float udBox(vec2 p, vec2 b){return length(max(abs(p)-b,0.0));} \n"
"vec4 getLoader(vec2 uv){\n"
"vec4 color; \n"
"float f = udBox(uv + vec2(1.-transition, 0.) , vec2(transition - BORDER*2., THICK - BORDER*3.));\n"
"float f0 = udBox(uv , vec2(1., THICK + BORDER)); \n"
"float f1 = udBox(uv , vec2(1. - BORDER, THICK - BORDER));\n"
"if (f0<LIM && f1>LIM) color = vec4(vec3(0.),1.);\n"
"if (f<LIM) color = vec4(vec3(0.),1.);\n"
"return color;\n"
"}\n"
"void main()\n"
"{\n"
"vec2 uv = gl_FragCoord.xy / "SHADER_UNIFORM_RESOLUTION".xy;\n"
"gl_FragColor = vec4(uv,0.5+0.5*sin(transition),1.0);\n" //< textura goez here
"uv -=.5; uv *=2.;\n" // < here you can transform it as your flavour. Note that 0.0 is the center, and the space is [-1..1]
"vec4 color = getLoader(uv*1.1);\n"
"gl_FragColor = vec4(mix(gl_FragColor.rgb, color.rgb, color.a),1.); \n"
"}\n"
#endif
);
return 0;
}
}
// sightly optimized version
#version 330 core
uniform float transition;
uniform vec2 resolution;
#define LIM .001
#define THICK .1
#define BORDER .01
const vec4 barColor = vec4(0.,0.,0.,1.);
const vec4 borderColor = vec4(0.,0.,0.,1.);
float udBox(vec2 p, vec2 b){return length(max(abs(p)-b,0.0));}
vec2 sincos( float x ){return vec2(sin(x), cos(x));}
vec2 rotate2d(vec2 uv, float phi){vec2 t = sincos(phi); return vec2(uv.x*t.y-uv.y*t.x, uv.x*t.x+uv.y*t.y);}
vec4 getLoader(vec2 uv){
if (udBox(uv , vec2(1., THICK + BORDER))<LIM && udBox(uv , vec2(1. - BORDER, THICK - BORDER))>LIM) return borderColor;
if (udBox(uv + vec2(1.-transition, 0.) , vec2(transition - BORDER*2., THICK - BORDER*3.))<LIM) return barColor;
return vec4(0.);
}
void main()
{
vec2 uv = gl_FragCoord.xy / resolution.xy;
gl_FragColor = vec4(uv,0.5+0.5*sin(transition),1.0);
uv -=.5; uv *=2.;
vec4 color = getLoader(uv * 1.1);
gl_FragColor = vec4(mix(gl_FragColor.rgb, color.rgb, color.a),1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment