spot light

This commit is contained in:
Confideo-IOM
2026-07-24 19:03:34 +01:00
parent 120f18b1f9
commit 3be84d07de
42 changed files with 311 additions and 265 deletions
@@ -25,8 +25,8 @@ struct light_structure
float constant;
float linear;
float quadratic;
float falloff;
float falloff_smooth;
float cutoff;
float cutoff_outer;
uint entity_id;
uint light_type;
};
@@ -166,51 +166,12 @@ vec3 DirectionalShading(vec3 normal,
vec3 view_pos)
{
// vec3 N = normalize(normal);
// vec3 V = normalize(vieview_pos - world_pos);
// // calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// // of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
// vec3 F0 = vec3(0.04);
// F0 = mix(F0, diffuse, metallic);
// // reflectance equation
// vec3 Lo = vec3(0.0);
// // calculate per-light radiance
// vec3 L = normalize(lightDir);
// vec3 H = normalize(V + L);
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
vec3 radiance = diffuse * attenuation;
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
// // Cook-Torrance BRDF
// float NDF = DistributionGGX(N, H, roughness);
// float G = GeometrySmith(N, V, L, roughness);
// vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
// vec3 numerator = NDF * G * F;
// float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
// vec3 specular = numerator / denominator;
// // kS is equal to Fresnel
// vec3 kS = F;
// // for energy conservation, the diffuse and specular light can't
// // be above 1.0 (unless the surface emits light); to preserve this
// // relationship the diffuse component (kD) should equal 1.0 - kS.
// vec3 kD = vec3(1.0) - kS;
// // multiply kD by the inverse metalness such that only non-metals
// // have diffuse lighting, or a linear blend if partly metal (pure metals
// // have no diffuse light).
// kD *= 1.0 - metallic;
// // scale light by NdotL
// float NdotL = max(dot(N, L), 0.0);
// // add to outgoing radiance Lo
// Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
return Lo;
}
@@ -229,57 +190,58 @@ vec3 PointShading(vec3 normal,
vec3 view_pos)
{
// vec3 N = normalize(normal);
// vec3 V = normalize(viewPos - worldPos);
// // calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// // of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
// vec3 F0 = vec3(0.04);
// F0 = mix(F0, diffuse, metallic);
// // reflectance equation
// vec3 Lo = vec3(0.0);
// // calculate per-light radiance
// vec3 L = normalize(lightPos - worldPos);
// vec3 H = normalize(V + L);
float distance = length(light_pos - world_pos);
float attenuation = 1.0;
//attenuation = light_intensity / (distance * distance);
attenuation = light_intensity / (light_constant + (light_linear * distance) + (light_quadratic * distance * distance));
vec3 radiance = diffuse * attenuation;
vec3 light_dir = normalize(light_pos - world_pos);
vec3 Lo = CalculatePBR(normal, view_pos, world_pos, light_dir, diffuse, radiance, roughness, metallic);
// // Cook-Torrance BRDF
// float NDF = DistributionGGX(N, H, roughness);
// float G = GeometrySmith(N, V, L, roughness);
// vec3 F = fresnelSchlick(clamp(dot(H, V), 0.0, 1.0), F0);
// vec3 numerator = NDF * G * F;
// float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
// vec3 specular = (numerator / denominator) * attenuation;
return Lo;
}
vec3 SpotShading(vec3 normal,
vec3 diffuse,
float roughness,
float metallic,
float light_intensity,
vec3 light_dir,
vec3 light_pos,
float cutoff,
float cutoff_outer,
float light_constant,
float light_linear,
float light_quadratic,
vec3 world_pos,
vec3 view_pos)
{
vec3 direction_to_light = normalize(light_pos - world_pos);
float theta = dot(direction_to_light, light_dir);
float falloff = 0.0f;
// // kS is equal to Fresnel
// vec3 kS = F;
// // for energy conservation, the diffuse and specular light can't
// // be above 1.0 (unless the surface emits light); to preserve this
// // relationship the diffuse component (kD) should equal 1.0 - kS.
// vec3 kD = vec3(1.0) - kS;
// // multiply kD by the inverse metalness such that only non-metals
// // have diffuse lighting, or a linear blend if partly metal (pure metals
// // have no diffuse light).
// kD *= 1.0 - metallic;
// // scale light by NdotL
// float NdotL = max(dot(N, L), 0.0);
// if(theta > cutoff)
// {
float epsilon = cutoff - cutoff_outer;
falloff = clamp((theta - cutoff_outer) / epsilon, 0.0, 1.0);
//}
// // add to outgoing radiance Lo
// Lo += (kD * diffuse / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
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 Lo = CalculatePBR(normal, view_pos, world_pos, direction_to_light, diffuse, radiance, roughness, metallic);
return Lo;
}
mat3 GetRotationOnlyMatrix(mat4 model_mat)
{
mat3 rot_mat;
@@ -313,17 +275,17 @@ void main()
{
mat4 model = model_matrix[lights[i].entity_id];
mat3 rot_mat = GetRotationOnlyMatrix(model);
vec3 light_direction = rot_mat * vec3(0,0,1);
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 += DirectionalShading(normal, diffuse.xyz, rough, metal, lights[i].intensity, light_direction, light_pos, world_pos, ubo_per_frame.viewer_pos);
}
else if(lights[i].light_type == HYDRA_LIGHT_POINT)
{
Lo+= PointShading(normal,
Lo += PointShading(normal,
diffuse.xyz,
rough,
metal,
@@ -335,6 +297,23 @@ void main()
world_pos,
ubo_per_frame.viewer_pos);
}
else if(lights[i].light_type == HYDRA_LIGHT_SPOT)
{
Lo += SpotShading(normal,
diffuse.xyz,
rough,
metal,
lights[i].intensity,
light_direction,
light_pos,
lights[i].cutoff,
lights[i].cutoff_outer,
lights[i].constant,
lights[i].linear,
lights[i].quadratic,
world_pos,
ubo_per_frame.viewer_pos);
}
}
@@ -347,7 +326,7 @@ void main()
float ao = 1;
vec3 ambient = vec3(0.05) * diffuse.xyz * ao;
vec3 output_color = Lo;
vec3 output_color = Lo + ambient;
// HDR tonemapping
output_color = output_color / (output_color + vec3(1.0));