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
//! Rust mirror of fog.wgsl's fog volume record, which the scene packs and
//! the fog stage reads, and the spread of the froxel volume's slices.
use crate::content::transient::FogVolume;
/// How the froxel volume spaces its depth slices, as fog.wgsl's
/// `fog_slice_depth` takes it: above 1, more of them lie near the camera.
/// Godot's default `volumetric_fog_detail_spread` (b130438
/// `scene/resources/environment.h`, MIT). The frame's uniform carries it to
/// draws, and the fog stage's to its passes and its fog volumes' froxel
/// bounds.
pub(crate) const DETAIL_SPREAD: f32 = 2.;
/// `projection`'s x and y scales and x and y offsets, as fog.wgsl's
/// `fog_froxel_view_position` takes them: the froxel columns' rays, which
/// the fog's froxels and the sky's aerial volume both follow.
pub(crate) fn froxel_projection(projection: glam::Mat4) -> [f32; 4] {
[
projection.x_axis.x,
projection.y_axis.y,
projection.z_axis.x,
projection.z_axis.y,
]
}
/// `FogLayout` in fog.wgsl: the frame's one fog volume's slices, which
/// `Frame`, the fog stage's integration and source completion hold.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct FogLayout {
/// One over the fog's length (0 while the fog does not run) and over
/// its detail spread.
pub inverse_length: f32,
pub inverse_detail_spread: f32,
/// How far the aerial slices reach, metres of view depth.
pub aerial_reach: f32,
pub fog_slices: u32,
pub aerial_slices: u32,
}
impl FogLayout {
/// The volume of a frame whose fog runs with `fog` slices over `length`
/// metres (none while it does not), with the aerial volume's slices to
/// its reach while aerial perspective is in effect (`aerial`).
pub fn new(fog: Option<(u32, f32)>, aerial: bool) -> Self {
Self {
inverse_length: fog.map_or(0., |(_, length)| length.recip()),
inverse_detail_spread: DETAIL_SPREAD.recip(),
aerial_reach: super::sky::AERIAL_REACH,
fog_slices: fog.map_or(0, |(slices, _)| slices),
aerial_slices: if aerial { super::sky::AERIAL_SLICES } else { 0 },
}
}
/// The volume's slices: the fog's, then the aerial slices and the fog
/// alone.
pub fn slices(&self) -> u32 {
self.fog_slices
+ if self.aerial_slices > 0 {
self.aerial_slices + 1
} else {
0
}
}
}
/// `FogVolumeRecord` in fog.wgsl: a box's frame, size and medium.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct FogVolumeRecord {
pub local_from_world: [[f32; 4]; 4],
pub half_size: [f32; 3],
pub density: f32,
pub albedo: [f32; 3],
pub edge_fade: f32,
pub center: [f32; 3],
pub radius_squared: f32,
}
impl FogVolumeRecord {
pub fn new(volume: &FogVolume) -> Self {
let world_from_local = world_from_local(volume);
let half_size = volume.size * 0.5;
Self {
local_from_world: world_from_local.inverse().to_cols_array_2d(),
half_size: half_size.to_array(),
density: volume.density,
albedo: volume.albedo,
edge_fade: volume.edge_fade,
center: volume.center.to_array(),
radius_squared: half_size.length_squared(),
}
}
}
/// The world positions of `volume`'s box's eight corners.
pub(crate) fn corners(volume: &FogVolume) -> [glam::Vec3; 8] {
let world_from_local = world_from_local(volume);
let half_size = volume.size * 0.5;
std::array::from_fn(|corner| {
let sign = glam::Vec3::new(
if corner & 1 == 0 { -1. } else { 1. },
if corner & 2 == 0 { -1. } else { 1. },
if corner & 4 == 0 { -1. } else { 1. },
);
world_from_local.transform_point3(half_size * sign)
})
}
/// `volume`'s box's frame in the world.
fn world_from_local(volume: &FogVolume) -> glam::Mat4 {
glam::Mat4::from_rotation_translation(volume.rotation.normalize(), volume.center)
}
/// The layouts this module mirrors.
#[cfg(test)]
pub(crate) fn mirrors() -> [crate::shading::layout_tests::Mirror; 2] {
use crate::shading::layout_tests::mirror;
[
mirror!(
"volumetric_fog",
"FogLayout",
FogLayout,
[
inverse_length,
inverse_detail_spread,
aerial_reach,
fog_slices,
aerial_slices
]
),
mirror!(
"volumetric_fog",
"FogVolumeRecord",
FogVolumeRecord,
[
local_from_world,
half_size,
density,
albedo,
edge_fade,
center,
radius_squared
]
),
]
}