-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vert3.cs
152 lines (138 loc) · 4.65 KB
/
Vert3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Runtime.InteropServices;
using System.Text;
/// <summary>
/// Organizes the components of a 3D mesh into a group of coordinate, normal and
/// texture coordinate such that they can be edited together. This is not used
/// by a mesh internally, it is created upon retrieval from a mesh.
/// </summary>
[Serializable]
[StructLayout(LayoutKind.Explicit, Pack = 32)]
public readonly struct Vert3 : IComparable<Vert3>, IEquatable<Vert3>
{
/// <summary>
/// The coordinate of the vertex in world space.
/// </summary>
[FieldOffset(0)] private readonly Vec3 coord;
/// <summary>
/// The direction in which light will bounce from the surface of the mesh at
/// the vertex.
/// </summary>
[FieldOffset(20)] private readonly Vec3 normal;
/// <summary>
/// The texture (UV) coordinate for an image mapped onto the mesh.
/// </summary>
[FieldOffset(12)] private readonly Vec2 texCoord;
/// <summary>
/// The coordinate of the vertex in world space.
/// </summary>
/// <value>coord</value>
public Vec3 Coord { get { return this.coord; } }
/// <summary>
/// The direction in which light will bounce from the surface of the mesh at
/// the vertex.
/// </summary>
/// <value>normal</value>
public Vec3 Normal { get { return this.normal; } }
/// <summary>
/// The texture (UV) coordinate for an image mapped onto the mesh.
/// </summary>
/// <value>texture coordinate</value>
public Vec2 TexCoord { get { return this.texCoord; } }
/// <summary>
/// Constructs a vertex from a coordinate, texture coordinate and normal.
/// </summary>
/// <param name="coord">coordinate</param>
/// <param name="texCoord">texture coordinate</param>
/// <param name="normal">normal</param>
public Vert3(in Vec3 coord, in Vec2 texCoord, in Vec3 normal)
{
this.coord = coord;
this.texCoord = texCoord;
this.normal = normal;
}
/// <summary>
/// Tests this vertex for equivalence with an object.
/// </summary>
/// <param name="value">the object</param>
/// <returns>equivalence</returns>
public override bool Equals(object value)
{
if (Object.ReferenceEquals(this, value)) { return true; }
if (value is null) { return false; }
if (value is Vert3 vert) { return this.Equals(vert); }
return false;
}
/// <summary>
/// Returns a hash code representing this vertex.
/// </summary>
/// <returns>hash code</returns>
public override int GetHashCode()
{
unchecked
{
return ((Utils.MulBase ^ this.coord.GetHashCode()) *
Utils.HashMul ^ this.texCoord.GetHashCode()) *
Utils.HashMul ^ this.normal.GetHashCode();
}
}
/// <summary>
/// Returns a string representation of this vertex.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Vert3.ToString(this);
}
/// <summary>
/// Compares this vertex to another in compliance with the IComparable
/// interface.
/// </summary>
/// <param name="v">comparisand</param>
/// <returns>evaluation</returns>
public int CompareTo(Vert3 v)
{
return this.coord.CompareTo(v.coord);
}
/// <summary>
/// Tests this vertex for equivalence with another in compliance with the
/// IEquatable interface.
/// </summary>
/// <param name="v">vertex</param>
/// <returns>equivalence</returns>
public bool Equals(Vert3 v)
{
return this.coord.Equals(v.coord);
}
/// <summary>
/// Returns a string representation of a vertex.
/// </summary>
/// <param name="v">vertex</param>
/// <param name="places">number of decimal places</param>
/// <returns>string</returns>
public static string ToString(in Vert3 v, in int places = 4)
{
return Vert3.ToString(new StringBuilder(256), v, places).ToString();
}
/// <summary>
/// Appends a representation of an vertex to a string builder.
/// </summary>
/// <param name="sb">string builder</param>
/// <param name="v">vertex</param>
/// <param name="places">number of decimal places</param>
/// <returns>string builder</returns>
public static StringBuilder ToString(
in StringBuilder sb,
in Vert3 v,
in int places = 4)
{
sb.Append("{\"coord\":");
Vec3.ToString(sb, v.coord, places);
sb.Append(",\"texCoord\":");
Vec2.ToString(sb, v.texCoord, places);
sb.Append(",\"normal\":");
Vec3.ToString(sb, v.normal, places);
sb.Append("}");
return sb;
}
}