Light tweeks

This commit is contained in:
Confideo-IOM
2026-08-17 14:12:11 +01:00
parent 98ff54c106
commit 139fb149b2
55 changed files with 593 additions and 265 deletions
@@ -327,12 +327,26 @@ float SampleCascadeShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_ma
float shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy, shadow_trans_idx)).r;
// calculate bias (based on depth map resolution and slope)
float bias = max(0.0005 * (1.0 - dot(normal, light_dir)), 0.00005);
float mapped_distance = (shadow_trans_idx*2);
float bias = 0.0f;//max(0.0001 * (1.0 - dot(normal, light_dir)), 0.00001);
// check whether current frag pos is in shadow
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// PCF
//bias = 0;
if(shadow_trans_idx < 3)
{
bias = 0.00001;
}
else if(shadow_trans_idx == 3)
{
bias = 0.0001;// + (0.000001 * mapped_distance);
}
else
{
bias = 0.0005;
}
float shadow = 0.0;
vec2 texel_size = 1.0 / textureSize(sampler_shadow_map, 0).xy;
if((frag_light_space.x >= 0.0)&&(frag_light_space.y >= 0.0)
@@ -343,7 +357,7 @@ float SampleCascadeShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_ma
for(int y = -1; y <= 1; ++y)
{
shadow_map_depth = texture(sampler_shadow_map, vec3(frag_light_space.xy + vec2(x, y) * texel_size, shadow_trans_idx)).r;
shadow += frag_light_space.z - bias > shadow_map_depth ? 1.0 : 0.0;
shadow += (frag_light_space.z - bias) > shadow_map_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
@@ -414,14 +428,39 @@ vec3 CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, ve
if(ignore_shadow == 1)
{
vec4 translated_pixel = directional_shadows[shadow_idx].light_space_proj[shadow_trans_idx] *
directional_shadows[shadow_idx].light_space_view[shadow_trans_idx] *
world_pos;
uvec2 shadow_handle = textures[directional_shadows[shadow_idx].shadow_texture_id];
shadow = 1.0 - SampleCascadeShadow(translated_pixel, sampler2DArray(shadow_handle), shadow_trans_idx, normal, light_dir);
}
return vec3(shadow);
if(shadow_trans_idx == 0)
{
return vec3(1,0,0);
}
if(shadow_trans_idx == 1)
{
return vec3(0,1,0);
}
if(shadow_trans_idx == 2)
{
return vec3(0,0,1);
}
if(shadow_trans_idx == 3)
{
return vec3(1,1,0);
}
if(shadow_trans_idx == 4)
{
return vec3(1,0,1);
}
if(shadow_trans_idx == 5)
{
return vec3(0,1,1);
}
}
float CalculateSpotShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir, vec3 viewer_pos)
{
@@ -460,7 +499,7 @@ float SampleCubemapShadow(vec3 frag_to_light, float far_plane, float view_distan
for(int i = 0; i < samples; ++i)
{
//float closest_depth = texture(sampler_shadow_map, frag_to_light).r;
float closest_depth = texture(sampler_shadow_map, frag_to_light + sample_offset_directions[i] * disk_radius).r;
closest_depth *= far_plane; // undo mapping [0;1]
if(frag_depth - bias > closest_depth)
@@ -468,15 +507,7 @@ float SampleCubemapShadow(vec3 frag_to_light, float far_plane, float view_distan
}
shadow /= float(samples);
return shadow;
// float frag_depth = length(frag_to_light);
// closest_depth = closest_depth * far_plane;
// float bias = 0.05f;
// float shadow = (frag_depth - bias) > closest_depth ? 1.0 : 0.0;
// return shadow;
}
float CalculatePointShadow(vec3 world_pos, uint shadow_idx, float view_distance, vec3 light_position)
@@ -515,15 +546,17 @@ void main()
mat4 model = model_matrix[lights[i].entity_id];
mat3 rot_mat = GetRotationOnlyMatrix(model);
vec3 light_direction = normalize(rot_mat * vec3(0,0,1));
vec3 light_pos = model[3].xyz;
if(lights[i].light_type == HYDRA_LIGHT_DIRECTIONAL)
{
Lo += DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
Lo *= lights[i].diffuse.xyz;
if(lights[i].shadow_caster == 1)
{
vec3 shadow = CalculateDirectionalShadow(vec4(world_pos, 1.0), lights[i].shadow_map_id, normal, light_direction, ubo_per_frame.viewer_pos);
Lo = Lo * shadow;
}
}
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
@@ -575,7 +608,7 @@ void main()
lights[i].quadratic,
world_pos,
ubo_per_frame.viewer_pos,
shadow);
shadow) * lights[i].diffuse.xyz;
}