【问题标题】:LibGDX pixmap not correctly updating pixelsLibGDX 像素图未正确更新像素
【发布时间】:2016-08-30 02:56:42
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: libgdx pixmap


    【解决方案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.draw,则没有理由使用 Sprite 对象。如果您使用batch.draw 绘制精灵,您还不如使用TextureRegion。

    【讨论】:

    • 我调用的是 fillCircle,而不是 drawCircle(第三个参数 (1) 是这个圆的半径)。此外,drawPixel() 调用不适用于任何参数(尝试 0(表示空)、255、1024 和 2048)。我使用精灵的原因与碰撞等有关,我的游戏中的所有对象都有一个精灵,所以在这里使用精灵比重写我的碰撞更容易。如果我的纹理已经使用我的像素图作为其源,我是否需要“绘制到纹理”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多