2017-06-21 187 views
0

我在libgdx的FBO上畫東西。比我只想繪製這個fbo WITH TRANSPARENT背景到我的屏幕上。但那是總之黑。Libgdx 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; 

THIS IS NOT

我着色器看起來像這樣

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; 

} 
+0

也許只是確保阿爾法真的是0通過測試'原稿.a <0.1'什麼的。出於某種原因,它可能非常接近0。 – Charanor

+0

vec4 orig = texture2D(u_texture,tc);如果(orig.a <0.2)丟棄; 嘗試過,但也沒有工作......背景只是紅色。它看起來像不能看到GL的clearcolor的alpha是什麼設置爲0.0 – lacas

回答

0

LOL我是個傻瓜傻瓜。

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; 
0

RGB565格式不h大道上一個阿爾法通道。如果您需要透明度,請使用如下格式:ARGB8888

+0

更改爲rgba8888和相同。任何惡棍? – lacas

+0

使用glClearColor(0f,0f,0f,0f);清除FBO,以便FBO透明 – dfour