From 27c1a1b591ef07ffd8843995459d1c1397abc5b2 Mon Sep 17 00:00:00 2001 From: Henry de Jongh Date: Sun, 4 Nov 2018 12:58:57 +0100 Subject: [PATCH] Cleanup and documentation: Scripts/Core/CSG/Vertex.cs --- Scripts/Core/CSG/Vertex.cs | 297 ++++++++++++++++++++++++------------- 1 file changed, 197 insertions(+), 100 deletions(-) diff --git a/Scripts/Core/CSG/Vertex.cs b/Scripts/Core/CSG/Vertex.cs index 4ab08048..9d845352 100644 --- a/Scripts/Core/CSG/Vertex.cs +++ b/Scripts/Core/CSG/Vertex.cs @@ -1,107 +1,204 @@ -// Disable warnings for missing == and != operators -#pragma warning disable 0660 -#pragma warning disable 0661 - #if UNITY_EDITOR || RUNTIME_CSG + +using System; +using System.Collections.Generic; using UnityEngine; -using System.Collections; namespace Sabresaurus.SabreCSG { - [System.Serializable] - public class Vertex : IDeepCopyable - { - public Vector3 Position; // Vertex position - public Vector2 UV; - // public Vector2 UV2; // Second UV, i.e. lightmapping UVs - public Vector3 Normal; - // public Vector3 Tangent; // Optional, needed for some shaders - public Color32 Color = UnityEngine.Color.white; // Vertex colour, used for tinting individual verts - - public Vertex() {} - - public Vertex(Vector3 position, Vector3 normal, Vector2 uv) - { - this.Position = position; - this.UV = uv; - this.Normal = normal; - } - - public Vertex(Vector3 position, Vector3 normal, Vector2 uv, Color32 color) - { - this.Position = position; - this.UV = uv; - this.Normal = normal; - this.Color = color; - } - - public void FlipNormal() - { - // Reverses the normal - Normal = -Normal; - } - - public static Vertex Lerp(Vertex from, Vertex to, float t) - { - return new Vertex() - { - Position = Vector3.Lerp(from.Position, to.Position, t), - UV = Vector2.Lerp(from.UV, to.UV, t), - Normal = Vector3.Lerp(from.Normal, to.Normal, t), - Color = Color32.Lerp(from.Color, to.Color, t), - }; - } - - public static bool operator ==(Vertex lhs, Vertex rhs) - { - if(ReferenceEquals(lhs, rhs)) - { - return true; - } - else if(ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) - { - return false; - } - - return lhs.Position == rhs.Position && lhs.UV == rhs.UV && lhs.Normal == rhs.Normal && lhs.Color.Equals(rhs.Color); - } - - public static bool operator !=(Vertex lhs, Vertex rhs) - { - return !(lhs == rhs); -// return lhs.Position != rhs.Position || lhs.UV != rhs.UV || lhs.Normal != rhs.Normal || !lhs.Color.Equals(rhs.Color); - } - - // public overtexPolygonMappingsverride bool Equals(object obj) -// { -// if (obj is Vertex) -// { -// return this == (Vertex)obj; -// } -// else -// { -// return false; -// } -// } - - // public override int GetHashCode() - // { - // return base.GetHashCode(); - // } - - public Vertex DeepCopy() - { - return new Vertex(Position, Normal, UV, Color); - } - - public override string ToString () - { - return string.Format (string.Format("[Vertex] Pos: {0},{1},{2}", Position.x,Position.y,Position.z)); - } - } + /// + /// A vertex (plural vertices) describes the position of a point in 3D space. With additional + /// attributes for lighting, texturing and coloring. + /// + /// + /// + [Serializable] + public class Vertex : IDeepCopyable, IEquatable + { + /// The position of the . + public Vector3 Position; + + /// + /// The uv coordinates of the . Determines how a texture is mapped onto a + /// . + /// + public Vector2 UV; + + /// + /// The normal direction of the . Used in lighting calculations to + /// determine the direction a is facing. + /// + public Vector3 Normal; + + /// + /// The color of the . Shaders can use this color to tint the model. + /// + public Color32 Color = UnityEngine.Color.white; + + /// + /// Initializes a new instance of the class. + /// + public Vertex() { } + + /// + /// Initializes a new instance of the class. + /// + /// The position. + /// The normal. + /// The uv coordinates. + public Vertex(Vector3 position, Vector3 normal, Vector2 uv) + { + Position = position; + UV = uv; + Normal = normal; + } + + /// + /// Initializes a new instance of the class. + /// + /// The position. + /// The normal. + /// The uv coordinates. + /// The tinting color. + public Vertex(Vector3 position, Vector3 normal, Vector2 uv, Color32 color) + { + Position = position; + UV = uv; + Normal = normal; + Color = color; + } + + /// + /// Flips the direction. + /// + public void FlipNormal() + { + Normal = -Normal; + } + + /// + /// Linearly interpolates between two vertices. + /// + /// Note that all of the vertex components (i.e. , , + /// , ) will be lerped. + /// + /// + /// The initial . + /// The final . + /// + /// When = 0 returns . When + /// = 1 returns . When = 0.5 returns the vertex + /// midway between and . + /// + /// The new interpolated . + /// + /// Thrown when or is null. + /// + public static Vertex Lerp(Vertex a, Vertex b, float t) + { + if (a == null) throw new ArgumentNullException("a"); + if (b == null) throw new ArgumentNullException("b"); + + return new Vertex() + { + Position = Vector3.Lerp(a.Position, b.Position, t), + UV = Vector2.Lerp(a.UV, b.UV, t), + Normal = Vector3.Lerp(a.Normal, b.Normal, t), + Color = Color32.Lerp(a.Color, b.Color, t), + }; + } + + /// + /// Creates a deep copy of the . Returs a new instance of a with the same value as this instance. + /// + /// The newly created copy with the same values. + public Vertex DeepCopy() + { + return new Vertex(Position, Normal, UV, Color); + } + + /// + /// Returns a that represents this . + /// + /// A that represents this . + public override string ToString() + { + return string.Format(string.Format("[Vertex] Pos: {0},{1},{2}", Position.x, Position.y, Position.z)); + } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; + /// otherwise, false. + /// + public override bool Equals(object obj) + { + return Equals(obj as Vertex); + } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public bool Equals(Vertex other) + { + return other != null && + EqualityComparer.Default.Equals(Position, other.Position) && + EqualityComparer.Default.Equals(UV, other.UV) && + EqualityComparer.Default.Equals(Normal, other.Normal) && + EqualityComparer.Default.Equals(Color, other.Color); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures + /// like a hash table. + /// + public override int GetHashCode() + { + return base.GetHashCode(); + } + + /// + /// True if the specified vertices are equal; otherwise, false. + /// + /// The left hand side operator. + /// The right hand side operator. + /// true if the specified vertices are equal; otherwise, false. + public static bool operator ==(Vertex lhs, Vertex rhs) + { + if (ReferenceEquals(lhs, rhs)) + { + return true; + } + else if (ReferenceEquals(lhs, null) || ReferenceEquals(rhs, null)) + { + return false; + } + + return lhs.Position == rhs.Position && lhs.UV == rhs.UV && lhs.Normal == rhs.Normal && lhs.Color.Equals(rhs.Color); + } + + /// + /// True if the specified vertices not are equal; otherwise, false. + /// + /// The left hand side operator. + /// The right hand side operator. + /// true if the specified vertices are not equal; otherwise, false. + public static bool operator !=(Vertex lhs, Vertex rhs) + { + return !(lhs == rhs); + } + } } -#endif -// Disable warnings for missing == and != operators -#pragma warning restore 0660 -#pragma warning restore 0661 \ No newline at end of file +#endif \ No newline at end of file