210 lines
6.2 KiB
C#
210 lines
6.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ProBuilder2.Common;
|
|
using UnityEngine;
|
|
|
|
namespace ProBuilder2.MeshOperations
|
|
{
|
|
// Token: 0x02000004 RID: 4
|
|
public static class pbTriangleOps
|
|
{
|
|
// Token: 0x06000033 RID: 51 RVA: 0x00005D2C File Offset: 0x0000412C
|
|
public static void ReverseWindingOrder(this pb_Object pb, pb_Face[] faces)
|
|
{
|
|
for (int i = 0; i < faces.Length; i++)
|
|
{
|
|
faces[i].ReverseIndices();
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000034 RID: 52 RVA: 0x00005D58 File Offset: 0x00004158
|
|
public static WindingOrder GetWindingOrder(this pb_Object pb, pb_Face face)
|
|
{
|
|
Vector2[] array = pb_Projection.PlanarProject(pb, face);
|
|
return pbTriangleOps.GetWindingOrder(array);
|
|
}
|
|
|
|
// Token: 0x06000035 RID: 53 RVA: 0x00005D74 File Offset: 0x00004174
|
|
public static WindingOrder GetWindingOrder(IList<pb_Vertex> vertices, IList<int> indices)
|
|
{
|
|
Vector2[] array = pb_Projection.PlanarProject(vertices, indices);
|
|
return pbTriangleOps.GetWindingOrder(array);
|
|
}
|
|
|
|
// Token: 0x06000036 RID: 54 RVA: 0x00005D90 File Offset: 0x00004190
|
|
public static WindingOrder GetWindingOrder(IList<Vector2> points)
|
|
{
|
|
float num = 0f;
|
|
int count = points.Count;
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Vector2 vector = points[i];
|
|
Vector2 vector2 = ((i >= count - 1) ? points[0] : points[i + 1]);
|
|
num += (vector2.x - vector.x) * (vector2.y + vector.y);
|
|
}
|
|
return (num != 0f) ? ((num <= 0f) ? WindingOrder.CounterClockwise : WindingOrder.Clockwise) : WindingOrder.Unknown;
|
|
}
|
|
|
|
// Token: 0x06000037 RID: 55 RVA: 0x00005E2C File Offset: 0x0000422C
|
|
public static bool FlipEdge(this pb_Object pb, pb_Face face)
|
|
{
|
|
int[] indices = face.indices;
|
|
if (indices.Length != 6)
|
|
{
|
|
return false;
|
|
}
|
|
int[] array = pbUtil.FilledArray<int>(1, indices.Length);
|
|
for (int i = 0; i < indices.Length - 1; i++)
|
|
{
|
|
for (int j = i + 1; j < indices.Length; j++)
|
|
{
|
|
if (indices[i] == indices[j])
|
|
{
|
|
array[i]++;
|
|
array[j]++;
|
|
}
|
|
}
|
|
}
|
|
if (array[0] + array[1] + array[2] != 5 || array[3] + array[4] + array[5] != 5)
|
|
{
|
|
return false;
|
|
}
|
|
int num = indices[(array[0] != 1) ? ((array[1] != 1) ? 2 : 1) : 0];
|
|
int num2 = indices[(array[3] != 1) ? ((array[4] != 1) ? 5 : 4) : 3];
|
|
int num3 = -1;
|
|
if (array[0] == 2)
|
|
{
|
|
num3 = indices[0];
|
|
indices[0] = num2;
|
|
}
|
|
else if (array[1] == 2)
|
|
{
|
|
num3 = indices[1];
|
|
indices[1] = num2;
|
|
}
|
|
else if (array[2] == 2)
|
|
{
|
|
num3 = indices[2];
|
|
indices[2] = num2;
|
|
}
|
|
if (array[3] == 2 && indices[3] != num3)
|
|
{
|
|
indices[3] = num;
|
|
}
|
|
else if (array[4] == 2 && indices[4] != num3)
|
|
{
|
|
indices[4] = num;
|
|
}
|
|
else if (array[5] == 2 && indices[5] != num3)
|
|
{
|
|
indices[5] = num;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Token: 0x06000038 RID: 56 RVA: 0x00005FA8 File Offset: 0x000043A8
|
|
public static bool RemoveDegenerateTriangles(this pb_Object pb, out int[] removed)
|
|
{
|
|
Dictionary<int, int> dictionary = pb.sharedIndices.ToDictionary();
|
|
Dictionary<int, int> dictionary2 = ((pb.sharedIndicesUV == null) ? new Dictionary<int, int>() : pb.sharedIndicesUV.ToDictionary());
|
|
Vector3[] vertices = pb.vertices;
|
|
Dictionary<int, int> dictionary3 = new Dictionary<int, int>();
|
|
Dictionary<int, int> dictionary4 = new Dictionary<int, int>();
|
|
List<pb_Face> list = new List<pb_Face>();
|
|
foreach (pb_Face pb_Face in pb.faces)
|
|
{
|
|
List<int> list2 = new List<int>();
|
|
int[] indices = pb_Face.indices;
|
|
for (int j = 0; j < indices.Length; j += 3)
|
|
{
|
|
float num = pb_Math.TriangleArea(vertices[indices[j]], vertices[indices[j + 1]], vertices[indices[j + 2]]);
|
|
if (num > Mathf.Epsilon)
|
|
{
|
|
int num2 = dictionary[indices[j]];
|
|
int num3 = dictionary[indices[j + 1]];
|
|
int num4 = dictionary[indices[j + 2]];
|
|
if (num2 != num3 && num2 != num4 && num3 != num4)
|
|
{
|
|
list2.Add(indices[j]);
|
|
list2.Add(indices[j + 1]);
|
|
list2.Add(indices[j + 2]);
|
|
if (!dictionary3.ContainsKey(indices[j]))
|
|
{
|
|
dictionary3.Add(indices[j], num2);
|
|
}
|
|
if (!dictionary3.ContainsKey(indices[j + 1]))
|
|
{
|
|
dictionary3.Add(indices[j + 1], num3);
|
|
}
|
|
if (!dictionary3.ContainsKey(indices[j + 2]))
|
|
{
|
|
dictionary3.Add(indices[j + 2], num4);
|
|
}
|
|
if (dictionary2.ContainsKey(indices[j]) && !dictionary4.ContainsKey(indices[j]))
|
|
{
|
|
dictionary4.Add(indices[j], dictionary2[indices[j]]);
|
|
}
|
|
if (dictionary2.ContainsKey(indices[j + 1]) && !dictionary4.ContainsKey(indices[j + 1]))
|
|
{
|
|
dictionary4.Add(indices[j + 1], dictionary2[indices[j + 1]]);
|
|
}
|
|
if (dictionary2.ContainsKey(indices[j + 2]) && !dictionary4.ContainsKey(indices[j + 2]))
|
|
{
|
|
dictionary4.Add(indices[j + 2], dictionary2[indices[j + 2]]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (list2.Count > 0)
|
|
{
|
|
pb_Face.SetIndices(list2.ToArray());
|
|
pb_Face.RebuildCaches();
|
|
list.Add(pb_Face);
|
|
}
|
|
}
|
|
pb.SetFaces(list.ToArray());
|
|
pb.SetSharedIndices(dictionary3);
|
|
pb.SetSharedIndicesUV(dictionary4);
|
|
removed = pb.RemoveUnusedVertices();
|
|
return removed.Length > 0;
|
|
}
|
|
|
|
// Token: 0x06000039 RID: 57 RVA: 0x00006270 File Offset: 0x00004670
|
|
public static pb_Face MergeFaces(this pb_Object pb, pb_Face[] faces)
|
|
{
|
|
List<int> list = new List<int>(faces[0].indices);
|
|
for (int i = 1; i < faces.Length; i++)
|
|
{
|
|
list.AddRange(faces[i].indices);
|
|
}
|
|
pb_Face pb_Face = new pb_Face(list.ToArray(), faces[0].material, faces[0].uv, faces[0].smoothingGroup, faces[0].textureGroup, faces[0].elementGroup, faces[0].manualUV);
|
|
pb_Face[] array = new pb_Face[pb.faces.Length - faces.Length + 1];
|
|
int num = 0;
|
|
foreach (pb_Face pb_Face2 in pb.faces)
|
|
{
|
|
if (Array.IndexOf<pb_Face>(faces, pb_Face2) < 0)
|
|
{
|
|
array[num++] = pb_Face2;
|
|
}
|
|
}
|
|
array[num] = pb_Face;
|
|
pb.SetFaces(array);
|
|
Dictionary<int, int> dictionary = new Dictionary<int, int>();
|
|
for (int k = 0; k < pb_Face.indices.Length; k++)
|
|
{
|
|
int num2 = pb.sharedIndices.IndexOf(pb_Face.indices[k]);
|
|
if (dictionary.ContainsKey(num2))
|
|
{
|
|
pb_Face.indices[k] = dictionary[num2];
|
|
}
|
|
else
|
|
{
|
|
dictionary.Add(num2, pb_Face.indices[k]);
|
|
}
|
|
}
|
|
pb.RemoveUnusedVertices();
|
|
return pb_Face;
|
|
}
|
|
}
|
|
}
|