Skip to content

Commit 14265d0

Browse files
committed
Extend mesh utils
1 parent 6c93905 commit 14265d0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

Scripts/Extensions/MeshExtensions.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,69 @@ public static Mesh ExtendMesh(this Mesh source, Vector3 minExtends, Vector3 maxE
6868
return mesh;
6969
}
7070

71+
/// <summary>
72+
/// Sets vertices that are bigger than center x size
73+
/// </summary>
74+
public static Mesh SetXSizeRight(this Mesh source, float size)
75+
{
76+
return source.SetXSizeRight(size, Vector3.one/2f);
77+
}
78+
79+
/// <summary>
80+
/// Sets vertices that are bigger than center x size
81+
/// </summary>
82+
public static Mesh SetXSizeRight(this Mesh source, float size, Vector3 normalizedCenter)
83+
{
84+
var mesh = source.Clone();
85+
var bounds = mesh.bounds;
86+
var vertices = mesh.vertices;
87+
var boundsCenter = bounds.min + bounds.size.Multiply(normalizedCenter);
88+
var offset = size - bounds.size.x;
89+
90+
for (var i = 0; i < vertices.Length; i++)
91+
{
92+
var vert = vertices[i];
93+
var x = vert.x < boundsCenter.x ? vert.x - offset : vert.x;
94+
vertices[i] = new Vector3(x, vert.y, vert.z);
95+
}
96+
97+
mesh.vertices = vertices;
98+
mesh.RecalculateBounds();
99+
100+
return mesh;
101+
}
102+
103+
/// <summary>
104+
/// Sets vertices that are bigger than center x size
105+
/// </summary>
106+
public static Mesh SetXSizeLeft(this Mesh source, float size)
107+
{
108+
return source.SetXSizeLeft(size, Vector3.one/2f);
109+
}
110+
111+
/// <summary>
112+
/// Sets vertices that are bigger than center x size
113+
/// </summary>
114+
public static Mesh SetXSizeLeft(this Mesh source, float size, Vector3 normalizedCenter)
115+
{
116+
var mesh = source.Clone();
117+
var bounds = mesh.bounds;
118+
var vertices = mesh.vertices;
119+
var boundsCenter = bounds.min + bounds.size.Multiply(normalizedCenter);
120+
var offset = size - bounds.size.x;
121+
122+
for (var i = 0; i < vertices.Length; i++)
123+
{
124+
var vert = vertices[i];
125+
var x = vert.x > boundsCenter.x ? vert.x + offset : vert.x;
126+
vertices[i] = new Vector3(x, vert.y, vert.z);
127+
}
128+
129+
mesh.vertices = vertices;
130+
mesh.RecalculateBounds();
131+
132+
return mesh;
133+
}
71134

72135
/// <summary>
73136
/// Moves vertices from the center to each direction

0 commit comments

Comments
 (0)