简体   繁体   English

libgdx - 着色器忽略第一帧中的混合/alpha

[英]libgdx - shader ignores blending/alpha in first frame

I'm rendering a libgdx mesh using shader with alpha color less than 1.0.我正在使用 alpha 颜色小于 1.0 的着色器渲染 libgdx 网格。

When rendering the first frame, the alpha value set in shader is being ignored and rendered as 1.0.渲染第一帧时,着色器中设置的 alpha 值将被忽略并渲染为 1.0。 All following rendering frames are fine.以下所有渲染帧都很好。

The same happened to me in previous project drawing lines and shapes using glDrawArrays and I haven't found the solution yet.在以前的项目中使用 glDrawArrays 绘制线条和形状时,我也遇到了同样的情况,但我还没有找到解决方案。

Code in libgdx render() loop: libgdx render() 循环中的代码:

    Gdx.gl20.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    Gdx.gl20.glEnable(GL20.GL_BLEND);
    MeshManager_colorFill.meshShader.begin();
    MeshManager_colorFill.meshShader.setUniformMatrix("u_worldView", Snappr.camMatrix);

    meshArray_colorFill.render(MeshManager_colorFill.meshShader, GL20.GL_TRIANGLE_FAN, 0, meshArray_colorFill.get(i).getNumVertices()); 

    MeshManager_colorFill.meshShader.end();
    Gdx.gl20.glDisable(GL20.GL_BLEND);

My shader (compiled in create(){}):我的着色器(在 create(){} 中编译):

  public static final String meshVertexShader_colorFill =

                    "attribute vec2 a_position;    \n" + 
                    "uniform mat4 u_worldView;\n" + 
                    "void main()                  \n" + 
                    "{                            \n" + 
                    "   gl_Position =  u_worldView * vec4(a_position.xy, 0, 1);  \n"      + 
                    "}                            \n" ;

  public static final String meshFragmentShader_colorFill =

                    "precision mediump float;\n" +  
                    "void main()                                  \n" + 
                    "{                                            \n" + 
                    "  gl_FragColor = vec4(1,1,1,0.2);\n" +
                    "}";

How do I render the very first frame as it should be?如何按原样渲染第一帧?

Thanks谢谢

glBlendFunc in create() does the trick, specifically: "Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, L20.GL_ONE_MINUS_SRC_ALPHA);" create() 中的 glBlendFunc 可以解决问题,特别是:“Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, L20.GL_ONE_MINUS_SRC_ALPHA);” I'm using SpriteBatch to render a font, right after the mesh.我正在使用 SpriteBatch 在网格之后渲染字体。 glBlendFunc is contained internally within the SpriteBatch, looks like that's why all the other frames were fine. glBlendFunc 包含在 SpriteBatch 内部,看起来这就是所有其他帧都很好的原因。

I found that ModelBatch.begin(...) will disable Blending by calling Gdx.gl.glDisable(GL20.GL_BLEND).我发现 ModelBatch.begin(...) 将通过调用 Gdx.gl.glDisable(GL20.GL_BLEND) 禁用混合。 So make sure to enable blending AFTER calling ModelBatch.begin().因此,请确保在调用 ModelBatch.begin() 后启用混合。

modelBatch.begin(camera); // resets Blending to false
// enable Blending
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
// draw mesh
mesh.render(shader, GL20.GL_TRIANGLES);
modelBatch.end();

Source https://stackoverflow.com/a/66820414/2413469来源https://stackoverflow.com/a/66820414/2413469

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM