1#define_import_path bevy_pbr::atmosphere::functions 3#import bevy_render::maths::{PI, HALF_PI, PI_2, fast_acos, fast_acos_4, fast_atan2, ray_sphere_intersect} 5#import bevy_pbr::atmosphere::{ 8 atmosphere, settings, view, lights, transmittance_lut, atmosphere_lut_sampler, 9 multiscattering_lut, sky_view_lut, aerial_view_lut, atmosphere_transforms, 10 medium_density_lut, medium_scattering_lut, medium_sampler, 13 transmittance_lut_r_mu_to_uv, ray_intersects_ground, 14 distance_to_top_atmosphere_boundary, distance_to_bottom_atmosphere_boundary 38const FRAC_PI: f32 = 0.3183098862; 39const FRAC_2_PI: f32 = 0.15915494309; 40const FRAC_3_16_PI: f32 = 0.0596831036594607509; 41const FRAC_4_PI: f32 = 0.07957747154594767; 42const ROOT_2: f32 = 1.41421356; 43const EPSILON: f32 = 1.0; 44const MIN_EXTINCTION: vec3<f32> = vec3(1e-12); 50const MIDPOINT_RATIO: f32 = 0.3; 54fn unit_to_sub_uvs(val: vec2<f32>, resolution: vec2<f32>) -> vec2<f32> { 55 return (val + 0.5f / resolution) * (resolution / (resolution + 1.0f)); 58fn sub_uvs_to_unit(val: vec2<f32>, resolution: vec2<f32>) -> vec2<f32> { 59 return (val - 0.5f / resolution) * (resolution / (resolution - 1.0f)); 62fn multiscattering_lut_r_mu_to_uv(r: f32, mu: f32) -> vec2<f32> { 63 let u = 0.5 + 0.5 * mu; 64 let v = saturate((r - atmosphere.inner_radius) / (atmosphere.outer_radius - atmosphere.inner_radius)); 65 return unit_to_sub_uvs(vec2(u, v), vec2<f32>(settings.multiscattering_lut_size)); 68fn multiscattering_lut_uv_to_r_mu(uv: vec2<f32>) -> vec2<f32> { 69 let adj_uv = sub_uvs_to_unit(uv, vec2<f32>(settings.multiscattering_lut_size)); 70 let r = mix(atmosphere.inner_radius, atmosphere.outer_radius, adj_uv.y); 71 let mu = adj_uv.x * 2 - 1; 75fn sky_view_lut_r_mu_azimuth_to_uv(r: f32, mu: f32, azimuth: f32) -> vec2<f32> { 76 let u = (azimuth * FRAC_2_PI) + 0.5; 78 let v_horizon = sqrt(r * r - atmosphere.inner_radius * atmosphere.inner_radius); 79 let cos_beta = v_horizon / r; 82 let beta = fast_acos_4(cos_beta); 83 let horizon_zenith = PI - beta; 84 let view_zenith = fast_acos_4(mu); 89 let l = view_zenith - horizon_zenith; 92 let v = 0.5 + 0.5 * sign(l) * sqrt(abs_l / HALF_PI); 94 return unit_to_sub_uvs(vec2(u, v), vec2<f32>(settings.sky_view_lut_size)); 97fn sky_view_lut_uv_to_zenith_azimuth(r: f32, uv: vec2<f32>) -> vec2<f32> { 98 let adj_uv = sub_uvs_to_unit(vec2(uv.x, 1.0 - uv.y), vec2<f32>(settings.sky_view_lut_size)); 99 let azimuth = (adj_uv.x - 0.5) * PI_2; 102 let v_horizon = sqrt(r * r - atmosphere.inner_radius * atmosphere.inner_radius); 103 let cos_beta = v_horizon / r; 104 let beta = fast_acos_4(cos_beta); 105 let horizon_zenith = PI - beta; 108 let t = abs(2.0 * (adj_uv.y - 0.5)); 109 let l = sign(adj_uv.y - 0.5) * HALF_PI * t * t; 111 return vec2(horizon_zenith - l, azimuth); 116fn sample_transmittance_lut(r: f32, mu: f32) -> vec3<f32> { 117 let uv = transmittance_lut_r_mu_to_uv(atmosphere, r, mu); 118 return textureSampleLevel(transmittance_lut, atmosphere_lut_sampler, uv, 0.0).rgb; 126fn sample_transmittance_lut_segment(r: f32, mu: f32, t: f32) -> vec3<f32> { 127 let r_t = get_local_r(r, mu, t); 128 let mu_t = clamp((r * mu + t) / r_t, -1.0, 1.0); 130 if ray_intersects_ground(r, mu) { 132 sample_transmittance_lut(r_t, -mu_t) / sample_transmittance_lut(r, -mu), 137 sample_transmittance_lut(r, mu) / sample_transmittance_lut(r_t, mu_t), vec3(1.0) 142fn sample_multiscattering_lut(r: f32, mu: f32) -> vec3<f32> { 143 let uv = multiscattering_lut_r_mu_to_uv(r, mu); 144 return textureSampleLevel(multiscattering_lut, atmosphere_lut_sampler, uv, 0.0).rgb; 147fn sample_sky_view_lut(r: f32, ray_dir_as: vec3<f32>) -> vec3<f32> { 148 let mu = ray_dir_as.y; 149 let azimuth = fast_atan2(ray_dir_as.x, -ray_dir_as.z); 150 let uv = sky_view_lut_r_mu_azimuth_to_uv(r, mu, azimuth); 151 return textureSampleLevel(sky_view_lut, atmosphere_lut_sampler, uv, 0.0).rgb; 154fn ndc_to_camera_dist(ndc: vec3<f32>) -> f32 { 155 let view_pos = view.view_from_clip * vec4(ndc, 1.0); 156 let p_view = view_pos.xyz / view_pos.w; 157 let p_world = (view.world_from_view * vec4(p_view, 1.0)).xyz; 158 let p_atmo = (atmosphere.world_to_atmosphere * vec4(p_world, 1.0)).xyz; 159 let cam_atmo = (atmosphere.world_to_atmosphere * vec4(view.world_position, 1.0)).xyz; 160 return length(p_atmo - cam_atmo); 165fn sample_aerial_view_lut(uv: vec2<f32>, t: f32) -> vec3<f32> { 166 let t_max = settings.aerial_view_lut_max_distance; 167 let num_slices = f32(settings.aerial_view_lut_size.z); 173 let uvw = vec3(uv, saturate(t / t_max - 0.5 / num_slices)); 174 let sample = textureSampleLevel(aerial_view_lut, atmosphere_lut_sampler, uvw, 0.0); 178 let t_slice = t_max / num_slices; 179 let fade = saturate(t / t_slice); 181 return exp(sample.rgb) * fade; 186const ABSORPTION_DENSITY: f32 = 0.0; 187const SCATTERING_DENSITY: f32 = 1.0; 193fn sample_density_lut(r: f32, component: f32) -> vec3<f32> { 195 let normalized_altitude = (r - atmosphere.inner_radius) / (atmosphere.outer_radius - atmosphere.inner_radius); 196 let uv = vec2(1.0 - normalized_altitude, component); 197 return textureSampleLevel(medium_density_lut, medium_sampler, uv, 0.0).xyz; 203const PHASE_MAPPING_N: f32 = 0.5; 204fn sample_scattering_lut(r: f32, neg_LdotV: f32) -> vec3<f32> { 205 let normalized_altitude = (r - atmosphere.inner_radius) / (atmosphere.outer_radius - atmosphere.inner_radius); 206 let phase_uv = 0.5 + 0.5 * sign(neg_LdotV) * (1.0 - pow(1.0 - abs(neg_LdotV), PHASE_MAPPING_N)); 207 let uv = vec2(1.0 - normalized_altitude, phase_uv); 208 return textureSampleLevel(medium_scattering_lut, medium_sampler, uv, 0.0).xyz; 212fn sample_local_inscattering(local_scattering: vec3<f32>, ray_dir: vec3<f32>, world_pos: vec3<f32>) -> vec3<f32> { 213 let local_r = length(world_pos); 214 let local_up = normalize(world_pos); 215 var inscattering = vec3(0.0); 216 for (var light_i: u32 = 0u; light_i < lights.n_directional_lights; light_i++) { 217 let light = &lights.directional_lights[light_i]; 219 let mu_light = dot((*light).direction_to_light, local_up); 223 let neg_LdotV = dot((*light).direction_to_light, ray_dir); 225 let transmittance_to_light = sample_transmittance_lut(local_r, mu_light); 226 let shadow_factor = transmittance_to_light * f32(!ray_intersects_ground(local_r, mu_light)); 227 let scattering_coeff = sample_scattering_lut(local_r, neg_LdotV); 230 let scattering_factor = shadow_factor * scattering_coeff; 233 let psi_ms = sample_multiscattering_lut(local_r, mu_light); 234 let multiscattering_factor = psi_ms * local_scattering; 236 inscattering += (*light).color.rgb * (scattering_factor + multiscattering_factor); 241fn sample_sun_radiance(ray_dir_ws: vec3<f32>) -> vec3<f32> { 242 let view_pos = get_view_position(); 243 let r = length(view_pos); 244 let up = normalize(view_pos); 245 let mu_view = dot(ray_dir_ws, up); 246 let shadow_factor = f32(!ray_intersects_ground(r, mu_view)); 247 var sun_radiance = vec3(0.0); 248 for (var light_i: u32 = 0u; light_i < lights.n_directional_lights; light_i++) { 249 let light = &lights.directional_lights[light_i]; 250 let neg_LdotV = dot((*light).direction_to_light, ray_dir_ws); 251 let angle_to_sun = fast_acos(clamp(neg_LdotV, -1.0, 1.0)); 252 let w = max(0.5 * fwidth(angle_to_sun), 1e-6); 253 let sun_angular_size = (*light).sun_disk_angular_size; 254 let sun_intensity = (*light).sun_disk_intensity; 255 if sun_angular_size > 0.0 && sun_intensity > 0.0 { 256 let factor = 1 - smoothstep(sun_angular_size * 0.5 - w, sun_angular_size * 0.5 + w, angle_to_sun); 257 let sun_solid_angle = (sun_angular_size * sun_angular_size) * 0.25 * PI; 258 sun_radiance += ((*light).color.rgb / sun_solid_angle) * sun_intensity * factor * shadow_factor; 264fn calculate_visible_sun_ratio(atmosphere: Atmosphere, r: f32, mu: f32, sun_angular_size: f32) -> f32 { 265 let inner_radius = atmosphere.inner_radius; 268 let horizon_cos = -sqrt(1.0 - (inner_radius * inner_radius) / (r * r)); 269 let horizon_angle = fast_acos_4(horizon_cos); 270 let sun_zenith_angle = fast_acos_4(mu); 273 if sun_zenith_angle + sun_angular_size * 0.5 <= horizon_angle { 278 if sun_zenith_angle - sun_angular_size * 0.5 >= horizon_angle { 283 let d = (horizon_angle - sun_zenith_angle) / (sun_angular_size * 0.5); 284 let visible_ratio = 0.5 + d * 0.5; 285 return clamp(visible_ratio, 0.0, 1.0); 291fn clamp_to_surface(atmosphere: Atmosphere, position: vec3<f32>) -> vec3<f32> { 292 let min_radius = atmosphere.inner_radius + EPSILON; 293 let r = length(position); 295 let up = normalize(position); 296 return up * min_radius; 301fn max_atmosphere_distance(r: f32, mu: f32) -> f32 { 302 let t_top = distance_to_top_atmosphere_boundary(atmosphere, r, mu); 303 let t_bottom = distance_to_bottom_atmosphere_boundary(r, mu); 304 let hits = ray_intersects_ground(r, mu); 305 return mix(t_top, t_bottom, f32(hits)); 309fn get_view_position() -> vec3<f32> { 310 let atmo_pos = (atmosphere.world_to_atmosphere * vec4(view.world_position, 1.0)).xyz; 311 return clamp_to_surface(atmosphere, atmo_pos); 317fn get_local_up(r: f32, t: f32, ray_dir: vec3<f32>) -> vec3<f32> { 318 return normalize(vec3(0.0, r, 0.0) + t * ray_dir); 323fn get_local_r(r: f32, mu: f32, t: f32) -> f32 { 324 return sqrt(t * t + 2.0 * r * mu * t + r * r); 328fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> { 329 return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0); 333fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> { 334 return ndc * vec2(0.5, -0.5) + vec2(0.5); 338fn direction_world_to_atmosphere(dir_ws: vec3<f32>) -> vec3<f32> { 339 let dir_as = atmosphere_transforms.atmosphere_from_world * vec4(dir_ws, 0.0); 344fn direction_atmosphere_to_world(dir_as: vec3<f32>) -> vec3<f32> { 345 let dir_ws = atmosphere_transforms.world_from_atmosphere * vec4(dir_as, 0.0); 351fn uv_to_ray_direction(uv: vec2<f32>) -> vec3<f32> { 360 let view_position_homogeneous = view.view_from_clip * vec4( 366 let view_ray_direction = view_position_homogeneous.xyz / view_position_homogeneous.w; 371 let ray_direction = (view.world_from_view * vec4(view_ray_direction, 0.0)).xyz; 373 return normalize(ray_direction); 376fn zenith_azimuth_to_ray_dir(zenith: f32, azimuth: f32) -> vec3<f32> { 377 let sin_zenith = sin(zenith); 378 let mu = cos(zenith); 379 let sin_azimuth = sin(azimuth); 380 let cos_azimuth = cos(azimuth); 381 return vec3(sin_azimuth * sin_zenith, mu, -cos_azimuth * sin_zenith); 384struct RaymarchSegment { 389fn get_raymarch_segment(r: f32, mu: f32) -> RaymarchSegment { 391 let atmosphere_intersections = ray_sphere_intersect(r, mu, atmosphere.outer_radius); 392 let ground_intersections = ray_sphere_intersect(r, mu, atmosphere.inner_radius); 394 var segment: RaymarchSegment; 396 if r < atmosphere.inner_radius { 398 segment.start = ground_intersections.y; 399 segment.end = atmosphere_intersections.y; 400 } else if r < atmosphere.outer_radius { 403 segment.end = select(atmosphere_intersections.y, ground_intersections.x, ray_intersects_ground(r, mu)); 406 if atmosphere_intersections.x < 0.0 { 411 segment.start = atmosphere_intersections.x; 412 segment.end = select(atmosphere_intersections.y, ground_intersections.x, ray_intersects_ground(r, mu)); 418struct RaymarchResult { 419 inscattering: vec3<f32>, 420 transmittance: vec3<f32>, 423fn raymarch_atmosphere( 432 let up = normalize(pos); 433 let mu = dot(ray_dir, up); 436 let sample_count = mix(1.0, f32(max_samples), saturate(t_max * 0.01)); 438 let segment = get_raymarch_segment(r, mu); 439 let t_start = segment.start; 440 var t_end = segment.end; 442 t_end = min(t_end, t_max); 443 let t_total = t_end - t_start; 445 var result: RaymarchResult; 446 result.inscattering = vec3(0.0); 447 result.transmittance = vec3(1.0); 454 var prev_t = t_start; 455 var optical_depth = vec3(0.0); 456 for (var s = 0.0; s < sample_count; s += 1.0) { 458 let t_i = t_start + t_total * (s + MIDPOINT_RATIO) / sample_count; 459 let dt_i = (t_i - prev_t); 462 let sample_pos = pos + ray_dir * t_i; 463 let local_r = length(sample_pos); 464 let local_up = normalize(sample_pos); 466 let absorption = sample_density_lut(local_r, ABSORPTION_DENSITY); 467 let scattering = sample_density_lut(local_r, SCATTERING_DENSITY); 468 let extinction = absorption + scattering; 470 let sample_optical_depth = extinction * dt_i; 471 optical_depth += sample_optical_depth; 472 let sample_transmittance = exp(-sample_optical_depth); 474 let inscattering = sample_local_inscattering( 480 let s_int = (inscattering - inscattering * sample_transmittance) / max(extinction, MIN_EXTINCTION); 481 result.inscattering += result.transmittance * s_int; 483 result.transmittance *= sample_transmittance; 484 if all(result.transmittance < vec3(0.001)) { 490 if ground && ray_intersects_ground(r, mu) { 491 for (var light_i: u32 = 0u; light_i < lights.n_directional_lights; light_i++) { 492 let light = &lights.directional_lights[light_i]; 493 let light_dir = (*light).direction_to_light; 494 let light_color = (*light).color.rgb; 495 let transmittance_to_ground = exp(-optical_depth); 497 let sphere_point = pos + ray_dir * t_end; 498 let sphere_normal = normalize(sphere_point); 499 let mu_light = dot(light_dir, sphere_normal); 500 let transmittance_to_light = sample_transmittance_lut(0.0, mu_light); 501 let light_luminance = transmittance_to_light * max(mu_light, 0.0) * light_color; 503 let ground_luminance = transmittance_to_ground * atmosphere.ground_albedo / PI; 504 result.inscattering += ground_luminance * light_luminance;