LibGdx 中动态彩色图形的像素图或纹理?

Pixmaps or Textures for dynamicalliy colored graphics in LibGdx?

我试图创建一个包含精灵的动态数组,它是从像素图纹理创建的。我也想给这个数组元素随机不同的颜色。 None 正在工作。

然后我尝试创建一个像素图。这也显示出相同的行为。

我在 show() 中创建了一个像这样的像素图:

pixmap = new Pixmap(128, 128, Format.RGBA8888);
Pixmap.setBlending(Pixmap.Blending.None);
pixmap.setColor(128, 0, 0, 1f);
pixmap.fillCircle(64, 64, 64);
texture = new Texture(pixmap);
pixmap.dispose();

render()

sprite = new Sprite(texture);
sprite.setPosition(b.getX()-sprite.getWidth()/2, b.getY()-sprite.getHeight()/2);
sprite.draw(batch);

每当我给出 rgb 颜色代码时,它都会给出一些不同的颜色或黑色作为输出。也尝试了十六进制代码。

我这里做错了什么?

以前我使用像素图作为覆盖和单一纹理等。但没有深入尝试。

在这里,我打算用pixmap画实心圆圈,而不是用图形。因为我的游戏元素是非常简单的实心圆圈和我应该实现的 10 多种颜色。

这些圆圈对象将在整个游戏过程中动态生成。

现在我想知道我计划做的事情对像素图是否有效。我在网上找不到例子。

是否可以用不同颜色的对象创建动态数组?

或者与像素图相比,使用图形是更好的选择?

如果能得到有经验的人的建议会很有帮助。

setColor()接受rgba参数将其转换为rgba8888格式-

public void setColor (float r, float g, float b, float a) {
    color = Color.rgba8888(r, g, b, a);
}

因此将 rgb 和 alpha 分量设置为 [0,1].

范围内的浮点数

使用

pixmap.setColor(128f/255, 0, 0, 1f);

而不是

pixmap.setColor(128, 0, 0, 1f);

如果你想使用十六进制那么-

Color color = new Color(0x000000a6) //(Black with 65% alpha).

r,g,b,a 值应在 0f 到 1f 的范围内。在 render() 中创建新精灵是不好的,因为它被调用每一帧。

我将尝试用这个小代码示例(在代码中留下注释)来回答您的问题:

public class MyGdxGame extends ApplicationAdapter {

    SpriteBatch spriteBatch;

    Pixmap pixmap;
    Texture texture;

    Array<Sprite> sprites = new Array<Sprite>();

    @Override
    public void create() {
        spriteBatch = new SpriteBatch();

        // you should use only one Pixmap object and one Texture object
        pixmap = new Pixmap(128, 128, Pixmap.Format.RGBA8888);
        pixmap.setBlending(Pixmap.Blending.None);
        pixmap.setColor(Color.WHITE);
        pixmap.fillCircle(64, 64, 64);
        texture = new Texture(pixmap);
        pixmap.dispose();

        // generate sprites with different colors
        // r,g,b,a values should be in range from 0f to 1f
        addCircleSprite(128f / 255f, 0f, 0f, 1f);
        addCircleSprite(0.4f, 0.2f, 0.5f, 1f);
        addCircleSprite(0.6f, 0f, 1f, 1f);
        addCircleSprite(0.3f, 0.8f, 1f, 1f);
        addCircleSprite(0.1f, 1f, 1f, 1f);
    }

    void addCircleSprite(float r, float g, float b, float a) {
        // every sprite references on the same Texture object
        Sprite sprite = new Sprite(texture);
        sprite.setColor(r, g, b, a);
        // I just set random positions, but you should handle them differently of course
        sprite.setCenter(
            MathUtils.random(Gdx.graphics.getWidth()),
            MathUtils.random(Gdx.graphics.getHeight()));
        sprites.add(sprite);
    }

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

        spriteBatch.begin();
        for (Sprite sprite : sprites) {
            sprite.draw(spriteBatch);
        }
        spriteBatch.end();
    }

    @Override
    public void dispose() {
        texture.dispose();
    }
}

也读了这个 Q/A。我猜你可以从带有白色圆圈的 .png 文件加载 Texture 对象:

texture = new Texture("whiteCircle.png");

但是创建 Pixmap 半径只有 64 像素的圆(然后从中创建 Texture)也可以,不会有太大区别。