From 8e13e7e2874adc8982e16d1d2ed2e28d7480b45f Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Sun, 19 May 2024 16:05:58 +0800 Subject: +1.57 --- .../NGS.MeshFusionPro/MeshSeparatorSimple.cs | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshSeparatorSimple.cs (limited to 'Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshSeparatorSimple.cs') diff --git a/Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshSeparatorSimple.cs b/Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshSeparatorSimple.cs new file mode 100644 index 0000000..6c79a16 --- /dev/null +++ b/Thronefall_1_57/Decompile/NGS.MeshFusionPro/MeshSeparatorSimple.cs @@ -0,0 +1,130 @@ +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Rendering; + +namespace NGS.MeshFusionPro; + +public class MeshSeparatorSimple +{ + private const int MAX_UV_CHANNELS = 4; + + private static Dictionary _meshToSubmeshes; + + private static List _srcVertices; + + private static List _srcNormals; + + private static List _srcTangents; + + private static List _srcUV; + + private static List _triangles; + + private static List _vertices; + + private static List _normals; + + private static List _tangents; + + private static List _uv; + + static MeshSeparatorSimple() + { + _meshToSubmeshes = new Dictionary(); + _srcVertices = new List(); + _srcNormals = new List(); + _srcTangents = new List(); + _srcUV = new List(); + _triangles = new List(); + _vertices = new List(); + _normals = new List(); + _tangents = new List(); + _uv = new List(); + } + + public Mesh GetSubmesh(Mesh source, int submesh) + { + if (!_meshToSubmeshes.TryGetValue(source, out var value)) + { + value = Separate(source); + _meshToSubmeshes.Add(source, value); + } + return value[submesh]; + } + + private Mesh[] Separate(Mesh mesh) + { + int subMeshCount = mesh.subMeshCount; + Mesh[] array = new Mesh[subMeshCount]; + CollectMeshData(mesh); + for (int i = 0; i < subMeshCount; i++) + { + array[i] = CreateFromSubmesh(mesh, i); + } + ClearData(); + return array; + } + + private void CollectMeshData(Mesh mesh) + { + mesh.GetVertices(_srcVertices); + mesh.GetNormals(_srcNormals); + mesh.GetTangents(_srcTangents); + } + + private Mesh CreateFromSubmesh(Mesh mesh, int submesh) + { + SubMeshDescriptor subMesh = mesh.GetSubMesh(submesh); + Mesh mesh2 = new Mesh(); + int indexCount = subMesh.indexCount; + int vertexCount = subMesh.vertexCount; + int firstVertex = subMesh.firstVertex; + int num = firstVertex + vertexCount; + _vertices.Clear(); + _normals.Clear(); + _tangents.Clear(); + mesh.GetIndices(_triangles, submesh); + for (int i = firstVertex; i < num; i++) + { + _vertices.Add(_srcVertices[i]); + _normals.Add(_srcNormals[i]); + _tangents.Add(_srcTangents[i]); + } + for (int j = 0; j < indexCount; j++) + { + _triangles[j] -= firstVertex; + } + mesh2.SetVertices(_vertices); + mesh2.SetNormals(_normals); + mesh2.SetTangents(_tangents); + mesh2.SetTriangles(_triangles, 0, calculateBounds: false); + mesh2.bounds = subMesh.bounds; + for (int k = 0; k < 4; k++) + { + mesh.GetUVs(k, _srcUV); + if (_srcUV.Count != 0) + { + _uv.Clear(); + for (int l = firstVertex; l < num; l++) + { + _uv.Add(_srcUV[l]); + } + mesh2.SetUVs(k, _uv); + } + } + return mesh2; + } + + private void ClearData() + { + _srcVertices.Clear(); + _srcNormals.Clear(); + _srcTangents.Clear(); + _srcUV.Clear(); + _triangles.Clear(); + _vertices.Clear(); + _normals.Clear(); + _tangents.Clear(); + _uv.Clear(); + } +} -- cgit v1.1-26-g67d0