diff --git a/shaders/compiled/frag_rt_quad.spv b/shaders/compiled/frag_rt_quad.spv index d3b4275..4b2eb61 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 a7f8001..65f1bdd 100644 --- a/shaders/rt_quad.frag +++ b/shaders/rt_quad.frag @@ -34,6 +34,9 @@ int half_diffuse_raster_steps = int(scene_info.infos[2]); float raster_distance = uintBitsToFloat(scene_info.infos[3]); int raster_points = (2 * half_diffuse_raster_steps + 1) * (2 * half_diffuse_raster_steps + 1); float pos_infinity = uintBitsToFloat(0x7F800000); +// set limit for maximal iterations +uint max_iterations = max_num_lights * max_iterations_per_light * raster_points; +uint iteration_num = 0; uvec4 unpack_color(uint val) { // left most 8 bits first @@ -212,11 +215,10 @@ struct Tracing { bool has_transparent_hit; }; -Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, float start_max_factor, uint start_cycle, uint max_cycle, bool allow_reflect) { +Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, float start_max_factor, bool allow_reflect) { vec3 direction = start_direction; float max_factor = start_max_factor; vec3 pos = starting_pos; - uint cycle = start_cycle; // setup volume info uint volume_index = volume_start; float volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]); @@ -251,8 +253,8 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl uvec2 end_raster_transparent; vec3 color_mul_transparent; - while (cycle < max_cycle) { - cycle ++; + while (iteration_num < max_iterations) { + iteration_num ++; float x_border = volume_pos_x + float((scene_info.infos[volume_index + 3]) * uint(x_pos)) * volume_scale - 0.5 * volume_scale; float y_border = volume_pos_y + float((scene_info.infos[volume_index + 4]) * uint(y_pos)) * volume_scale - 0.5 * volume_scale; float z_border = volume_pos_z + float((scene_info.infos[volume_index + 5]) * uint(z_pos)) * volume_scale - 0.5 * volume_scale; @@ -370,8 +372,14 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl } } } + /*if (iteration_num == max_iterations) { + end_color_transparent = uvec4(0, 0, 255, 255); + result.end_color = uvec4(0, 0, 255, 255); + result.color_mul = vec3(0.0, 0.0, 1.0); + }*/ + result.end_factor = min(min(x_factor, y_factor), z_factor); - result.end_cycle = cycle; + result.end_cycle = iteration_num; // in case we have a transparent hit but no hit afterwards if (!result.has_hit && result.has_transparent_hit) { @@ -395,11 +403,9 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa uint light_num = 0; // initialize color - vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.005); + vec3 color_sum = vec3(0.0, 0.0, 0.0);// + (orig_color_sample.xyz * 0.005); - uint max_iterations = max_num_lights * max_iterations_per_light; - uint iteration = 0; - while (iteration < max_iterations) { + while (iteration_num < max_iterations) { // setup light info uint light_index = scene_info.infos[volume_start + 6 + light_num]; if (light_index == 0) { @@ -419,11 +425,12 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa } vec3 light_color = get_light_color(light_index); - Tracing result = trace_ray(volume_start, starting_pos, light_direction, max_factor, iteration, max_iterations, false); + Tracing result = trace_ray(volume_start, starting_pos, light_direction, max_factor, false); // add result, if there is a hit the null vector will be added color_sum += float(!result.has_hit) * result.color_mul * max(dot(normal, normalize(light_direction)), 0.0) * (orig_color_sample.xyz * light_color) / (length(light_direction) * length(light_direction)); - - iteration = result.end_cycle; + /*if (scene_info.infos[light_index] == 1) { + color_sum = float(!result.has_hit) * result.color_mul; + }*/ light_num += 1; if (light_num >= max_num_lights) { @@ -487,7 +494,7 @@ vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 if (reflectivity > 0.01) { vec3 orig_color_sample = vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0); vec3 reflection_direction = reflect_vector(view_vector, f); - Tracing reflection_tracing = trace_ray(volume_start, pos, reflection_direction, pos_infinity, 0, max_iterations_per_light, true); + 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; color_sum = color_sum * (1.0 - reflectivity) + color_from_reflection * reflectivity; @@ -507,20 +514,21 @@ 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); - color_direct = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_direct); - Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, clamped_pos - ubo.camera_pos, pos_infinity, 0, max_iterations_per_light, false); + 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) { - vec3 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_raster, 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); - - color_sum = opacity * color_direct + (1.0 - opacity) * color_seen_through; } else { // Todo: hit sky box - color_sum = opacity * color_direct + (1.0 - opacity) * vec3(0.0, 0.0, 0.0); + color_seen_through = vec3(0.0, 0.0, 0.0); } + + color_direct = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_direct); + color_sum = opacity * color_direct + (1.0 - opacity) * color_seen_through; } else { color_sum = diffuse_tracing(fragVolumeStart, clamped_raster_pos, clamped_pos, facing); diff --git a/src/main.rs b/src/main.rs index 223503e..84a07ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,8 +224,8 @@ impl App { image::create_texture_image_view(&device, &mut data)?; image::create_texture_sampler(&device, &mut data)?; - //let cur_pos = generators::generate_test_scene(&mut scene_handler, &mut data)?; - let cur_pos = generators::generate_test_scene2(&mut scene_handler, &mut data, 31, 31,1, 5)?; + let cur_pos = generators::generate_test_scene(&mut scene_handler, &mut data)?; + //let cur_pos = generators::generate_test_scene2(&mut scene_handler, &mut data, 31, 31,1, 5)?; scene_handler.prepare_data(&instance, &device, &mut data)?; buffer::create_uniform_buffers(&instance, &device, &mut data)?; diff --git a/src/scene/empty_volume.rs b/src/scene/empty_volume.rs index 893cc92..b7f2cb9 100644 --- a/src/scene/empty_volume.rs +++ b/src/scene/empty_volume.rs @@ -553,16 +553,18 @@ impl EmptyVolume { let mut bottom_neighbors = vec![]; let mut bottom_elements_num = 0; let mut all_same = true; - for x in 0..reference.borrow().size_x { - for y in 0..reference.borrow().size_y { - if let Some(c) = neighbors.get_element(reference.borrow().grid_position.x + x, reference.borrow().grid_position.y + y, z_min_pos) { - bottom_elements_num += 1; - bottom_neighbors.push(Some(c.clone())); - all_same = all_same && (bottom_neighbors[0] == Some(c)); - } - else { - bottom_neighbors.push(None); - all_same = all_same && (bottom_neighbors[0] == None); + if reference.borrow().grid_position.z != 0 { + for x in 0..reference.borrow().size_x { + for y in 0..reference.borrow().size_y { + if let Some(c) = neighbors.get_element(reference.borrow().grid_position.x + x, reference.borrow().grid_position.y + y, z_min_pos) { + bottom_elements_num += 1; + bottom_neighbors.push(Some(c.clone())); + all_same = all_same && (bottom_neighbors[0] == Some(c)); + } + else { + bottom_neighbors.push(None); + all_same = all_same && (bottom_neighbors[0] == None); + } } } } @@ -639,16 +641,18 @@ impl EmptyVolume { let mut front_neighbors = vec![]; let mut front_elements_num = 0; let mut all_same = true; - for x in 0..reference.borrow().size_x { - for z in 0..reference.borrow().size_z { - if let Some(c) = neighbors.get_element(reference.borrow().grid_position.x + x, y_min_pos, reference.borrow().grid_position.z + z) { - front_elements_num += 1; - front_neighbors.push(Some(c.clone())); - all_same = all_same && (front_neighbors[0] == Some(c)); - } - else { - front_neighbors.push(None); - all_same = all_same && (front_neighbors[0] == None); + if reference.borrow().grid_position.y != 0 { + for x in 0..reference.borrow().size_x { + for z in 0..reference.borrow().size_z { + if let Some(c) = neighbors.get_element(reference.borrow().grid_position.x + x, y_min_pos, reference.borrow().grid_position.z + z) { + front_elements_num += 1; + front_neighbors.push(Some(c.clone())); + all_same = all_same && (front_neighbors[0] == Some(c)); + } + else { + front_neighbors.push(None); + all_same = all_same && (front_neighbors[0] == None); + } } } } @@ -668,16 +672,18 @@ impl EmptyVolume { let mut left_neighbors = vec![]; let mut left_elements_num = 0; let mut all_same = true; - for y in 0..reference.borrow().size_y { - for z in 0..reference.borrow().size_z { - if let Some(c) = neighbors.get_element(x_min_pos, reference.borrow().grid_position.y + y, reference.borrow().grid_position.z + z) { - left_elements_num += 1; - left_neighbors.push(Some(c.clone())); - all_same = all_same && (left_neighbors[0] == Some(c)); - } - else { - left_neighbors.push(None); - all_same = all_same && (left_neighbors[0] == None); + if reference.borrow().grid_position.x != 0 { + for y in 0..reference.borrow().size_y { + for z in 0..reference.borrow().size_z { + if let Some(c) = neighbors.get_element(x_min_pos, reference.borrow().grid_position.y + y, reference.borrow().grid_position.z + z) { + left_elements_num += 1; + left_neighbors.push(Some(c.clone())); + all_same = all_same && (left_neighbors[0] == Some(c)); + } + else { + left_neighbors.push(None); + all_same = all_same && (left_neighbors[0] == None); + } } } } diff --git a/src/scene/generators.rs b/src/scene/generators.rs index c40c5da..9461344 100644 --- a/src/scene/generators.rs +++ b/src/scene/generators.rs @@ -58,8 +58,8 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi pos: vec3(10.0, 10.0, 10.0), color: vec3(0.0, 0.0, 0.9), tex_coord: vec2(0.0, 0.0), - transparent: false, - roughness: 128, + transparent: true, + roughness: 32, }; oct_tree1.set_cube(cube.clone()); @@ -67,8 +67,8 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi pos: vec3(10.0, 10.0, 9.0), color: vec3(0.0, 0.0, 0.9), tex_coord: vec2(0.0, 0.0), - transparent: false, - roughness: 128, + transparent: true, + roughness: 32, }; oct_tree1.set_cube(cube.clone()); @@ -91,7 +91,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi }; oct_tree2.set_cube(cube.clone()); - scene.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, color: vec3(1.0, 1.0, 1.0), memory_start: 0 }))); + scene.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(11.0 + grid_size as f32, 11.0 + grid_size as f32, 11.0) * scale, color: vec3(2.0, 2.0, 2.0), memory_start: 0 }))); scene.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(9.0 + grid_size as f32, 9.0 + grid_size as f32, 11.0) * scale, color: vec3(0.5, 0.5, 0.5), memory_start: 0 }))); scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight { direction: vec3(1.0, 1.0, -1.0), color: vec3(0.1, 0.1, 0.1), memory_start: 0 })));