You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Objective
There was issue #191 requesting subdivisions on the shape::Plane.
I also could have used this recently. I then write the solution.
Fixes#191
## Solution
I changed the shape::Plane to include subdivisions field and the code to create the subdivisions. I don't know how people are counting subdivisions so as I put in the doc comments 0 subdivisions results in the original geometry of the Plane.
Greater then 0 results in the number of lines dividing the plane.
I didn't know if it would be better to create a new struct that implemented this feature, say SubdivisionPlane or change Plane. I decided on changing Plane as that was what the original issue was.
It would be trivial to alter this to use another struct instead of altering Plane.
The issues of migration, although small, would be eliminated if a new struct was implemented.
## Changelog
### Added
Added subdivisions field to shape::Plane
## Migration Guide
All the examples needed to be updated to initalize the subdivisions field.
Also there were two tests in tests/window that need to be updated.
A user would have to update all their uses of shape::Plane to initalize the subdivisions field.
Copy file name to clipboardExpand all lines: crates/bevy_render/src/mesh/shape/mod.rs
+58-15
Original file line number
Diff line number
Diff line change
@@ -187,33 +187,76 @@ impl From<Quad> for Mesh {
187
187
pubstructPlane{
188
188
/// The total side length of the square.
189
189
pubsize:f32,
190
+
/// The number of subdivisions in the mesh.
191
+
///
192
+
/// 0 - is the original plane geometry, the 4 points in the XZ plane.
193
+
///
194
+
/// 1 - is split by 1 line in the middle of the plane on both the X axis and the Z axis, resulting in a plane with 4 quads / 8 triangles.
195
+
///
196
+
/// 2 - is a plane split by 2 lines on both the X and Z axes, subdividing the plane into 3 equal sections along each axis, resulting in a plane with 9 quads / 18 triangles.
197
+
///
198
+
/// and so on...
199
+
pubsubdivisions:u32,
190
200
}
191
201
192
202
implDefaultforPlane{
193
203
fndefault() -> Self{
194
-
Plane{size:1.0}
204
+
Plane{
205
+
size:1.0,
206
+
subdivisions:0,
207
+
}
208
+
}
209
+
}
210
+
211
+
implPlane{
212
+
/// Creates a new plane centered at the origin with the supplied side length and zero subdivisions.
213
+
pubfnfrom_size(size:f32) -> Self{
214
+
Self{
215
+
size,
216
+
subdivisions:0,
217
+
}
195
218
}
196
219
}
197
220
198
221
implFrom<Plane>forMesh{
199
222
fnfrom(plane:Plane) -> Self{
200
-
let extent = plane.size / 2.0;
201
-
202
-
let vertices = [
203
-
([extent,0.0, -extent],[0.0,1.0,0.0],[1.0,1.0]),
204
-
([extent,0.0, extent],[0.0,1.0,0.0],[1.0,0.0]),
205
-
([-extent,0.0, extent],[0.0,1.0,0.0],[0.0,0.0]),
206
-
([-extent,0.0, -extent],[0.0,1.0,0.0],[0.0,1.0]),
207
-
];
208
-
209
-
let indices = Indices::U32(vec![0,2,1,0,3,2]);
223
+
// here this is split in the z and x directions if one ever needs asymetrical subdivision
224
+
// two Plane struct fields would need to be added instead of the single subdivisions field
225
+
let z_vertex_count = plane.subdivisions + 2;
226
+
let x_vertex_count = plane.subdivisions + 2;
227
+
let num_vertices = (z_vertex_count * x_vertex_count)asusize;
228
+
let num_indices = ((z_vertex_count - 1)*(x_vertex_count - 1)*6)asusize;
0 commit comments