LibGDX绘制点而不是线

时间:2020-01-25 14:57:50

标签: graphics libgdx

我想实现某种橡皮擦,所以在我的渲染方法中,使上层透明。

@Override
public void render () {
  cam.update();
  Gdx.gl.glClearColor(1, 1, 1,1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  if (Gdx.input.isTouched()) {
    pos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
    pixmap.setColor(new Color(1, 1, 1, 0.5f)); //transparency
    pixmap.fillCircle((int)pos.x, (int)pos.y, 10);
  }
  texture3.draw(pixmap, 0, 0);
  batch.begin();
    batch.draw(texture, 0, 0);
    batch.draw(texture3, 0, 0);
  batch.end();
}

但是我在刷卡时得到了分数。它需要非常慢的速度来制作线条而不是点。 所以我希望用连续的线代替点。 你能给点建议吗? Dots instead of line

1 个答案:

答案 0 :(得分:1)

这是由于输入状态的更新频率引起的,这里的解决方案是手动计算制作一条线所需的缺失点,您可以通过在每对点之间进行线性插值来实现,您可以根据最近的点与上一个点之间的距离来计算需要多少个额外的点,在我的示例中,我使用任意数量的额外点(20),如下所示:

public class TestDraw extends Game {

    private Pixmap pixmap;
    private Texture texture;
    private SpriteBatch batch;
    private Vector2 lastPos;

    @Override
    public void create() {
        pixmap = new Pixmap(1000, 1000, Pixmap.Format.RGBA8888);
        texture = new Texture(pixmap);
        batch = new SpriteBatch();
        lastPos = new Vector2();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        if (Gdx.input.isTouched()) {
            pixmap.setColor(new Color(1, 1, 1, 0.5f)); //transparency
            int newX = Gdx.input.getX();
            int newY = Gdx.input.getY();
            pixmap.setColor(Color.RED);
            pixmap.fillCircle(newX, newY, 10);

            // If the distance is too far, fill with extra dots
            if (lastPos.dst(newX, newY) > 10) {           // Here far is 10, you can adjust as needed
                int extraDots = 20;                       // How many extra dots to draw a line, I use 20, adjust as needed or calculate according to distance (for example lastPos.dst(newX,newY) * 5)
                for (int i = 0; i < extraDots; i++) {
                    float progress = (1f / extraDots) * i;
                    int dotX = (int) MathUtils.lerp(lastPos.x, newX, progress);
                    int dotY = (int) MathUtils.lerp(lastPos.y, newY, progress);
                    pixmap.setColor(Color.BLUE);
                    pixmap.fillCircle(dotX, dotY, 10);
                }
            }
            // Store last position for next render() call
            lastPos.set(newX, newY);
        }
        texture.draw(pixmap, 0, 0);
        batch.begin();
        batch.draw(texture, 0, 0);
        batch.end();
    }
}

根据需要屈服于您的代码,我不知道什么是texture3,因此我没有在示例中包含

由于渲染和存储成本,我不太喜欢的另一个选择是使用多边形绘制线条。