Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/rotate_environment_map.rs
9330 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
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.
15
pub 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.
24
fn 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.
37
fn 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.
50
fn 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.
62
fn 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(materials.add(StandardMaterial {
71
clearcoat: 1.0,
72
clearcoat_perceptual_roughness: 0.3,
73
clearcoat_normal_texture: Some(asset_server.load_with_settings(
74
"textures/ScratchedGold-Normal.png",
75
|settings: &mut ImageLoaderSettings| settings.is_srgb = false,
76
)),
77
metallic: 0.9,
78
perceptual_roughness: 0.1,
79
base_color: GOLD.into(),
80
..default()
81
})),
82
Transform::from_xyz(0.0, 0.0, 0.0).with_scale(Vec3::splat(1.25)),
83
));
84
}
85
86
/// Spawns a light.
87
fn spawn_light(commands: &mut Commands) {
88
commands.spawn(PointLight {
89
color: WHITE.into(),
90
intensity: 100000.0,
91
..default()
92
});
93
}
94
95
/// Spawns a camera with associated skybox and environment map.
96
fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
97
commands
98
.spawn((
99
Camera3d::default(),
100
Hdr,
101
Projection::Perspective(PerspectiveProjection {
102
fov: 27.0 / 180.0 * PI,
103
..default()
104
}),
105
Transform::from_xyz(0.0, 0.0, 10.0),
106
AcesFitted,
107
))
108
.insert(Skybox {
109
brightness: 5000.0,
110
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
111
..default()
112
})
113
.insert(EnvironmentMapLight {
114
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
115
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
116
intensity: 2000.0,
117
..default()
118
});
119
}
120
121