Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - added subdivisions to shape::Plane #7546

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions crates/bevy_render/src/mesh/shape/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,61 @@ impl From<Quad> for Mesh {
pub struct Plane {
/// The total side length of the square.
pub size: f32,
/// The number of subdivisions in the mesh.
/// 0 is the original plane geometry.
/// 1 is one line in both the X direction and the Z direction splitting the plane resulting in a plane with 4 quads / 8 triangles.
/// > 1 is multiple splits.
pub subdivisions: u32,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These doc strings aren't totally clear about the number of segments produced at higher subdivisions.

My intuition would be that this number reflects the number of evenly sized segments the plane contains.

Copy link
Member

@mockersf mockersf Feb 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(subdivisions + 1)^2

}

impl Default for Plane {
fn default() -> Self {
Plane { size: 1.0 }
Plane {
size: 1.0,
subdivisions: 0,
}
}
}

impl From<Plane> for Mesh {
fn from(plane: Plane) -> Self {
let extent = plane.size / 2.0;

let vertices = [
([extent, 0.0, -extent], [0.0, 1.0, 0.0], [1.0, 1.0]),
([extent, 0.0, extent], [0.0, 1.0, 0.0], [1.0, 0.0]),
([-extent, 0.0, extent], [0.0, 1.0, 0.0], [0.0, 0.0]),
([-extent, 0.0, -extent], [0.0, 1.0, 0.0], [0.0, 1.0]),
];

let indices = Indices::U32(vec![0, 2, 1, 0, 3, 2]);
// here this is split in the z and x directions if one ever needs asymetrical subdivision
// two Plane struct fields would need to be added instead of the single subdivisions field
let z_vertex_count = plane.subdivisions + 2;
let x_vertex_count = plane.subdivisions + 2;
let num_vertices = (z_vertex_count * x_vertex_count) as usize;
let num_indices = ((z_vertex_count - 1) * (x_vertex_count - 1) * 6) as usize;
let up = Vec3::Y.to_array();

let mut positions: Vec<[f32; 3]> = Vec::with_capacity(num_vertices);
let mut normals: Vec<[f32; 3]> = Vec::with_capacity(num_vertices);
let mut uvs: Vec<[f32; 2]> = Vec::with_capacity(num_vertices);
let mut indices: Vec<u32> = Vec::with_capacity(num_indices);

for y in 0..z_vertex_count {
for x in 0..x_vertex_count {
let tx = x as f32 / (x_vertex_count - 1) as f32;
let ty = y as f32 / (z_vertex_count - 1) as f32;
positions.push([(-0.5 + tx) * plane.size, 0.0, (-0.5 + ty) * plane.size]);
normals.push(up);
uvs.push([tx, 1.0 - ty]);
}
}

let positions: Vec<_> = vertices.iter().map(|(p, _, _)| *p).collect();
let normals: Vec<_> = vertices.iter().map(|(_, n, _)| *n).collect();
let uvs: Vec<_> = vertices.iter().map(|(_, _, uv)| *uv).collect();
for y in 0..z_vertex_count - 1 {
for x in 0..x_vertex_count - 1 {
let quad = y * x_vertex_count + x;
indices.push(quad + x_vertex_count + 1);
indices.push(quad + 1);
indices.push(quad + x_vertex_count);
indices.push(quad);
indices.push(quad + x_vertex_count);
indices.push(quad + 1);
}
}

let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.set_indices(Some(indices));
mesh.set_indices(Some(Indices::U32(indices)));
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ fn setup(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
8 changes: 7 additions & 1 deletion examples/3d/3d_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ fn setup(

// ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane { size: 50. }.into()),
mesh: meshes.add(
shape::Plane {
size: 50.,
subdivisions: 0,
}
.into(),
),
material: materials.add(Color::SILVER.into()),
..default()
});
Expand Down
8 changes: 7 additions & 1 deletion examples/3d/blend_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ fn setup(
// Chessboard Plane
let black_material = materials.add(Color::BLACK.into());
let white_material = materials.add(Color::WHITE.into());
let plane_mesh = meshes.add(shape::Plane { size: 2.0 }.into());
let plane_mesh = meshes.add(
shape::Plane {
size: 2.0,
subdivisions: 0,
}
.into(),
);

for x in -3..4 {
for z in -3..4 {
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/fxaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ fn setup(

// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ fn setup(
) {
// ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 10.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 10.0,
subdivisions: 0,
})),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
perceptual_roughness: 1.0,
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/orthographic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ fn setup(

// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
1 change: 1 addition & 0 deletions examples/3d/shadow_biases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn setup(
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane {
size: 2.0 * spawn_plane_depth,
subdivisions: 0,
})),
material: white_handle,
..default()
Expand Down
10 changes: 8 additions & 2 deletions examples/3d/shadow_caster_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ fn setup(
// floating plane - initially not a shadow receiver and not a caster
commands.spawn((
PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 20.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 20.0,
subdivisions: 0,
})),
material: materials.add(Color::GREEN.into()),
transform: Transform::from_xyz(0.0, 1.0, -10.0),
..default()
Expand All @@ -78,7 +81,10 @@ fn setup(

// lower ground plane - initially a shadow receiver
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 20.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 20.0,
subdivisions: 0,
})),
material: white_handle,
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/spherical_area_lights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ fn setup(

// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 100.0,
subdivisions: 0,
})),
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.2, 0.2, 0.2),
perceptual_roughness: 0.08,
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/split_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ fn setup(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 100.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ fn setup(
) {
// ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 100.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 100.0,
subdivisions: 0,
})),
material: materials.add(StandardMaterial {
base_color: Color::GREEN,
perceptual_roughness: 1.0,
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/transparency_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ fn setup(
) {
// opaque plane, uses `alpha_mode: Opaque` by default
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 6.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 6.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/two_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ fn setup(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/vertex_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ fn setup(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/3d/wireframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ fn setup(
wireframe_config.global = false;
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/animation/animated_fox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ fn setup(

// Plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 500000.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 500000.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/mobile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ fn setup_scene(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.1, 0.2, 0.1).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/shader/shader_material_screenspace_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ fn setup(
mut standard_materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: standard_materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
8 changes: 7 additions & 1 deletion examples/shader/shader_prepass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ fn setup(

// plane
commands.spawn(PbrBundle {
mesh: meshes.add(shape::Plane { size: 5.0 }.into()),
mesh: meshes.add(
shape::Plane {
size: 5.0,
subdivisions: 0,
}
.into(),
),
material: std_materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion examples/stress_tests/many_foxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ fn setup(

// Plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5000.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5000.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion tests/window/minimising.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ fn setup_3d(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down
5 changes: 4 additions & 1 deletion tests/window/resizing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ fn setup_3d(
) {
// plane
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
});
Expand Down