Skip to content
Merged
Prev Previous commit
Next Next commit
fix(render): use one screen-y mapping for fog and shafts
Fog froxels and the shaft sun follow the sky and pbr mapping:
ndc.y = 1 - 2*uv.y, uv.y = 0.5 - 0.5*ndc.y. The pixel test samples
an upper sky row and a lower ground row with an off-center sun, so a
mirrored composite fails. The budget camera far plane is GAME_ZFAR.

Co-authored-by: Andrew Brown <lazyxeon@users.noreply.github.com>
  • Loading branch information
cursoragent and lazyxeon committed Oct 21, 2026
commit 91cff899b96a88f0ed2dd2321b758f7af05c0e3e
2 changes: 1 addition & 1 deletion astraweave-camera/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ pub use freefly::{CameraController, CameraMode, FreeFly};
pub use producer::CameraProducer;
pub use projection::Projection;
pub use render_view::RenderView;
pub use third_person::ThirdPersonCamera;
pub use third_person::{ThirdPersonCamera, GAME_ZFAR};
5 changes: 4 additions & 1 deletion astraweave-camera/src/third_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use glam::{Mat4, Vec3};
use crate::freefly::FreeFly;
use crate::{CameraProducer, Projection, RenderView};

/// Far plane of the Aravath third-person camera. The heavy-scene budget uses it.
pub const GAME_ZFAR: f32 = 500.0;

/// Follow camera orbiting a pivot (usually the character center).
pub struct ThirdPersonCamera {
pub pivot: Vec3,
Expand All @@ -34,7 +37,7 @@ impl ThirdPersonCamera {
fovy: 60_f32.to_radians(),
aspect: 16.0 / 9.0,
znear: 0.1,
zfar: 500.0,
zfar: GAME_ZFAR,
}
}

Expand Down
10 changes: 10 additions & 0 deletions astraweave-render/shaders/screen_ndc.wgsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Screen UV for this Y-up view. uv.y 0 is the top of the texture, NDC y +1.
// pbr.wgsl uses the same pair: ndc.y = 1 - 2*uv.y, uv.y = 0.5 - 0.5*ndc.y.

fn screen_uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return vec2<f32>(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0);
}

fn ndc_to_screen_uv(ndc: vec2<f32>) -> vec2<f32> {
return vec2<f32>(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5);
}
2 changes: 1 addition & 1 deletion astraweave-render/shaders/volumetrics/fog_density.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn froxel_to_world(froxel: vec3<u32>) -> vec3<f32> {
let depth = z_near * pow(z_far / z_near, linear_t);

// Unproject screen UV + depth to world space
let ndc_xy = screen_uv * 2.0 - 1.0;
let ndc_xy = screen_uv_to_ndc(screen_uv);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
// Convert linear depth to NDC depth (reverse-Z or standard)
let ndc_z = (z_far * (depth - z_near)) / (depth * (z_far - z_near));

Expand Down
2 changes: 1 addition & 1 deletion astraweave-render/shaders/volumetrics/scatter.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn froxel_to_world(froxel: vec3<u32>) -> vec3<f32> {
let z_near = params.near_plane;
let z_far = params.far_plane;
let depth = z_near * pow(z_far / z_near, uvw.z);
let ndc_xy = screen_uv * 2.0 - 1.0;
let ndc_xy = screen_uv_to_ndc(screen_uv);
let ndc_z = (z_far * (depth - z_near)) / (depth * (z_far - z_near));
let clip = vec4<f32>(ndc_xy, ndc_z, 1.0);
let world_h = params.inv_view_proj * clip;
Expand Down
2 changes: 1 addition & 1 deletion astraweave-render/src/bin/gpu_frame_budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ fn fixed_camera() -> RenderView {
60_f32.to_radians(),
WIDTH as f32 / HEIGHT as f32,
0.1,
200.0,
astraweave_camera::GAME_ZFAR,
);
RenderView::new(
view,
Expand Down
29 changes: 27 additions & 2 deletions astraweave-render/src/god_rays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl GodRayPass {

let (sun_screen, visible) = if clip.w > 0.0 {
let ndc = glam::Vec2::new(clip.x / clip.w, clip.y / clip.w);
let uv = ndc * 0.5 + 0.5;
let uv = ndc_to_screen_uv(ndc);
// Check if on screen (with some margin)
let on_screen = uv.x >= -0.2 && uv.x <= 1.2 && uv.y >= -0.2 && uv.y <= 1.2;
(uv.to_array(), if on_screen { 1.0_f32 } else { 0.0 })
Expand Down Expand Up @@ -583,14 +583,19 @@ impl GodRayPass {
}
}

/// Screen UV for this Y-up view. uv.y 0 is NDC y +1, matching `screen_ndc.wgsl`.
pub fn ndc_to_screen_uv(ndc: glam::Vec2) -> glam::Vec2 {
glam::Vec2::new(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5)
}

/// Project a sun direction to screen-space UV (CPU utility).
pub fn sun_to_screen(view_proj: Mat4, sun_dir: Vec3) -> Option<[f32; 2]> {
let clip = view_proj * (sun_dir * 10000.0).extend(1.0);
if clip.w <= 0.0 {
return None;
}
let ndc = glam::Vec2::new(clip.x / clip.w, clip.y / clip.w);
let uv = ndc * 0.5 + 0.5;
let uv = ndc_to_screen_uv(ndc);
if uv.x >= -0.2 && uv.x <= 1.2 && uv.y >= -0.2 && uv.y <= 1.2 {
Some(uv.to_array())
} else {
Expand Down Expand Up @@ -639,6 +644,26 @@ mod tests {
assert!((v - 0.5).abs() < 0.1, "v={v}");
}

#[test]
fn sun_above_the_view_is_the_top_of_the_texture() {
let vp = Mat4::perspective_rh(1.0, 1.0, 0.1, 1000.0)
* Mat4::look_at_rh(Vec3::ZERO, Vec3::NEG_Z, Vec3::Y);
let [u, v] = sun_to_screen(vp, Vec3::new(0.25, 0.35, -1.0).normalize()).expect("in front");
assert!((u - 0.5).abs() > 0.02, "sun should sit off center, u={u}");
assert!(
v < 0.5,
"uv.y {v} should be the top half, not the mirrored bottom"
);
}

#[test]
fn ndc_y_up_is_the_top_of_the_texture() {
let top = ndc_to_screen_uv(glam::Vec2::new(0.0, 1.0));
let bottom = ndc_to_screen_uv(glam::Vec2::new(0.0, -1.0));
assert!(top.y.abs() < 1e-5, "uv.y {}", top.y);
assert!((bottom.y - 1.0).abs() < 1e-5, "uv.y {}", bottom.y);
}

#[test]
fn god_ray_pass_creation() {
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor::default());
Expand Down
6 changes: 5 additions & 1 deletion astraweave-render/src/volumetric_fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,10 @@ impl VolumetricFogPass {
device,
&density_bgl,
"vol_density",
include_str!("../shaders/volumetrics/fog_density.wgsl"),
concat!(
include_str!("../shaders/screen_ndc.wgsl"),
include_str!("../shaders/volumetrics/fog_density.wgsl")
),
"fog_density_main",
);
let scatter_pipeline = create_compute_pipeline(
Expand All @@ -504,6 +507,7 @@ impl VolumetricFogPass {
"vol_scatter",
concat!(
include_str!("../shaders/constants.wgsl"),
include_str!("../shaders/screen_ndc.wgsl"),
include_str!("../shaders/volumetrics/scatter.wgsl")
),
"scatter_main",
Expand Down
39 changes: 28 additions & 11 deletions astraweave-render/tests/gated_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,23 +276,30 @@ fn atmosphere_sky_differs_from_geometry_and_aerial_tints_distance() {
"aerial perspective did not change the far quad: on {far:?} off {far_off:?} sky {sky:?}"
);
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

// Aim the sun at the empty sky pixel so shafts have a source there.
renderer.set_light_direction_override(glam::Vec3::new(-0.9, 0.0, 0.4), 1.0);
// Sun in the upper-right, not on the horizon. A mirrored screen y
// puts that sun and the dense height fog on the lower row instead.
renderer.set_light_direction_override(glam::Vec3::new(-0.55, -0.72, 0.42), 1.0);
renderer.update_view(&astraweave_camera::RenderView::new(
view,
&projection,
eye,
(target - eye).normalize(),
));
let sky_only = sample(&draw_and_read(&mut renderer, true), WIDTH, 60, 32);
const UPPER: (u32, u32) = (56, 6);
const LOWER: (u32, u32) = (56, 58);
let frame_only = draw_and_read(&mut renderer, true);
let up_only = sample(&frame_only, WIDTH, UPPER.0, UPPER.1);
let lo_only = sample(&frame_only, WIDTH, LOWER.0, LOWER.1);
renderer.set_volumetric_fog_config(
astraweave_render::volumetric_fog::VolumetricFogPass::gameplay_config(),
);
assert!(
renderer.volumetric_fog_enabled(),
"fog pipeline did not build"
);
let sky_fog = sample(&draw_and_read(&mut renderer, true), WIDTH, 60, 32);
let frame_fog = draw_and_read(&mut renderer, true);
let up_fog = sample(&frame_fog, WIDTH, UPPER.0, UPPER.1);
let lo_fog = sample(&frame_fog, WIDTH, LOWER.0, LOWER.1);
renderer.set_god_ray_config(astraweave_render::GodRayConfig {
enabled: true,
half_res: true,
Expand All @@ -301,18 +308,28 @@ fn atmosphere_sky_differs_from_geometry_and_aerial_tints_distance() {
..astraweave_render::GodRayConfig::default()
});
assert!(renderer.god_rays_enabled(), "shaft pipeline did not build");
let sky_both = sample(&draw_and_read(&mut renderer, true), WIDTH, 60, 32);
let frame_both = draw_and_read(&mut renderer, true);
let up_both = sample(&frame_both, WIDTH, UPPER.0, UPPER.1);
let lo_both = sample(&frame_both, WIDTH, LOWER.0, LOWER.1);
let fog_upper = channel_distance(up_fog, up_only);
let fog_lower = channel_distance(lo_fog, lo_only);
assert!(
channel_distance(sky_fog, sky_only) > 8,
"fog did not survive on the sky pixel: fog {sky_fog:?} sky {sky_only:?}"
fog_lower > fog_upper + 6,
"height fog should change the lower view more than the upper sky. \
A mirrored y swaps them. upper {fog_upper} {up_only:?}->{up_fog:?} \
lower {fog_lower} {lo_only:?}->{lo_fog:?}"
);
let shaft_upper = channel_distance(up_both, up_fog);
let shaft_lower = channel_distance(lo_both, lo_fog);
assert!(
channel_distance(sky_both, sky_fog) > 5,
"shafts did not survive on the sky pixel: both {sky_both:?} fog {sky_fog:?}"
shaft_upper > shaft_lower + 4,
"shafts should land on the upper sun, not the mirrored lower row. \
upper {shaft_upper} {up_fog:?}->{up_both:?} \
lower {shaft_lower} {lo_fog:?}->{lo_both:?}"
);
assert!(
channel_distance(sky_both, magenta) > 40,
"fogged sky {sky_both:?} is the debug magenta clear {magenta:?}"
channel_distance(up_both, magenta) > 40,
"upper sky {up_both:?} is the debug magenta clear {magenta:?}"
);
});
}