Editing images in libGDX

Hello everyone!

I am currently trying to edit a Texture in libGDX. The ideea is simple: parse every byte in an “connection” image, if byte is white, set it as another(1), else if it is black, set is as another(2).

Here are some images of what i want:

So… What is the best method to do this? :persecutioncomplex:

Deprecated stencil buffer functionality aside, it’s as simple as multiplying the alpha in your brown texture by one of the color components in your mask (doesn’t matter which one if it’s black and white) and drawing it over the green texture.

which would lead to overdraw. In case you need that very often (for example some tiled surface), you would overdraw a bit too much… (and games really need to be optimized in the view of overdraw, because almost every game is fill-rate limited).

So what I’d do is write a little shader. Simply takes 3 textures: texture 1, texture 2, and the mask.

Wait… here is the kind of shader I thought about:


//                      R  G  B  A
const vec4 white = vec4(0, 0, 0, 1);

sampler2D texture1;
sampler2D texture2;
sampler2D mask;

void main(void) {
    // This is more pseudo-code than shader. It's some time ago the last time I wrote a shader...
    // Use this for getting the Idea, not for using this :) (first solve all errors :D )
    vec4 mask_color = texture2D(mask, gl_TexCoords);
    if (mask_color == white) {
        gl_FragColor = texture2D(texture1, gl_TexCoords);
    } else {
        gl_FragColor = texture2D(texture2, gl_TexCoords);
    }
}

You could also avoid repeated overdraw by just rendering the desired result to a texture. Should be easy enough with the glutils.FrameBuffer class, but I’ve never actually used it.

Couldn’t this be done with some blending mode?