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

dew*_*s92 10 java libgdx

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

float[] points =  new float[]{50,60,50,70,60,70, 60,60,50,60};
Run Code Online (Sandbox Code Playgroud)

我在这里画这个:

shapeRenderer.polygon(floatNew);
Run Code Online (Sandbox Code Playgroud)

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

Anu*_*tum 9

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

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

像这样编辑ShapeRenderer.java:

EarClippingTriangulator ear = new EarClippingTriangulator();

public void polygon(float[] vertices, int offset, int count)
{
    if (shapeType != ShapeType.Filled && shapeType != 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.");

    check(shapeType, null, count);

    final float firstX = vertices[0];
    final float firstY = vertices[1];
    if (shapeType == 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
    {
        ShortArray arrRes = ear.computeTriangles(vertices);

        for (int i = 0; i < arrRes.size - 2; i = i + 3)
        {
            float x1 = vertices[arrRes.get(i) * 2];
            float y1 = vertices[(arrRes.get(i) * 2) + 1];

            float x2 = vertices[(arrRes.get(i + 1)) * 2];
            float y2 = vertices[(arrRes.get(i + 1) * 2) + 1];

            float x3 = vertices[arrRes.get(i + 2) * 2];
            float y3 = vertices[(arrRes.get(i + 2) * 2) + 1];

            this.triangle(x1, y1, x2, y2, x3, y3);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ben*_*nnX 2

您还无法使用 shaperender 绘制填充的多边形。从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