gpt4 book ai didi

java - 如何修改纹理中像素的颜色?

转载 作者:行者123 更新时间:2023-12-01 09:20:16 24 4
gpt4 key购买 nike

我有一个通过 AssetManager 加载的纹理:

AssetManager manager = new AssetManager(...);
manager.load(textureFilepath, Texture.class);
manager.finishLoading();
Texture texture = manager.get(textureFilepath, Texture.class);

我想在运行时修改一些像素颜色。我怎样才能做到这一点?

谢谢

--- 更新 ---------

这是一个测试应用程序,尝试更改像素图不会改变纹理的外观:

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

最佳答案

简短:

pixmap.drawPixel(x, y, color);

所以首先您需要从纹理数据创建一个像素图:

TextureData textureData = texture.getTextureData();
textureData.prepare();
Pixmap pixmap = textureData.consumePixmap();

如果您想将红色变为绿色,您需要一个循环并获取 rgb888 值,因为 getPixel 会为您返回该值:

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

如果您已经知道要更改什么,则无需循环,只需使用 pixmap.drawPixel()

完成Pixmap魔法后,您可以为其创建一个新的纹理。

Texture pixmapTexture = new Texture(pixmap);

关于java - 如何修改纹理中像素的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210765/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com