transparency first version

This commit is contained in:
zomseffen 2025-01-30 11:03:30 +01:00
parent f0fa28bdd8
commit 66902df99c
14 changed files with 225 additions and 92 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -5,6 +5,7 @@ layout(binding = 0) uniform UniformBufferObject {
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;

View file

@ -5,6 +5,7 @@ layout(binding = 0) uniform UniformBufferObject {
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;

View file

@ -7,6 +7,15 @@ layout(location = 3) flat in uint facing;
layout(location = 0) out vec4 outColor;
layout(binding = 0) uniform UniformBufferObject {
mat4 model;
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;
layout(binding = 2) buffer SceneInfoBuffer{
uint infos[];
} scene_info;
@ -122,7 +131,7 @@ uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f)
uint vs[6] = {top_color_size_v, bottom_color_size_v, left_color_size_v, right_color_size_v, front_color_size_v, back_color_size_v};
uint u_size = us[f];
uint v_size = vs[f];
uint value = scene_info.infos[array_start + raster_pos.x * v_size * uint(u_size > 1) + raster_pos.y * uint(v_size > 1)];
uint value = scene_info.infos[array_start + clamp(raster_pos.x, 0, u_size) * v_size * uint(u_size > 1) + clamp(raster_pos.y, 0, v_size) * uint(v_size > 1)];
return unpack_color(value);
}
@ -142,6 +151,8 @@ struct Tracing {
float end_factor;
uint end_cycle;
bool has_hit;
vec3 color_mul;
uvec2 end_raster;
};
Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 direction, float max_factor, uint start_cycle, uint max_cycle) {
@ -167,6 +178,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 direction, float ma
float z_factor = max_factor;
Tracing result;
result.color_mul = vec3(1.0, 1.0, 1.0);
while (cycle < max_cycle) {
cycle ++;
@ -250,10 +262,20 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 direction, float ma
break;
}
} else {
// color hit, move on
result.end_color = color_sample;
result.has_hit = true;
break;
if (next_neighbor != 0) {
// transparent hit, move on but change the color
volume_index = next_neighbor;
volume_pos_x = scene_info.infos[volume_index + 0];
volume_pos_y = scene_info.infos[volume_index + 1];
volume_pos_z = scene_info.infos[volume_index + 2];
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);
} else {
// color hit, move on
result.end_color = color_sample;
result.end_raster = uvec2(u, v);
result.has_hit = true;
break;
}
}
}
}
@ -264,7 +286,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 direction, float ma
return result;
}
vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample) {
vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample, vec3 normal) {
uint max_light_num = scene_info.infos[0];
uint light_num = 0;
@ -286,7 +308,7 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa
Tracing result = trace_ray(volume_start, starting_pos, light_direction, 1.0, iteration, max_iterations);
if (!result.has_hit) {
// no hit, add light color result
color_sum += (orig_color_sample.xyz * light_color) / ((0.01 * length(light_direction) * length(light_direction)) + 1.0);
color_sum += 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;
@ -299,15 +321,33 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa
return color_sum;
}
void main() {
uint max_length = scene_info.infos[0];
uint last = scene_info.infos[max_length];
uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, fragRasterPos, facing);
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_for_facing(uint facing) {
if (facing == 0) {
return vec3(0.0, 0.0, -1.0);
}
if (facing == 1) {
return vec3(0.0, 0.0, 1.0);
}
if (facing == 2) {
return vec3(0.0, 1.0, 0.0);
}
if (facing == 3) {
return vec3(0.0, -1.0, 0.0);
}
if (facing == 4) {
return vec3(1.0, 0.0, 0.0);
}
if (facing == 5) {
return vec3(-1.0, 0.0, 0.0);
}
// singular raytracing
//vec3 color_sum = get_lighting_color(fragVolumeStart, origPosition, orig_color_sample);
return vec3(0.0, 0.0, 0.0);
}
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);
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);
// diffuse raytracing using a quadratic raster of rays
int raster_half_steps = 0;
@ -317,16 +357,54 @@ void main() {
vec3 color_sum = vec3(0.0, 0.0, 0.0);
for (int u_offset = -raster_half_steps; u_offset <= raster_half_steps; u_offset++) {
for (int v_offset = -raster_half_steps; v_offset <= raster_half_steps; v_offset++) {
float x_offset = raster_distance * float(u_offset) * float(facing == 0 || facing == 1 || facing == 4 || facing == 5);
float y_offset = raster_distance * float(u_offset) * float(facing == 2 || facing == 3);
y_offset += raster_distance * float(v_offset) * float(facing == 0 || facing == 1);
float z_offset = raster_distance * float(v_offset) * float(facing == 4 || facing == 5 || facing == 2 || facing == 3);
float x_offset = raster_distance * float(u_offset) * float(f == 0 || f == 1 || f == 4 || f == 5);
float y_offset = raster_distance * float(u_offset) * float(f == 2 || f == 3);
y_offset += raster_distance * float(v_offset) * float(f == 0 || f == 1);
float z_offset = raster_distance * float(v_offset) * float(f == 4 || f == 5 || f == 2 || f == 3);
vec3 offset = vec3(x_offset, y_offset, z_offset);
color_sum += get_lighting_color(fragVolumeStart, origPosition + offset, orig_color_sample) / float(raster_points);
color_sum += get_lighting_color(volume_start, origPosition + offset, orig_color_sample, normal) / float(raster_points);
}
}
return color_sum;
}
vec3 clamp_to_volume(uint volume_start, vec3 position) {
uint volume_pos_x = scene_info.infos[volume_start + 0];
uint volume_pos_y = scene_info.infos[volume_start + 1];
uint volume_pos_z = scene_info.infos[volume_start + 2];
float high_x_border = float(volume_pos_x + (scene_info.infos[volume_start + 3])) - 0.5;
float high_y_border = float(volume_pos_y + (scene_info.infos[volume_start + 4])) - 0.5;
float high_z_border = float(volume_pos_z + (scene_info.infos[volume_start + 5])) - 0.5;
float low_x_border = float(volume_pos_x) - 0.5;
float low_y_border = float(volume_pos_y) - 0.5;
float low_z_border = float(volume_pos_z) - 0.5;
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));
}
void main() {
vec3 clamped_pos = clamp_to_volume(fragVolumeStart, origPosition);
vec3 color_sum = diffuse_tracing(fragVolumeStart, fragRasterPos, clamped_pos, facing);
uint orig_neighbor = sample_neighbor_from_scene_info(fragVolumeStart, fragRasterPos, facing);
if (orig_neighbor != 0) {
float pos_infinity = uintBitsToFloat(0x7F800000);
Tracing t = trace_ray(fragVolumeStart, ubo.camera_pos, clamped_pos - ubo.camera_pos, 100.0, 0, 20);
if (t.has_hit) {
color_sum += diffuse_tracing(t.end_volume, t.end_raster, t.end_pos, t.end_facing);
}
else {
// Todo: hit sky box
color_sum += vec3(0.0, 0.0, 0.0);
}
}
outColor = vec4(color_sum, 1.0);
}

View file

@ -5,6 +5,7 @@ layout(binding = 0) uniform UniformBufferObject {
mat4 geom_rot;
mat4 view;
mat4 proj;
vec3 camera_pos;
bool[16] use_geom_shader;
} ubo;