using UnityEngine; using System.Collections.Generic; using System.Linq; namespace UnityEngine.ProBuilder.Experimental.CSG { /// /// Representation of a mesh in CSG terms. Contains methods for translating to and from UnityEngine.Mesh. /// sealed class CSG_Model { public List vertices; public List indexes; public CSG_Model() { vertices = new List(); indexes = new List(); } /** * Initialize a CSG_Model with the mesh of a gameObject. */ public CSG_Model(GameObject go) { var mesh = go.GetComponent().sharedMesh; var transform = go.GetComponent(); vertices = CSG_VertexUtility.GetVertices(mesh).Select(x => transform.TransformVertex(x)).ToList(); indexes = new List(mesh.triangles); } public CSG_Model(List list) { this.vertices = new List(); this.indexes = new List(); int p = 0; for (int i = 0; i < list.Count; i++) { CSG_Polygon poly = list[i]; for (int j = 2; j < poly.vertices.Count; j++) { this.vertices.Add(poly.vertices[0]); this.indexes.Add(p++); this.vertices.Add(poly.vertices[j - 1]); this.indexes.Add(p++); this.vertices.Add(poly.vertices[j]); this.indexes.Add(p++); } } } public List ToPolygons() { List list = new List(); for (int i = 0; i < indexes.Count; i += 3) { List triangle = new List() { vertices[indexes[i + 0]], vertices[indexes[i + 1]], vertices[indexes[i + 2]] }; list.Add(new CSG_Polygon(triangle)); } return list; } /** * Converts a CSG_Model to a Unity mesh. */ public Mesh ToMesh() { var mesh = new Mesh(); CSG_VertexUtility.SetMesh(mesh, vertices); mesh.triangles = indexes.ToArray(); return mesh; } } }