Libgdx fbo背景透明度

时间:2017-06-21 22:08:08

标签: android libgdx framebuffer

我在libgdx中的FBO上绘制东西。比我只想将具有透明背景的fbo绘制到我的屏幕上。但总是黑色。

是否可以在spriteBatch上使用透明背景?

尝试了很多东西,但看起来我不能完成这个简单的任务。

我创建了一个自定义着色器并且:

vec4 orig = texture2D(u_texture, tc);
if (orig.a==0.0) discard;
FBO创建代码:

fbo = new FrameBuffer(Format.RGB565, width, height, hasDepth);

比:

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();
}

看起来着色器无法确定FBO的alpha值。为什么?我只需要清除alpha = 0.0像素。

所以在我的着色器中,背景被删除了:

if (orig.r==1.0) discard;

这是工作

if (orig.a==0.0) discard;

if (orig.a<0.2) discard;

if (orig.a<0.9) discard;

这不是

我的着色器看起来像这样

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;

}

2 个答案:

答案 0 :(得分:0)

RGB565格式没有Alpha通道。如果您需要透明度,请使用ARGB8888

等格式

答案 1 :(得分:0)

大声笑我是个疯子。

    vec4 all = v_color * vec4(sum.rgb, 1.0);
    if (all.a < 0.9) discard;
    else gl_FragColor = all;

这是BAD着色器(它的alpha总是1.0)

这是有效的:

vec4 all = sum;
if (all.a  < 0.5) discard;
else
    gl_FragColor = all;