1use std::f32::consts::PI;
23
24use bevy::{color::palettes::tailwind::*, picking::pointer::PointerInteraction, prelude::*};
25
26fn main() {
27 App::new()
28 .add_plugins((DefaultPlugins, MeshPickingPlugin))
30 .add_systems(Startup, setup_scene)
31 .add_systems(Update, (draw_mesh_intersections, rotate))
32 .run();
33}
34
35#[derive(Component)]
37struct Shape;
38
39const SHAPES_X_EXTENT: f32 = 14.0;
40const EXTRUSION_X_EXTENT: f32 = 16.0;
41const Z_EXTENT: f32 = 5.0;
42
43fn setup_scene(
44 mut commands: Commands,
45 mut meshes: ResMut<Assets<Mesh>>,
46 mut materials: ResMut<Assets<StandardMaterial>>,
47) {
48 let white_matl = materials.add(Color::WHITE);
50 let ground_matl = materials.add(Color::from(GRAY_300));
51 let hover_matl = materials.add(Color::from(CYAN_300));
52 let pressed_matl = materials.add(Color::from(YELLOW_300));
53
54 let shapes = [
55 meshes.add(Cuboid::default()),
56 meshes.add(Tetrahedron::default()),
57 meshes.add(Capsule3d::default()),
58 meshes.add(Torus::default()),
59 meshes.add(Cylinder::default()),
60 meshes.add(Cone::default()),
61 meshes.add(ConicalFrustum::default()),
62 meshes.add(Sphere::default().mesh().ico(5).unwrap()),
63 meshes.add(Sphere::default().mesh().uv(32, 18)),
64 ];
65
66 let extrusions = [
67 meshes.add(Extrusion::new(Rectangle::default(), 1.)),
68 meshes.add(Extrusion::new(Capsule2d::default(), 1.)),
69 meshes.add(Extrusion::new(Annulus::default(), 1.)),
70 meshes.add(Extrusion::new(Circle::default(), 1.)),
71 meshes.add(Extrusion::new(Ellipse::default(), 1.)),
72 meshes.add(Extrusion::new(RegularPolygon::default(), 1.)),
73 meshes.add(Extrusion::new(Triangle2d::default(), 1.)),
74 ];
75
76 let num_shapes = shapes.len();
77
78 for (i, shape) in shapes.into_iter().enumerate() {
80 commands
81 .spawn((
82 Mesh3d(shape),
83 MeshMaterial3d(white_matl.clone()),
84 Transform::from_xyz(
85 -SHAPES_X_EXTENT / 2. + i as f32 / (num_shapes - 1) as f32 * SHAPES_X_EXTENT,
86 2.0,
87 Z_EXTENT / 2.,
88 )
89 .with_rotation(Quat::from_rotation_x(-PI / 4.)),
90 Shape,
91 ))
92 .observe(update_material_on::<Pointer<Over>>(hover_matl.clone()))
93 .observe(update_material_on::<Pointer<Out>>(white_matl.clone()))
94 .observe(update_material_on::<Pointer<Press>>(pressed_matl.clone()))
95 .observe(update_material_on::<Pointer<Release>>(hover_matl.clone()))
96 .observe(rotate_on_drag);
97 }
98
99 let num_extrusions = extrusions.len();
100
101 for (i, shape) in extrusions.into_iter().enumerate() {
102 commands
103 .spawn((
104 Mesh3d(shape),
105 MeshMaterial3d(white_matl.clone()),
106 Transform::from_xyz(
107 -EXTRUSION_X_EXTENT / 2.
108 + i as f32 / (num_extrusions - 1) as f32 * EXTRUSION_X_EXTENT,
109 2.0,
110 -Z_EXTENT / 2.,
111 )
112 .with_rotation(Quat::from_rotation_x(-PI / 4.)),
113 Shape,
114 ))
115 .observe(update_material_on::<Pointer<Over>>(hover_matl.clone()))
116 .observe(update_material_on::<Pointer<Out>>(white_matl.clone()))
117 .observe(update_material_on::<Pointer<Press>>(pressed_matl.clone()))
118 .observe(update_material_on::<Pointer<Release>>(hover_matl.clone()))
119 .observe(rotate_on_drag);
120 }
121
122 commands.spawn((
124 Mesh3d(meshes.add(Plane3d::default().mesh().size(50.0, 50.0).subdivisions(10))),
125 MeshMaterial3d(ground_matl.clone()),
126 Pickable::IGNORE, ));
128
129 commands.spawn((
131 PointLight {
132 shadows_enabled: true,
133 intensity: 10_000_000.,
134 range: 100.0,
135 shadow_depth_bias: 0.2,
136 ..default()
137 },
138 Transform::from_xyz(8.0, 16.0, 8.0),
139 ));
140
141 commands.spawn((
143 Camera3d::default(),
144 Transform::from_xyz(0.0, 7., 14.0).looking_at(Vec3::new(0., 1., 0.), Vec3::Y),
145 ));
146
147 commands.spawn((
149 Text::new("Hover over the shapes to pick them\nDrag to rotate"),
150 Node {
151 position_type: PositionType::Absolute,
152 top: px(12),
153 left: px(12),
154 ..default()
155 },
156 ));
157}
158
159fn update_material_on<E: EntityEvent>(
161 new_material: Handle<StandardMaterial>,
162) -> impl Fn(On<E>, Query<&mut MeshMaterial3d<StandardMaterial>>) {
163 move |event, mut query| {
167 if let Ok(mut material) = query.get_mut(event.event_target()) {
168 material.0 = new_material.clone();
169 }
170 }
171}
172
173fn draw_mesh_intersections(pointers: Query<&PointerInteraction>, mut gizmos: Gizmos) {
175 for (point, normal) in pointers
176 .iter()
177 .filter_map(|interaction| interaction.get_nearest_hit())
178 .filter_map(|(_entity, hit)| hit.position.zip(hit.normal))
179 {
180 gizmos.sphere(point, 0.05, RED_500);
181 gizmos.arrow(point, point + normal.normalize() * 0.5, PINK_100);
182 }
183}
184
185fn rotate(mut query: Query<&mut Transform, With<Shape>>, time: Res<Time>) {
187 for mut transform in &mut query {
188 transform.rotate_y(time.delta_secs() / 2.);
189 }
190}
191
192fn rotate_on_drag(drag: On<Pointer<Drag>>, mut transforms: Query<&mut Transform>) {
194 let mut transform = transforms.get_mut(drag.entity).unwrap();
195 transform.rotate_y(drag.delta.x * 0.02);
196 transform.rotate_x(drag.delta.y * 0.02);
197}