diff --git a/Scripts/Core/CSG/Polygon.cs b/Scripts/Core/CSG/Polygon.cs index 0603629f..fec8e651 100644 --- a/Scripts/Core/CSG/Polygon.cs +++ b/Scripts/Core/CSG/Polygon.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Reflection; +using System; namespace Sabresaurus.SabreCSG { @@ -107,6 +108,36 @@ public Polygon(Vertex[] vertices, Material material, bool isTemporary, bool user CalculatePlane(); } + /// + /// Initializes a new instance of the class. + /// + /// The vertex positions of a that make up this polygonal shape. Normals and UVs will be zero (see and to generate them automatically). + /// The Unity applied to the surface of this polygon. + /// If set to true excludes the polygon from the final CSG build, it's only temporarily created during the build process to determine whether a point is inside/outside of a convex chunk (usually you set this argument to false, also see ). + /// If set to true the user requested that this polygon be excluded from the final CSG build (i.e. not rendered, it does affect CSG operations). + /// When a polygon is split or cloned, this number is preserved inside of those new polygons so they can track where they originally came from. + /// Thrown when is null. + /// A polygon must have at least 3 vertices. + public Polygon(Vector3[] vertices, Material material, bool isTemporary, bool userExcludeFromFinal, int uniqueIndex = -1) + { + if (vertices == null) throw new ArgumentNullException("vertices"); + if (vertices.Length < 3) throw new ArgumentOutOfRangeException("A polygon must have at least 3 vertices."); + // consideration: check array for null elements? + + // create vertices from the vector3 array. + this.vertices = new Vertex[vertices.Length]; + for (int i = 0; i < vertices.Length; i++) + this.vertices[i] = new Vertex(vertices[i], Vector3.zero, Vector2.zero); + + this.material = material; + this.uniqueIndex = uniqueIndex; + this.excludeFromFinal = isTemporary; + this.userExcludeFromFinal = userExcludeFromFinal; + + // calculate the cached plane. + CalculatePlane(); + } + public Polygon DeepCopy() { return new Polygon(this.vertices.DeepCopy(), this.material, this.excludeFromFinal, this.userExcludeFromFinal, this.uniqueIndex); @@ -402,6 +433,21 @@ public void RemoveExtraneousVertices() } } + /// + /// Generates the UV coordinates for this polygon automatically. This works similarly to the + /// "AutoUV" button in the surface editor. This method may throw warnings in the console if + /// the normal of the polygon is zero. + /// You may have to call first if you modified the polygon. + /// + public void GenerateUvCoordinates() + { + // stolen code from the surface editor "AutoUV". + Quaternion cancellingRotation = Quaternion.Inverse(Quaternion.LookRotation(-Plane.normal)); + // sets the uv at each point to the position on the plane. + for (int i = 0; i < vertices.Length; i++) + vertices[i].UV = (cancellingRotation * vertices[i].Position) * 0.5f; + } + public static bool SplitPolygon(Polygon polygon, out Polygon frontPolygon, out Polygon backPolygon, out Vertex newVertex1, out Vertex newVertex2, UnityEngine.Plane clipPlane) { newVertex1 = null;