From a160d4d0687009e6954a03d073db1518f6c50ee6 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sat, 13 Sep 2025 19:28:53 -0400 Subject: [PATCH] fixes to world_from_clip --- assets/trace.wgsl | 53 ++++++++++++++++-------------------------- src/app.rs | 2 +- src/render/pipeline.rs | 7 +++--- 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/assets/trace.wgsl b/assets/trace.wgsl index 46b6d61..ebe2fe9 100644 --- a/assets/trace.wgsl +++ b/assets/trace.wgsl @@ -37,37 +37,27 @@ fn update(@builtin(global_invocation_id) invocation_id: vec3) { let size = textureDimensions(output); let loc = vec2(f32(invocation_id.x), f32(invocation_id.y)) / vec2(size.xy); - // let ndc = loc * 2.0f - 1.0f; + let ndc = loc * 2.0f - 1.0f; - // var ray = createCameraRay(ndc); + var ray = createCameraRay(ndc); + var result = vec3(0.0, 0.0, 0.0); - // var result = vec3(0.0f); - - // var hit = trace(ray); - // result += ray.energy * shade(&ray, hit); - - // var rayTest = createCameraRay2(ndc); - // let dirColor = (rayTest.direction * 0.5) + vec3(0.5, 0.5, 0.5); - // let color = vec4(dirColor, 1.0); - - let ndc = vec2( - (f32(invocation_id.x) / f32(size.x)) * 2.0 - 1.0, - (f32(invocation_id.y) / f32(size.y)) * 2.0 - 1.0 - ); + var hit = trace(ray); + result += ray.energy * shade(&ray, hit); 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); - // map directly to color - let color = vec4((world_pos * 0.1) + vec3(0.5), 1.0); + // let color = vec4((ray.direction * 0.5) + vec3(0.5), 1.0); + let color = vec4(result, 1.0); let location = vec2(i32(invocation_id.x), i32(invocation_id.y)); textureStore(output, location, color); } - struct Ray { origin: vec3, direction: vec3, @@ -94,14 +84,13 @@ struct Sphere fn createRayHit() -> RayHit { var hit: RayHit; hit.position = vec3(0.0, 0.0, 0.0); - hit.distance = -1.0f; // A negative number to represent infinity + hit.distance = 9999999.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); return hit; } - fn createRay(origin: vec3, direction: vec3) -> Ray { var ray: Ray; @@ -111,15 +100,13 @@ fn createRay(origin: vec3, direction: vec3) -> Ray return ray; } - fn createCameraRay(ndc: vec2) -> Ray { - let origin = config.world_position; - let target_point = config.world_from_clip * vec4(ndc, 0.0f, 1.0f); + let target_point = config.world_from_clip * vec4(ndc, 0.0, 1.0); let direction_point = target_point.xyz / target_point.w; - let direction = normalize(direction_point - origin); + let direction = normalize(direction_point - config.world_position); - return createRay(origin, direction); + return createRay(config.world_position, direction); } fn createCameraRay2(ndc: vec2) -> Ray { @@ -154,7 +141,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, -10.0f), 1.0f); + var sphere = createSphere(vec3(0.0f, 0.0f, 0.0f), 2.0f); var d = ray.origin - sphere.position; @@ -189,7 +176,7 @@ fn intersectGroundPlane(ray: Ray, bestHit: ptr) (*bestHit).distance = t; (*bestHit).position = ray.origin + t * ray.direction; (*bestHit).normal = vec3(0.0f, 1.0f, 0.0f); - (*bestHit).albedo = vec3(0.8f); + (*bestHit).albedo = vec3(0.1f); (*bestHit).specular = vec3(0.3f); } } @@ -205,7 +192,7 @@ fn trace(ray: Ray) -> RayHit fn shade(ray: ptr, hit: RayHit) -> vec3 { - if hit.distance > -1.0f + if hit.distance < 9999999.0f { (*ray).origin = hit.position + hit.normal * 0.001f; (*ray).direction = reflect((*ray).direction, hit.normal); @@ -213,9 +200,9 @@ fn shade(ray: ptr, hit: RayHit) -> vec3 //Shadows // var shadow = false; - // Ray shadowRay = createRay(hit.position + hit.normal * 0.001f, -1 * _DirectionalLight.xyz); - // RayHit shadowHit = Trace(shadowRay); - // if (shadowHit.distance != 1.#INF) + // var shadowRay = createRay(hit.position + hit.normal * 0.001f, -1 * _DirectionalLight.xyz); + // var shadowHit = trace(shadowRay); + // if (shadowHit.distance != 9999999.0f) // { // return float3(0.0f, 0.0f, 0.0f); // } @@ -227,8 +214,8 @@ fn shade(ray: ptr, hit: RayHit) -> vec3 { (*ray).energy = vec3(0.0f); return config.sky_color.xyz; - // var theta = acos(ray.direction.y) / -PI; - // var phi = atan2(ray.direction.x, -ray.direction.z) / -PI * .5f; + // var theta = acos((*ray).direction.y) / -PI; + // var phi = atan2((*ray).direction.x, -(*ray).direction.z) / -PI * .5f; // return _SkyboxTexture.SampleLevel(sampler_SkyboxTexture, float2(phi, theta), 0).xyz; } } diff --git a/src/app.rs b/src/app.rs index 9d3c5c2..9f3324a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -24,7 +24,7 @@ impl Plugin for Blackhole { app.add_systems(Startup, setup); app.add_plugins(TracerPipelinePlugin); app.insert_resource(TracerUniforms { - sky_color: LinearRgba::BLUE, + sky_color: LinearRgba::rgb(0.1, 0.0, 0.01), ..default() }); diff --git a/src/render/pipeline.rs b/src/render/pipeline.rs index a89c2f8..3321450 100644 --- a/src/render/pipeline.rs +++ b/src/render/pipeline.rs @@ -174,9 +174,9 @@ pub struct TracerImageBindGroups(pub [BindGroup; 2]); fn update_tracer_uniforms( mut tracer_uniforms: ResMut, - rt_camera: Single<(&GlobalTransform, &Projection), With>, + rt_camera: Single<(&GlobalTransform, &Projection, &Camera), With>, ) { - let (transform, projection) = rt_camera.into_inner(); + let (transform, projection, 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(), @@ -184,7 +184,8 @@ fn update_tracer_uniforms( }; let clip_from_world = clip_from_view * view; let world_from_clip = clip_from_world.inverse(); - info_once!("world_from_clip = {:?}", world_from_clip); + // info!("clip_from_view = {:?}", clip_from_view); + // info!("world_from_clip = {:?}", world_from_clip); tracer_uniforms.world_from_clip = world_from_clip; tracer_uniforms.world_position = transform.translation();