-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGeometry.cs
More file actions
234 lines (191 loc) · 6.04 KB
/
Geometry.cs
File metadata and controls
234 lines (191 loc) · 6.04 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using UnityEngine;
using Forge.Extensions;
using System.Collections.Generic;
namespace Forge {
public enum Surface {None, Triangulate, Converge};
public struct Geometry {
public Vector3[] Vertices;
public Vector3[] Normals;
public Vector4[] Tangents;
public Vector2[] UV;
public int[] Triangles;
public int[] Polygons;
public const float TAU = Mathf.PI * 2;
public static Geometry Empty {
get {
return new Geometry() {
Vertices = new Vector3[0],
Normals = new Vector3[0],
Tangents = new Vector4[0],
UV = new Vector2[0],
Triangles = new int[0],
Polygons = new int[0]
};
}
}
public Geometry(int vertexCount, int faceCount=0, int polyCount=0) {
Vertices = new Vector3[vertexCount];
Normals = new Vector3[vertexCount];
Tangents = new Vector4[vertexCount];
UV = new Vector2[vertexCount];
Triangles = new int[faceCount];
Polygons = new int[polyCount];
}
public Geometry Copy() {
Geometry geometry = new Geometry();
if (Vertices != null) {
Vector3[] vertices = new Vector3[Vertices.Length];
System.Array.Copy(Vertices, vertices, Vertices.Length);
geometry.Vertices = vertices;
}
if (Normals != null) {
Vector3[] normals = new Vector3[Normals.Length];
System.Array.Copy(Normals, normals, Normals.Length);
geometry.Normals = normals;
}
if (Tangents != null) {
Vector4[] tangents = new Vector4[Tangents.Length];
System.Array.Copy(Tangents, tangents, Tangents.Length);
geometry.Tangents = tangents;
}
if (UV != null) {
Vector2[] uv = new Vector2[UV.Length];
System.Array.Copy(UV, uv, UV.Length);
geometry.UV = uv;
}
if (Triangles != null) {
int[] triangles = new int[Triangles.Length];
System.Array.Copy(Triangles, triangles, Triangles.Length);
geometry.Triangles = triangles;
}
if (Polygons != null) {
int[] polygons = new int[Polygons.Length];
System.Array.Copy(Polygons, polygons, Polygons.Length);
geometry.Polygons = polygons;
}
return geometry;
}
public void RecalculateNormals() {
Normals = new Vector3[Vertices.Length];
for (int i = 0; i < Vertices.Length; i++) {
Normals[i] = CalculateNormal(i);
}
}
public void Offset(Vector3 offset) {
if (offset.magnitude != 0f) {
for (int i = 0; i < Vertices.Length; i++) {
Vertices[i] = Vertices[i] + offset;
}
}
}
public Vector3 CalculateNormal(int vertexIndex) {
HashSet<Vector3> normalSet = new HashSet<Vector3>();
int[] faces = FacesSharingVertex(vertexIndex);
for (int i = 0; i < faces.Length; i++) {
normalSet.Add(FaceNormal(faces[i]));
}
Vector3 sum = Vector3.zero;
foreach (Vector3 normal in normalSet) {
sum += normal;
}
return (sum / normalSet.Count).normalized;
}
public int[] FacesSharingVertex(int vertexIndex) {
if (Triangles == null) return new int[0];
List<int> faces = new List<int>();
for (int i = 0; i < Triangles.Length; i++) {
if (Triangles[i] == vertexIndex) {
faces.Add(Mathf.FloorToInt(i / 3));
}
}
return faces.ToArray();
}
public Vector3 FaceNormal(int faceIndex) {
Vector3 a = Vertices[Triangles[faceIndex * 3]];
Vector3 b = Vertices[Triangles[faceIndex * 3 + 1]];
Vector3 c = Vertices[Triangles[faceIndex * 3 + 2]];
Vector3 normal = Vector3.Cross(b-a, c-a).normalized;
return normal;
}
public Vector3 AxisVariance() {
if (Vertices.Length == 0) return Vector3.zero;
Vector3 delta = Vector3.zero;
Vector3 v0 = Vector3.zero;
for (int i = 0; i < Vertices.Length; i++) {
if (i > 0) {
delta += (v0 - Vertices[i]).Abs();
}
v0 = Vertices[i];
}
return delta;
}
public static bool IsCoplanar(Vector3 axisVariance) {
Vector3 v = axisVariance;
return v.x == 0f || v.y == 0f || v.z == 0f;
}
public bool IsCoplanar() { return IsCoplanar(AxisVariance()); }
public static int InvariantAxis(Vector3 axisVariance) {
Vector3 v = axisVariance;
if (v.x == 0f && v.y != 0f && v.z != 0f) return 0;
if (v.x != 0f && v.y == 0f && v.z != 0f) return 1;
if (v.x != 0f && v.y != 0f && v.z == 0f) return 2;
return -1;
}
public int InvariantAxis() { return InvariantAxis(AxisVariance()); }
public static Vector2 PlanarCoordinates(int invariantAxis, Vector3 point) {
switch (invariantAxis) {
case 0: return new Vector2(point.y, point.z);
case 1: return new Vector2(point.x, point.z);
case 2: return new Vector2(point.x, point.y);
default: return Vector2.zero;
}
}
public Vector2 PlanarCoordinates(Vector3 point) {
return PlanarCoordinates(InvariantAxis(AxisVariance()), point);
}
// Minimum bounding box value in the axis
public float Min(Axis axis) {
float min = Mathf.Infinity;
for (int i = 0; i < Vertices.Length; i++) {
if (Vertices[i][(int)axis] < min) min = Vertices[i][(int)axis];
}
return min;
}
// Maximum bounding box value in the axis
public float Max(Axis axis) {
float max = -Mathf.Infinity;
for (int i = 0; i < Vertices.Length; i++) {
if (Vertices[i][(int)axis] > max) max = Vertices[i][(int)axis];
}
return max;
}
// Span of the bounding box in the axis
public float Span(Axis axis) {
return Max(axis) - Min(axis);
}
public Vector3 Centroid() {
Vector3 sum = Vector3.zero;
for (int i = 0; i < Vertices.Length; i++) {
sum += Vertices[i];
}
return sum / Vertices.Length;
}
public Vector2 PlanarCentroid(Axis ignoredAxis) {
Vector2 sum = Vector2.zero;
for (int i = 0; i < Vertices.Length; i++) {
sum += PlanarCoordinates((int)ignoredAxis, Vertices[i]);
}
return sum / Vertices.Length;
}
public override string ToString() {
return System.String.Format("vert:{0}, nor:{1}, tan:{2} uv:{3}, tri:{4} poly:{5}",
Vertices != null ? Vertices.Length.ToString() : "-",
Normals != null ? Normals.Length.ToString() : "-",
Tangents != null ? Tangents.Length.ToString() : "-",
UV != null ? UV.Length.ToString() : "-",
Triangles != null ? (Triangles.Length / 3).ToString() : "-",
Polygons != null ? (Polygons.Length / 2).ToString() : "-"
);
}
}
}