着色器绘制意外的透明背景

时间:2019-07-17 11:03:14

标签: libgdx glsl shader

我有一个新的类MyActor扩展的Actor,在其中应用了着色器。

但是,着色器会意外填充透明背景。

draw()中的

MyActor方法代码如下:

@Override
    public void draw(Batch batch, float parentAlpha) {
        if(shaderProgram!=null)
        {
            batch.setShader(shaderProgram);           
        }

        if(!drawParentAtBack)super.draw(batch, parentAlpha); // by default is false
        Color c = getColor(); // used to apply tint color effect
        batch.setColor(c.r, c.g, c.b, c.a * parentAlpha);
        if ( isVisible() )
        {            
                if(displayFrame !=null) // a textureregion
                {
                    batch.draw(displayFrame,
                            getX(),getY(),
                            getOriginX(),getOriginY(),
                            getWidth(),getHeight(),
                            getScaleX(),getScaleY(),
                            getRotation());
                }           
        }
        if(drawParentAtBack)super.draw(batch, parentAlpha);

        if(shaderProgram!=null)
        {
            batch.setShader(null);
        }
}


public void setShader(String vs, String fs){
        vertexShaderCode   =    Gdx.files.internal("shaders/" + vs + ".vs").readString();
        fragmentShaderCode = Gdx.files.internal("shaders/" + fs + ".fs").readString();
        shaderProgram = new ShaderProgram(vertexShaderCode, fragmentShaderCode);
        if (!shaderProgram.isCompiled())
        {
            d( "Shader compile error: " + shaderProgram.getLog() );
        }
    }

我的定义是这样的:

     MyActor myActor1;myActor2;
     ..... // setting up myActor1 & myActor2
     myActor1.setShader("default","greyScale");
     myActor2.setShader("default","greyScale"); 

我简单的this tutorial中的着色器代码:

#ifdef GL_ES
    precision mediump float;
#endif

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;

void main() {
        vec3 color = texture2D(u_texture, v_texCoords).rgb;
        float gray = (color.r + color.g + color.b) / 3.0;
        vec3 grayscale = vec3(gray);

        gl_FragColor = vec4(grayscale, 1.0);
}

我来自this tutorial的顶点着色器代码:

#ifdef GL_ES
    precision mediump float;
#endif

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;

void main() {
        vec3 color = texture2D(u_texture, v_texCoords).rgb;
        float gray = (color.r + color.g + color.b) / 3.0;
        vec3 grayscale = vec3(gray);

        gl_FragColor = vec4(grayscale, 1.0);
}

我的预期结果图像是具有透明背景的灰色形状,但结果是这样的:

error image

不带着色器的形状样本图像:

sample shape assets without any shader

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

一般来说,透明度是Alpha Blending。片段的Alpha通道控制透明度,必须进行设置。

在片段着色器中,省略了纹理的Alpha通道:

  
gl_FragColor = vec4(grayscale, 1.0);

将纹理(u_texture)的alpha通道设置为输出(gl_FragColor.a)的alpha通道:

#ifdef GL_ES
    precision mediump float;
#endif

varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform mat4 u_projTrans;

void main() {

        // read RGB color channels and alpha channel
        vec4 color = texture2D(u_texture, v_texCoords);

        float gray = (color.r + color.g + color.b) / 3.0;
        vec3 grayscale = vec3(gray);

        // write gray scale and alpha channel
        gl_FragColor = vec4(grayscale.rgb, color.a);
}