#version 450

layout(location = 0) in vec2 fragRasterPos;
layout(location = 1) flat in uint fragVolumeStart;
layout(location = 2) in vec3 origPosition;
layout(location = 3) flat in uint facing;
layout(location = 4) flat in uvec2 minRasterPos;
layout(location = 5) flat in uvec2 maxRasterPos;

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;

// 0 - location for the maximum number of lights referenced per chunk (also will be the invalid memory allocation for pointing to a nonexistant neighbor)
// 1 - location for the max iterations per light
// 2 - diffuse raster samples (2*n + 1) * (2*n + 1) so as to always have at least the central fragment covered
// 3 - diffuse raster size (float, needs to be decoded)
// 4 - max recursive rays
// 5 - diffuse rays per hit
layout(binding = 2) readonly buffer SceneInfoBuffer{
     uint infos[]; 
} scene_info;

layout(binding = 4) buffer SceneInfoBuffer2 {
   uint infos[];
} scene_info2;
uint max_num_lights = scene_info.infos[0];
uint max_iterations_per_light = scene_info.infos[1];
// diffuse raytracing using a quadratic raster of rays
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
    uint val1 = (val >> 24);
    uint val2 = (val << 8) >> 24;
    uint val3 = (val << 16) >> 24;
    uint val4 = (val << 24) >> 24;

    return uvec4(val4, val3, val2, val1);
}

uint array_descr_offset = 6 + max_num_lights;
uint color_array_offset = 24 + 1;

uint sample_neighbor_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) {
    uint array_descr_start = volume_start + array_descr_offset;
    uint color_array_start = array_descr_start + color_array_offset;

    uint top_color_size_u = scene_info.infos[array_descr_start];
    uint top_color_size_v = scene_info.infos[array_descr_start + 1];

    uint bottom_color_size_u = scene_info.infos[array_descr_start + 2];
    uint bottom_color_size_v = scene_info.infos[array_descr_start + 3];

    uint left_color_size_u = scene_info.infos[array_descr_start + 4];
    uint left_color_size_v = scene_info.infos[array_descr_start + 5];

    uint right_color_size_u = scene_info.infos[array_descr_start + 6];
    uint right_color_size_v = scene_info.infos[array_descr_start + 7];

    uint front_color_size_u = scene_info.infos[array_descr_start + 8];
    uint front_color_size_v = scene_info.infos[array_descr_start + 9];

    uint back_color_size_u = scene_info.infos[array_descr_start + 10];
    uint back_color_size_v = scene_info.infos[array_descr_start + 11];

    uint top_neighbor_size_u = scene_info.infos[array_descr_start + 12];
    uint top_neighbor_size_v = scene_info.infos[array_descr_start + 13];

    uint bottom_neighbor_size_u = scene_info.infos[array_descr_start + 14];
    uint bottom_neighbor_size_v = scene_info.infos[array_descr_start + 15];

    uint left_neighbor_size_u = scene_info.infos[array_descr_start + 16];
    uint left_neighbor_size_v = scene_info.infos[array_descr_start + 17];

    uint right_neighbor_size_u = scene_info.infos[array_descr_start + 18];
    uint right_neighbor_size_v = scene_info.infos[array_descr_start + 19];

    uint front_neighbor_size_u = scene_info.infos[array_descr_start + 20];
    uint front_neighbor_size_v = scene_info.infos[array_descr_start + 21];

    uint back_neighbor_size_u = scene_info.infos[array_descr_start + 22];
    uint back_neighbor_size_v = scene_info.infos[array_descr_start + 23];

    uint top_color_size = top_color_size_u * top_color_size_v;
    uint bottom_color_size = bottom_color_size_u * bottom_color_size_v;
    uint left_color_size = left_color_size_u * left_color_size_v;
    uint right_color_size = right_color_size_u * right_color_size_v;
    uint front_color_size = front_color_size_u * front_color_size_v;
    uint back_color_size = back_color_size_u * back_color_size_v;

    uint color_array_end = color_array_start + top_color_size + bottom_color_size + left_color_size + right_color_size + front_color_size + back_color_size;

    uint top_neighbor_size = top_neighbor_size_u * top_neighbor_size_v;
    uint bottom_neighbor_size = bottom_neighbor_size_u * bottom_neighbor_size_v;
    uint left_neighbor_size = left_neighbor_size_u * left_neighbor_size_v;
    uint right_neighbor_size = right_neighbor_size_u * right_neighbor_size_v;
    uint front_neighbor_size = front_neighbor_size_u * front_neighbor_size_v;
    uint back_neighbor_size = back_neighbor_size_u * back_neighbor_size_v;

    // maybe do an array solution for this as well
    uint array_start = color_array_end + uint(f > 0) * top_neighbor_size + uint(f > 1) * bottom_neighbor_size + uint(f > 2) * left_neighbor_size + uint(f > 3) * right_neighbor_size + uint(f > 4) * front_neighbor_size;
    uint us[6] = {top_neighbor_size_u, bottom_neighbor_size_u, left_neighbor_size_u, right_neighbor_size_u, front_neighbor_size_u, back_neighbor_size_u};
    uint vs[6] = {top_neighbor_size_v, bottom_neighbor_size_v, left_neighbor_size_v, right_neighbor_size_v, front_neighbor_size_v, back_neighbor_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)];
    return value; 
}

uint sample_neighbor_from_scene_info(uint volume_start, vec2 raster_pos, uint f) {
    return sample_neighbor_from_scene_info(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), f);
}

uvec4 sample_color_from_scene_info(uint volume_start, uvec2 raster_pos, uint f) {
    uint array_descr_start = volume_start + array_descr_offset;
    uint color_array_start = array_descr_start + color_array_offset;

    uint top_color_size_u = scene_info.infos[array_descr_start];
    uint top_color_size_v = scene_info.infos[array_descr_start + 1];

    uint bottom_color_size_u = scene_info.infos[array_descr_start + 2];
    uint bottom_color_size_v = scene_info.infos[array_descr_start + 3];

    uint left_color_size_u = scene_info.infos[array_descr_start + 4];
    uint left_color_size_v = scene_info.infos[array_descr_start + 5];

    uint right_color_size_u = scene_info.infos[array_descr_start + 6];
    uint right_color_size_v = scene_info.infos[array_descr_start + 7];

    uint front_color_size_u = scene_info.infos[array_descr_start + 8];
    uint front_color_size_v = scene_info.infos[array_descr_start + 9];

    uint back_color_size_u = scene_info.infos[array_descr_start + 10];
    uint back_color_size_v = scene_info.infos[array_descr_start + 11];

    uint top_size = top_color_size_u * top_color_size_v;
    uint bottom_size = bottom_color_size_u * bottom_color_size_v;
    uint left_size = left_color_size_u * left_color_size_v;
    uint right_size = right_color_size_u * right_color_size_v;
    uint front_size = front_color_size_u * front_color_size_v;
    uint back_size = back_color_size_u * back_color_size_v;

    // maybe do an array solution for this as well
    uint array_start = color_array_start + uint(f > 0) * top_size + uint(f > 1) * bottom_size + uint(f > 2) * left_size + uint(f > 3) * right_size + uint(f > 4) * front_size;
    uint us[6] = {top_color_size_u, bottom_color_size_u, left_color_size_u, right_color_size_u, front_color_size_u, back_color_size_u};
    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 + 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); 
}

uvec4 sample_color_from_scene_info(uint volume_start, vec2 raster_pos, uint f) {
    return sample_color_from_scene_info(volume_start, uvec2(uint(floor(raster_pos.x)), uint(floor(raster_pos.y))), f);
}

vec3 get_light_position(uint light_index) {
    return vec3(uintBitsToFloat(scene_info.infos[light_index + 1]), uintBitsToFloat(scene_info.infos[light_index + 2]), uintBitsToFloat(scene_info.infos[light_index + 3]));
}

vec3 get_light_color(uint light_index) {
    return vec3(float(scene_info.infos[light_index + 4]) / 255.0, float(scene_info.infos[light_index + 5]) / 255.0, float(scene_info.infos[light_index + 6]) / 255.0);
}

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(1.0, 0.0, 0.0);
    }
    if (facing == 3) {
        return vec3(-1.0, 0.0, 0.0);
    }
    if (facing == 4) {
        return vec3(0.0, 1.0, 0.0);
    }
    if (facing == 5) {
        return vec3(0.0, -1.0, 0.0);
    }

    return vec3(0.0, 0.0, 0.0);
}

vec3 reflect_vector(vec3 direction, uint facing) {
    vec3 normal = normal_for_facing(facing);
    return direction - 2.0 * dot(direction, normal) * normal;
}

struct Tracing {
    vec3 end_pos;
    uvec4 end_color;
    uint end_volume;
    uint end_facing;
    float end_factor;
    uint end_cycle;
    bool has_hit;
    vec3 color_mul;
    uvec2 end_raster;

    vec3 end_direction;
    bool has_transparent_hit;
};

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;
    // setup volume info
    uint volume_index = volume_start;
    float volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]);
    float volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); 
    float volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); 
    float volume_pos_z = uintBitsToFloat(scene_info.infos[volume_index + 2]); 

    bool x_pos = direction.x > 0.0;
    bool x_null = (direction.x == 0.0);
    
    bool y_pos = direction.y > 0.0;
    bool y_null = (direction.y == 0.0);

    bool z_pos = direction.z > 0.0;
    bool z_null = (direction.z == 0.0);

    // default is max factor, that way we avoid collision when going parallel to an axis. The other directions will score a hit
    float x_factor = max_factor;
    float y_factor = max_factor;
    float z_factor = max_factor;

    Tracing result;
    result.has_hit = false;
    result.has_transparent_hit = false;
    result.color_mul = vec3(1.0, 1.0, 1.0);

    // intermediate storage for transparent hit values
    vec3 end_pos_transparent;
    uvec4 end_color_transparent;
    uint end_volume_transparent;
    uint end_facing_transparent;
    uvec2 end_raster_transparent;
    vec3 color_mul_transparent;

    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;
        
        bool needs_next_light = false;

        if (!x_null) {
            x_factor = (x_border - pos.x) / direction.x;
        }
        if (!y_null) {
            y_factor = (y_border - pos.y) / direction.y;
        }
        if (!z_null) {
            z_factor = (z_border - pos.z) / direction.z;
        }

        if ((x_factor >= max_factor) && (y_factor >= max_factor) && (z_factor >= max_factor)) {
            // no hit, finish tracking
            break;
        } else {
            // if there is a border hit before reaching the end
            // change to the relevant next volume
            // Todo: look into removing ifs from this
            uint hit_facing = 0;
            uint u = 0;
            uint v = 0;

            bool is_x_smallest = x_factor < y_factor && x_factor < z_factor;
            bool is_y_smallest = y_factor < x_factor && y_factor < z_factor;
            bool is_z_smallest = z_factor <= x_factor && z_factor <= y_factor;

            hit_facing = uint(is_x_smallest) * (2 + uint(x_pos)) + uint(is_y_smallest) * (4 + uint(y_pos)) + uint(is_z_smallest && !z_pos);
            float smallest_factor = min(min(x_factor, y_factor), z_factor); // maybe use multiplication instead?
            vec3 intersection_pos = pos + smallest_factor * direction;
            u = uint(is_x_smallest) * (uint(round((intersection_pos.y - volume_pos_y) / volume_scale))) + 
                uint(is_y_smallest || is_z_smallest) * (uint(round((intersection_pos.x - volume_pos_x) / volume_scale)));
            v = uint(is_x_smallest || is_y_smallest) * (uint(round((intersection_pos.z - volume_pos_z) / volume_scale))) +
                uint(is_z_smallest) * (uint(round((intersection_pos.y - volume_pos_y) / volume_scale)));

            uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing);
            uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing);

            if (color_sample.xyz == uvec3(0, 0, 0)) {
                // not a color hit, so check neighbor
                if (next_neighbor != 0) {
                    volume_index = next_neighbor;
                    volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]);
                    volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); 
                    volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); 
                    volume_pos_z = uintBitsToFloat(scene_info.infos[volume_index + 2]);
                } else {
                    // neighbor miss
                    end_color_transparent = uvec4(255, 0, 0, 255);
                    result.end_color = uvec4(255, 0, 0, 255);
                    break;
                }
            } else {
                if (next_neighbor != 0) {
                    // transparent hit, move on but change the color
                    end_volume_transparent = volume_index;
                    color_mul_transparent = result.color_mul;

                    volume_index = next_neighbor;
                    volume_scale = uintBitsToFloat(scene_info.infos[volume_index + array_descr_offset + color_array_offset - 1]);
                    volume_pos_x = uintBitsToFloat(scene_info.infos[volume_index + 0]); 
                    volume_pos_y = uintBitsToFloat(scene_info.infos[volume_index + 1]); 
                    volume_pos_z = uintBitsToFloat(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);
                    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);
                    end_pos_transparent = intersection_pos;
                    end_facing_transparent = hit_facing;

                    // stop iterating if there is barely anything left to see
                    if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) {
                        break;
                    }                    
                } else {
                    // color hit, either reflect or move on
                    result.end_pos = intersection_pos;
                    result.end_facing = hit_facing;
                    result.end_color = color_sample;
                    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);
                    vec3 visibility_after_reflection = refltective_color_mul * reflectivity;
                    //break;
                    //max(visibility_after_reflection.x, max(visibility_after_reflection.y, visibility_after_reflection.z)) >= 0.1 && 
                    if (max(visibility_after_reflection.x, max(visibility_after_reflection.y, visibility_after_reflection.z)) >= 0.1 && allow_reflect) {
                        // do reflect
                        direction = reflect_vector(direction, hit_facing);
                        pos = intersection_pos;
                        //max_factor -= smallest_factor;

                        x_pos = direction.x > 0.0;
                        x_null = (direction.x == 0.0);
                        
                        y_pos = direction.y > 0.0;
                        y_null = (direction.y == 0.0);

                        z_pos = direction.z > 0.0;
                        z_null = (direction.z == 0.0);
                    } else {
                        break;
                    }
                }
            }
        }
    }

    result.end_factor = min(min(x_factor, y_factor), z_factor);
    result.end_cycle = iteration_num;

    // in case we have a transparent hit but no hit afterwards
    if (!result.has_hit && result.has_transparent_hit) {
        // did we stop because nothing could be seen through the object?
        if (max(result.color_mul.x, max(result.color_mul.y, result.color_mul.z)) < 0.1) {
            // if so count it as a hit and recover the pre transparent color multiplier
            result.has_hit = true;
            result.color_mul = color_mul_transparent;
        }
        result.end_pos = end_pos_transparent;
        result.end_color = end_color_transparent;
        result.end_volume = end_volume_transparent;
        result.end_facing = end_facing_transparent;
        result.end_raster = end_raster_transparent;
    }

    return result;
}

vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sample, vec3 normal) {
    uint light_num = 0;

    // initialize color
    vec3 color_sum = vec3(0.0, 0.0, 0.0);// + (orig_color_sample.xyz * 0.005);

    while (iteration_num < max_iterations) {
        // setup light info
        uint light_index = scene_info.infos[volume_start + 6 + light_num];
        if (light_index == 0) {
            // abort if there is no new light
            break;
        }
        vec3 light_direction;
        float max_factor;
        if (scene_info.infos[light_index] == 0) {
            //point light
            light_direction = get_light_position(light_index) - starting_pos;
            max_factor = 1.0;
        } else if (scene_info.infos[light_index] == 1) {
            // directional light
            light_direction = -normalize(get_light_position(light_index));
            max_factor = pos_infinity;
        }
        vec3 light_color = get_light_color(light_index);

        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));

        light_num += 1;
        if (light_num >= max_num_lights) {
            break;
        }
    }

    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);
    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);

    vec3 color_sum = vec3(0.0, 0.0, 0.0);
    for (int u_offset = -half_diffuse_raster_steps; u_offset <= half_diffuse_raster_steps; u_offset++) {
        for (int v_offset = -half_diffuse_raster_steps; v_offset <= half_diffuse_raster_steps; v_offset++) {
            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(volume_start, pos + offset, orig_color_sample, normal) / float(raster_points);
        }
    }

    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]); 
    float volume_pos_z = uintBitsToFloat(scene_info.infos[volume_start + 2]); 
    float volume_scale = uintBitsToFloat(scene_info.infos[volume_start + array_descr_offset + color_array_offset - 1]);

    float high_x_border = volume_pos_x + float(scene_info.infos[volume_start + 3]) * volume_scale - 0.501 * volume_scale;
    float high_y_border = volume_pos_y + float(scene_info.infos[volume_start + 4]) * volume_scale - 0.501 * volume_scale;
    float high_z_border = volume_pos_z + float(scene_info.infos[volume_start + 5]) * volume_scale - 0.501 * volume_scale;

    float low_x_border = float(volume_pos_x) - 0.501 * volume_scale;
    float low_y_border = float(volume_pos_y) - 0.501 * volume_scale;
    float low_z_border = float(volume_pos_z) - 0.501 * volume_scale;

    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));
}

vec2 clamp_to_quad(vec2 raster_pos, uvec2 min_raster_pos, uvec2 max_raster_pos) {
    return vec2(max(min_raster_pos.x, min(max_raster_pos.x - 1, raster_pos.x)), max(min_raster_pos.y, min(max_raster_pos.y - 1, raster_pos.y)));
}

vec3 add_reflection(vec3 view_vector, uint f, uint volume_start, vec3 pos, uvec4 color_sample, vec3 color_sum) {
    float reflectivity = 1.0 - float(color_sample.w) / 255.0;

    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, 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;
        }
    }

    return color_sum;
}

void main() {
    vec3 clamped_pos = clamp_to_volume(fragVolumeStart, origPosition);
    vec2 clamped_raster_pos = clamp_to_quad(fragRasterPos, minRasterPos, maxRasterPos);
    uvec4 color_roughness = sample_color_from_scene_info(fragVolumeStart, clamped_raster_pos, facing);
    vec3 orig_color_sample = vec3(float(color_roughness.x) / 255.0, float(color_roughness.y) / 255.0, float(color_roughness.z) / 255.0);
    vec3 color_sum;

    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);

        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 = 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);
        }
        else {
            // Todo: hit sky box
            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);

        color_sum = add_reflection(normalize(clamped_pos - ubo.camera_pos), facing, fragVolumeStart, clamped_pos, color_roughness, color_sum);
    }

    outColor = vec4(color_sum, 1.0);
}