Skip to content
This repository has been archived by the owner on Feb 11, 2024. It is now read-only.

Create polygons from Vector3s (Updated) #64

Merged
merged 2 commits into from
Mar 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Scripts/Core/CSG/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System;

namespace Sabresaurus.SabreCSG
{
Expand Down Expand Up @@ -107,6 +108,36 @@ public Polygon(Vertex[] vertices, Material material, bool isTemporary, bool user
CalculatePlane();
}

/// <summary>
/// Initializes a new instance of the <see cref="Polygon"/> class.
/// </summary>
/// <param name="vertices">The vertex positions of a <see cref="Vertex"/> that make up this polygonal shape. Normals and UVs will be zero (see <see cref="ResetVertexNormals"/> and <see cref="GenerateUvCoordinates"/> to generate them automatically).</param>
/// <param name="material">The Unity <see cref="UnityEngine.Material"/> applied to the surface of this polygon.</param>
/// <param name="isTemporary">If set to <c>true</c> 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 <c>false</c>, also see <paramref name="userExcludeFromFinal"/>).</param>
/// <param name="userExcludeFromFinal">If set to <c>true</c> the user requested that this polygon be excluded from the final CSG build (i.e. not rendered, it does affect CSG operations).</param>
/// <param name="uniqueIndex">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.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="vertices"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException">A polygon must have at least 3 vertices.</exception>
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);
Expand Down Expand Up @@ -402,6 +433,21 @@ public void RemoveExtraneousVertices()
}
}

/// <summary>
/// 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.
/// <para>You may have to call <see cref="CalculatePlane"/> first if you modified the polygon.</para>
/// </summary>
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;
Expand Down