Skip to content

Commit b693b55

Browse files
committed
Set xz size extensions
1 parent 065f915 commit b693b55

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Scripts/Extensions/MeshExtensions.cs

+40
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ public static Mesh SetXSize(this Mesh source, float min, float max)
7676
return source.SetXSize(min, max, Vector3.one / 2f);
7777
}
7878

79+
/// <summary>
80+
/// Sets vertices from min to max value on the x and z axis
81+
/// </summary>
82+
public static Mesh SetXZSize(this Mesh source, Vector2 min, Vector2 max)
83+
{
84+
return source.SetXZSize(min, max, Vector3.one / 2f);
85+
}
86+
7987
/// <summary>
8088
/// Sets vertices from min to max value on the x axis
8189
/// </summary>
@@ -104,6 +112,38 @@ public static Mesh SetXSize(this Mesh source, float min, float max, Vector3 norm
104112
return mesh;
105113
}
106114

115+
/// <summary>
116+
/// Sets vertices from min to max value on the x axis
117+
/// </summary>
118+
public static Mesh SetXZSize(this Mesh source, Vector2 min, Vector2 max, Vector3 normalizedCenter)
119+
{
120+
var mesh = source.Clone();
121+
var bounds = mesh.bounds;
122+
var vertices = mesh.vertices;
123+
var boundsCenter = (bounds.min + bounds.size.Multiply(normalizedCenter));
124+
// var originalLeftSize = boundsCenterX - bounds.min.x;
125+
// var originalRightSize = bounds.max.x - boundsCenterX;
126+
127+
var leftOffsetX = min.x - bounds.min.x;
128+
var rightOffsetX = max.x - bounds.max.x;
129+
130+
var leftOffsetZ = min.y - bounds.min.z;
131+
var rightOffsetZ = max.y - bounds.max.z;
132+
133+
for (var i = 0; i < vertices.Length; i++)
134+
{
135+
var vert = vertices[i];
136+
var x = vert.x < boundsCenter.x ? vert.x + leftOffsetX : vert.x + rightOffsetX;
137+
var z = vert.z < boundsCenter.z ? vert.z + leftOffsetZ : vert.z + rightOffsetZ;
138+
vertices[i] = new Vector3(x, vert.y, z);
139+
}
140+
141+
mesh.vertices = vertices;
142+
mesh.RecalculateBounds();
143+
144+
return mesh;
145+
}
146+
107147
/// <summary>
108148
/// Moves vertices from the center to each direction
109149
/// </summary>

0 commit comments

Comments
 (0)