rotate_environment_map/
rotate_environment_map.rs

1//! Demonstrates how to rotate the skybox and the environment map simultaneously.
2
3use std::f32::consts::PI;
4
5use bevy::{
6    color::palettes::css::{GOLD, WHITE},
7    core_pipeline::{tonemapping::Tonemapping::AcesFitted, Skybox},
8    image::ImageLoaderSettings,
9    prelude::*,
10};
11
12/// Entry point.
13pub fn main() {
14    App::new()
15        .add_plugins(DefaultPlugins)
16        .add_systems(Startup, setup)
17        .add_systems(Update, rotate_skybox_and_environment_map)
18        .run();
19}
20
21/// Initializes the scene.
22fn setup(
23    mut commands: Commands,
24    mut meshes: ResMut<Assets<Mesh>>,
25    mut materials: ResMut<Assets<StandardMaterial>>,
26    asset_server: Res<AssetServer>,
27) {
28    let sphere_mesh = create_sphere_mesh(&mut meshes);
29    spawn_sphere(&mut commands, &mut materials, &asset_server, &sphere_mesh);
30    spawn_light(&mut commands);
31    spawn_camera(&mut commands, &asset_server);
32}
33
34/// Rotate the skybox and the environment map per frame.
35fn rotate_skybox_and_environment_map(
36    mut environments: Query<(&mut Skybox, &mut EnvironmentMapLight)>,
37    time: Res<Time>,
38) {
39    let now = time.elapsed_secs();
40    let rotation = Quat::from_rotation_y(0.2 * now);
41    for (mut skybox, mut environment_map) in environments.iter_mut() {
42        skybox.rotation = rotation;
43        environment_map.rotation = rotation;
44    }
45}
46
47/// Generates a sphere.
48fn create_sphere_mesh(meshes: &mut Assets<Mesh>) -> Handle<Mesh> {
49    // We're going to use normal maps, so make sure we've generated tangents, or
50    // else the normal maps won't show up.
51
52    let mut sphere_mesh = Sphere::new(1.0).mesh().build();
53    sphere_mesh
54        .generate_tangents()
55        .expect("Failed to generate tangents");
56    meshes.add(sphere_mesh)
57}
58
59/// Spawn a regular object with a clearcoat layer. This looks like car paint.
60fn spawn_sphere(
61    commands: &mut Commands,
62    materials: &mut Assets<StandardMaterial>,
63    asset_server: &AssetServer,
64    sphere_mesh: &Handle<Mesh>,
65) {
66    commands.spawn((
67        Mesh3d(sphere_mesh.clone()),
68        MeshMaterial3d(materials.add(StandardMaterial {
69            clearcoat: 1.0,
70            clearcoat_perceptual_roughness: 0.3,
71            clearcoat_normal_texture: Some(asset_server.load_with_settings(
72                "textures/ScratchedGold-Normal.png",
73                |settings: &mut ImageLoaderSettings| settings.is_srgb = false,
74            )),
75            metallic: 0.9,
76            perceptual_roughness: 0.1,
77            base_color: GOLD.into(),
78            ..default()
79        })),
80        Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(1.25)),
81    ));
82}
83
84/// Spawns a light.
85fn spawn_light(commands: &mut Commands) {
86    commands.spawn(PointLight {
87        color: WHITE.into(),
88        intensity: 100000.0,
89        ..default()
90    });
91}
92
93/// Spawns a camera with associated skybox and environment map.
94fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
95    commands
96        .spawn((
97            Camera3d::default(),
98            Camera {
99                hdr: true,
100                ..default()
101            },
102            Projection::Perspective(PerspectiveProjection {
103                fov: 27.0 / 180.0 * PI,
104                ..default()
105            }),
106            Transform::from_xyz(0.0, 0.0, 10.0),
107            AcesFitted,
108        ))
109        .insert(Skybox {
110            brightness: 5000.0,
111            image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
112            ..default()
113        })
114        .insert(EnvironmentMapLight {
115            diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
116            specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
117            intensity: 2000.0,
118            ..default()
119        });
120}