Libgdx fbo 背景透明度



我在libgdx的FBO上画东西。而不是我只想在我的屏幕上绘制带有透明背景的 fbo。但那是黑色的。

是否可以在精灵批处理上使用透明背景?

尝试了很多事情,但看起来我无法完成这项简单的任务。

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

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;

最新更新