Invalid polygon triangular for a circular-ish shape (example and bug provoking code provided)
Hi Freya,
I ran into this error "Invalid polygon triangulation - no convex edges found. Your polygon is likely self-intersecting" on some potions I was
procedurally generating. I boiled it down to this test case. If I draw.Polygon(path) I get the error on Unity 2020.1.17f1 on macOS 10.15.7.
Thank you for Shapes. It's really cool. I'm having fun using it.
-Shane
#define PROVOKE_ERROR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Shapes;
/** Hi Freya,
I ran into this error "Invalid polygon triangulation - no convex edges
found. Your polygon is likely self-intersecting" on some potions I was
procedurally generating. I boiled it down to this test case. If I
draw.Polygon(path) I get the error on Unity 2020.1.17f1 on macOS 10.15.7.
Thank you for Shapes. It's really cool. I'm having fun using it.
*/
public class ShapesTestCase : ImmediateModeShapeDrawer {
PolygonPath path;
void Awake() {
path = new PolygonPath();
// These points (and others) seem to provoke an error in Shapes.
var elements = new [] {
-43.6142349f, -73.051796f,
-38.8184395f, -75.787384f,
-56.4297829f, -60.0620575f,
-68.4223404f, -40.6156502f,
-75.5113449f, -19.670639f,
-78.6865387f, 2.04874039f,
-77.9659195f, 24.708168f,
-72.1172638f, 48.053299f,
-59.5250397f, 70.3607178f,
-39.6231956f, 88.4185333f,
-13.9431047f, 98.6328506f,
13.943121f, 98.6328506f,
39.6231956f, 88.4185333f,
59.5250282f, 70.3607407f,
72.1172638f, 48.053299f,
77.9659195f, 24.708168f,
78.6865311f, 2.04873085f,
75.5113449f, -19.670639f,
68.4223404f, -40.6156502f,
56.4297829f, -60.0620575f,
38.8184395f, -75.787384f,
43.6142349f, -73.051796f,
};
for (int i = 0; i < elements.Length; i+= 2)
path.AddPoint(elements[i], elements[i + 1]);
}
void OnDestroy() => path?.Dispose();
public override void DrawShapes( Camera cam ){
/*
Getting error "Invalid polygon triangulation - no convex edges found. Your
polygon is likely self-intersecting" on this path (copied from Rider debugger):
Count = 22[0] = {Vector2} (-43.6142349, -73.051796)
[1] = {Vector2} (-38.8184395, -75.787384)
[2] = {Vector2} (-56.4297829, -60.0620575)
[3] = {Vector2} (-68.4223404, -40.6156502)
[4] = {Vector2} (-75.5113449, -19.670639)
[5] = {Vector2} (-78.6865387, 2.04874039)
[6] = {Vector2} (-77.9659195, 24.708168)
[7] = {Vector2} (-72.1172638, 48.053299)
[8] = {Vector2} (-59.5250397, 70.3607178)
[9] = {Vector2} (-39.6231956, 88.4185333)
[10] = {Vector2} (-13.9431047, 98.6328506)
[11] = {Vector2} (13.943121, 98.6328506)
[12] = {Vector2} (39.6231956, 88.4185333)
[13] = {Vector2} (59.5250282, 70.3607407)
[14] = {Vector2} (72.1172638, 48.053299)
[15] = {Vector2} (77.9659195, 24.708168)
[16] = {Vector2} (78.6865311, 2.04873085)
[17] = {Vector2} (75.5113449, -19.670639)
[18] = {Vector2} (68.4223404, -40.6156502)
[19] = {Vector2} (56.4297829, -60.0620575)
[20] = {Vector2} (38.8184395, -75.787384)
[21] = {Vector2} (43.6142349, -73.051796)
It's just a circular-ish path with.
*/
using (Draw.Command(cam)) {
Draw.Matrix = transform.localToWorldMatrix * Matrix4x4.Scale(4f * Vector3.one);
#if PROVOKE_ERROR
// Uncomment the next line to remove the error.
Draw.Polygon(path, new Color(1f, 1f, 1f, 0.25f));
#endif
// This just shows the points in the path and the order they come in
// (white point shown first).
float dc = 1f / path.Count;
for (int i = 0; i < path.Count; i++) {
Color c = Color.white * (1f - dc * i);
c.a = 1f;
Draw.Disc(path[i], 1f, c);
}
}
}
}
ah! glad it worked out <3