简体   繁体   中英

How to paint over texture [Libgdx]

I have original image: http://i.stack.imgur.com/7qYW2.png

How I can to paint over this image, than to get the following: http://i.stack.imgur.com/w3ij3.png

PS: this code not valid:

public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batcher.begin();
    batcher.setColor(Color.BLUE);
    batcher.draw(texture, x, y, width, height);
    batcher.end();
}

You can use a fragment shader with the following logic:

if(alpha of the pixel is 1.0) then pixel=cyan
else pixel=white

this of course requires the space that surrounds the penguin to be transparent(have a=0)

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

void main() {
    vec4 color=v_color * texture2D(u_texture, v_texCoords);
    if(color.a==1.0)gl_FragColor=vec4(0.5,0.5,1,1);
    else gl_FragColor=vec(0.0,0.0,0.0,0.0);
}

Full valid code:

        String vertexShader = "attribute vec4 a_position;\n" +
                "attribute vec4 a_color;\n" +
                "attribute vec2 a_texCoord0;\n" +
                "\n" +
                "uniform mat4 u_projTrans;\n" +
                "\n" +
                "varying vec4 v_color;\n" +
                "varying vec2 v_texCoords;\n" +
                "\n" +
                "void main() {\n" +
                "    v_color = a_color;\n" +
                "    v_texCoords = a_texCoord0;\n" +
                "    gl_Position = u_projTrans * a_position;\n" +
                "}";

        String fragmentShader = "#ifdef GL_ES\n" +
                "precision mediump float;\n" +
                "#endif\n" +
                "\n" +
                "varying vec4 v_color;\n" +
                "varying vec2 v_texCoords;\n" +
                "uniform sampler2D u_texture;\n" +
                "uniform mat4 u_projTrans;\n" +
                "\n" +
                "void main() {\n" +
                "       vec4 color = v_color * texture2D( u_texture, v_texCoords );\n" +
                "       if( color.a == 0.0 ) gl_FragColor = vec4( color.r, color.g, color.b, 0.0 );\n" +
                "       else gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );" +
                "}";

        ShaderProgram shaderProgram = new ShaderProgram(vertexShader,fragmentShader);

        public void render() {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

            batch.begin();
            batch.setShader(shaderProgram);
            batch.draw(texture, x, y, width, height);
            batch.end();

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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