着色器绘制出意想不到的透明背景

Shaders draw unexpected transparent background

我有一个新的 class 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"); 

我的简单 shaderCodes 来自 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);
}

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

没有着色器的样本形状图像:

任何帮助请。

总体上实现了透明度 Alpha Blending。片段的alpha通道控制透明度,必须设置。

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

gl_FragColor = vec4(grayscale, 1.0);

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

#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);
}