use bevy::{math::ops, prelude::*};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, fade_transparency)
.run();
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(6.0, 6.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.5, 0.3))),
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgba(0.2, 0.7, 0.1, 0.0),
alpha_mode: AlphaMode::Mask(0.5),
..default()
})),
Transform::from_xyz(1.0, 0.5, -1.5),
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgba(0.2, 0.7, 0.1, 0.0),
alpha_mode: AlphaMode::Mask(0.5),
unlit: true,
..default()
})),
Transform::from_xyz(-1.0, 0.5, -1.5),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::default())),
MeshMaterial3d(materials.add(Color::srgba(0.5, 0.5, 1.0, 0.0))),
Transform::from_xyz(0.0, 0.5, 0.0),
));
commands.spawn((
Mesh3d(meshes.add(Cuboid::default())),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgba(0.5, 1.0, 0.5, 0.0),
alpha_mode: AlphaMode::AlphaToCoverage,
..default()
})),
Transform::from_xyz(-1.5, 0.5, 0.0),
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.5).mesh().ico(3).unwrap())),
MeshMaterial3d(materials.add(Color::srgb(0.7, 0.2, 0.1))),
Transform::from_xyz(0.0, 0.5, -1.5),
));
commands.spawn((
PointLight {
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));
}
pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) {
let alpha = (ops::sin(time.elapsed_secs()) / 2.0) + 0.5;
for (_, material) in materials.iter_mut() {
material.base_color.set_alpha(alpha);
}
}