Spot works

This commit is contained in:
Confideo-IOM
2026-08-06 21:11:33 +01:00
parent 6d7d7571b5
commit f9b696f9e8
61 changed files with 701 additions and 418 deletions
@@ -35,10 +35,10 @@ struct LightStructure
uint padding1;
};
struct ShadowMapSet
struct DirectionalShadowMapSet
{
uint entity_id;
uint cascade_count;
uint shadow_id;
uint shadow_texture_id;
uint frame_buffer_id;
float near_plane[8];
@@ -47,6 +47,17 @@ struct ShadowMapSet
mat4 light_space_view[6];
};
struct SpotShadowMapSet
{
uint entity_id;
uint shadow_id;
uint shadow_texture_id;
uint frame_buffer_id;
float far_plane;
uint padding[3];
mat4 light_space_proj;
mat4 light_space_view;
};
layout(binding = 0) uniform uniform_per_frame
{
@@ -76,12 +87,15 @@ layout (std430, binding = 4) readonly buffer light_buffer
LightStructure lights[500];
};
layout (std430, binding = 5) readonly buffer shadow_map_sets
layout (std430, binding = 5) readonly buffer directional_shadow_map_sets
{
ShadowMapSet shadows[10];
DirectionalShadowMapSet directional_shadows[];
};
layout (std430, binding = 6) readonly buffer spot_shadow_map_sets
{
SpotShadowMapSet spot_shadows[];
};
layout (location=0) in VS_OUT
{
vec2 vsUV;
@@ -235,28 +249,27 @@ vec3 SpotShading(vec3 normal,
float light_linear,
float light_quadratic,
vec3 world_pos,
vec3 view_pos)
vec3 view_pos,
float shadow_val)
{
vec3 direction_to_light = normalize(light_pos - world_pos);
float theta = dot(direction_to_light, light_dir);
float falloff = 0.0f;
// if(theta > cutoff)
// {
float epsilon = cutoff - cutoff_outer;
falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0);
float epsilon = cutoff - cutoff_outer;
//}
falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0);
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
vec3 radiance = diffuse * attenuation * falloff;
vec3 radiance = diffuse * attenuation * falloff * (1.0-shadow_val);
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, direction_to_light, diffuse, radiance, roughness, metallic);
return Lo;
return Lo;// * shadow;
}
mat3 GetRotationOnlyMatrix(mat4 model_mat)
@@ -284,7 +297,7 @@ float SimpleSampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map
return 1;
}
float SampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx, vec3 normal, vec3 light_dir)
float SampleCascadeShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint shadow_trans_idx, vec3 normal, vec3 light_dir)
{
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
frag_light_space = (frag_light_space * 0.5) + 0.5;
@@ -319,6 +332,41 @@ float SampleShadow(vec4 frag_clip_space, sampler2DArray sampler_shadow_map, uint
return shadow;
}
float SampleSingleShadow(vec4 frag_clip_space, sampler2D sampler_shadow_map, vec3 normal, vec3 light_dir)
{
vec3 frag_light_space = frag_clip_space.xyz / frag_clip_space.w;
frag_light_space = (frag_light_space * 0.5) + 0.5;
float shadow_map_depth = texture(sampler_shadow_map, frag_light_space.xy).r;
// calculate bias (based on depth map resolution and slope)
float bias = max(0.00005 * (1.0 - dot(normal, light_dir)), 0.000005);
// check whether current frag pos is in shadow
// float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
// PCF
//bias = 0;
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)
&&(frag_light_space.x <= 1.0)&&(frag_light_space.y <= 1.0))
{
for(int x = -1; x <= 1; ++x)
{
for(int y = -1; y <= 1; ++y)
{
shadow_map_depth = texture(sampler_shadow_map, frag_light_space.xy + vec2(x, y) * texel_size).r;
shadow += frag_light_space.z - bias > shadow_map_depth ? 1.0 : 0.0;
}
}
shadow /= 9.0;
}
// keep the shadow at 0.0 when outside the far_plane region of the light's frustum.
if(frag_light_space.z > 1.0)
shadow = 0.0;
return shadow;
}
vec3 CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir, vec3 viewer_pos)
{
float shadow = 1;
@@ -326,12 +374,12 @@ vec3 CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, ve
//measure the distance of the fragment from the light
float frag_distance = abs(length(viewer_pos - world_pos.xyz));
uint shadow_trans_idx = 6;
uint shadow_trans_idx = 5;
uint ignore_shadow = 0;
//Work out which shadowmap is appropriate for this distance
for(uint i = 0; i < 6; i++)
{
if(frag_distance <= shadows[shadow_idx].far_plane[i])
if(frag_distance <= directional_shadows[shadow_idx].far_plane[i])
{
//record the shadow map level
shadow_trans_idx = i;
@@ -343,39 +391,30 @@ vec3 CalculateDirectionalShadow(vec4 world_pos, uint shadow_idx, vec3 normal, ve
if(ignore_shadow == 1)
{
vec4 translated_pixel = shadows[shadow_idx].light_space_proj[shadow_trans_idx] *
shadows[shadow_idx].light_space_view[shadow_trans_idx] *
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[shadows[shadow_idx].shadow_texture_id];
shadow = 1.0 - SampleShadow(translated_pixel, sampler2DArray(shadow_handle), shadow_trans_idx, normal, light_dir);
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) * shadow;
}
if(shadow_trans_idx == 1)
{
return vec3(1,1,0) * shadow;
}
if(shadow_trans_idx == 2)
{
return vec3(0.5,0.5,1) * shadow;
}
if(shadow_trans_idx == 3)
{
return vec3(1,0,1) * shadow;
}
if(shadow_trans_idx == 4)
{
return vec3(1,1,1) * shadow;
}
return vec3(0,1,1) * shadow;
}
float CalculateSpotShadow(vec4 world_pos, uint shadow_idx, vec3 normal, vec3 light_dir, vec3 viewer_pos)
{
vec4 translated_pixel = spot_shadows[shadow_idx].light_space_proj *
spot_shadows[shadow_idx].light_space_view *
world_pos;
uvec2 shadow_handle = textures[spot_shadows[shadow_idx].shadow_texture_id];
float shadow =(SampleSingleShadow(translated_pixel, sampler2D(shadow_handle), normal, light_dir));
return shadow;
}
void main()
{
uvec2 diffuse_handle = textures[texture_ids[DIFFUSE_TEXTURE_ID]];
@@ -431,6 +470,16 @@ void main()
}
else if(lights[i].light_type == HYDRA_LIGHT_SPOT)
{
float shadow = 0;
if(lights[i].shadow_caster == 1)
{
float distance = length(light_pos - world_pos);
shadow = CalculateSpotShadow(vec4(world_pos, 1.0),
lights[i].shadow_map_id,
normal,
light_direction,
ubo_per_frame.viewer_pos);
}
Lo += SpotShading(normal,
diffuse.xyz,
rough,
@@ -444,16 +493,15 @@ void main()
lights[i].linear,
lights[i].quadratic,
world_pos,
ubo_per_frame.viewer_pos);
ubo_per_frame.viewer_pos,
shadow);
}
}
float lighting = 0;
// ambient lighting (note that the next IBL tutorial will replace
// this ambient lighting with environment lighting).
vec3 output_color = Lo;
// HDR tonemapping