forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshE.cs
209 lines (198 loc) · 6.45 KB
/
MeshE.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
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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Mesh extensions
/// </summary>
public static class MeshE
{
/// <summary>
/// Moves mesh vertices by <paramref name="vector"/>
/// </summary>
public static void Move(this Mesh mesh, Vector3 vector)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] += vector;
}
mesh.vertices = vertices;
}
/// <summary>
/// Rotates mesh vertices by <paramref name="rotation"/>
/// </summary>
public static void Rotate(this Mesh mesh, Quaternion rotation)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var vertices = mesh.vertices;
var normals = mesh.normals;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = rotation*vertices[i];
normals[i] = rotation*normals[i];
}
mesh.vertices = vertices;
mesh.normals = normals;
}
/// <summary>
/// Scales mesh vertices uniformly by <paramref name="scale"/>
/// </summary>
public static void Scale(this Mesh mesh, float scale)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var vertices = mesh.vertices;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] *= scale;
}
mesh.vertices = vertices;
}
/// <summary>
/// Scales mesh vertices non-uniformly by <paramref name="scale"/>
/// </summary>
public static void Scale(this Mesh mesh, Vector3 scale)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var vertices = mesh.vertices;
var normals = mesh.normals;
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = Vector3.Scale(vertices[i], scale);
normals[i] = Vector3.Scale(normals[i], scale).normalized;
}
mesh.vertices = vertices;
mesh.normals = normals;
}
/// <summary>
/// Paints mesh vertices with <paramref name="color"/>
/// </summary>
public static void Paint(this Mesh mesh, Color color)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var colors = new Color[mesh.vertexCount];
for (int i = 0; i < mesh.vertexCount; i++)
{
colors[i] = color;
}
mesh.colors = colors;
}
/// <summary>
/// Flips mesh faces
/// </summary>
public static void FlipFaces(this Mesh mesh)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
mesh.FlipTriangles();
mesh.FlipNormals();
}
/// <summary>
/// Reverses the winding order of mesh triangles
/// </summary>
public static void FlipTriangles(this Mesh mesh)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
for (int i = 0; i < mesh.subMeshCount; i++)
{
var triangles = mesh.GetTriangles(i);
for (int j = 0; j < triangles.Length; j += 3)
{
PTUtils.Swap(ref triangles[j], ref triangles[j + 1]);
}
mesh.SetTriangles(triangles, i);
}
}
/// <summary>
/// Reverses the direction of mesh normals
/// </summary>
public static void FlipNormals(this Mesh mesh)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var normals = mesh.normals;
for (int i = 0; i < normals.Length; i++)
{
normals[i] = -normals[i];
}
mesh.normals = normals;
}
/// <summary>
/// Flips the UV map horizontally in the selected <paramref name="channel"/>
/// </summary>
public static void FlipUVHorizontally(this Mesh mesh, int channel = 0)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var list = new List<Vector2>();
mesh.GetUVs(channel, list);
for (var i = 0; i < list.Count; i++)
{
list[i] = new Vector2(1 - list[i].x, list[i].y);
}
mesh.SetUVs(channel, list);
}
/// <summary>
/// Flips the UV map vertically in the selected <paramref name="channel"/>
/// </summary>
public static void FlipUVVertically(this Mesh mesh, int channel = 0)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var list = new List<Vector2>();
mesh.GetUVs(channel, list);
for (var i = 0; i < list.Count; i++)
{
list[i] = new Vector2(list[i].x, 1 - list[i].y);
}
mesh.SetUVs(channel, list);
}
/// <summary>
/// Projects vertices on a sphere with the given <paramref name="radius"/> and <paramref name="center"/>, recalculates normals
/// </summary>
public static void Spherify(this Mesh mesh, float radius, Vector3 center = default)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
var vertices = mesh.vertices;
var normals = mesh.normals;
for (var i = 0; i < vertices.Length; i++)
{
normals[i] = (vertices[i] - center).normalized;
vertices[i] = normals[i]*radius;
}
mesh.vertices = vertices;
mesh.normals = normals;
}
}
}