private static bool IsTriangleHidden(Vector3 v0, Vector3 v1, Vector3 v2, float maxDistance, List <Vector3> triMinCorners, List <Vector3> triMaxCorners, List <Vector3> hidingVs, List <int> hidingTs) { Vector3 normal = MeshExtensions.GetNormal(v0, v1, v2); List <int> trianglesWithinRange = MeshExtensions.GetTrianglesWithinRange(v0, v1, v2, maxDistance, triMinCorners, triMaxCorners); return(MeshExtensions.IsHidden((v0 + v1 + v2) / 3f, normal, maxDistance, hidingVs, hidingTs, trianglesWithinRange) && MeshExtensions.IsHidden(v0, normal, maxDistance, hidingVs, hidingTs, trianglesWithinRange) && MeshExtensions.IsHidden(v1, normal, maxDistance, hidingVs, hidingTs, trianglesWithinRange) && MeshExtensions.IsHidden(v2, normal, maxDistance, hidingVs, hidingTs, trianglesWithinRange)); }
private static bool IsHidden(Vector3 v, Vector3 n, float maxDistance, List <Vector3> hidingVs, List <int> hidingTs, List <int> trianglesToCheck) { for (int i = 0; i < trianglesToCheck.Count; i++) { int num = trianglesToCheck[i] * 3; Vector3 vector = hidingVs[hidingTs[num]]; Vector3 vector2 = hidingVs[hidingTs[num + 1]]; Vector3 vector3 = hidingVs[hidingTs[num + 2]]; Vector3 normal = MeshExtensions.GetNormal(vector, vector2, vector3); float num2 = Vector3.Angle(n, normal); if (num2 < 60f) { float num3 = MeshExtensions.DistanceToPlane(v, n, vector, normal); if (num3 > 0f && num3 <= maxDistance) { Vector3 p = v + n * num3; Vector2 v2 = p.Barycentric(vector, vector2, vector3); if (v2.IsBarycentricInTriangle()) { return(true); } } } } return(false); }
public static Mesh CopyAndRemoveHiddenTriangles(this Mesh orig, int subMeshIdx, Matrix4x4 localToWorldMatrix, Mesh[] hidingMeshes, int[] hidingSubMeshes, Matrix4x4[] hidingLocalToWorldMatrices, float maxRemoveDistance = 0.01f) { if (subMeshIdx >= orig.subMeshCount) { return(null); } if (hidingMeshes.Length <= 0) { return(null); } if (hidingMeshes.Length != hidingSubMeshes.Length || hidingMeshes.Length != hidingLocalToWorldMatrices.Length) { return(null); } Mesh mesh = UnityEngine.Object.Instantiate <Mesh>(orig); List <List <int> > list = new List <List <int> >(mesh.subMeshCount); List <Vector3> list2 = new List <Vector3>(orig.vertices); List <Vector3> list3 = new List <Vector3>(orig.vertexCount); List <Vector2> list4 = new List <Vector2>(orig.vertexCount); List <Vector2> list5 = new List <Vector2>(); List <Vector2> list6 = new List <Vector2>(); List <Vector2> list7 = new List <Vector2>(); List <Color32> list8 = new List <Color32>(); List <BoneWeight> list9 = new List <BoneWeight>(); list3.AddRange(orig.normals); if (list3 == null || list3.Count <= 0) { orig.RecalculateNormals(); list3.AddRange(orig.normals); } list4.AddRange(orig.uv); list5.AddRange(orig.uv2); list6.AddRange(orig.uv3); list7.AddRange(orig.uv4); list8.AddRange(orig.colors32); list9.AddRange(orig.boneWeights); for (int i = 0; i < orig.subMeshCount; i++) { List <int> list10 = new List <int>(); list10.AddRange(orig.GetTriangles(i)); list.Add(list10); } List <int> list11 = list[subMeshIdx]; List <Vector3> list12 = new List <Vector3>(); List <int> list13 = new List <int>(); Mesh x = null; int num = 0; int num2 = 0; for (int j = 0; j < hidingMeshes.Length; j++) { Mesh mesh2 = hidingMeshes[j]; int[] triangles = mesh2.GetTriangles(hidingSubMeshes[j]); if (x != mesh2) { num += num2; } for (int k = 0; k < triangles.Length; k++) { list13.Add(triangles[k] + num); } if (x != mesh2) { Matrix4x4 matrix4x = hidingLocalToWorldMatrices[j]; Vector3[] vertices = mesh2.vertices; for (int l = 0; l < vertices.Length; l++) { list12.Add(matrix4x.MultiplyPoint3x4(vertices[l])); } num2 = vertices.Length; x = mesh2; } } List <Vector3> list14 = new List <Vector3>(); List <Vector3> list15 = new List <Vector3>(); for (int m = 0; m < list13.Count; m += 3) { Vector3 vector = list12[list13[m]]; Vector3 vector2 = list12[list13[m + 1]]; Vector3 vector3 = list12[list13[m + 2]]; list14.Add(new Vector3(Mathf.Min(Mathf.Min(vector.x, vector2.x), vector3.x), Mathf.Min(Mathf.Min(vector.y, vector2.y), vector3.y), Mathf.Min(Mathf.Min(vector.z, vector2.z), vector3.z))); list15.Add(new Vector3(Mathf.Max(Mathf.Max(vector.x, vector2.x), vector3.x), Mathf.Max(Mathf.Max(vector.y, vector2.y), vector3.y), Mathf.Max(Mathf.Max(vector.z, vector2.z), vector3.z))); } List <int> list16 = new List <int>(); for (int n = 0; n < list11.Count; n += 3) { Vector3 v = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n]]); Vector3 v2 = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n + 1]]); Vector3 v3 = localToWorldMatrix.MultiplyPoint3x4(list2[list11[n + 2]]); if (!MeshExtensions.IsTriangleHidden(v, v2, v3, maxRemoveDistance, list14, list15, list12, list13)) { list16.Add(list11[n]); list16.Add(list11[n + 1]); list16.Add(list11[n + 2]); } } list[subMeshIdx] = list16; LODMaker.RemoveUnusedVertices(list2, list3, list4, list5, list6, list7, list8, list9, list); mesh.uv4 = null; mesh.uv3 = null; mesh.uv2 = null; mesh.uv2 = null; mesh.boneWeights = null; mesh.colors32 = null; mesh.normals = null; mesh.tangents = null; mesh.triangles = null; mesh.vertices = list2.ToArray(); if (list3.Count > 0) { mesh.normals = list3.ToArray(); } if (list4.Count > 0) { mesh.uv = list4.ToArray(); } if (list5.Count > 0) { mesh.uv2 = list5.ToArray(); } if (list6.Count > 0) { mesh.uv3 = list6.ToArray(); } if (list7.Count > 0) { mesh.uv4 = list7.ToArray(); } if (list8.Count > 0) { mesh.colors32 = list8.ToArray(); } if (list9.Count > 0) { mesh.boneWeights = list9.ToArray(); } mesh.subMeshCount = list.Count; for (int num3 = 0; num3 < list.Count; num3++) { if (num3 == subMeshIdx) { mesh.SetTriangles(list16.ToArray(), num3); } else { mesh.SetTriangles(list[num3].ToArray(), num3); } } if (list3 == null || list3.Count <= 0) { mesh.RecalculateNormals(); } mesh.RecalculateTangents(); mesh.RecalculateBounds(); return(mesh); }