简体   繁体   English

libGdx:在舞台上画线并保持在那里

[英]libGdx: Draw Line on Stage and keep it there

I am working on an app, where I need to draw the trace of my Sprites. 我正在开发一个应用程序,需要在其中绘制我的Sprites的痕迹。 I tried to do it with a ShapeRenderer, and it draws the Lines correctly, but after the line was drawn, it gets erased again. 我尝试使用ShapeRenderer进行此操作,它可以正确绘制线条,但是在绘制线条之后,它将再次被擦除。 So I tried to do it with a Polyline, and add the Points with each update, but after some time this polyline is too large to draw. 因此,我尝试使用折线进行此操作,并在每次更新时添加点,但是一段时间后,此折线太大了,无法绘制。

I have my code in an overriden Actor class, which on every draw call checks if the sprite has moved, and if so, the line gets drawn with the ShapeRenderer. 我将代码放在重写的Actor类中,该类在每个draw调用中检查精灵是否已移动,如果已移动,则使用ShapeRenderer绘制线条。 Is there a way, to keep that line ? 有办法保持这条线吗?

Code: 码:

ShapeRenderer renderer = StageActivity.stageListener.shapeRenderer;
renderer.setColor(color);
Gdx.gl.glLineWidth(strokeWidth);
line.add(sprite.look.getX());
line.add(sprite.look.getY());
renderer.polyline(getLineVertices());

The getLineVertices() Method is just converting my ArrayList to a float[] array. getLineVertices()方法只是将ArrayList转换为float []数组。

EDIT: 编辑:

Since the code above has very poor Performance with a lot of Lines, I tried out the Framebuffer which is essentially what I need (I think), but it does not get draw, what am I doing wrong ? 由于上面的代码在很多行中的性能非常差,因此我尝试了帧缓冲,这实际上是我需要的(我认为),但是它没有绘制,我在做什么错?

buffer.begin();
renderer.begin(ShapeRenderer.ShapeType.Line);
renderer.line(previousPoint.x, previousPoint.y, sprite.look.getX(), sprite.look.getY());
renderer.end();
buffer.end();
batch.draw(buffer.getColorBufferTexture(), 0, 0);

The Buffer is a global variable in my Actor. Buffer是我的Actor中的全局变量。

You should keep lines points in some collections when it is necessary to update and draw lines from these collections in your render method independently 当有必要在render方法中独立更新和绘制这些集合中的线条时,应在某些集合中保留线条点

    Array<Vector2> lines = new Array<Vector2>();

    ...

    //when you need to draw next line you just update lines array
    lines.add(new Vector2(sprite.look.getX(), sprite.look.getY()));

    //and in your render method you're just rendering these lines independently
    if(lines.size() > 1) { //one point is not a line
        for(int i = 1; i < lines.size(); i++)
            renderer.line(lines.get(i-1), lines.get(i));
    }

Also consider not drawing all lines all the time - if count of lines will be very big (like thousands) you will have performance issues anyway - drawing is rather expensive operation 还请考虑不要一直画所有的线-如果线的数量很大(例如数千条),那么您仍然会遇到性能问题-绘制是相当昂贵的操作

I resolved my Issue with the framebuffer. 我用帧缓冲解决了我的问题。 It was not rendering, because I had to call batch.end() before calling framebuffer.getColorBufferTexture(). 它没有渲染,因为我必须在调用framebuffer.getColorBufferTexture()之前先调用batch.end()。

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

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