1

Have an Android application running Libgdx with many sprites all floating around. Currently I colour them all differently depending on which group they belong to, eg "red", "green", "blue" etc. However, I think I'd prefer to only colour part of the sprite instead of the whole thing.

Currently I use:

batch.begin();
batch.setColor(color1);
batch.draw(group1);
batch.setColor(color2);
batch.draw(group2);
etc..

batch.end();

batch.setColor(color) used to colour the batch and apply that to the whole sprite. When that group is done, I change the colour and move to the next group. I reset the batch colour when done with the groups.

Is there a shader/blender way of only colouring part of the sprite instead? For example, anything that's coloured red in my texture should be treated as a "dynamic area" that can then be coloured to whatever I dynamically set.

I can't use transparency as the edges of the textures are transparent and need to remain that way for blending.

Many thanks

Jammo
  • 1,838
  • 4
  • 25
  • 39

1 Answers1

0

You can achieve this using second TextureRegion in your Sprite. Thus you can use other color for this additional region. This other region should only contain part of the image which would change colors and transparent background.

public class MySprite extends Sprite {
    TextureRegion region1, region2;
    Color extraColor;

    ...

    @Override
    public void draw(Batch batch, float parentAlpha) {
        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(region1, getX(), getY(), getOriginX(), getOriginY(),
                getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());

        batch.setColor(extraColor);
        batch.draw(region2, getX(), getY(), getOriginX(), getOriginY(),
                getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());
    }
}
Rara
  • 639
  • 9
  • 22
  • Hi Rara, thanks for getting back to me. Bit confused by your method. Currently the textureregion is the returned section of the atlas for the sprite I want to display, let's say the character. How would I go about colouring only a portion of this returned sprite? Are you creating different texture regions for each part of that character? – Jammo Mar 31 '15 at 09:06
  • Yes. You should add another texture to your atlas at which visible only the part of your character for which your want to change color. Then create `MySprite` where `region1` is character texture and `region2` is this second texture. Don't forget to set `extraColor` value before rendering. – Rara Mar 31 '15 at 16:20
  • That method seems like quite a lot of overhead though? I have a sprite which has been rotated in 8 directions, and each direction is animated with 4 frames. So I would need a texture region image in the atlas for each of those? Just seems like a lot of admin nightmare, especially if the main character graphic is updated. The sprites used have transparencies - is there no way to target a particular colour of the sprite, such as, Magenta, and only dynamically colour that part ? Like how a Green/Blue Screen works with tv/films – Jammo Apr 02 '15 at 10:52
  • Well, you should include such details in question :) In that case I can recommend you to look at `Pixmap` class: http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/Pixmap.html . You can achieve what you want using `getColor()` and `drawPixel()` methods. I'm not sure that this is optimal solution but it should work. – Rara Apr 03 '15 at 13:12