i stil have no idea what i'm doing

This commit is contained in:
2025-10-18 14:31:15 -04:00
parent 8f4bd37e1e
commit 58ae477bca
2 changed files with 58 additions and 56 deletions

View File

@@ -7,27 +7,27 @@
struct View { struct View {
view_proj: mat4x4<f32>, view_proj: mat4x4<f32>,
view: mat4x4<f32>, view: mat4x4<f32>,
projection: mat4x4<f32>, projection: mat4x4<f32>,
inv_view_proj: mat4x4<f32>, inv_view_proj: mat4x4<f32>,
inv_view: mat4x4<f32>, // equiv to Unity's _CameraToWorld inv_view: mat4x4<f32>, // equiv to Unity's _CameraToWorld
inv_projection: mat4x4<f32>, // equic to Unity's _CameraInverseProjection inv_projection: mat4x4<f32>, // equic to Unity's _CameraInverseProjection
}; };
struct TracerUniforms { struct TracerUniforms {
sky_color: vec4<f32>, sky_color: vec4<f32>,
world_from_clip: mat4x4<f32>, world_from_clip: mat4x4<f32>,
world_position: vec3<f32>, world_position: vec3<f32>,
} }
@compute @workgroup_size(8, 8, 1) @compute @workgroup_size(8, 8, 1)
fn init(@builtin(global_invocation_id) invocation_id: vec3<u32>, @builtin(num_workgroups) num_workgroups: vec3<u32>) { fn init(@builtin(global_invocation_id) invocation_id: vec3<u32>, @builtin(num_workgroups) num_workgroups: vec3<u32>) {
let location = vec2<i32>(i32(invocation_id.x), i32(invocation_id.y)); let location = vec2<i32>(i32(invocation_id.x), i32(invocation_id.y));
let color = vec4(0.0); let color = vec4(0.0);
textureStore(output, location, color); textureStore(output, location, color);
} }
@@ -39,35 +39,37 @@ fn update(@builtin(global_invocation_id) invocation_id: vec3<u32>) {
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 = createCameraRay2(ndc); var ray = createCameraRay2(ndc);
var result = vec3<f32>(0.0); var result = vec3<f32>(0.0);
for (var i: i32 = 0; i < 8; i++){ for (var i: i32 = 0; i < 1; i++){
var hit = trace(ray); var hit = trace(ray);
result += ray.energy * shade(&ray, hit); result += ray.energy * shade(&ray, hit);
if !any(ray.energy != vec3<f32>(0.0)) if !any(ray.energy != vec3<f32>(0.0))
{ {
break; break;
} }
} }
// 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); let dir = normalize(world_pos - config.world_position);
// let color = vec4<f32>((ray.direction * 0.5) + vec3<f32>(0.5), 1.0); let color = vec4<f32>((ray.direction * 0.1), 1.0);
// let color = vec4<f32>(ray.direction, 1.0);
// let color = vec4<f32>((ray.direction * 0.1) + vec3<f32>(0.5), 1.0);
let color = vec4<f32>(result, 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>,
energy: vec3<f32>, energy: vec3<f32>,
} }
@@ -88,13 +90,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 = 999999999999.0f; hit.distance = -999999999.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
@@ -108,30 +110,30 @@ fn createRay(origin: vec3<f32>, direction: vec3<f32>) -> Ray
fn createCameraRay(ndc: vec2<f32>) -> Ray { fn createCameraRay(ndc: vec2<f32>) -> Ray {
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.0, 1.0);
let direction_point = target_point.xyz / target_point.w; let direction_point = target_point.xyz / target_point.w;
let direction = normalize(direction_point - config.world_position); let direction = normalize(direction_point - config.world_position);
return createRay(config.world_position, direction); return createRay(config.world_position, direction);
} }
fn createCameraRay2(ndc: vec2<f32>) -> Ray { fn createCameraRay2(ndc: vec2<f32>) -> Ray {
// clip points at near and far // clip points at near and far
let near_clip = vec4<f32>(ndc, 0.0, 1.0); let near_clip = vec4<f32>(ndc, 0.0, 1.0);
let far_clip = vec4<f32>(ndc, 1.0, 1.0); let far_clip = vec4<f32>(ndc, 1.0, 1.0);
// project into world space // project into world space
let near_world4 = config.world_from_clip * near_clip; let near_world4 = config.world_from_clip * near_clip;
let far_world4 = config.world_from_clip * far_clip; let far_world4 = config.world_from_clip * far_clip;
let near_world = near_world4.xyz / near_world4.w; let near_world = near_world4.xyz / near_world4.w;
let far_world = far_world4.xyz / far_world4.w; let far_world = far_world4.xyz / far_world4.w;
// ray starts at near plane, points toward far plane // ray starts at near plane, points toward far plane
let origin = near_world; let origin = near_world + config.world_position;
let direction = normalize(far_world - near_world); let direction = normalize(origin - near_world);
return createRay(origin, direction); return createRay(origin, direction);
} }
fn createSphere(position: vec3<f32>, radius: f32) -> Sphere fn createSphere(position: vec3<f32>, radius: f32) -> Sphere
@@ -147,7 +149,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, 0.0f), 1.0f); var sphere = createSphere(vec3<f32>(0.0f, 2.0f, 0.0f), 2.0f);
var d = ray.origin - sphere.position; var d = ray.origin - sphere.position;
@@ -198,7 +200,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 < 999999999999.0f if hit.distance > -999999999.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);

View File

@@ -178,9 +178,9 @@ fn update_tracer_uniforms(
) { ) {
let (transform, cam) = rt_camera.into_inner(); let (transform, cam) = rt_camera.into_inner();
let view = transform.compute_matrix().inverse();
let clip_from_view = cam.clip_from_view(); let clip_from_view = cam.clip_from_view();
let world_from_clip = view * clip_from_view.inverse(); let world_from_clip = clip_from_view.inverse() * transform.compute_matrix().inverse();
// cam.ndc_to_world(camera_transform, ndc)
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();