I have a Texture loaded via the AssetManager:
AssetManager manager = new AssetManager(...);
manager.load(textureFilepath, Texture.class);
manager.finishLoading();
Texture texture = manager.get(textureFilepath, Texture.class);
and I want to modify some of the pixel colors at runtime. How can I do that?
Thanks
--- Update ---------
Here's a test app, attempting to alter the pixmap doesn't change the appearance of the texture though:
public class AppTest extends ApplicationAdapter {
SpriteBatch batch;
Texture texture;
public void create() {
batch = new SpriteBatch();
texture = new Texture("foo.jpg");
}
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, 100, 100);
batch.end();
}
private void test() {
TextureData textureData = texture.getTextureData();
textureData.prepare();
Pixmap pixmap = textureData.consumePixmap();
for (int y = 0; y < 200; y++) {
for (int x = 0; x < 200; x++) {
pixmap.drawPixel(x, y, 0xff0000ff);
}
}
}
public boolean touchUp (int screenX, int screenY, int pointer, int button) {
test();
}
}
Short:
pixmap.drawPixel(x, y, color);
So first you need to create a Pixmap from the texture data:
TextureData textureData = texture.getTextureData();
textureData.prepare();
Pixmap pixmap = textureData.consumePixmap();
If you want to change red into green you need a loop and get the rgb888 value since getPixel
return that for you:
for (int y = 0; y < pixmap.getHeight(); y++) {
for (int x = 0; x < pixmap.getWidth(); x++) {
if (pixmap.getPixel(x, y) == Color.rgb888(Color.Red))
{
pixmap.drawPixel(x, y, Color.Green);
}
}
}
If you already know what to change there is no need to loop and just use pixmap.drawPixel()
.
When you are done with your Pixmap
magic you can create a new Texture for it.
Texture pixmapTexture = new Texture(pixmap);