summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshCutterSimpleLW.cs
blob: 4a70c566c6f2437d1f76b4f0373f5063402cda49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using Unity.Collections;
using UnityEngine;

namespace NGS.MeshFusionPro;

public class MeshCutterSimpleLW : IMeshCutter
{
	private static List<int> _triangles;

	static MeshCutterSimpleLW()
	{
		_triangles = new List<int>();
	}

	public void Cut(Mesh mesh, MeshCuttingInfo info)
	{
		Cut(mesh, new MeshCuttingInfo[1] { info });
	}

	public void Cut(Mesh mesh, IList<MeshCuttingInfo> infos)
	{
		ValidateMeshOrThrowException(mesh);
		mesh.GetTriangles(_triangles, 0);
		using (Mesh.MeshDataArray meshDataArray = Mesh.AcquireReadOnlyMeshData(mesh))
		{
			NativeArray<LightweightVertex> other = meshDataArray[0].GetVertexData<LightweightVertex>();
			NativeList<LightweightVertex> nativeList = new NativeList<LightweightVertex>(Allocator.Temp);
			nativeList.CopyFrom(in other);
			foreach (MeshCuttingInfo item in infos.OrderByDescending((MeshCuttingInfo i) => i.triangleStart))
			{
				nativeList.RemoveRange(item.vertexStart, item.vertexCount);
				_triangles.RemoveRange(item.triangleStart, item.triangleCount);
				OffsetTriangles(item);
			}
			mesh.SetTriangles(_triangles, 0);
			mesh.SetVertexBufferData<LightweightVertex>(nativeList, 0, 0, nativeList.Length);
		}
		_triangles.Clear();
	}

	private void ValidateMeshOrThrowException(Mesh mesh)
	{
		if (mesh == null)
		{
			throw new ArgumentNullException("mesh");
		}
		if (mesh.subMeshCount > 1)
		{
			throw new ArgumentException("SimpleMeshCutter::'mesh' should has only 1 submesh");
		}
	}

	private void OffsetTriangles(MeshCuttingInfo info)
	{
		int vertexCount = info.vertexCount;
		int triangleStart = info.triangleStart;
		int count = _triangles.Count;
		for (int i = triangleStart; i < count; i++)
		{
			_triangles[i] -= vertexCount;
		}
	}
}