From faa18bbd8cbf0bb01b58edafe63bb585acefa4c4 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 14 Sep 2025 12:35:20 -0400 Subject: [PATCH] I have no idea what i'm doing --- assets/trace.wgsl | 30 ++++++++++++++++++------------ src/app.rs | 13 +++++++------ src/render/pipeline.rs | 18 ++++++++---------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/assets/trace.wgsl b/assets/trace.wgsl index ebe2fe9..e132b74 100644 --- a/assets/trace.wgsl +++ b/assets/trace.wgsl @@ -39,16 +39,22 @@ fn update(@builtin(global_invocation_id) invocation_id: vec3) { let loc = vec2(f32(invocation_id.x), f32(invocation_id.y)) / vec2(size.xy); let ndc = loc * 2.0f - 1.0f; - var ray = createCameraRay(ndc); - var result = vec3(0.0, 0.0, 0.0); + var ray = createCameraRay2(ndc); + var result = vec3(0.0); - var hit = trace(ray); - result += ray.energy * shade(&ray, hit); + for (var i: i32 = 0; i < 8; i++){ + var hit = trace(ray); + result += ray.energy * shade(&ray, hit); + if !any(ray.energy != vec3(0.0)) + { + break; + } + } - let clip = vec4(ndc, 0.0, 1.0); - let world = config.world_from_clip * clip; - let world_pos = world.xyz / world.w; - let dir = normalize(world_pos - config.world_position); + // let clip = vec4(ndc, 0.0, 1.0); + // let world = config.world_from_clip * clip; + // let world_pos = world.xyz / world.w; + // let dir = normalize(world_pos - config.world_position); // let color = vec4((ray.direction * 0.5) + vec3(0.5), 1.0); @@ -84,7 +90,7 @@ struct Sphere fn createRayHit() -> RayHit { var hit: RayHit; hit.position = vec3(0.0, 0.0, 0.0); - hit.distance = 9999999.0f; + hit.distance = 999999999999.0f; hit.normal = vec3(0.0, 0.0, 0.0); hit.albedo = vec3(0.0, 0.0, 0.0); hit.specular = vec3(0.0, 0.0, 0.0); @@ -141,7 +147,7 @@ fn createSphere(position: vec3, radius: f32) -> Sphere fn intersectSphere(ray: Ray, bestHit: ptr, sphereIndex: u32) { //Sphere sphere = _Spheres[sphereIndex]; - var sphere = createSphere(vec3(0.0f, 0.0f, 0.0f), 2.0f); + var sphere = createSphere(vec3(0.0f, 0.0f, 0.0f), 1.0f); var d = ray.origin - sphere.position; @@ -184,15 +190,15 @@ fn intersectGroundPlane(ray: Ray, bestHit: ptr) fn trace(ray: Ray) -> RayHit { var bestHit = createRayHit(); - intersectGroundPlane(ray, &bestHit); intersectSphere(ray, &bestHit, 0); + intersectGroundPlane(ray, &bestHit); return bestHit; } fn shade(ray: ptr, hit: RayHit) -> vec3 { - if hit.distance < 9999999.0f + if hit.distance < 999999999999.0f { (*ray).origin = hit.position + hit.normal * 0.001f; (*ray).direction = reflect((*ray).direction, hit.normal); diff --git a/src/app.rs b/src/app.rs index 9f3324a..6efb688 100644 --- a/src/app.rs +++ b/src/app.rs @@ -83,13 +83,14 @@ fn setup(mut commands: Commands, mut images: ResMut>, window: Sing commands.spawn(Camera2d); commands.spawn(( - // Camera3d::default(), - Projection::Perspective(PerspectiveProjection { - aspect_ratio: size.x as f32 / size.y as f32, - ..default() - }), + Camera3d::default(), + Camera { order: -1, ..default() }, + // Projection::Perspective(PerspectiveProjection { + // aspect_ratio: size.x as f32 / size.y as f32, + // ..default() + // }), RTCamera, - Transform::from_xyz(0.0, 0.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y), + Transform::from_xyz(0.0, 5.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), Name::new("RT Camera"), )); diff --git a/src/render/pipeline.rs b/src/render/pipeline.rs index 3321450..98796d9 100644 --- a/src/render/pipeline.rs +++ b/src/render/pipeline.rs @@ -174,21 +174,19 @@ pub struct TracerImageBindGroups(pub [BindGroup; 2]); fn update_tracer_uniforms( mut tracer_uniforms: ResMut, - rt_camera: Single<(&GlobalTransform, &Projection, &Camera), With>, + rt_camera: Single<(&GlobalTransform, &Camera), With>, ) { - let (transform, projection, cam) = rt_camera.into_inner(); + let (transform, cam) = rt_camera.into_inner(); + let view = transform.compute_matrix().inverse(); - let clip_from_view = match projection { - Projection::Perspective(perspective_projection) => perspective_projection.get_clip_from_view(), - _ => unreachable!("This should never happen: Invalid projection type on RT Camera"), - }; - let clip_from_world = clip_from_view * view; - let world_from_clip = clip_from_world.inverse(); - // info!("clip_from_view = {:?}", clip_from_view); - // info!("world_from_clip = {:?}", world_from_clip); + let clip_from_view = cam.clip_from_view(); + let world_from_clip = view * clip_from_view.inverse(); tracer_uniforms.world_from_clip = world_from_clip; tracer_uniforms.world_position = transform.translation(); + + // info!("clip_from_view = {:?}", clip_from_view); + // info!("world_from_clip = {:?}", world_from_clip); } fn prepare_bind_groups(