From c09014e34e720eab731020a7eecb030930f2eee0 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Thu, 4 May 2023 11:28:07 +0100 Subject: [PATCH] Simplified ray stepping --- assets/shaders/voxel_volume.wgsl | 62 +++++++++++--------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/assets/shaders/voxel_volume.wgsl b/assets/shaders/voxel_volume.wgsl index e8d3597..b9c931f 100644 --- a/assets/shaders/voxel_volume.wgsl +++ b/assets/shaders/voxel_volume.wgsl @@ -147,30 +147,19 @@ fn brick_ray_cast( break; } - if (side_dist.x < side_dist.y) { - if (side_dist.x < side_dist.z) { - side_dist.x += delta_dist.x; - map_pos.x += ray_step.x; - hit_info.mask = vec3(true, false, false); - } - else { - side_dist.z += delta_dist.z; - map_pos.z += ray_step.z; - hit_info.mask = vec3(false, false, true); - } + let smallest = min(side_dist.x, min(side_dist.y, side_dist.z)); + if (smallest == side_dist.x) { + hit_info.mask = vec3(true, false, false); + } + else if (smallest == side_dist.y) { + hit_info.mask = vec3(false, true, false); } else { - if (side_dist.y < side_dist.z) { - side_dist.y += delta_dist.y; - map_pos.y += ray_step.y; - hit_info.mask = vec3(false, true, false); - } - else { - side_dist.z += delta_dist.z; - map_pos.z += ray_step.z; - hit_info.mask = vec3(false, false, true); - } + hit_info.mask = vec3(false, false, true); } + + side_dist += vec3(hit_info.mask) * delta_dist; + map_pos += vec3(hit_info.mask) * ray_step; } } @@ -248,30 +237,19 @@ fn grid_cast_ray(orig_ray_pos: vec3, ray_dir: vec3) -> HitInfo { } } - if (side_dist.x < side_dist.y) { - if (side_dist.x < side_dist.z) { - side_dist.x += delta_dist.x; - map_pos.x += ray_step.x; - hit_info.mask = vec3(true, false, false); - } - else { - side_dist.z += delta_dist.z; - map_pos.z += ray_step.z; - hit_info.mask = vec3(false, false, true); - } + let smallest = min(side_dist.x, min(side_dist.y, side_dist.z)); + if (smallest == side_dist.x) { + hit_info.mask = vec3(true, false, false); + } + else if (smallest == side_dist.y) { + hit_info.mask = vec3(false, true, false); } else { - if (side_dist.y < side_dist.z) { - side_dist.y += delta_dist.y; - map_pos.y += ray_step.y; - hit_info.mask = vec3(false, true, false); - } - else { - side_dist.z += delta_dist.z; - map_pos.z += ray_step.z; - hit_info.mask = vec3(false, false, true); - } + hit_info.mask = vec3(false, false, true); } + + side_dist += vec3(hit_info.mask) * delta_dist; + map_pos += vec3(hit_info.mask) * ray_step; } }