109 lines
3.5 KiB
C#
109 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Poly2Tri;
|
|
using ProBuilder2.Common;
|
|
using UnityEngine;
|
|
|
|
namespace ProBuilder2.MeshOperations
|
|
{
|
|
// Token: 0x02000013 RID: 19
|
|
public static class pb_Triangulation
|
|
{
|
|
// Token: 0x0600009D RID: 157 RVA: 0x0000CBF4 File Offset: 0x0000AFF4
|
|
public static bool SortAndTriangulate(IList<Vector2> points, out List<int> indices, bool convex = false)
|
|
{
|
|
IList<Vector2> list = pb_Projection.Sort(points, SortMethod.CounterClockwise);
|
|
Dictionary<int, int> dictionary = new Dictionary<int, int>();
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
dictionary.Add(i, points.IndexOf(list[i]));
|
|
}
|
|
if (!pb_Triangulation.Triangulate(list, out indices, convex))
|
|
{
|
|
return false;
|
|
}
|
|
for (int j = 0; j < indices.Count; j++)
|
|
{
|
|
indices[j] = dictionary[indices[j]];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Token: 0x0600009E RID: 158 RVA: 0x0000CC78 File Offset: 0x0000B078
|
|
public static bool TriangulateVertices(IList<pb_Vertex> vertices, out List<int> triangles, bool unordered = true, bool convex = false)
|
|
{
|
|
Vector3[] array = new Vector3[vertices.Count];
|
|
for (int i = 0; i < vertices.Count; i++)
|
|
{
|
|
array[i] = vertices[i].position;
|
|
}
|
|
return pb_Triangulation.TriangulateVertices(array, out triangles, unordered, convex);
|
|
}
|
|
|
|
// Token: 0x0600009F RID: 159 RVA: 0x0000CCCC File Offset: 0x0000B0CC
|
|
public static bool TriangulateVertices(Vector3[] vertices, out List<int> triangles, bool unordered = true, bool convex = false)
|
|
{
|
|
triangles = null;
|
|
int num = ((vertices != null) ? vertices.Length : 0);
|
|
if (num < 3)
|
|
{
|
|
return false;
|
|
}
|
|
if (num == 3)
|
|
{
|
|
triangles = new List<int> { 0, 1, 2 };
|
|
return true;
|
|
}
|
|
Vector3 normal = pb_Projection.FindBestPlane(vertices, null).normal;
|
|
Vector2[] array = pb_Projection.PlanarProject(vertices, normal);
|
|
if (unordered)
|
|
{
|
|
return pb_Triangulation.SortAndTriangulate(array, out triangles, convex);
|
|
}
|
|
return pb_Triangulation.Triangulate(array, out triangles, convex);
|
|
}
|
|
|
|
// Token: 0x060000A0 RID: 160 RVA: 0x0000CD50 File Offset: 0x0000B150
|
|
public static bool Triangulate(IList<Vector2> points, out List<int> indices, bool convex = false)
|
|
{
|
|
indices = new List<int>();
|
|
int index = 0;
|
|
Triangulatable triangulatable2;
|
|
if (convex)
|
|
{
|
|
Triangulatable triangulatable = new PointSet(points.Select((Vector2 x) => new TriangulationPoint((double)x.x, (double)x.y, index++)).ToList<TriangulationPoint>());
|
|
triangulatable2 = triangulatable;
|
|
}
|
|
else
|
|
{
|
|
triangulatable2 = new Polygon(points.Select((Vector2 x) => new PolygonPoint((double)x.x, (double)x.y, index++)));
|
|
}
|
|
Triangulatable triangulatable3 = triangulatable2;
|
|
P2T.Triangulate(TriangulationAlgorithm.DTSweep, triangulatable3);
|
|
foreach (DelaunayTriangle delaunayTriangle in triangulatable3.Triangles)
|
|
{
|
|
if (delaunayTriangle.Points[0].Index < 0 || delaunayTriangle.Points[1].Index < 0 || delaunayTriangle.Points[2].Index < 0)
|
|
{
|
|
pb_Log.Warning("Triangulation failed - additional vertices were inserted.");
|
|
return false;
|
|
}
|
|
indices.Add(delaunayTriangle.Points[0].Index);
|
|
indices.Add(delaunayTriangle.Points[1].Index);
|
|
indices.Add(delaunayTriangle.Points[2].Index);
|
|
}
|
|
WindingOrder windingOrder = pbTriangleOps.GetWindingOrder(points);
|
|
if (pbTriangleOps.GetWindingOrder(new Vector2[]
|
|
{
|
|
points[indices[0]],
|
|
points[indices[1]],
|
|
points[indices[2]]
|
|
}) != windingOrder)
|
|
{
|
|
indices.Reverse();
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|