如果锥角超过 90 度,我的聚光灯的角度衰减不能正常工作。从 0.1 到 90,衰减从锥体中心到边缘是平滑的,但从 90 到 179.9,它会变得越来越锐利。
这是我的衰减代码:
uniform vec3 lightPosition; // Light's position
uniform vec3 lightDirection; // Light's direction
uniform float lightAngleCos: // Cosine of the half of the cone angle
uniform float lightRange: // Light's range
// Get the light vector
vec3 pixelToLight = lightPosition - position.xyz;
vec3 normPTL = normalize(pixelToLight);
// Get the dot product between the light direction and the light vector
float rho = dot(normPTL, -lightDirection);
if(rho > lightAngleCos)
{
float dif = 1.0 - lightAngleCos;
float angularAttenuation = clamp((rho - lightAngleCos) / dif, 0.0, 1.0);
float radialAttenuation = 1.0 - clamp(length(pixelToLight) / (lightRange), 0.0, 1.0);
float attenuation = angularAttenuation * radialAttenuation;
// Apply attenuation
out_color = color * attenuation;
}