Skip to main content

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