Add antialiasing

This commit is contained in:
Matthew Gordon 2025-01-06 11:18:43 -04:00
parent 53221856aa
commit 91a66e3bb4
1 changed files with 55 additions and 36 deletions

View File

@ -38,44 +38,63 @@ fn fs_solid(vertex: VertexOutput) -> @location(0) vec4<f32> {
var ray: Ray; var ray: Ray;
ray.origin = vertex.world_space_position.xyz; ray.origin = vertex.world_space_position.xyz;
ray.direction = normalize(vertex.world_space_position.xyz - uniforms.camera_position.xyz); ray.direction = normalize(vertex.world_space_position.xyz - uniforms.camera_position.xyz);
// Spread rays into RGSS antialiasing pattern.
let ray_dx = dpdy(ray.direction);
let ray_dy = dpdy(ray.direction);
var ray_directions: array<vec3<f32>,4>;
ray_directions[0] = ray.direction + 0.125*ray_dx + 0.375*ray_dy;
ray_directions[1] = ray.direction - 0.125*ray_dx - 0.375*ray_dy;
ray_directions[2] = ray.direction + 0.375*ray_dx + 0.125*ray_dy;
ray_directions[3] = ray.direction - 0.375*ray_dx - 0.125*ray_dy;
var root_node: BoundingNode; // Possibly these ray directions could be stored in a matrix and we could
root_node.index = vec2<u32>(0); // evaluate them all at once instead of looping.
root_node.level = textureNumLevels(dembvh_texture) - 1;
var hit_index = vec2<u32>(0); var color_accumulator = vec4<f32>(0);
var hit_location: vec3<f32>; for(var i=0; i<4; i++) {
var hit_normal: vec3<f32>; ray.direction = ray_directions[i];
if intersect_ray_with_node(dembvh_texture,
uniforms.dem_min_corner, var root_node: BoundingNode;
uniforms.dem_cell_size, root_node.index = vec2<u32>(0);
uniforms.dem_z_range, root_node.level = textureNumLevels(dembvh_texture) - 1;
ray,
root_node, var hit_index = vec2<u32>(0);
&hit_index, var hit_location: vec3<f32>;
&hit_location, var hit_normal: vec3<f32>;
&hit_normal) { if intersect_ray_with_node(dembvh_texture,
// Calculate light uniforms.dem_min_corner,
let sun_direction = (uniforms.camera_to_world_matrix uniforms.dem_cell_size,
* vec4<f32>(normalize(vec3<f32>(1.0, 1.0, 1.0)), 0.0)).xyz; uniforms.dem_z_range,
let l = dot(hit_normal, sun_direction); ray,
var shadow_ray :Ray; root_node,
shadow_ray.origin = hit_location + hit_normal * 0.1; &hit_index,
shadow_ray.direction = sun_direction; &hit_location,
var dummy0: vec3<f32>; &hit_normal) {
var dummy1: vec3<f32>; // Calculate light
let shadow_value = let sun_direction =
select(1.0, 0.0, intersect_ray_with_node(dembvh_texture, (uniforms.camera_to_world_matrix
uniforms.dem_min_corner, * vec4<f32>(normalize(vec3<f32>(1.0, 1.0, 1.0)), 0.0)).xyz;
uniforms.dem_cell_size, let l = dot(hit_normal, sun_direction);
uniforms.dem_z_range, var shadow_ray :Ray;
shadow_ray, shadow_ray.origin = hit_location + hit_normal * 0.1;
root_node, shadow_ray.direction = sun_direction;
&hit_index, var dummy0: vec3<f32>;
&dummy0, var dummy1: vec3<f32>;
&dummy1)); let shadow_value =
return vec4<f32>(vec3<f32>(l)*shadow_value, 1.0); select(1.0, 0.0, intersect_ray_with_node(dembvh_texture,
} else { uniforms.dem_min_corner,
uniforms.dem_cell_size,
uniforms.dem_z_range,
shadow_ray,
root_node,
&hit_index,
&dummy0,
&dummy1));
color_accumulator += vec4<f32>(vec3<f32>(l)*shadow_value, 1.0);
}
}
if color_accumulator.w == 0.0 {
discard; discard;
} }
return color_accumulator * 0.25;
} }