mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
cf15e6bba3
# Objective Split up from #11007, fixing most of the remaining work for #10569. Implement `Meshable` for `Cuboid`, `Sphere`, `Cylinder`, `Capsule`, `Torus`, and `Plane3d`. This covers all shapes that Bevy has mesh structs for in `bevy_render::mesh::shapes`. `Cone` and `ConicalFrustum` are new shapes, so I can add them in a follow-up, or I could just add them here directly if that's preferrable. ## Solution Implement `Meshable` for `Cuboid`, `Sphere`, `Cylinder`, `Capsule`, `Torus`, and `Plane3d`. The logic is mostly just a copy of the the existing `bevy_render` shapes, but `Plane3d` has a configurable surface normal that affects the orientation. Some property names have also been changed to be more consistent. The default values differ from the old shapes to make them a bit more logical: - Spheres now have a radius of 0.5 instead of 1.0. The default capsule is equivalent to the default cylinder with the sphere's halves glued on. - The inner and outer radius of the torus are now 0.5 and 1.0 instead of 0.5 and 1.5 (i.e. the new minor and major radii are 0.25 and 0.75). It's double the width of the default cuboid, half of its height, and the default sphere matches the size of the hole. - `Cuboid` is 1x1x1 by default unlike the dreaded `Box` which is 2x1x1. Before, with "old" shapes: ![old](https://github.com/bevyengine/bevy/assets/57632562/733f3dda-258c-4491-8152-9829e056a1a3) Now, with primitive meshing: ![new](https://github.com/bevyengine/bevy/assets/57632562/5a1af14f-bb98-401d-82cf-de8072fea4ec) I only changed the `3d_shapes` example to use primitives for now. I can change them all in this PR or a follow-up though, whichever way is preferrable. ### Sphere API Spheres have had separate `Icosphere` and `UVSphere` structs, but with primitives we only have one `Sphere`. We need to handle this with builders: ```rust // Existing structs let ico = Mesh::try_from(Icophere::default()).unwrap(); let uv = Mesh::from(UVSphere::default()); // Primitives let ico = Sphere::default().mesh().ico(5).unwrap(); let uv = Sphere::default().mesh().uv(32, 18); ``` We could add methods on `Sphere` directly to skip calling `.mesh()`. I also added a `SphereKind` enum that can be used with the `kind` method: ```rust let ico = Sphere::default() .mesh() .kind(SphereKind::Ico { subdivisions: 8 }) .build(); ``` The default mesh for a `Sphere` is an icosphere with 5 subdivisions (like the default `Icosphere`). --- ## Changelog - Implement `Meshable` and `Default` for `Cuboid`, `Sphere`, `Cylinder`, `Capsule`, `Torus`, and `Plane3d` - Use primitives in `3d_shapes` example --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
124 lines
3.5 KiB
Rust
124 lines
3.5 KiB
Rust
//! This example demonstrates the built-in 3d shapes in Bevy.
|
|
//! The scene includes a patterned texture and a rotation for visualizing the normals and UVs.
|
|
|
|
use std::f32::consts::PI;
|
|
|
|
use bevy::{
|
|
prelude::*,
|
|
render::{
|
|
render_asset::RenderAssetUsages,
|
|
render_resource::{Extent3d, TextureDimension, TextureFormat},
|
|
},
|
|
};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, rotate)
|
|
.run();
|
|
}
|
|
|
|
/// A marker component for our shapes so we can query them separately from the ground plane
|
|
#[derive(Component)]
|
|
struct Shape;
|
|
|
|
const X_EXTENT: f32 = 12.0;
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut images: ResMut<Assets<Image>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
) {
|
|
let debug_material = materials.add(StandardMaterial {
|
|
base_color_texture: Some(images.add(uv_debug_texture())),
|
|
..default()
|
|
});
|
|
|
|
let shapes = [
|
|
meshes.add(Cuboid::default()),
|
|
meshes.add(Capsule3d::default()),
|
|
meshes.add(Torus::default()),
|
|
meshes.add(Cylinder::default()),
|
|
meshes.add(Sphere::default().mesh().ico(5).unwrap()),
|
|
meshes.add(Sphere::default().mesh().uv(32, 18)),
|
|
];
|
|
|
|
let num_shapes = shapes.len();
|
|
|
|
for (i, shape) in shapes.into_iter().enumerate() {
|
|
commands.spawn((
|
|
PbrBundle {
|
|
mesh: shape,
|
|
material: debug_material.clone(),
|
|
transform: Transform::from_xyz(
|
|
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
|
|
2.0,
|
|
0.0,
|
|
)
|
|
.with_rotation(Quat::from_rotation_x(-PI / 4.)),
|
|
..default()
|
|
},
|
|
Shape,
|
|
));
|
|
}
|
|
|
|
commands.spawn(PointLightBundle {
|
|
point_light: PointLight {
|
|
intensity: 1500000.0,
|
|
range: 100.,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_xyz(8.0, 16.0, 8.0),
|
|
..default()
|
|
});
|
|
|
|
// ground plane
|
|
commands.spawn(PbrBundle {
|
|
mesh: meshes.add(Plane3d::default().mesh().size(50.0, 50.0)),
|
|
material: materials.add(Color::SILVER),
|
|
..default()
|
|
});
|
|
|
|
commands.spawn(Camera3dBundle {
|
|
transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) {
|
|
for mut transform in &mut query {
|
|
transform.rotate_y(time.delta_seconds() / 2.);
|
|
}
|
|
}
|
|
|
|
/// Creates a colorful test pattern
|
|
fn uv_debug_texture() -> Image {
|
|
const TEXTURE_SIZE: usize = 8;
|
|
|
|
let mut palette: [u8; 32] = [
|
|
255, 102, 159, 255, 255, 159, 102, 255, 236, 255, 102, 255, 121, 255, 102, 255, 102, 255,
|
|
198, 255, 102, 198, 255, 255, 121, 102, 255, 255, 236, 102, 255, 255,
|
|
];
|
|
|
|
let mut texture_data = [0; TEXTURE_SIZE * TEXTURE_SIZE * 4];
|
|
for y in 0..TEXTURE_SIZE {
|
|
let offset = TEXTURE_SIZE * y * 4;
|
|
texture_data[offset..(offset + TEXTURE_SIZE * 4)].copy_from_slice(&palette);
|
|
palette.rotate_right(4);
|
|
}
|
|
|
|
Image::new_fill(
|
|
Extent3d {
|
|
width: TEXTURE_SIZE as u32,
|
|
height: TEXTURE_SIZE as u32,
|
|
depth_or_array_layers: 1,
|
|
},
|
|
TextureDimension::D2,
|
|
&texture_data,
|
|
TextureFormat::Rgba8UnormSrgb,
|
|
RenderAssetUsages::RENDER_WORLD,
|
|
)
|
|
}
|