forked from bevyengine/bevy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatmosphere.rs
More file actions
309 lines (284 loc) · 10.3 KB
/
atmosphere.rs
File metadata and controls
309 lines (284 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! This example showcases pbr atmospheric scattering
#[cfg(feature = "free_camera")]
use bevy::camera_controller::free_camera::{FreeCamera, FreeCameraPlugin};
use std::f32::consts::PI;
use bevy::{
anti_alias::taa::TemporalAntiAliasing,
camera::Exposure,
color::palettes::css::BLACK,
core_pipeline::tonemapping::Tonemapping,
image::{
ImageAddressMode, ImageFilterMode, ImageLoaderSettings, ImageSampler,
ImageSamplerDescriptor,
},
input::keyboard::KeyCode,
light::{
light_consts::lux, AtmosphereEnvironmentMapLight, CascadeShadowConfigBuilder, FogVolume,
VolumetricFog, VolumetricLight,
},
pbr::{
Atmosphere, AtmosphereMode, AtmosphereSettings, DefaultOpaqueRendererMethod,
ExtendedMaterial, MaterialExtension, ScatteringMedium, ScreenSpaceReflections,
},
post_process::bloom::Bloom,
prelude::*,
render::render_resource::{AsBindGroup, ShaderType},
shader::ShaderRef,
};
#[derive(Resource, Default)]
struct GameState {
paused: bool,
}
fn main() {
App::new()
.insert_resource(DefaultOpaqueRendererMethod::deferred())
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(GameState::default())
.insert_resource(GlobalAmbientLight::NONE)
.add_plugins((
DefaultPlugins,
#[cfg(feature = "free_camera")]
FreeCameraPlugin,
))
.add_plugins(MaterialPlugin::<ExtendedMaterial<StandardMaterial, Water>>::default())
.add_systems(
Startup,
(setup_camera_fog, setup_terrain_scene, print_controls),
)
.add_systems(Update, (dynamic_scene, atmosphere_controls))
.run();
}
fn print_controls() {
println!("Atmosphere Example Controls:");
println!(" 1 - Switch to lookup texture rendering method");
println!(" 2 - Switch to raymarched rendering method");
println!(" Enter - Pause/Resume sun motion");
println!(" Up/Down - Increase/Decrease exposure");
}
fn atmosphere_controls(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut atmosphere_settings: Query<&mut AtmosphereSettings>,
mut game_state: ResMut<GameState>,
mut camera_exposure: Query<&mut Exposure, With<Camera3d>>,
time: Res<Time>,
) {
if keyboard_input.just_pressed(KeyCode::Digit1) {
for mut settings in &mut atmosphere_settings {
settings.rendering_method = AtmosphereMode::LookupTexture;
println!("Switched to lookup texture rendering method");
}
}
if keyboard_input.just_pressed(KeyCode::Digit2) {
for mut settings in &mut atmosphere_settings {
settings.rendering_method = AtmosphereMode::Raymarched;
println!("Switched to raymarched rendering method");
}
}
if keyboard_input.just_pressed(KeyCode::Enter) {
game_state.paused = !game_state.paused;
}
if keyboard_input.pressed(KeyCode::ArrowUp) {
for mut exposure in &mut camera_exposure {
exposure.ev100 -= time.delta_secs() * 2.0;
}
}
if keyboard_input.pressed(KeyCode::ArrowDown) {
for mut exposure in &mut camera_exposure {
exposure.ev100 += time.delta_secs() * 2.0;
}
}
}
fn setup_camera_fog(
mut commands: Commands,
mut scattering_mediums: ResMut<Assets<ScatteringMedium>>,
) {
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.4, 0.04, 0.0).looking_at(Vec3::Y * 0.1, Vec3::Y),
// Earthlike atmosphere
Atmosphere::earthlike(scattering_mediums.add(ScatteringMedium::default())),
// Can be adjusted to change the scene scale and rendering quality
AtmosphereSettings::default(),
// The directional light illuminance used in this scene
// (the one recommended for use with this feature) is
// quite bright, so raising the exposure compensation helps
// bring the scene to a nicer brightness range.
Exposure { ev100: 13.0 },
// Tonemapper chosen just because it looked good with the scene, any
// tonemapper would be fine :)
Tonemapping::AcesFitted,
// Bloom gives the sun a much more natural look.
Bloom::NATURAL,
// Enables the atmosphere to drive reflections and ambient lighting (IBL) for this view
AtmosphereEnvironmentMapLight::default(),
#[cfg(feature = "free_camera")]
FreeCamera::default(),
VolumetricFog {
ambient_intensity: 0.0,
..default()
},
Msaa::Off,
TemporalAntiAliasing::default(),
ScreenSpaceReflections::default(),
));
}
#[derive(Component)]
struct Terrain;
/// A custom [`ExtendedMaterial`] that creates animated water ripples.
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
struct Water {
/// The normal map image.
///
/// Note that, like all normal maps, this must not be loaded as sRGB.
#[texture(100)]
#[sampler(101)]
normals: Handle<Image>,
// Parameters to the water shader.
#[uniform(102)]
settings: WaterSettings,
}
/// Parameters to the water shader.
#[derive(ShaderType, Debug, Clone)]
struct WaterSettings {
/// How much to displace each octave each frame, in the u and v directions.
/// Two octaves are packed into each `vec4`.
octave_vectors: [Vec4; 2],
/// How wide the waves are in each octave.
octave_scales: Vec4,
/// How high the waves are in each octave.
octave_strengths: Vec4,
}
impl MaterialExtension for Water {
fn deferred_fragment_shader() -> ShaderRef {
"shaders/water_material.wgsl".into()
}
}
fn setup_terrain_scene(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut water_materials: ResMut<Assets<ExtendedMaterial<StandardMaterial, Water>>>,
asset_server: Res<AssetServer>,
) {
// Configure a properly scaled cascade shadow map for this scene (defaults are too large, mesh units are in km)
let cascade_shadow_config = CascadeShadowConfigBuilder {
first_cascade_far_bound: 0.3,
maximum_distance: 15.0,
..default()
}
.build();
// Sun
commands.spawn((
DirectionalLight {
shadow_maps_enabled: true,
// lux::RAW_SUNLIGHT is recommended for use with this feature, since
// other values approximate sunlight *post-scattering* in various
// conditions. RAW_SUNLIGHT in comparison is the illuminance of the
// sun unfiltered by the atmosphere, so it is the proper input for
// sunlight to be filtered by the atmosphere.
illuminance: lux::RAW_SUNLIGHT,
..default()
},
Transform::from_xyz(1.0, 0.4, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
VolumetricLight,
cascade_shadow_config,
));
// spawn the fog volume
commands.spawn((
FogVolume::default(),
Transform::from_scale(Vec3::new(10.0, 1.0, 10.0)).with_translation(Vec3::Y * 0.5),
));
let sphere_mesh = meshes.add(Mesh::from(Sphere { radius: 1.0 }));
// light probe spheres
commands.spawn((
Mesh3d(sphere_mesh.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::WHITE,
metallic: 1.0,
perceptual_roughness: 0.0,
..default()
})),
Transform::from_xyz(-1.0, 0.1, -0.1).with_scale(Vec3::splat(0.05)),
));
commands.spawn((
Mesh3d(sphere_mesh.clone()),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::WHITE,
metallic: 0.0,
perceptual_roughness: 1.0,
..default()
})),
Transform::from_xyz(-1.0, 0.1, 0.1).with_scale(Vec3::splat(0.05)),
));
// Terrain
commands.spawn((
Terrain,
SceneRoot(
asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/terrain/terrain.glb")),
),
Transform::from_xyz(-1.0, 0.0, -0.5)
.with_scale(Vec3::splat(0.5))
.with_rotation(Quat::from_rotation_y(PI / 2.0)),
));
spawn_water(
&mut commands,
&asset_server,
&mut meshes,
&mut water_materials,
);
}
// Spawns the water plane.
fn spawn_water(
commands: &mut Commands,
asset_server: &AssetServer,
meshes: &mut Assets<Mesh>,
water_materials: &mut Assets<ExtendedMaterial<StandardMaterial, Water>>,
) {
commands.spawn((
Mesh3d(meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(1.0)))),
MeshMaterial3d(water_materials.add(ExtendedMaterial {
base: StandardMaterial {
base_color: BLACK.into(),
perceptual_roughness: 0.0,
..default()
},
extension: Water {
normals: asset_server.load_with_settings::<Image, ImageLoaderSettings>(
"textures/water_normals.png",
|settings| {
settings.is_srgb = false;
settings.sampler = ImageSampler::Descriptor(ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Linear,
min_filter: ImageFilterMode::Linear,
..default()
});
},
),
// These water settings are just random values to create some
// variety.
settings: WaterSettings {
octave_vectors: [
vec4(0.080, 0.059, 0.073, -0.062),
vec4(0.153, 0.138, -0.149, -0.195),
],
octave_scales: vec4(1.0, 2.1, 7.9, 14.9) * 500.0,
octave_strengths: vec4(0.16, 0.18, 0.093, 0.044) * 0.2,
},
},
})),
Transform::from_scale(Vec3::splat(100.0)),
));
}
fn dynamic_scene(
mut suns: Query<&mut Transform, With<DirectionalLight>>,
time: Res<Time>,
sun_motion_state: Res<GameState>,
) {
// Only rotate the sun if motion is not paused
if !sun_motion_state.paused {
suns.iter_mut()
.for_each(|mut tf| tf.rotate_x(-time.delta_secs() * PI / 10.0));
}
}