I draw things on an FBO in libgdx. Than I just want to draw that fbo WITH TRANSPARENT background to my screen. But that is allways BLACK.
Is it possible to use a transparent background on the spriteBatch?
Tried a lot of things, but it looks like I cannot dop this simple task.
I created a custom shader and:
vec4 orig = texture2D(u_texture, tc);
if (orig.a==0.0) discard;
ths FBO create code:
fbo = new FrameBuffer(Format.RGB565, width, height, hasDepth);
than:
public void begin() {
fbo.begin();
Gdx.gl.glClearColor(1.0f, 0.0f, 0.0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
}
public void render() {
renderIt();
}
private void renderIt() {
spriteBatch.begin();
spriteBatch.setShader(shader);
spriteBatch.draw(fboRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.end();
}
public void end() {
Gdx.gl.glDisable(GLES20.GL_BLEND);
fbo.end();
}
It looks like the shader cannot determinate the FBO's alpha values. why? I just need to clear the alpha=0.0 pixels out.
So in my shader the background cut out:
if (orig.r==1.0) discard;
this is WORKING
if (orig.a==0.0) discard;
if (orig.a<0.2) discard;
if (orig.a<0.9) discard;
THIS IS NOT
my shader looks like this
void main() {
vec4 sum = vec4(0.0);
vec2 tc = v_texCoord0;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
vec4 all = v_color * vec4(sum.rgb, 1.0);
if (all.a < 0.9) discard;
else gl_FragColor = all;
}
LOL I was a fckin idiote.
vec4 all = v_color * vec4(sum.rgb, 1.0);
if (all.a < 0.9) discard;
else gl_FragColor = all;
this is the BAD shader (its alpha is allways 1.0)
this is the worked one:
vec4 all = sum;
if (all.a < 0.5) discard;
else
gl_FragColor = all;