ADD: spot light

This commit is contained in:
2025-12-20 23:43:34 +09:00
parent 9ebc01b3a9
commit 0ec865e0ee
15 changed files with 585 additions and 5 deletions

View File

@@ -340,6 +340,52 @@ void main(){
direct += contrib;
}
// Spot lights
uint spotCount = sceneData.lightCounts.y;
for (uint i = 0u; i < spotCount; ++i)
{
vec3 contrib = eval_spot_light(sceneData.spotLights[i], pos, N, V, albedo, roughness, metallic);
// Optional RT shadow for the first few spot lights (hybrid mode)
#ifdef GL_EXT_ray_query
if (sceneData.rtOptions.x == 1u && sceneData.rtParams.y > 0.0 && i < 4u)
{
vec3 toL = sceneData.spotLights[i].position_radius.xyz - pos;
float maxT = length(toL);
if (maxT > 0.01)
{
vec3 L = toL / maxT;
vec3 dir = normalize(sceneData.spotLights[i].direction_cos_outer.xyz);
float cosTheta = dot(-L, dir);
if (cosTheta > sceneData.spotLights[i].direction_cos_outer.w)
{
vec3 origin = pos + N * SHADOW_RAY_ORIGIN_BIAS;
rayQueryEXT rq;
rayQueryInitializeEXT(
rq,
topLevelAS,
gl_RayFlagsTerminateOnFirstHitEXT | gl_RayFlagsOpaqueEXT,
0xFF,
origin,
SHADOW_RAY_TMIN,
L,
maxT
);
while (rayQueryProceedEXT(rq)) { }
bool hit = (rayQueryGetIntersectionTypeEXT(rq, true) != gl_RayQueryCommittedIntersectionNoneEXT);
if (hit)
{
contrib = vec3(0.0);
}
}
}
}
#endif
direct += contrib;
}
// Image-Based Lighting: split-sum approximation
vec3 R = reflect(-V, N);
float levels = float(textureQueryLevels(iblSpec2D));

View File

@@ -235,6 +235,13 @@ void main(){
direct += eval_point_light(sceneData.punctualLights[i], pos, N, V, albedo, roughness, metallic);
}
// Spot lights
uint spotCount = sceneData.lightCounts.y;
for (uint i = 0u; i < spotCount; ++i)
{
direct += eval_spot_light(sceneData.spotLights[i], pos, N, V, albedo, roughness, metallic);
}
// Image-Based Lighting: split-sum approximation
vec3 R = reflect(-V, N);
float levels = float(textureQueryLevels(iblSpec2D));

View File

@@ -2,12 +2,21 @@
#define MAX_CASCADES 4
// Maximum number of punctual (point) lights
#define MAX_PUNCTUAL_LIGHTS 64
// Maximum number of spot lights
#define MAX_SPOT_LIGHTS 32
struct GPUPunctualLight {
vec4 position_radius;
vec4 color_intensity;
};
struct GPUSpotLight {
vec4 position_radius; // xyz: position, w: radius
vec4 direction_cos_outer; // xyz: direction (unit), w: cos(outer_angle)
vec4 color_intensity; // rgb: color, a: intensity
vec4 cone; // x: cos(inner_angle), yzw: unused
};
layout(set = 0, binding = 0) uniform SceneData{
mat4 view;
@@ -32,6 +41,9 @@ layout(set = 0, binding = 0) uniform SceneData{
vec4 rtParams;
GPUPunctualLight punctualLights[MAX_PUNCTUAL_LIGHTS];
GPUSpotLight spotLights[MAX_SPOT_LIGHTS];
// lightCounts.x = point light count
// lightCounts.y = spot light count
uvec4 lightCounts;
} sceneData;

View File

@@ -82,5 +82,41 @@ vec3 eval_point_light(GPUPunctualLight light, vec3 pos, vec3 N, vec3 V, vec3 alb
return brdf * lightColor * falloff;
}
#endif // LIGHTING_COMMON_GLSL
vec3 eval_spot_light(GPUSpotLight light, vec3 pos, vec3 N, vec3 V, vec3 albedo, float roughness, float metallic)
{
vec3 lightPos = light.position_radius.xyz;
float radius = max(light.position_radius.w, 0.0001);
vec3 toLight = lightPos - pos;
float dist = length(toLight);
if (dist <= 0.0001)
{
return vec3(0.0);
}
vec3 L = toLight / dist; // surface -> light
vec3 dir = normalize(light.direction_cos_outer.xyz); // light -> forward
float cosOuter = light.direction_cos_outer.w;
float cosInner = light.cone.x;
float cosTheta = dot(-L, dir); // light -> surface vs light forward
if (cosTheta <= cosOuter)
{
return vec3(0.0);
}
float denom = max(cosInner - cosOuter, 0.0001);
float spot = clamp((cosTheta - cosOuter) / denom, 0.0, 1.0);
spot *= spot;
// Smooth falloff: inverse-square with soft clamp at radius
float att = 1.0 / max(dist * dist, 0.0001);
float x = clamp(dist / radius, 0.0, 1.0);
float smth = (1.0 - x * x);
smth *= smth;
float falloff = att * smth;
vec3 brdf = evaluate_brdf(N, V, L, albedo, roughness, metallic);
vec3 lightColor = light.color_intensity.rgb * light.color_intensity.a;
return brdf * lightColor * falloff * spot;
}
#endif // LIGHTING_COMMON_GLSL

View File

@@ -58,6 +58,13 @@ void main()
direct += eval_point_light(sceneData.punctualLights[i], inWorldPos, N, V, albedo, roughness, metallic);
}
// Spot lights
uint spotCount = sceneData.lightCounts.y;
for (uint i = 0u; i < spotCount; ++i)
{
direct += eval_spot_light(sceneData.spotLights[i], inWorldPos, N, V, albedo, roughness, metallic);
}
// IBL: specular from equirect 2D mips; diffuse from SH
vec3 R = reflect(-V, N);
float levels = float(textureQueryLevels(iblSpec2D));