diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index a738849..ff99d3f 100644 Binary files a/shaders/compiled/frag_rt_quad.spv and b/shaders/compiled/frag_rt_quad.spv differ diff --git a/shaders/rt_quad.frag b/shaders/rt_quad.frag index d43de76..c59b4e4 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -337,6 +337,9 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl float y_border = compound_pos.y + float((compound_grid_size) * uint(!y_pos)) * compound_scale; float z_border = compound_pos.z + float((compound_grid_size) * uint(!z_pos)) * compound_scale; + // go over the borders by this amount + float overstep = 0.00001 / length(direction); + if (!x_null) { x_factor = (x_border - pos.x) / direction.x; } else { @@ -352,6 +355,9 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl } else { z_factor = max_factor; } + x_factor += overstep; + y_factor += overstep; + z_factor += overstep; vec3 intersection_pos = pos; bool is_x_hit = false; @@ -361,14 +367,14 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl if ((compound_pos.x <= intersection_pos.x && intersection_pos.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && (compound_pos.y <= intersection_pos.y && intersection_pos.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && (compound_pos.z <= intersection_pos.z && intersection_pos.z <= compound_pos.z + float(compound_grid_size) * compound_scale)){ - hit_inside = true; + //hit_inside = true; } else { vec3 intersection_pos_x = pos + x_factor * direction; vec3 intersection_pos_y = pos + y_factor * direction; vec3 intersection_pos_z = pos + z_factor * direction; if ((compound_pos.x <= intersection_pos_x.x && intersection_pos_x.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && (compound_pos.y <= intersection_pos_x.y && intersection_pos_x.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && - (compound_pos.z <= intersection_pos_x.z && intersection_pos_x.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && x_factor > 0.0) { + (compound_pos.z <= intersection_pos_x.z && intersection_pos_x.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && x_factor > 0.0 && x_factor <= max_factor) { hit_inside = true; is_x_hit = true; intersection_pos = intersection_pos_x; @@ -376,7 +382,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl if ((compound_pos.x <= intersection_pos_y.x && intersection_pos_y.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && (compound_pos.y <= intersection_pos_y.y && intersection_pos_y.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && - (compound_pos.z <= intersection_pos_y.z && intersection_pos_y.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && y_factor > 0.0 && (y_factor < x_factor || !is_x_hit)) { + (compound_pos.z <= intersection_pos_y.z && intersection_pos_y.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && y_factor > 0.0 && y_factor <= max_factor && (y_factor < x_factor || !is_x_hit)) { hit_inside = true; is_y_hit = true; intersection_pos = intersection_pos_y; @@ -384,7 +390,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl if ((compound_pos.x <= intersection_pos_z.x && intersection_pos_z.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && (compound_pos.y <= intersection_pos_z.y && intersection_pos_z.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && - (compound_pos.z <= intersection_pos_z.z && intersection_pos_z.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && z_factor > 0.0 && (z_factor < x_factor || !is_x_hit) && (z_factor < y_factor || !is_y_hit)) { + (compound_pos.z <= intersection_pos_z.z && intersection_pos_z.z <= compound_pos.z + float(compound_grid_size) * compound_scale) && z_factor > 0.0 && z_factor <= max_factor && (z_factor < x_factor || !is_x_hit) && (z_factor < y_factor || !is_y_hit)) { hit_inside = true; is_z_hit = true; intersection_pos = intersection_pos_z; @@ -400,6 +406,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl uint oct_tree_address = oct_tree_index; // iterate through the oct_tree uint check_it = 0; + uint max_check_it = 60; uint prev_child = 0; uint prev_prev_child = 0; @@ -407,7 +414,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl uvec3 parent_pos = uvec3(0, 0, 0); bool has_moved = false; - while (!result.has_hit && check_it < 60) { + while (!result.has_hit && check_it < max_check_it) { // failsafe to get out in case has_moved runs into an accuracy issue check_it ++; oct_tree_pos = vec3(grid_pos) * compound_scale + compound_pos; @@ -428,11 +435,6 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl child_index = 1 * uint(oct_tree_address == oct_tree_mem[parent_index + 1]) + 2 * uint(oct_tree_address == oct_tree_mem[parent_index + 2]) + 3 * uint(oct_tree_address == oct_tree_mem[parent_index + 3]) + 4 * uint(oct_tree_address == oct_tree_mem[parent_index + 4]) + 5 * uint(oct_tree_address == oct_tree_mem[parent_index + 5]) + 6 * uint(oct_tree_address == oct_tree_mem[parent_index + 6]) + 7 * uint(oct_tree_address == oct_tree_mem[parent_index + 7]) + 8 * uint(oct_tree_address == oct_tree_mem[parent_index + 8]); // mark as done to avoid reinvestigating, since intersection_pos is on its edge children_open[child_index - 1] = false; - if (!has_moved) { - for (int i=0; i < child_index; i++) { - children_open[i] = false; - } - } prev_prev_child = prev_child; prev_child = oct_tree_address; @@ -440,15 +442,13 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl grid_pos -= parent_child_vec(current_size, child_index); current_size *= 2; oct_tree_address = parent_index; - // todo remove once parent is implemented - //break; } else { // go down into child if (current_size == 2) { // check block if hit break if (oct_tree_mem[oct_tree_address + child_index] != 0) { result.has_hit = true; - result.end_color = uvec4(255, 0, 0, 32); + result.end_color = unpack_color(oct_tree_mem[oct_tree_address + child_index]); break; } } else { @@ -477,43 +477,57 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl if (!x_null) { x_factor = (x_border - pos.x) / direction.x; + if (x_factor <= 0.0) { + x_factor = max_factor; + } } else { x_factor = max_factor; } if (!y_null) { y_factor = (y_border - pos.y) / direction.y; + if (y_factor <= 0.0) { + y_factor = max_factor; + } } else { y_factor = max_factor; } if (!z_null) { z_factor = (z_border - pos.z) / direction.z; + if (z_factor <= 0.0) { + z_factor = max_factor; + } } else { z_factor = max_factor; } float smallest_factor = min(min(x_factor, y_factor), z_factor); - has_moved = length(intersection_pos - (pos + smallest_factor * direction)) > 0.00001; - //has_moved = intersection_pos != (pos + smallest_factor * direction); + if (x_factor == smallest_factor) { + is_x_hit = true; + is_y_hit = false; + is_z_hit = false; + } + if (y_factor == smallest_factor) { + is_x_hit = false; + is_y_hit = true; + is_z_hit = false; + } + if (z_factor == smallest_factor) { + is_x_hit = false; + is_y_hit = false; + is_z_hit = true; + } + + // move a bit further to fully enter the next quadrant + smallest_factor += overstep; + + //has_moved = length(intersection_pos - (pos + smallest_factor * direction)) >= 0.00001; + has_moved = intersection_pos != (pos + smallest_factor * direction); intersection_pos = pos + smallest_factor * direction; } } uint hit_facing = uint(is_x_hit) * (2 + uint(x_pos)) + uint(is_y_hit) * (4 + uint(y_pos)) + uint(is_z_hit && !z_pos); //result.has_hit = true; - if (!has_moved) { - //result.has_hit = true; - } - if (!((compound_pos.x <= intersection_pos.x && intersection_pos.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && - (compound_pos.y <= intersection_pos.y && intersection_pos.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && - (compound_pos.z <= intersection_pos.z && intersection_pos.z <= compound_pos.z + float(compound_grid_size) * compound_scale))) { - //result.has_hit = true; - } - - if (!((compound_pos.x <= oct_tree_pos.x && oct_tree_pos.x <= compound_pos.x + float(compound_grid_size) * compound_scale) && - (compound_pos.y <= oct_tree_pos.y && oct_tree_pos.y <= compound_pos.y + float(compound_grid_size) * compound_scale) && - (compound_pos.z <= oct_tree_pos.z && oct_tree_pos.z <= compound_pos.z + float(compound_grid_size) * compound_scale))) { - //result.has_hit = true; - } result.end_pos = intersection_pos; result.end_facing = hit_facing; result.end_volume = volume_index; @@ -711,8 +725,7 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa return color_sum; } -vec3 diffuse_tracing(uint volume_start, uvec2 raster_pos, vec3 pos, uint f) { - uvec4 color_roughness = sample_color_from_scene_info(volume_start, raster_pos, f); +vec3 diffuse_tracing(uint volume_start, uvec4 color_roughness, vec3 pos, uint f) { vec4 orig_color_sample = vec4(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0, 1); vec3 normal = normal_for_facing(f); @@ -733,10 +746,6 @@ vec3 diffuse_tracing(uint volume_start, uvec2 raster_pos, vec3 pos, uint f) { return color_sum; } -vec3 diffuse_tracing(uint volume_start, vec2 raster_pos, vec3 pos, uint f) { - return diffuse_tracing(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), pos, f); -} - vec3 clamp_to_volume(uint volume_start, vec3 position) { float volume_pos_x = uintBitsToFloat(scene_info.infos[volume_start + 0]); float volume_pos_y = uintBitsToFloat(scene_info.infos[volume_start + 1]); @@ -766,7 +775,7 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 vec3 reflection_direction = reflect_vector(view_vector, f); Tracing reflection_tracing = trace_ray(volume_start, pos, reflection_direction, pos_infinity, true); if (reflection_tracing.has_hit || reflection_tracing.has_transparent_hit) { - vec3 color_from_reflection = diffuse_tracing(reflection_tracing.end_volume, reflection_tracing.end_raster, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; + vec3 color_from_reflection = diffuse_tracing(reflection_tracing.end_volume, reflection_tracing.end_color, reflection_tracing.end_pos, reflection_tracing.end_facing) * orig_color_sample; color_sum = color_sum * (1.0 - reflectivity) + color_from_reflection * reflectivity; } } @@ -783,14 +792,15 @@ void main() { uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, clamped_raster_pos, facing); if (orig_neighbor != 0) { - vec3 color_direct = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); + vec3 color_direct = diffuse_tracing(fragVolumeStart, color_roughness, clamped_pos, facing); Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, clamped_pos - ubo.camera_pos, pos_infinity, false); float opacity = float(color_roughness.w) / 255.0; vec3 color_seen_through; if (t.has_hit) { //color_seen_through = vec3(float(t.end_color.x) / 255.0, float(t.end_color.y) / 255.0, float(t.end_color.z) / 255.0); - color_seen_through = diffuse_tracing(t.end_volume, t.end_raster, t.end_pos, t.end_facing) * orig_color_sample * t.color_mul; + + color_seen_through = diffuse_tracing(t.end_volume, t.end_color, t.end_pos, t.end_facing) * orig_color_sample * t.color_mul; color_seen_through = add_reflection(t.end_direction, t.end_facing, t.end_volume, t.end_pos, t.end_color, color_seen_through); } else { @@ -804,7 +814,7 @@ void main() { //color_sum = color_seen_through; } else { - color_sum = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); + color_sum = diffuse_tracing(fragVolumeStart, color_roughness, clamped_pos, facing); color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum); } diff --git a/src/scene/generators.rs b/src/scene/generators.rs index a1a4a89..eca7594 100644 --- a/src/scene/generators.rs +++ b/src/scene/generators.rs @@ -94,7 +94,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi scene.point_lights.push(Rc::new(RefCell::new(PointLight::init(vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, vec3(2.0, 2.0, 2.0))))); //scene.point_lights.push(Rc::new(RefCell::new(PointLight::init(vec3(9.0 + grid_size as f32, 9.0 + grid_size as f32, 11.0) * scale, vec3(0.5, 0.5, 0.5))))); - scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight::init(vec3(1.0, 1.0, -1.0), vec3(0.1, 0.1, 0.1))))); + //scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight::init(vec3(1.0, 1.0, -1.0), vec3(0.1, 0.1, 0.1))))); let cube = Cuboid { pos: vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, @@ -116,8 +116,8 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi let tree_ref_one = Rc::new(RefCell::new(oct_tree1.clone())); let tree_ref_two = Rc::new(RefCell::new(oct_tree2.clone())); - scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]], vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; - //scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; + //scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]], vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; + scene.oct_trees = vec![vec![vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_one.clone(), tree_ref_two.clone()], vec![tree_ref_two.clone(), tree_ref_two.clone(), tree_ref_two.clone()]]]; let mut comp = ShapeComposition::new(64); //comp.included_shapes.push(Rc::new(RefCell::new(Rect::new(Vector3 { x: 5.0 + grid_size as f32, y: 5.0 + grid_size as f32, z: 10.0 }, Vector3 { x: 0.0, y: 0.0, z: 0.0 }, Vector3 { x: 5.0, y: 5.0, z: 5.0 },Vector3 { x: 0, y: 0, z: 255 }, 64, false))));