mirror of
https://github.com/bevyengine/bevy
synced 2024-11-25 22:20:20 +00:00
Add 3d shapes example (#4613)
# Objective - As requested here: https://github.com/bevyengine/bevy/pull/4520#issuecomment-1109302039 - Make it easier to spot issues with built-in shapes ## Solution https://user-images.githubusercontent.com/200550/165624709-c40dfe7e-0e1e-4bd3-ae52-8ae66888c171.mp4 - Add an example showcasing the built-in 3d shapes with lighting/shadows - Rotate objects in such a way that all faces are seen by the camera - Add a UV debug texture ## Discussion I'm not sure if this is what @alice-i-cecile had in mind, but I adapted the little "torus playground" from the issue linked above to include all built-in shapes. This exact arrangement might not be particularly scalable if many more shapes are added. Maybe a slow camera pan, or cycling with the keyboard or on a timer, or a sidebar with buttons would work better. If one of the latter options is used, options for showing wireframes or computed flat normals might add some additional utility. Ideally, I think we'd have a better way of visualizing normals. Happy to rework this or close it if there's not a consensus around it being useful.
This commit is contained in:
parent
51509a9a3e
commit
61a3494a06
3 changed files with 126 additions and 0 deletions
|
@ -200,6 +200,10 @@ path = "examples/3d/pbr.rs"
|
|||
name = "shadow_biases"
|
||||
path = "examples/3d/shadow_biases.rs"
|
||||
|
||||
[[example]]
|
||||
name = "3d_shapes"
|
||||
path = "examples/3d/shapes.rs"
|
||||
|
||||
[[example]]
|
||||
name = "shadow_caster_receiver"
|
||||
path = "examples/3d/shadow_caster_receiver.rs"
|
||||
|
|
121
examples/3d/shapes.rs
Normal file
121
examples/3d/shapes.rs
Normal file
|
@ -0,0 +1,121 @@
|
|||
//! 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 bevy::{
|
||||
prelude::*,
|
||||
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
|
||||
};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.insert_resource(Msaa { samples: 4 })
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_startup_system(setup)
|
||||
.add_system(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 = 14.;
|
||||
|
||||
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(shape::Cube::default().into()),
|
||||
meshes.add(shape::Box::default().into()),
|
||||
meshes.add(shape::Capsule::default().into()),
|
||||
meshes.add(shape::Torus::default().into()),
|
||||
meshes.add(shape::Icosphere::default().into()),
|
||||
meshes.add(shape::UVSphere::default().into()),
|
||||
];
|
||||
|
||||
let num_shapes = shapes.len();
|
||||
|
||||
for (i, shape) in shapes.into_iter().enumerate() {
|
||||
commands
|
||||
.spawn_bundle(PbrBundle {
|
||||
mesh: shape,
|
||||
material: debug_material.clone(),
|
||||
transform: Transform {
|
||||
translation: Vec3::new(
|
||||
-X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * X_EXTENT,
|
||||
2.0,
|
||||
0.0,
|
||||
),
|
||||
..default()
|
||||
},
|
||||
..Default::default()
|
||||
})
|
||||
.insert(Shape);
|
||||
}
|
||||
|
||||
commands.spawn_bundle(PointLightBundle {
|
||||
point_light: PointLight {
|
||||
intensity: 9000.0,
|
||||
range: 100.,
|
||||
shadows_enabled: true,
|
||||
..Default::default()
|
||||
},
|
||||
transform: Transform::from_xyz(8.0, 16.0, 8.0),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
// ground plane
|
||||
commands.spawn_bundle(PbrBundle {
|
||||
mesh: meshes.add(shape::Plane { size: 50. }.into()),
|
||||
material: materials.add(Color::SILVER.into()),
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
commands.spawn_bundle(PerspectiveCameraBundle {
|
||||
transform: Transform::from_xyz(0.0, 6., 12.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
|
||||
..Default::default()
|
||||
});
|
||||
}
|
||||
|
||||
fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) {
|
||||
for mut transform in query.iter_mut() {
|
||||
transform.rotation = Quat::from_rotation_y(time.seconds_since_startup() as f32 / 2.)
|
||||
* Quat::from_rotation_x(-std::f32::consts::PI / 4.)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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,
|
||||
)
|
||||
}
|
|
@ -116,6 +116,7 @@ Example | File | Description
|
|||
`texture` | [`3d/texture.rs`](./3d/texture.rs) | Shows configuration of texture materials
|
||||
`update_gltf_scene` | [`3d/update_gltf_scene.rs`](./3d/update_gltf_scene.rs) | Update a scene from a gltf file, either by spawning the scene as a child of another entity, or by accessing the entities of the scene
|
||||
`wireframe` | [`3d/wireframe.rs`](./3d/wireframe.rs) | Showcases wireframe rendering
|
||||
`3d_shapes` | [`3d/shapes.rs`](./3d/shapes.rs) | A scene showcasing the built-in 3D shapes
|
||||
|
||||
## Animation
|
||||
|
||||
|
|
Loading…
Reference in a new issue