LIBGDX:如何使用shaperenderer绘制填充多边形?

时间:2013-04-19 10:36:31

标签: java libgdx

我使用顶点数组定义了一个形状:

float[] points =  new float[]{50,60,50,70,60,70, 60,60,50,60};

我在这里画这个:

shapeRenderer.polygon(floatNew);

这只是给出了形状的轮廓 如何填充颜色?
感谢

4 个答案:

答案 0 :(得分:8)

目前,ShapeRenderer支持多边形绘制(按行)但不填充。

此代码在三角形上剪切多边形,然后分别绘制每个三角形。

像这样编辑ShapeRenderer.java:

<asp:Label id="plCurrentWorkVersion" runat="server" text="Version" ResourceKey="plCurrentWorkVersion"  />

答案 1 :(得分:2)

你不能用shaperender绘制一个填充的Polygon。从bugtracker中查看此内容 您也可以在API中阅读。

  

public void polygon(float [] vertices)
  在x / y平面中绘制多边形。顶点必须至少包含3个   点(6个浮点x,y)。传递给begin的ShapeRenderer.ShapeType具有   成为ShapeRenderer.ShapeType.Line。


API ShapeRender
当然,如果它与ShapeType.Line你只是得到轮廓。
在这种情况下,您需要使用三角形自己绘制它。应该可以填充至少三角形。
也许从Stackoverflow看一下这个:drawing-filled-polygon-with-libgdx

答案 2 :(得分:1)

您应该使用mesh。看看链接。另外,您可能需要查看PolygonSpriteBatch

答案 3 :(得分:-2)

使用以下代码编辑ShapeRenderer.java类替换polygon()方法:

public void polygon(float[] vertices, int offset, int count) {
    if (currType != ShapeType.Filled && currType != ShapeType.Line)
        throw new GdxRuntimeException(
                "Must call begin(ShapeType.Filled) or begin(ShapeType.Line)");
    if (count < 6)
        throw new IllegalArgumentException(
                "Polygons must contain at least 3 points.");
    if (count % 2 != 0)
        throw new IllegalArgumentException(
                "Polygons must have an even number of vertices.");

    checkDirty();
    checkFlush(count);

    final float firstX = vertices[0];
    final float firstY = vertices[1];
    if (currType == ShapeType.Line) {
        for (int i = offset, n = offset + count; i < n; i += 2) {
            final float x1 = vertices[i];
            final float y1 = vertices[i + 1];

            final float x2;
            final float y2;

            if (i + 2 >= count) {
                x2 = firstX;
                y2 = firstY;
            } else {
                x2 = vertices[i + 2];
                y2 = vertices[i + 3];
            }

            renderer.color(color);
            renderer.vertex(x1, y1, 0);
            renderer.color(color);
            renderer.vertex(x2, y2, 0);

        }
    } else {

        for (int i = offset, n = offset + count; i < n; i += 4) {

            final float x1 = vertices[i];
            final float y1 = vertices[i + 1];

            if (i + 2 >= count) {
                break;
            }

            final float x2 = vertices[i + 2];
            final float y2 = vertices[i + 3];

            final float x3;
            final float y3;

            if (i + 4 >= count) {
                x3 = firstX;
                y3 = firstY;
            } else {
                x3 = vertices[i + 4];
                y3 = vertices[i + 5];
            }

            renderer.color(color);
            renderer.vertex(x1, y1, 0);
            renderer.color(color);
            renderer.vertex(x2, y2, 0);
            renderer.color(color);
            renderer.vertex(x3, y3, 0);


        }

    }
}

用法:

    gdx_shape_renderer.begin(ShapeType.Filled);
    gdx_shape_renderer.setColor(fill_r, fill_g, fill_b, fill_a);
    gdx_shape_renderer.polygon(vertices);
    gdx_shape_renderer.end();

    gdx_shape_renderer.begin(ShapeType.Line);
    gdx_shape_renderer.setColor(border_r, border_g, border_b, border_a);
    gdx_shape_renderer.polygon(vertices);
    gdx_shape_renderer.end();
相关问题