1use crate::sdf::V3;
20
21#[derive(Clone, Copy, Debug, PartialEq)]
28pub struct CloudLayer {
29 pub base_y: f32,
30 pub top_y: f32,
31 pub band: f32,
32 pub coverage: f32,
33 pub density: f32,
34 pub scale: f32,
35 pub g: f32,
36 pub ms: f32,
37 pub albedo: V3,
38 pub seed: u32,
39}
40
41pub fn cloud_hash(ix: i32, iz: i32, seed: u32) -> u32 {
46 let mut n = (ix as u32).wrapping_mul(0x27d4eb2d) ^ (iz as u32).wrapping_mul(0x165667b1) ^ seed;
47 n ^= n >> 15;
48 n = n.wrapping_mul(0x2c1b3c6d);
49 n ^= n >> 12;
50 n = n.wrapping_mul(0x297a2d39);
51 n ^= n >> 15;
52 n
53}
54
55pub fn cloud_noise(x: f32, z: f32, seed: u32) -> f32 {
58 let bx = x.floor();
59 let bz = z.floor();
60 let fx = x - bx;
61 let fz = z - bz;
62 let ux = fx * fx * (3.0 - 2.0 * fx);
63 let uz = fz * fz * (3.0 - 2.0 * fz);
64 let h = |a: f32, b: f32| -> f32 {
65 (cloud_hash(a as i32, b as i32, seed) >> 8) as f32 * (1.0 / 16777216.0)
66 };
67 let a00 = h(bx, bz);
68 let a10 = h(bx + 1.0, bz);
69 let a01 = h(bx, bz + 1.0);
70 let a11 = h(bx + 1.0, bz + 1.0);
71 let s0 = a00 + (a10 - a00) * ux;
72 let s1 = a01 + (a11 - a01) * ux;
73 s0 + (s1 - s0) * uz
74}
75
76pub fn cloud_fbm(x: f32, z: f32, seed: u32) -> f32 {
79 let mut amp = 0.5f32;
80 let mut freq = 1.0f32;
81 let mut sum = 0.0f32;
82 let mut norm = 0.0f32;
83 for k in 0..3u32 {
84 sum += amp * cloud_noise(x * freq, z * freq, seed.wrapping_add(k));
85 norm += amp;
86 amp *= 0.5;
87 freq *= 2.0;
88 }
89 sum / norm
90}
91
92fn cloud_ss(e0: f32, e1: f32, x: f32) -> f32 {
96 let t = ((x - e0) / (e1 - e0)).clamp(0.0, 1.0);
97 t * t * (3.0 - 2.0 * t)
98}
99
100pub fn cloud_density(l: &CloudLayer, w: V3) -> f32 {
104 let e = l.band.max(1e-3);
105 let lower = cloud_ss(l.base_y, l.base_y + e, w[1]);
106 let upper = 1.0 - cloud_ss(l.top_y - e, l.top_y, w[1]);
107 let shell = lower * upper;
108 let fbm = cloud_fbm(w[0] / l.scale, w[2] / l.scale, l.seed);
109 let cover = ((fbm - (1.0 - l.coverage)) / l.coverage.max(0.02)).clamp(0.0, 1.0);
110 l.density * (shell * cover)
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116 use crate::camera::Camera;
117 use crate::froxel::{FogField, FroxelGrid};
118
119 fn layer() -> CloudLayer {
120 CloudLayer {
121 base_y: 18.0,
122 top_y: 40.0,
123 band: 6.0,
124 coverage: 0.5,
125 density: 0.8,
126 scale: 90.0,
127 g: 0.6,
128 ms: 0.15,
129 albedo: [1.0, 1.0, 1.0],
130 seed: 7,
131 }
132 }
133
134 fn dark_fog() -> FogField {
137 FogField {
138 plane_y: 0.0,
139 base_density: 0.0,
140 falloff: 0.35,
141 extinction: 1.0,
142 albedo: [0.8, 0.85, 0.9],
143 ambient: [0.0, 0.0, 0.0],
144 phase_g: 0.4,
145 sun_intensity: 0.0,
146 fog_start: 2.0,
147 fog_end: 40.0,
148 }
149 }
150
151 #[test]
152 fn hash_ist_stabil_und_die_felder_bleiben_in_0_bis_1() {
153 assert_eq!(cloud_hash(3, -5, 7), cloud_hash(3, -5, 7));
154 assert_ne!(cloud_hash(3, -5, 7), cloud_hash(3, -5, 8));
155 assert_ne!(cloud_hash(3, -5, 7), cloud_hash(4, -5, 7));
156 for i in 0..64 {
157 let x = i as f32 * 0.37 - 11.0;
158 let z = i as f32 * -0.53 + 5.0;
159 let n = cloud_noise(x, z, 7);
160 assert!((0.0..=1.0).contains(&n), "Rauschen außerhalb: {n}");
161 let f = cloud_fbm(x, z, 7);
162 assert!((0.0..=1.0).contains(&f), "FBM außerhalb: {f}");
163 }
164 let erw = (cloud_hash(4, -2, 7) >> 8) as f32 * (1.0 / 16777216.0);
167 assert_eq!(cloud_noise(4.0, -2.0, 7), erw);
168 }
169
170 #[test]
171 fn huelle_laesst_nur_zwischen_boden_und_grenze_dichte_zu() {
172 let l = layer();
173 for i in 0..40 {
174 let x = i as f32 * 13.0 - 250.0;
175 assert_eq!(
176 cloud_density(&l, [x, 10.0, x * 0.5]),
177 0.0,
178 "unter der Huelle ist Wolke"
179 );
180 assert_eq!(
181 cloud_density(&l, [x, 60.0, x * 0.5]),
182 0.0,
183 "ueber der Huelle ist Wolke"
184 );
185 }
186 let any = (0..200).any(|i| {
189 let x = i as f32 * 7.0;
190 cloud_density(&l, [x, 29.0, x * -0.3]) > 0.0
191 });
192 assert!(any, "kein Wolkenfleck in der Schicht");
193 let kahl = CloudLayer { coverage: 0.0, ..l };
196 for i in 0..200 {
197 let x = i as f32 * 7.0;
198 assert_eq!(
199 cloud_density(&kahl, [x, 29.0, x * -0.3]),
200 0.0,
201 "Bewoelkung 0 laesst Dichte durch"
202 );
203 }
204 for i in 0..200 {
206 let x = i as f32 * 3.0 - 300.0;
207 let d = cloud_density(&l, [x, 29.0, x * 0.7]);
208 assert!(d <= l.density + 1e-6, "Dichte ueber Maximum: {d}");
209 }
210 }
211
212 #[test]
213 fn injektion_traegt_wolkenzellen_und_laesst_leere_zellen_ruhen() {
214 let l = layer();
215 let cam = Camera::perspective(
216 glam::Vec3::new(0.0, 2.0, 10.0),
217 glam::Vec3::new(0.0, 29.0, -20.0),
218 glam::Vec3::Y,
219 std::f32::consts::FRAC_PI_3,
220 1.5,
221 );
222 let far = cam.depth_range().expect("Perspektiv-Kamera").1;
223 let fog = dark_fog();
224 let mut ohne = FroxelGrid::new(6, 6, 12, far);
225 ohne.inject(&fog, &[], &cam, [0.4, 0.8, 0.3]);
226 let mut mit = FroxelGrid::new(6, 6, 12, far);
227 mit.inject_with_clouds(&fog, &[], &[], Some(&l), &cam, [0.4, 0.8, 0.3]);
228 let wolke = (0..mit.cells.len())
230 .filter(|i| mit.cells[*i][3] > 0.0)
231 .count();
232 let vorher = (0..ohne.cells.len())
233 .filter(|i| ohne.cells[*i][3] > 0.0)
234 .count();
235 assert_eq!(vorher, 0, "Nebel ohne Dichtequelle streut doch");
236 assert!(wolke > 0, "die Huuelle injiziert keine Zelle");
237 assert!(
238 mit.cells.iter().all(|c| c[0] >= 0.0 && c[3] >= 0.0),
239 "Injektion wird negativ"
240 );
241 for z in 0..mit.fz {
243 for y in 0..mit.fy {
244 for x in 0..mit.fx {
245 let w = mit.cell_world(x, y, z, &cam);
246 if w[1] >= l.top_y || w[1] <= l.base_y {
247 assert_eq!(mit.cell(x, y, z), [0.0, 0.0, 0.0, 0.0]);
248 }
249 }
250 }
251 }
252 }
253}