-
Notifications
You must be signed in to change notification settings - Fork 0
WIP: Atmosphere PBR support #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a7d7c8e
600cbe1
81d6b40
b1ce73f
b48003d
3108faf
a7a65ac
2eaaf84
a7ad0ca
969418d
d060aa7
73f49f3
975dd97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,19 +64,19 @@ | |
| // Assuming r between ground and top atmosphere boundary, and mu= cos(zenith_angle) | ||
| // Chosen to increase precision near the ground and to work around a discontinuity at the horizon | ||
| // See Bruneton and Neyret 2008, "Precomputed Atmospheric Scattering" section 4 | ||
| fn transmittance_lut_r_mu_to_uv(r: f32, mu: f32) -> vec2<f32> { | ||
| fn transmittance_lut_r_mu_to_uv(atm: Atmosphere, r: f32, mu: f32) -> vec2<f32> { | ||
| // Distance along a horizontal ray from the ground to the top atmosphere boundary | ||
| let H = sqrt(atmosphere.top_radius * atmosphere.top_radius - atmosphere.bottom_radius * atmosphere.bottom_radius); | ||
| let H = sqrt(atm.top_radius * atm.top_radius - atm.bottom_radius * atm.bottom_radius); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this atm now to avoid name conflict with a binding of the same name? i prefer
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that's right I was getting name conflicts. Any tips on how to keep it
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think putting this function in a file that doesnt import the binding should work, not sure though |
||
|
|
||
| // Distance from a point at height r to the horizon | ||
| // ignore the case where r <= atmosphere.bottom_radius | ||
| let rho = sqrt(max(r * r - atmosphere.bottom_radius * atmosphere.bottom_radius, 0.0)); | ||
| let rho = sqrt(max(r * r - atm.bottom_radius * atm.bottom_radius, 0.0)); | ||
|
|
||
| // Distance from a point at height r to the top atmosphere boundary at zenith angle mu | ||
| let d = distance_to_top_atmosphere_boundary(r, mu); | ||
| let d = distance_to_top_atmosphere_boundary(atm, r, mu); | ||
|
|
||
| // Minimum and maximum distance to the top atmosphere boundary from a point at height r | ||
| let d_min = atmosphere.top_radius - r; // length of the ray straight up to the top atmosphere boundary | ||
| let d_min = atm.top_radius - r; // length of the ray straight up to the top atmosphere boundary | ||
| let d_max = rho + H; // length of the ray to the top atmosphere boundary and grazing the horizon | ||
|
|
||
| let u = (d - d_min) / (d_max - d_min); | ||
|
|
@@ -121,9 +121,9 @@ fn transmittance_lut_uv_to_r_mu(uv: vec2<f32>) -> vec2<f32> { | |
| /// Center of sphere, c = [0,0,0] | ||
| /// Radius of sphere, r = atmosphere.top_radius | ||
| /// This function solves the quadratic equation for line-sphere intersection simplified under these assumptions | ||
| fn distance_to_top_atmosphere_boundary(r: f32, mu: f32) -> f32 { | ||
| // ignore the case where r > atmosphere.top_radius | ||
| let positive_discriminant = max(r * r * (mu * mu - 1.0) + atmosphere.top_radius * atmosphere.top_radius, 0.0); | ||
| fn distance_to_top_atmosphere_boundary(atm: Atmosphere, r: f32, mu: f32) -> f32 { | ||
| // ignore the case where r > atm.top_radius | ||
| let positive_discriminant = max(r * r * (mu * mu - 1.0) + atm.top_radius * atm.top_radius, 0.0); | ||
| return max(-r * mu + sqrt(positive_discriminant), 0.0); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #import bevy_render::maths::{PI} | ||
| #import bevy_pbr::{ | ||
| atmosphere::{ | ||
| types::{Atmosphere, AtmosphereSettings}, | ||
| bindings::{atmosphere, settings, probe_transform_buffer}, | ||
| functions::{max_atmosphere_distance, raymarch_atmosphere}, | ||
| }, | ||
| utils::sample_cube_dir | ||
| } | ||
|
|
||
| @group(0) @binding(13) var output: texture_storage_2d_array<rgba16float, write>; | ||
|
|
||
| @compute @workgroup_size(8, 8, 1) | ||
| fn main(@builtin(global_invocation_id) global_id: vec3<u32>) { | ||
| let dimensions = textureDimensions(output); | ||
| let slice_index = global_id.z; | ||
|
|
||
| if (global_id.x >= dimensions.x || global_id.y >= dimensions.y || slice_index >= 6u) { | ||
| return; | ||
| } | ||
|
|
||
| // Calculate normalized UV coordinates for this pixel | ||
| let uv = vec2<f32>( | ||
| (f32(global_id.x) + 0.5) / f32(dimensions.x), | ||
| (f32(global_id.y) + 0.5) / f32(dimensions.y) | ||
| ); | ||
|
|
||
| var world_pos = probe_transform_buffer[3].xyz; | ||
|
|
||
| // offset by the origin point of the atmosphere scene | ||
| world_pos += atmosphere.origin; | ||
|
|
||
| let r = length(world_pos); | ||
|
|
||
| let ray_dir_ws = sample_cube_dir(uv, slice_index); | ||
| let up = normalize(world_pos); | ||
| let mu = dot(ray_dir_ws, up); | ||
| let raymarch_steps = 16.0; | ||
| let t_max = max_atmosphere_distance(r, mu); | ||
| let sample_count = mix(1.0, raymarch_steps, clamp(t_max * 0.01, 0.0, 1.0)); | ||
| let result = raymarch_atmosphere(world_pos, ray_dir_ws, t_max, sample_count, uv, false, true, false); | ||
| let inscattering = result.inscattering; | ||
| let color = vec4<f32>(inscattering, 1.0); | ||
|
|
||
| textureStore(output, vec2<i32>(global_id.xy), i32(slice_index), color); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is POT-ness verified/asserted anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, good idea to add that assert
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert is panic, hold off on this for now, theres a pattern we gotta make for this