1

我正在尝试使用 Pixmap 在 LibGDX 中创建“可破坏”地形(2D)。但是,我设置的颜色没有正确显示在我的屏幕上。

完整源代码(颜色当前用于调试,发布时应为 alpha=0):

public class Terrain extends Actor implements Disposable {
private Sprite mySprite;
private Texture myTexture;
private Pixmap myMap;

public Terrain(Texture texture, Vector2 position)
{
    setPosition(position.x, position.y);
    if (!texture.getTextureData().isPrepared()) {
        texture.getTextureData().prepare();
    }
  // Final product will take original image from a texture to create pixmap
  //  myMap = texture.getTextureData().consumePixmap();
    myMap = new Pixmap(400, 400, Pixmap.Format.RGBA8888);
    myMap.setBlending(Pixmap.Blending.SourceOver);
    myMap.setColor(Color.BLUE);
    myMap.fillRectangle(0, 0, 400, 400);
    myTexture = new Texture(myMap, Pixmap.Format.RGBA8888, false);
    mySprite = new Sprite(myTexture);
}

public void removePixels(List<Vector2> pixels)
{
    myMap.setColor(Color.GREEN);
    myMap.fillCircle(1,1,1);
    myMap.setColor(Color.YELLOW);
    for (Vector2 pixel:pixels) {
        myMap.fillCircle((int)pixel.x, (int)pixel.y, 1); // Works, but is Black?
        myMap.drawPixel((int)pixel.x, (int)pixel.y, 255) // Does not work
    }
    myTexture = new Texture(myMap, Pixmap.Format.RGBA8888, false);
    mySprite = new Sprite(myTexture);

    System.out.println("Pixels Removed");
}

@Override
public void draw(Batch batch, float parentAlpha) {
    batch.draw(mySprite, getX(), getY(), mySprite.getWidth(), mySprite.getHeight());
 //   batch.draw(myMap, getX(), getY(), myMap.getWidth(), myMap.getHeight());
}

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

由于某种原因,在 fillCircle 中绘制的形状始终为黑色,而使用 drawPixel 绘制的像素似乎甚至没有更新(保持原始颜色)。如何在 LibGDX 中设置像素(alpha 为 0)?(最好不使用 glsl)

4

1 回答 1

1

当您调用 时drawCircle(x, y, 1),您传入的是 RGBA8888 颜色 1,我认为这相当于 R=0、G=0、B=0、A=1/255。当您调用时.drawPixel((int)pixel.x, (int)pixel.y, 255),您传入的是 RGBA8888 颜色 255,我认为它是 R=0、G=0、B=0、A=1。在这两种情况下,调用都会忽略您在像素图上设置的最后一种颜色。使用不带颜色参数的这些方法的版本。

我看到了其他一些错误。myTexture = new Texture(...)如果您不处理旧纹理,则每次调用时都会泄漏纹理。但实际上,您应该只将更新的像素图绘制到您的纹理上,这可能会更快。 myTexture.draw(myMap, 0, 0);此外,如果您不使用 Sprite 对象,则没有理由使用sprite.draw. 如果您使用 绘制精灵batch.draw,您还不如使用 TextureRegion。

于 2016-05-04T16:29:23.443 回答