private void CreateMeshSimulationMeshVessel(Vessel v) { if (v.isActiveVessel) { debugMesh = PANELFARPartLocalMeshGenerator.GenerateFromPart(v.rootPart); } }
//Take the raw part geometry and simplify it so that further simplification of the entire vessel is faster public static PANELFARPartLocalMesh PreProcessLocalMesh(PANELFARPartLocalMesh mesh) { //Array of vertices; indexing must not change Vector3[] verts = new Vector3[mesh.vertexes.Length]; mesh.vertexes.CopyTo(verts, 0); //Array of triangles; each triangle points to an index in verts MeshIndexTriangle[] indexTris = new MeshIndexTriangle[mesh.triangles.Length]; mesh.triangles.CopyTo(indexTris, 0); //Array of a list of triangles that contain a given vertex; indexing is same as verts, each index in list points to an index in indexTris List <int>[] trisAttachedToVerts = GetTrisAttachedToVerts(verts, indexTris); //Array of quadrics associated with a particular vertex; indexing is same as verts Quadric[] vertQuadrics = CalculateVertQuadrics(verts, indexTris); //A list of possible vertex pairs that can be contracted into a single point MinHeap <MeshPairContraction> pairContractions = GeneratePairContractions(indexTris, verts, vertQuadrics); int faces = (int)Math.Floor(indexTris.Length * 0.5); faces = DecimateVertices(faces, ref pairContractions, ref verts, ref indexTris, ref trisAttachedToVerts, ref vertQuadrics); //This will be used to update the old array (which has many empty elements) to a new vertex array and allow the indexTris to be updated as well Dictionary <int, int> beforeIndexAfterIndex = new Dictionary <int, int>(); int currentIndex = 0; List <Vector3> newVerts = new List <Vector3>(); for (int i = 0; i < verts.Length; i++) { Vector3 v = verts[i]; if (trisAttachedToVerts[i] != null) { beforeIndexAfterIndex.Add(i, currentIndex); currentIndex++; newVerts.Add(v); } } MeshIndexTriangle[] newIndexTris = new MeshIndexTriangle[faces]; currentIndex = 0; foreach (MeshIndexTriangle tri in indexTris) { if (tri != null) { MeshIndexTriangle newTri = new MeshIndexTriangle(beforeIndexAfterIndex[tri.v0], beforeIndexAfterIndex[tri.v1], beforeIndexAfterIndex[tri.v2]); newIndexTris[currentIndex] = newTri; currentIndex++; } } mesh.vertexes = newVerts.ToArray(); mesh.triangles = newIndexTris; return(mesh); }
public static PANELFARPartLocalMesh GenerateFromPart(Part p) { PANELFARPartLocalMesh mesh = new PANELFARPartLocalMesh(); Matrix4x4 partUpMatrix = p.transform.worldToLocalMatrix; List <Vector3> vertexList = new List <Vector3>(); List <MeshIndexTriangle> vertexForTriList = new List <MeshIndexTriangle>(); int vertexOffset = 0; foreach (Transform t in p.FindModelComponents <Transform>()) { MeshFilter mf = t.GetComponent <MeshFilter>(); if (mf == null) { continue; } Mesh m = mf.mesh; if (m == null) { continue; } if (p.InternalModelName == m.name) { continue; } Matrix4x4 matrix = partUpMatrix * t.localToWorldMatrix; foreach (Vector3 vertex in m.vertices) { Vector3 v = matrix.MultiplyPoint(vertex); vertexList.Add(v); } for (int i = 0; i < m.triangles.Length; i += 3) { MeshIndexTriangle tri = new MeshIndexTriangle(); tri.v0 = m.triangles[i] + vertexOffset; tri.v1 = m.triangles[i + 1] + vertexOffset; tri.v2 = m.triangles[i + 2] + vertexOffset; vertexForTriList.Add(tri); //Vertex offset since otherwise there will be problems with parts that contain multiple mesh transforms } vertexOffset += m.vertices.Length; } mesh.vertexes = vertexList.ToArray(); mesh.triangles = vertexForTriList.ToArray(); mesh.part = p; mesh = PANELFARMeshSimplification.PreProcessLocalMesh(mesh); return(mesh); }