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

LIBGDX: How do I draw a filled polygon with the shaperenderer?(LIBGDX:如何使用 shaperenderer 绘制填充多边形?)
本文介绍了LIBGDX:如何使用 shaperenderer 绘制填充多边形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have defined a shape using an array of vertices:

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

我在这里画这个:

shapeRenderer.polygon(floatNew);

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

This just gives an outline of the shape.
How do I fill it with colour?
Thanks

推荐答案

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

Currently, ShapeRenderer supports polygon drawing (by line) but not filling.

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

This code is clipping the polygon on triangles, and then drawing each triangle separately.

像这样编辑 ShapeRenderer.java:

Edit ShapeRenderer.java like this:

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);
        }
    }
}

这篇关于LIBGDX:如何使用 shaperenderer 绘制填充多边形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Show padding zeros using DecimalFormat(使用 DecimalFormat 显示填充零)
Java decimal formatting using String.format?(使用 String.format 的 Java 十进制格式?)
Sprintf equivalent in Java(Java 中的 Sprintf 等价物)
StAX XML formatting in Java(Java 中的 StAX XML 格式)
Java - format double value as dollar amount(Java - 将双值格式化为美元金额)
Java printf using variable field size?(Java printf使用可变字段大小?)