fixes to world_from_clip

This commit is contained in:
2025-09-13 19:28:53 -04:00
parent 275f4a905e
commit a160d4d068
3 changed files with 25 additions and 37 deletions

View File

@@ -37,37 +37,27 @@ fn update(@builtin(global_invocation_id) invocation_id: vec3<u32>) {
let size = textureDimensions(output); let size = textureDimensions(output);
let loc = vec2<f32>(f32(invocation_id.x), f32(invocation_id.y)) / vec2<f32>(size.xy); let loc = vec2<f32>(f32(invocation_id.x), f32(invocation_id.y)) / vec2<f32>(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<f32>(0.0, 0.0, 0.0);
// var result = vec3<f32>(0.0f); var hit = trace(ray);
result += ray.energy * shade(&ray, hit);
// var hit = trace(ray);
// result += ray.energy * shade(&ray, hit);
// var rayTest = createCameraRay2(ndc);
// let dirColor = (rayTest.direction * 0.5) + vec3<f32>(0.5, 0.5, 0.5);
// let color = vec4(dirColor, 1.0);
let ndc = vec2<f32>(
(f32(invocation_id.x) / f32(size.x)) * 2.0 - 1.0,
(f32(invocation_id.y) / f32(size.y)) * 2.0 - 1.0
);
let clip = vec4<f32>(ndc, 0.0, 1.0); let clip = vec4<f32>(ndc, 0.0, 1.0);
let world = config.world_from_clip * clip; let world = config.world_from_clip * clip;
let world_pos = world.xyz / world.w; let world_pos = world.xyz / world.w;
let dir = normalize(world_pos - config.world_position);
// map directly to color // let color = vec4<f32>((ray.direction * 0.5) + vec3<f32>(0.5), 1.0);
let color = vec4<f32>((world_pos * 0.1) + vec3<f32>(0.5), 1.0);
let color = vec4<f32>(result, 1.0);
let location = vec2<i32>(i32(invocation_id.x), i32(invocation_id.y)); let location = vec2<i32>(i32(invocation_id.x), i32(invocation_id.y));
textureStore(output, location, color); textureStore(output, location, color);
} }
struct Ray { struct Ray {
origin: vec3<f32>, origin: vec3<f32>,
direction: vec3<f32>, direction: vec3<f32>,
@@ -94,14 +84,13 @@ struct Sphere
fn createRayHit() -> RayHit { fn createRayHit() -> RayHit {
var hit: RayHit; var hit: RayHit;
hit.position = vec3<f32>(0.0, 0.0, 0.0); hit.position = vec3<f32>(0.0, 0.0, 0.0);
hit.distance = -1.0f; // A negative number to represent infinity hit.distance = 9999999.0f;
hit.normal = vec3<f32>(0.0, 0.0, 0.0); hit.normal = vec3<f32>(0.0, 0.0, 0.0);
hit.albedo = vec3<f32>(0.0, 0.0, 0.0); hit.albedo = vec3<f32>(0.0, 0.0, 0.0);
hit.specular = vec3<f32>(0.0, 0.0, 0.0); hit.specular = vec3<f32>(0.0, 0.0, 0.0);
return hit; return hit;
} }
fn createRay(origin: vec3<f32>, direction: vec3<f32>) -> Ray fn createRay(origin: vec3<f32>, direction: vec3<f32>) -> Ray
{ {
var ray: Ray; var ray: Ray;
@@ -111,15 +100,13 @@ fn createRay(origin: vec3<f32>, direction: vec3<f32>) -> Ray
return ray; return ray;
} }
fn createCameraRay(ndc: vec2<f32>) -> Ray { fn createCameraRay(ndc: vec2<f32>) -> Ray {
let origin = config.world_position; let target_point = config.world_from_clip * vec4<f32>(ndc, 0.0, 1.0);
let target_point = config.world_from_clip * vec4<f32>(ndc, 0.0f, 1.0f);
let direction_point = target_point.xyz / target_point.w; 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<f32>) -> Ray { fn createCameraRay2(ndc: vec2<f32>) -> Ray {
@@ -154,7 +141,7 @@ fn createSphere(position: vec3<f32>, radius: f32) -> Sphere
fn intersectSphere(ray: Ray, bestHit: ptr<function, RayHit>, sphereIndex: u32) fn intersectSphere(ray: Ray, bestHit: ptr<function, RayHit>, sphereIndex: u32)
{ {
//Sphere sphere = _Spheres[sphereIndex]; //Sphere sphere = _Spheres[sphereIndex];
var sphere = createSphere(vec3<f32>(0.0f, 0.0f, -10.0f), 1.0f); var sphere = createSphere(vec3<f32>(0.0f, 0.0f, 0.0f), 2.0f);
var d = ray.origin - sphere.position; var d = ray.origin - sphere.position;
@@ -189,7 +176,7 @@ fn intersectGroundPlane(ray: Ray, bestHit: ptr<function,RayHit>)
(*bestHit).distance = t; (*bestHit).distance = t;
(*bestHit).position = ray.origin + t * ray.direction; (*bestHit).position = ray.origin + t * ray.direction;
(*bestHit).normal = vec3<f32>(0.0f, 1.0f, 0.0f); (*bestHit).normal = vec3<f32>(0.0f, 1.0f, 0.0f);
(*bestHit).albedo = vec3(0.8f); (*bestHit).albedo = vec3(0.1f);
(*bestHit).specular = vec3(0.3f); (*bestHit).specular = vec3(0.3f);
} }
} }
@@ -205,7 +192,7 @@ fn trace(ray: Ray) -> RayHit
fn shade(ray: ptr<function, Ray>, hit: RayHit) -> vec3<f32> fn shade(ray: ptr<function, Ray>, hit: RayHit) -> vec3<f32>
{ {
if hit.distance > -1.0f if hit.distance < 9999999.0f
{ {
(*ray).origin = hit.position + hit.normal * 0.001f; (*ray).origin = hit.position + hit.normal * 0.001f;
(*ray).direction = reflect((*ray).direction, hit.normal); (*ray).direction = reflect((*ray).direction, hit.normal);
@@ -213,9 +200,9 @@ fn shade(ray: ptr<function, Ray>, hit: RayHit) -> vec3<f32>
//Shadows //Shadows
// var shadow = false; // var shadow = false;
// Ray shadowRay = createRay(hit.position + hit.normal * 0.001f, -1 * _DirectionalLight.xyz); // var shadowRay = createRay(hit.position + hit.normal * 0.001f, -1 * _DirectionalLight.xyz);
// RayHit shadowHit = Trace(shadowRay); // var shadowHit = trace(shadowRay);
// if (shadowHit.distance != 1.#INF) // if (shadowHit.distance != 9999999.0f)
// { // {
// return float3(0.0f, 0.0f, 0.0f); // return float3(0.0f, 0.0f, 0.0f);
// } // }
@@ -227,8 +214,8 @@ fn shade(ray: ptr<function, Ray>, hit: RayHit) -> vec3<f32>
{ {
(*ray).energy = vec3(0.0f); (*ray).energy = vec3(0.0f);
return config.sky_color.xyz; return config.sky_color.xyz;
// var theta = acos(ray.direction.y) / -PI; // var theta = acos((*ray).direction.y) / -PI;
// var phi = atan2(ray.direction.x, -ray.direction.z) / -PI * .5f; // var phi = atan2((*ray).direction.x, -(*ray).direction.z) / -PI * .5f;
// return _SkyboxTexture.SampleLevel(sampler_SkyboxTexture, float2(phi, theta), 0).xyz; // return _SkyboxTexture.SampleLevel(sampler_SkyboxTexture, float2(phi, theta), 0).xyz;
} }
} }

View File

@@ -24,7 +24,7 @@ impl Plugin for Blackhole {
app.add_systems(Startup, setup); app.add_systems(Startup, setup);
app.add_plugins(TracerPipelinePlugin); app.add_plugins(TracerPipelinePlugin);
app.insert_resource(TracerUniforms { app.insert_resource(TracerUniforms {
sky_color: LinearRgba::BLUE, sky_color: LinearRgba::rgb(0.1, 0.0, 0.01),
..default() ..default()
}); });

View File

@@ -174,9 +174,9 @@ pub struct TracerImageBindGroups(pub [BindGroup; 2]);
fn update_tracer_uniforms( fn update_tracer_uniforms(
mut tracer_uniforms: ResMut<TracerUniforms>, mut tracer_uniforms: ResMut<TracerUniforms>,
rt_camera: Single<(&GlobalTransform, &Projection), With<RTCamera>>, rt_camera: Single<(&GlobalTransform, &Projection, &Camera), With<RTCamera>>,
) { ) {
let (transform, projection) = rt_camera.into_inner(); let (transform, projection, cam) = rt_camera.into_inner();
let view = transform.compute_matrix().inverse(); let view = transform.compute_matrix().inverse();
let clip_from_view = match projection { let clip_from_view = match projection {
Projection::Perspective(perspective_projection) => perspective_projection.get_clip_from_view(), 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 clip_from_world = clip_from_view * view;
let world_from_clip = clip_from_world.inverse(); 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_from_clip = world_from_clip;
tracer_uniforms.world_position = transform.translation(); tracer_uniforms.world_position = transform.translation();