reflection for transparent objects

This commit is contained in:
zomseffen 2025-02-07 12:19:48 +01:00
parent c07e8a3061
commit 4a6dde4535
2 changed files with 24 additions and 10 deletions

Binary file not shown.

View file

@ -31,6 +31,7 @@ uint max_iterations_per_light = scene_info.infos[1];
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);
uvec4 unpack_color(uint val) {
// left most 8 bits first
@ -194,6 +195,7 @@ struct Tracing {
vec3 color_mul;
uvec2 end_raster;
vec3 end_direction;
bool has_transparent_hit;
};
@ -303,6 +305,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
result.color_mul = result.color_mul * vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0);
result.has_transparent_hit = true;
result.end_volume = volume_index;
result.end_direction = direction;
end_color_transparent = color_sample;
end_raster_transparent = uvec2(u, v);
@ -321,6 +324,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
result.end_raster = uvec2(u, v);
result.has_hit = true;
result.end_volume = volume_index;
result.end_direction = direction;
float reflectivity = 1.0 - float(color_sample.w) / 255.0;
vec3 refltective_color_mul = result.color_mul * vec3(float(color_sample.x) / 255.0, float(color_sample.y) / 255.0, float(color_sample.z) / 255.0);
@ -440,6 +444,20 @@ vec3 clamp_to_volume(uint volume_start, vec3 position) {
return vec3(min(max(position.x, low_x_border), high_x_border), min(max(position.y, low_y_border), high_y_border), min(max(position.z, low_z_border), high_z_border));
}
vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 color_sample, vec3 color_sum) {
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);
float reflectivity = 1.0 - float(color_sample.w) / 255.0;
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;
}
return color_sum;
}
void main() {
vec3 clamped_pos = clamp_to_volume(fragVolumeStart, origPosition);
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
@ -447,31 +465,27 @@ void main() {
vec3 color_sum;
uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, fragRasterPos, facing);
float pos_infinity = uintBitsToFloat(0x7F800000);
if (orig_neighbor != 0) {
vec3 color_direct = diffuse_tracing(fragVolumeStart, fragRasterPos, 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);
float opacity = float(color_roughness.w) / 255.0;
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;
vec3 color_direct = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing);
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
vec3 color_direct = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing);
color_sum = opacity * color_direct + (1.0 - opacity) * vec3(0.0, 0.0, 0.0);
}
}
else {
color_sum = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing);
vec3 reflection_direction = reflect_vector(normalize(clamped_pos - ubo.camera_pos), facing);
Tracing reflection_tracing = trace_ray(fragVolumeStart, clamped_pos, reflection_direction, pos_infinity, 0, max_iterations_per_light, true);
float reflectivity = 1.0 - float(color_roughness.w) / 255.0;
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;
}
color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum);
}
outColor = vec4(color_sum, 1.0);