pub fn default<T>() -> Twhere
T: Default,Expand description
An ergonomic abbreviation for Default::default() to make initializing structs easier.
This is especially helpful when combined with “struct update syntax”.
use bevy_utils::default;
#[derive(Default)]
struct Foo {
a: usize,
b: usize,
c: usize,
}
// Normally you would initialize a struct with defaults using "struct update syntax"
// combined with `Default::default()`. This example sets `Foo::a` to 10 and the remaining
// values to their defaults.
let foo = Foo {
a: 10,
..Default::default()
};
// But now you can do this, which is equivalent:
let foo = Foo {
a: 10,
..default()
};Examples found in repository?
More examples
examples/3d/spherical_area_lights.rs (line 9)
5fn main() {
6 App::new()
7 .insert_resource(GlobalAmbientLight {
8 brightness: 60.0,
9 ..default()
10 })
11 .add_plugins(DefaultPlugins)
12 .add_systems(Startup, setup)
13 .run();
14}
15
16fn setup(
17 mut commands: Commands,
18 mut meshes: ResMut<Assets<Mesh>>,
19 mut materials: ResMut<Assets<StandardMaterial>>,
20) {
21 // camera
22 commands.spawn((
23 Camera3d::default(),
24 Transform::from_xyz(0.2, 1.5, 2.5).looking_at(Vec3::ZERO, Vec3::Y),
25 ));
26
27 // plane
28 commands.spawn((
29 Mesh3d(meshes.add(Plane3d::default().mesh().size(100.0, 100.0))),
30 MeshMaterial3d(materials.add(StandardMaterial {
31 base_color: Color::srgb(0.2, 0.2, 0.2),
32 perceptual_roughness: 0.08,
33 ..default()
34 })),
35 ));
36
37 const COUNT: usize = 6;
38 let position_range = -2.0..2.0;
39 let radius_range = 0.0..0.4;
40 let pos_len = position_range.end - position_range.start;
41 let radius_len = radius_range.end - radius_range.start;
42 let mesh = meshes.add(Sphere::new(1.0).mesh().uv(120, 64));
43
44 for i in 0..COUNT {
45 let percent = i as f32 / COUNT as f32;
46 let radius = radius_range.start + percent * radius_len;
47
48 // sphere light
49 commands
50 .spawn((
51 Mesh3d(mesh.clone()),
52 MeshMaterial3d(materials.add(StandardMaterial {
53 base_color: Color::srgb(0.5, 0.5, 1.0),
54 unlit: true,
55 ..default()
56 })),
57 Transform::from_xyz(position_range.start + percent * pos_len, 0.3, 0.0)
58 .with_scale(Vec3::splat(radius)),
59 ))
60 .with_child(PointLight {
61 radius,
62 color: Color::srgb(0.2, 0.2, 1.0),
63 ..default()
64 });
65 }
66}examples/animation/animated_transform.rs (line 16)
10fn main() {
11 App::new()
12 .add_plugins(DefaultPlugins)
13 .insert_resource(GlobalAmbientLight {
14 color: Color::WHITE,
15 brightness: 150.0,
16 ..default()
17 })
18 .add_systems(Startup, setup)
19 .run();
20}
21
22fn setup(
23 mut commands: Commands,
24 mut meshes: ResMut<Assets<Mesh>>,
25 mut materials: ResMut<Assets<StandardMaterial>>,
26 mut animations: ResMut<Assets<AnimationClip>>,
27 mut graphs: ResMut<Assets<AnimationGraph>>,
28) {
29 // Camera
30 commands.spawn((
31 Camera3d::default(),
32 Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
33 ));
34
35 // Light
36 commands.spawn((
37 PointLight {
38 intensity: 500_000.0,
39 ..default()
40 },
41 Transform::from_xyz(0.0, 2.5, 0.0),
42 ));
43
44 // Let's use the `Name` component to target entities. We can use anything we
45 // like, but names are convenient.
46 let planet = Name::new("planet");
47 let orbit_controller = Name::new("orbit_controller");
48 let satellite = Name::new("satellite");
49
50 // Creating the animation
51 let mut animation = AnimationClip::default();
52 // A curve can modify a single part of a transform: here, the translation.
53 let planet_animation_target_id = AnimationTargetId::from_name(&planet);
54 animation.add_curve_to_target(
55 planet_animation_target_id,
56 AnimatableCurve::new(
57 animated_field!(Transform::translation),
58 UnevenSampleAutoCurve::new([0.0, 1.0, 2.0, 3.0, 4.0].into_iter().zip([
59 Vec3::new(1.0, 0.0, 1.0),
60 Vec3::new(-1.0, 0.0, 1.0),
61 Vec3::new(-1.0, 0.0, -1.0),
62 Vec3::new(1.0, 0.0, -1.0),
63 // in case seamless looping is wanted, the last keyframe should
64 // be the same as the first one
65 Vec3::new(1.0, 0.0, 1.0),
66 ]))
67 .expect("should be able to build translation curve because we pass in valid samples"),
68 ),
69 );
70 // Or it can modify the rotation of the transform.
71 // To find the entity to modify, the hierarchy will be traversed looking for
72 // an entity with the right name at each level.
73 let orbit_controller_animation_target_id =
74 AnimationTargetId::from_names([planet.clone(), orbit_controller.clone()].iter());
75 animation.add_curve_to_target(
76 orbit_controller_animation_target_id,
77 AnimatableCurve::new(
78 animated_field!(Transform::rotation),
79 UnevenSampleAutoCurve::new([0.0, 1.0, 2.0, 3.0, 4.0].into_iter().zip([
80 Quat::IDENTITY,
81 Quat::from_axis_angle(Vec3::Y, PI / 2.),
82 Quat::from_axis_angle(Vec3::Y, PI / 2. * 2.),
83 Quat::from_axis_angle(Vec3::Y, PI / 2. * 3.),
84 Quat::IDENTITY,
85 ]))
86 .expect("Failed to build rotation curve"),
87 ),
88 );
89 // If a curve in an animation is shorter than the other, it will not repeat
90 // until all other curves are finished. In that case, another animation should
91 // be created for each part that would have a different duration / period.
92 let satellite_animation_target_id = AnimationTargetId::from_names(
93 [planet.clone(), orbit_controller.clone(), satellite.clone()].iter(),
94 );
95 animation.add_curve_to_target(
96 satellite_animation_target_id,
97 AnimatableCurve::new(
98 animated_field!(Transform::scale),
99 UnevenSampleAutoCurve::new(
100 [0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0]
101 .into_iter()
102 .zip([
103 Vec3::splat(0.8),
104 Vec3::splat(1.2),
105 Vec3::splat(0.8),
106 Vec3::splat(1.2),
107 Vec3::splat(0.8),
108 Vec3::splat(1.2),
109 Vec3::splat(0.8),
110 Vec3::splat(1.2),
111 Vec3::splat(0.8),
112 ]),
113 )
114 .expect("Failed to build scale curve"),
115 ),
116 );
117 // There can be more than one curve targeting the same entity path.
118 animation.add_curve_to_target(
119 AnimationTargetId::from_names(
120 [planet.clone(), orbit_controller.clone(), satellite.clone()].iter(),
121 ),
122 AnimatableCurve::new(
123 animated_field!(Transform::rotation),
124 UnevenSampleAutoCurve::new([0.0, 1.0, 2.0, 3.0, 4.0].into_iter().zip([
125 Quat::IDENTITY,
126 Quat::from_axis_angle(Vec3::Y, PI / 2.),
127 Quat::from_axis_angle(Vec3::Y, PI / 2. * 2.),
128 Quat::from_axis_angle(Vec3::Y, PI / 2. * 3.),
129 Quat::IDENTITY,
130 ]))
131 .expect("should be able to build translation curve because we pass in valid samples"),
132 ),
133 );
134
135 // Create the animation graph
136 let (graph, animation_index) = AnimationGraph::from_clip(animations.add(animation));
137
138 // Create the animation player, and set it to repeat
139 let mut player = AnimationPlayer::default();
140 player.play(animation_index).repeat();
141
142 // Create the scene that will be animated
143 // First entity is the planet
144 let planet_entity = commands
145 .spawn((
146 Mesh3d(meshes.add(Sphere::default())),
147 MeshMaterial3d(materials.add(Color::srgb(0.8, 0.7, 0.6))),
148 // Add the animation graph and player
149 planet,
150 AnimationGraphHandle(graphs.add(graph)),
151 player,
152 ))
153 .id();
154 commands.entity(planet_entity).insert((
155 planet_animation_target_id,
156 AnimatedBy(planet_entity),
157 children![(
158 Transform::default(),
159 Visibility::default(),
160 orbit_controller,
161 orbit_controller_animation_target_id,
162 AnimatedBy(planet_entity),
163 children![(
164 Mesh3d(meshes.add(Cuboid::new(0.5, 0.5, 0.5))),
165 MeshMaterial3d(materials.add(Color::srgb(0.3, 0.9, 0.3))),
166 Transform::from_xyz(1.5, 0.0, 0.0),
167 satellite_animation_target_id,
168 AnimatedBy(planet_entity),
169 satellite,
170 )],
171 )],
172 ));
173}examples/app/no_renderer.rs (line 18)
12fn main() {
13 App::new()
14 .add_plugins(
15 DefaultPlugins.set(RenderPlugin {
16 render_creation: WgpuSettings {
17 backends: None,
18 ..default()
19 }
20 .into(),
21 ..default()
22 }),
23 )
24 .run();
25}Additional examples can be found in:
- examples/app/return_after_run.rs
- examples/window/scale_factor_override.rs
- examples/app/logs.rs
- examples/2d/sprite_tile.rs
- examples/ui/window_fallthrough.rs
- examples/audio/audio_control.rs
- examples/ui/font_atlas_debug.rs
- examples/gltf/load_gltf_extras.rs
- examples/window/transparent_window.rs
- examples/3d/parenting.rs
- examples/remote/server.rs
- examples/window/screenshot.rs
- examples/3d/animated_material.rs
- examples/gltf/load_gltf.rs
- examples/gltf/update_gltf_scene.rs
- examples/dev_tools/fps_overlay.rs
- examples/ui/virtual_keyboard.rs
- examples/3d/vertex_colors.rs
- examples/2d/bloom_2d.rs
- examples/ui/ui_texture_slice.rs
- examples/ui/ui_texture_slice_flip_and_tile.rs
- examples/picking/dragdrop_picking.rs
- examples/ui/ui_texture_atlas_slice.rs
- examples/2d/mesh2d_alpha_mode.rs
- examples/2d/mesh2d_repeated_texture.rs
- examples/ui/tab_navigation.rs
- examples/ui/z_index.rs