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