** Warning, Virtual Nomad pointed me on a problem on android and it seems my shader has some issues on other platforms, i'm not going to remove it but be aware if you're planning to use it, i think it's a bug and i'll try to solve it or address the issue if it's a AppGameKit thing **
My first post..
I created a shader for sprites that generates a dynamic shadow. Works with animations etc.
I had a tough search for documentation (still no real good found) and debugging shaders is very hard. Unless i miss something like a debuglog where you could output to but i found it most by trial and error.
But i think it works ok, it suits my need, i'm still not sure the shadow_offset is managed well, seems a bit off when changeing resolutions. Also some other parameters might nog be proof for your own resolution but the demo project might help you get it to use in your own project.
It creates a shadow based on the alpa of the pixels above the shadow line. Took me a while to come up with such a simple idea.
It requires the actual position within the sprite and therfor the shader values sprite_y and sprite_height must be set within AGK.
And if the sprite moves you will need to keep setting it and reapplying the shader to the Sprite (this vars are not dynamically applied to the shader when changed i think)
Also the baseline of the shadow must be set by setting shadow_offsety
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#endif
uniform vec2 agk_resolution;
uniform sampler2D texture0;
varying vec2 uvVarying;
uniform float sprite_y;
uniform float sprite_height;
uniform vec2 shadow_offsety;
vec2 agk_spritepos;
vec2 agk_spritesize;
void main()
{
float y = float(gl_FragCoord.y);
y = y - sprite_y; // offset
if( y > sprite_height * float(shadow_offsety.x) && y < sprite_height * float(shadow_offsety.y)){
vec4 thiscolor = texture2D(texture0, uvVarying.st);
if(thiscolor.a < 1.0){
vec2 scale = 2.0 /agk_resolution.xy;
thiscolor = vec4(0,0,0,0);
for (float i = sprite_height * 0.2; i < sprite_height; i++) {
vec2 offSet = vec2( 0.0, -1.0* i );
thiscolor += texture2D( texture0, uvVarying + offSet*scale ) * 0.015;
}
vec3 color = vec3(0,0,0);
gl_FragColor = vec4(color, thiscolor.a);
} else {
gl_FragColor = thiscolor;
}
} else {
gl_FragColor = texture2D(texture0, uvVarying.st);
}
}