feat: per-pixel camera ray generation
This commit is contained in:
parent
265ec9925d
commit
9665d12348
@ -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);
|
||||
}
|
||||
|
||||
14
assets/shaders/ray_gen.wgsl
Normal file
14
assets/shaders/ray_gen.wgsl
Normal 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);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user