【问题标题】:Draw a smooth line画一条平滑的线
【发布时间】:2016-03-22 05:48:59
【问题描述】:

我正在使用 libgdx 开发游戏,我想使用形状渲染器绘制一条平滑线。

        shaperenderer.begin(ShapeType.Line);
        shaperenderer.line(fisrstVec2,secondVec2);
        shaperenderer.end();

我已经尝试过来自 libgdx 博客的 Multi Sample anti aliasing。 我也经历过Anti aliased filed shape in libgdx 但不幸的是,这些行不在 libgdx 的最新版本中。

   Gdx.gl.glEnable(GL10.GL_LINE_SMOOTH);
   Gdx.gl.glEnable(GL10.GL_POINT_SMOOTH); 

【问题讨论】:

  • 你是在安卓设备还是桌面上运行这个?
  • 我在安卓设备上运行这个。
  • GL_LINE_SMOOTH 已过时(来自 OpenGL ES 1.0,LibGDX 不再支持)。您应该通过在从启动器类传递到游戏的 ApplicationConfiguration 中设置 numSamples 来启用多重采样。如果这不起作用,也许您正在测试不支持它的 GPU。也可以通过绘制长而细的矩形精灵来获得平滑的线条而无需抗锯齿,这些精灵在侧面有一些空白填充。
  • @Tenfour04 这可能是一个很好的答案,而不仅仅是评论

标签: android libgdx


【解决方案1】:

在配置中启用抗锯齿:

对于桌面:

    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    config.samples = 2;
    new LwjglApplication(new MyGdxGame(Helper.arrayList(arg)), config);

对于安卓:

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    config.numSamples = 2;
    initialize(new MyGdxGame(null), config);

或者您可以创建一个 1x1 的白色像素图并使用它来创建一个精灵并使用该精灵绘制线条,我个人更喜欢这种方法而不是 ShapeRenderer(请注意,此方法中没有旋转):

/**
 * draws a line on the given axis between given points
 *
 * @param batch       spriteBatch
 * @param axis        axis of the line, vertical or horizontal
 * @param x          x position of the start of the line
 * @param y          y position of the start of the line
 * @param widthHeight width or height of the line according to the axis
 * @param thickness thickness of the line
 * @param color       color of the line, if the color is null, color will not be changed.
 */
public static void line(SpriteBatch batch, Axis axis, float x, float y, float widthHeight, float thickness, Color color, float alpha) {
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    if (axis == Axis.vertical) {
        sprite.setSize(thickness, widthHeight);
    } else if (axis == Axis.horizontal) {
        sprite.setSize(widthHeight, 1);
    }
    sprite.setPosition(x,y);
    sprite.draw(batch);
    sprite.setAlpha(1);
}

对之前的方法做一些修改,可以想出这个方法来画一条带旋转的线。

public static void rotationLine(SpriteBatch batch, float x1, float y1, float x2, float y2, float thickness, Color color, float alpha) {
    // set color and alpha
    if (color != null) sprite.setColor(color);
    sprite.setAlpha(alpha);
    // set origin and rotation
    sprite.setOrigin(0,0);
    sprite.setRotation(getDegree(x2,y2, x1, y1));
    // set position and dimension
    sprite.setSize(distance(x1,y1,x2,y2),thickness);
    sprite.setPosition(x1, y1);
    // draw
    sprite.draw(batch);
    // reset rotation
    sprite.rotate(0);
}

public static float getDegree(float x, float y, float originX, float originY) {
    float angle = (float) Math.toDegrees(Math.atan2(y - originY, x - originX));
    while (angle < 0 || angle > 360)
        if (angle < 0) angle += 360;
        else if (angle > 360) angle -= 360;
    return angle;
}

public static float distance(float x, float y, float x2, float y2) {
    return (float) Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));
}

【讨论】:

  • 您的旋转方法仅在您启用多重采样时才有效。
  • @GuilhermeCamposHazan 默认不启用多重采样吗?
  • 不,它是可选的。默认值为 samples=0。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多