Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/fog_volumes.rs
6592 views
1
//! Demonstrates fog volumes with voxel density textures.
2
//!
3
//! We render the Stanford bunny as a fog volume. Parts of the bunny become
4
//! lighter and darker as the camera rotates. This is physically-accurate
5
//! behavior that results from the scattering and absorption of the directional
6
//! light.
7
8
use bevy::{
9
light::{FogVolume, VolumetricFog, VolumetricLight},
10
math::vec3,
11
prelude::*,
12
render::view::Hdr,
13
};
14
15
/// Entry point.
16
fn main() {
17
App::new()
18
.add_plugins(DefaultPlugins.set(WindowPlugin {
19
primary_window: Some(Window {
20
title: "Bevy Fog Volumes Example".into(),
21
..default()
22
}),
23
..default()
24
}))
25
.insert_resource(AmbientLight::NONE)
26
.add_systems(Startup, setup)
27
.add_systems(Update, rotate_camera)
28
.run();
29
}
30
31
/// Spawns all the objects in the scene.
32
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
33
// Spawn a fog volume with a voxelized version of the Stanford bunny.
34
commands.spawn((
35
Transform::from_xyz(0.0, 0.5, 0.0),
36
FogVolume {
37
density_texture: Some(asset_server.load("volumes/bunny.ktx2")),
38
density_factor: 1.0,
39
// Scatter as much of the light as possible, to brighten the bunny
40
// up.
41
scattering: 1.0,
42
..default()
43
},
44
));
45
46
// Spawn a bright directional light that illuminates the fog well.
47
commands.spawn((
48
Transform::from_xyz(1.0, 1.0, -0.3).looking_at(vec3(0.0, 0.5, 0.0), Vec3::Y),
49
DirectionalLight {
50
shadows_enabled: true,
51
illuminance: 32000.0,
52
..default()
53
},
54
// Make sure to add this for the light to interact with the fog.
55
VolumetricLight,
56
));
57
58
// Spawn a camera.
59
commands.spawn((
60
Camera3d::default(),
61
Transform::from_xyz(-0.75, 1.0, 2.0).looking_at(vec3(0.0, 0.0, 0.0), Vec3::Y),
62
Hdr,
63
VolumetricFog {
64
// Make this relatively high in order to increase the fog quality.
65
step_count: 64,
66
// Disable ambient light.
67
ambient_intensity: 0.0,
68
..default()
69
},
70
));
71
}
72
73
/// Rotates the camera a bit every frame.
74
fn rotate_camera(mut cameras: Query<&mut Transform, With<Camera3d>>) {
75
for mut camera_transform in cameras.iter_mut() {
76
*camera_transform =
77
Transform::from_translation(Quat::from_rotation_y(0.01) * camera_transform.translation)
78
.looking_at(vec3(0.0, 0.5, 0.0), Vec3::Y);
79
}
80
}
81
82