feat: per-pixel camera ray generation

This commit is contained in:
xfy 2026-07-10 10:36:18 +08:00
parent 265ec9925d
commit 9665d12348
2 changed files with 22 additions and 3 deletions

View File

@ -1,4 +1,5 @@
#import bevy_sprite::mesh2d_vertex_output::VertexOutput
#import "shaders/ray_gen.wgsl"
struct BlackHoleUniforms {
eye: vec4<f32>,
@ -30,7 +31,11 @@ struct BlackHoleUniforms {
@fragment
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> {
// Prove the camera basis + uniforms flow: tint by eye direction.
let n = normalize(uniforms.eye.xyz);
return vec4<f32>(abs(n) * 0.5 + 0.3, 1.0);
// in.uv is [0,1] across the quad. Center and flip y, apply aspect.
let aspect = uniforms.resolution.x / uniforms.resolution.y;
var uv = (in.uv * 2.0 - 1.0);
uv.x *= aspect;
let dir = ray_direction(uv);
// Visualize ray direction as a color (sanity check).
return vec4<f32>(abs(dir), 1.0);
}

View File

@ -0,0 +1,14 @@
// Builds a world-space camera ray direction for the current pixel.
// `uv` is the pixel coordinate normalized to [-1,1] with aspect correction.
fn ray_direction(uv: vec2<f32>) -> vec3<f32> {
// NOTE: `fov` is packed into the `.w` of `up` in BlackHoleUniforms
// (the Rust struct lays out `up: Vec3` + `fov: f32` as one vec4 block).
// The WGSL struct must stay exactly as-is per the task spec, so read fov
// from `uniforms.up.w` rather than a separate `uniforms.fov` field.
let tan_half_fov = tan(uniforms.up.w * 0.5);
let dir =
normalize(uniforms.forward.xyz)
+ uniforms.right.xyz * (uv.x * tan_half_fov)
+ uniforms.up.xyz * (uv.y * tan_half_fov);
return normalize(dir);
}