263 lines
10 KiB
C#
263 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using ProBuilder2.Common;
|
|
using UnityEngine;
|
|
|
|
namespace ProBuilder2.MeshOperations
|
|
{
|
|
// Token: 0x0200000C RID: 12
|
|
public static class pb_ConnectEdges
|
|
{
|
|
// Token: 0x06000074 RID: 116 RVA: 0x0000A364 File Offset: 0x00008764
|
|
public static pb_ActionResult Connect(this pb_Object pb, IEnumerable<pb_Face> faces, out pb_Face[] subdividedFaces)
|
|
{
|
|
IEnumerable<pb_Edge> enumerable = faces.SelectMany((pb_Face x) => x.edges);
|
|
HashSet<pb_Face> hashSet = new HashSet<pb_Face>(faces);
|
|
pb_Edge[] array;
|
|
return pb.Connect(enumerable, out subdividedFaces, out array, true, false, hashSet);
|
|
}
|
|
|
|
// Token: 0x06000075 RID: 117 RVA: 0x0000A3AC File Offset: 0x000087AC
|
|
public static pb_ActionResult Connect(this pb_Object pb, IEnumerable<pb_Edge> edges, out pb_Face[] faces)
|
|
{
|
|
pb_Edge[] array;
|
|
return pb.Connect(edges, out faces, out array, true, false, null);
|
|
}
|
|
|
|
// Token: 0x06000076 RID: 118 RVA: 0x0000A3C8 File Offset: 0x000087C8
|
|
public static pb_ActionResult Connect(this pb_Object pb, IEnumerable<pb_Edge> edges, out pb_Edge[] connections)
|
|
{
|
|
pb_Face[] array;
|
|
return pb.Connect(edges, out array, out connections, false, true, null);
|
|
}
|
|
|
|
// Token: 0x06000077 RID: 119 RVA: 0x0000A3E4 File Offset: 0x000087E4
|
|
private static pb_ActionResult Connect(this pb_Object pb, IEnumerable<pb_Edge> edges, out pb_Face[] addedFaces, out pb_Edge[] connections, bool returnFaces = false, bool returnEdges = false, HashSet<pb_Face> faceMask = null)
|
|
{
|
|
Dictionary<int, int> dictionary = pb.sharedIndices.ToDictionary();
|
|
Dictionary<int, int> dictionary2 = ((pb.sharedIndicesUV == null) ? null : pb.sharedIndicesUV.ToDictionary());
|
|
HashSet<pb_EdgeLookup> hashSet = new HashSet<pb_EdgeLookup>(pb_EdgeLookup.GetEdgeLookup(edges, dictionary));
|
|
List<pb_WingedEdge> wingedEdges = pb_WingedEdge.GetWingedEdges(pb, false);
|
|
Dictionary<pb_Face, List<pb_WingedEdge>> dictionary3 = new Dictionary<pb_Face, List<pb_WingedEdge>>();
|
|
foreach (pb_WingedEdge pb_WingedEdge in wingedEdges)
|
|
{
|
|
if (hashSet.Contains(pb_WingedEdge.edge))
|
|
{
|
|
List<pb_WingedEdge> list;
|
|
if (dictionary3.TryGetValue(pb_WingedEdge.face, out list))
|
|
{
|
|
list.Add(pb_WingedEdge);
|
|
}
|
|
else
|
|
{
|
|
dictionary3.Add(pb_WingedEdge.face, new List<pb_WingedEdge> { pb_WingedEdge });
|
|
}
|
|
}
|
|
}
|
|
Dictionary<pb_Face, List<pb_WingedEdge>> dictionary4 = new Dictionary<pb_Face, List<pb_WingedEdge>>();
|
|
foreach (KeyValuePair<pb_Face, List<pb_WingedEdge>> keyValuePair in dictionary3)
|
|
{
|
|
if (keyValuePair.Value.Count <= 1)
|
|
{
|
|
pb_WingedEdge opposite = keyValuePair.Value[0].opposite;
|
|
if (opposite == null)
|
|
{
|
|
continue;
|
|
}
|
|
List<pb_WingedEdge> list2;
|
|
if (!dictionary3.TryGetValue(opposite.face, out list2))
|
|
{
|
|
continue;
|
|
}
|
|
if (list2.Count <= 1)
|
|
{
|
|
continue;
|
|
}
|
|
}
|
|
dictionary4.Add(keyValuePair.Key, keyValuePair.Value);
|
|
}
|
|
List<pb_Vertex> list3 = new List<pb_Vertex>(pb_Vertex.GetVertices(pb, null));
|
|
List<ConnectFaceRebuildData> list4 = new List<ConnectFaceRebuildData>();
|
|
List<pb_Face> list5 = new List<pb_Face>();
|
|
HashSet<int> hashSet2 = new HashSet<int>(pb.faces.Select((pb_Face x) => x.textureGroup));
|
|
int num = 1;
|
|
foreach (KeyValuePair<pb_Face, List<pb_WingedEdge>> keyValuePair2 in dictionary4)
|
|
{
|
|
pb_Face key = keyValuePair2.Key;
|
|
List<pb_WingedEdge> value = keyValuePair2.Value;
|
|
int count = value.Count;
|
|
Vector3 vector = pb_Math.Normal(list3, key.indices);
|
|
if (count == 1 || (faceMask != null && !faceMask.Contains(key)))
|
|
{
|
|
ConnectFaceRebuildData connectFaceRebuildData = pb_ConnectEdges.InsertVertices(key, value, list3);
|
|
Vector3 vector2 = pb_Math.Normal(connectFaceRebuildData.faceRebuildData.vertices, connectFaceRebuildData.faceRebuildData.face.indices);
|
|
if (Vector3.Dot(vector, vector2) < 0f)
|
|
{
|
|
connectFaceRebuildData.faceRebuildData.face.ReverseIndices();
|
|
}
|
|
list4.Add(connectFaceRebuildData);
|
|
}
|
|
else if (count > 1)
|
|
{
|
|
List<ConnectFaceRebuildData> list6 = ((count != 2) ? pb_ConnectEdges.ConnectEdgesInFace(key, value, list3) : pb_ConnectEdges.ConnectEdgesInFace(key, value[0], value[1], list3));
|
|
if (key.textureGroup < 0)
|
|
{
|
|
while (hashSet2.Contains(num))
|
|
{
|
|
num++;
|
|
}
|
|
hashSet2.Add(num);
|
|
}
|
|
foreach (ConnectFaceRebuildData connectFaceRebuildData2 in list6)
|
|
{
|
|
list5.Add(connectFaceRebuildData2.faceRebuildData.face);
|
|
Vector3 vector3 = pb_Math.Normal(connectFaceRebuildData2.faceRebuildData.vertices, connectFaceRebuildData2.faceRebuildData.face.indices);
|
|
if (Vector3.Dot(vector, vector3) < 0f)
|
|
{
|
|
connectFaceRebuildData2.faceRebuildData.face.ReverseIndices();
|
|
}
|
|
connectFaceRebuildData2.faceRebuildData.face.textureGroup = ((key.textureGroup >= 0) ? key.textureGroup : num);
|
|
connectFaceRebuildData2.faceRebuildData.face.uv = new pb_UV(key.uv);
|
|
connectFaceRebuildData2.faceRebuildData.face.smoothingGroup = key.smoothingGroup;
|
|
connectFaceRebuildData2.faceRebuildData.face.manualUV = key.manualUV;
|
|
connectFaceRebuildData2.faceRebuildData.face.material = key.material;
|
|
}
|
|
list4.AddRange(list6);
|
|
}
|
|
}
|
|
pb_FaceRebuildData.Apply(list4.Select((ConnectFaceRebuildData x) => x.faceRebuildData), pb, list3, null, dictionary, dictionary2);
|
|
pb.SetSharedIndicesUV(new pb_IntArray[0]);
|
|
int num2 = pb.DeleteFaces(dictionary4.Keys).Length;
|
|
pb.SetSharedIndices(pb_IntArrayUtility.ExtractSharedIndices(pb.vertices));
|
|
pb.ToMesh();
|
|
if (returnEdges)
|
|
{
|
|
HashSet<int> appendedIndices = new HashSet<int>();
|
|
for (int i = 0; i < list4.Count; i++)
|
|
{
|
|
for (int j = 0; j < list4[i].newVertexIndices.Count; j++)
|
|
{
|
|
appendedIndices.Add(list4[i].newVertexIndices[j] + list4[i].faceRebuildData.Offset() - num2);
|
|
}
|
|
}
|
|
Dictionary<int, int> dictionary5 = pb.sharedIndices.ToDictionary();
|
|
IEnumerable<pb_Edge> enumerable = from x in list4.SelectMany((ConnectFaceRebuildData x) => x.faceRebuildData.face.edges)
|
|
where appendedIndices.Contains(x.x) && appendedIndices.Contains(x.y)
|
|
select x;
|
|
IEnumerable<pb_EdgeLookup> edgeLookup = pb_EdgeLookup.GetEdgeLookup(enumerable, dictionary5);
|
|
connections = (from x in edgeLookup.Distinct<pb_EdgeLookup>()
|
|
select x.local).ToArray<pb_Edge>();
|
|
}
|
|
else
|
|
{
|
|
connections = null;
|
|
}
|
|
if (returnFaces)
|
|
{
|
|
addedFaces = list5.ToArray();
|
|
}
|
|
else
|
|
{
|
|
addedFaces = null;
|
|
}
|
|
return new pb_ActionResult(Status.Success, string.Format("Connected {0} Edges", list4.Count));
|
|
}
|
|
|
|
// Token: 0x06000078 RID: 120 RVA: 0x0000AA50 File Offset: 0x00008E50
|
|
private static List<ConnectFaceRebuildData> ConnectEdgesInFace(pb_Face face, pb_WingedEdge a, pb_WingedEdge b, List<pb_Vertex> vertices)
|
|
{
|
|
List<pb_Edge> list = pb_WingedEdge.SortEdgesByAdjacency(face);
|
|
List<pb_Vertex>[] array = new List<pb_Vertex>[]
|
|
{
|
|
new List<pb_Vertex>(),
|
|
new List<pb_Vertex>()
|
|
};
|
|
List<int>[] array2 = new List<int>[]
|
|
{
|
|
new List<int>(),
|
|
new List<int>()
|
|
};
|
|
int num = 0;
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
array[num % 2].Add(vertices[list[i].x]);
|
|
if (list[i].Equals(a.edge.local) || list[i].Equals(b.edge.local))
|
|
{
|
|
pb_Vertex pb_Vertex = pb_Vertex.Mix(vertices[list[i].x], vertices[list[i].y], 0.5f);
|
|
array2[num % 2].Add(array[num % 2].Count);
|
|
array[num % 2].Add(pb_Vertex);
|
|
num++;
|
|
array2[num % 2].Add(array[num % 2].Count);
|
|
array[num % 2].Add(pb_Vertex);
|
|
}
|
|
}
|
|
List<ConnectFaceRebuildData> list2 = new List<ConnectFaceRebuildData>();
|
|
for (int j = 0; j < array.Length; j++)
|
|
{
|
|
pb_FaceRebuildData pb_FaceRebuildData = pb_AppendPolygon.FaceWithVertices(array[j], false);
|
|
list2.Add(new ConnectFaceRebuildData(pb_FaceRebuildData, array2[j]));
|
|
}
|
|
return list2;
|
|
}
|
|
|
|
// Token: 0x06000079 RID: 121 RVA: 0x0000ABBC File Offset: 0x00008FBC
|
|
private static List<ConnectFaceRebuildData> ConnectEdgesInFace(pb_Face face, List<pb_WingedEdge> edges, List<pb_Vertex> vertices)
|
|
{
|
|
List<pb_Edge> list = pb_WingedEdge.SortEdgesByAdjacency(face);
|
|
int count = edges.Count;
|
|
pb_Vertex pb_Vertex = pb_Vertex.Average(vertices, face.distinctIndices);
|
|
List<List<pb_Vertex>> list2 = pbUtil.Fill<List<pb_Vertex>>((int x) => new List<pb_Vertex>(), count);
|
|
List<List<int>> list3 = pbUtil.Fill<List<int>>((int x) => new List<int>(), count);
|
|
HashSet<pb_Edge> hashSet = new HashSet<pb_Edge>(edges.Select((pb_WingedEdge x) => x.edge.local));
|
|
int num = 0;
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
list2[num % count].Add(vertices[list[i].x]);
|
|
if (hashSet.Contains(list[i]))
|
|
{
|
|
pb_Vertex pb_Vertex2 = pb_Vertex.Mix(vertices[list[i].x], vertices[list[i].y], 0.5f);
|
|
list3[num].Add(list2[num].Count);
|
|
list2[num].Add(pb_Vertex2);
|
|
list3[num].Add(list2[num].Count);
|
|
list2[num].Add(pb_Vertex);
|
|
num = (num + 1) % count;
|
|
list2[num].Add(pb_Vertex2);
|
|
}
|
|
}
|
|
List<ConnectFaceRebuildData> list4 = new List<ConnectFaceRebuildData>();
|
|
for (int j = 0; j < list2.Count; j++)
|
|
{
|
|
pb_FaceRebuildData pb_FaceRebuildData = pb_AppendPolygon.FaceWithVertices(list2[j], false);
|
|
list4.Add(new ConnectFaceRebuildData(pb_FaceRebuildData, list3[j]));
|
|
}
|
|
return list4;
|
|
}
|
|
|
|
// Token: 0x0600007A RID: 122 RVA: 0x0000AD94 File Offset: 0x00009194
|
|
private static ConnectFaceRebuildData InsertVertices(pb_Face face, List<pb_WingedEdge> edges, List<pb_Vertex> vertices)
|
|
{
|
|
List<pb_Edge> list = pb_WingedEdge.SortEdgesByAdjacency(face);
|
|
List<pb_Vertex> list2 = new List<pb_Vertex>();
|
|
List<int> list3 = new List<int>();
|
|
HashSet<pb_Edge> hashSet = new HashSet<pb_Edge>(edges.Select((pb_WingedEdge x) => x.edge.local));
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
list2.Add(vertices[list[i].x]);
|
|
if (hashSet.Contains(list[i]))
|
|
{
|
|
list3.Add(list2.Count);
|
|
list2.Add(pb_Vertex.Mix(vertices[list[i].x], vertices[list[i].y], 0.5f));
|
|
}
|
|
}
|
|
pb_FaceRebuildData pb_FaceRebuildData = pb_AppendPolygon.FaceWithVertices(list2, false);
|
|
pb_FaceRebuildData.face.textureGroup = face.textureGroup;
|
|
pb_FaceRebuildData.face.uv = new pb_UV(face.uv);
|
|
pb_FaceRebuildData.face.smoothingGroup = face.smoothingGroup;
|
|
pb_FaceRebuildData.face.manualUV = face.manualUV;
|
|
pb_FaceRebuildData.face.material = face.material;
|
|
return new ConnectFaceRebuildData(pb_FaceRebuildData, list3);
|
|
}
|
|
}
|
|
}
|