Libgdx fbo背景透明度

Libgdx fbo background transparency

我在 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;

}

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

这样的格式

大声笑我是个白痴。

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

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

这是作品:

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