简体   繁体   English

使用 libGDX 以编程方式绘制漂亮的线条

[英]Draw beautiful line programmatically using libGDX

In my libGDX project I have to draw such lines:在我的 libGDX 项目中,我必须绘制这样的线: 在此处输入图片说明

It may have various number of vertices.它可能有不同数量的顶点。 Currently I'm just adding it to stage as a texture with transparent background.目前我只是将它作为具有透明背景的纹理添加到舞台上。 And if I have a lot of such lines, it takes quite a lot of space in atlases.如果我有很多这样的行,它在地图集中需要相当多的空间。 So I want to draw these polygonal chains programmatically using libGDX.所以我想使用 libGDX 以编程方式绘制这些多边形链。 I don't have much time and will to write my own libraries to achieve this, so I'm interested is some existing solutions.我没有太多时间也不会编写自己的库来实现这一点,所以我对一些现有的解决方案感兴趣。 Two questions:两个问题:

  1. I've found that a line could be drawn using ShapeRenderer.rectLine().我发现可以使用 ShapeRenderer.rectLine() 绘制一条线。 Now how can add some shades, glowing, rounded ends, gradients, etc to make it look better?现在如何添加一些阴影、发光、圆角、渐变等以使其看起来更好? Or do I have to implement all this on my own?还是我必须自己实现所有这些?
  2. I suppose that the answer is no, but still - is there a neat way to multiply some small texture along a line?我想答案是否定的,但仍然 - 有没有一种巧妙的方法可以沿着一条线乘以一些小纹理? Thanks.谢谢。

For this case, I think this might be the best way to do this, but take in mind that using OpenGL with Stage is not the easiest way to solve it...对于这种情况,我认为可能是最好的方法,但请记住,将 OpenGL 与 Stage 结合使用并不是解决它的最简单方法......

On the other hand, an easier, but probably not that "beautiful" nor the faster way to solve it, could be using a TextureRegionDrawable, and scale, rotate and move for each point in your line path.另一方面,一种更简单但可能不是那么“漂亮”或更快的方法来解决它,可以使用 TextureRegionDrawable,并缩放、旋转和移动线路径中的每个点。

I wrote a little class, at least for a reference, it will need some polish, specially with corners.我写了一个小类,至少作为参考,它需要一些润色,特别是带有角落的。

public class LineDrawer extends Actor {
    private Array<Vector2> path;
    private float thickness;
    private Image image;

    public LineDrawer(Array<Vector2> path, float thickness, TextureRegionDrawable drawable) {
        this.path = path;
        this.thickness = thickness;
        this.image = new Image(drawable);
        this.image.setOrigin(Align.left);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        float dst, angle;
        Vector2 pointA, pointB;
        for (int i = 0; i < path.size - 1; i++) {
            pointA = path.get(i);
            pointB = path.get(i + 1);
            dst = pointA.dst(pointB);
            angle = MathUtils.atan2(pointB.y - pointA.y, pointB.x - pointA.x) * 180f / MathUtils.PI;

            image.setSize(dst, thickness);
            image.setPosition(pointA.x, pointA.y, Align.left);
            image.setRotation(angle);

            image.draw(batch, parentAlpha);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM