在上一篇文章中,我们知道了URP下是怎么获取额外灯数量的。
在这篇文章中,我们来了解一下怎么获取每一盏额外灯的数据。
读源码后,可知,Unity在获取额外灯信息前。获取了额外灯索引。
所以,我们也分成两步来分析:获取额外灯索引、获取索引对应的额外灯信息。
这里我们主要只是看一下,在Shader种,并不会去做修改。
// Fills a light struct given a perObjectLightIndex
Light GetAdditionalPerObjectLight(int perObjectLightIndex, float3 positionWS)
{
// Abstraction over Light input constants
#if USE_STRUCTURED_BUFFER_FOR_LIGHT_DATA
float4 lightPositionWS = _AdditionalLightsBuffer[perObjectLightIndex].position;
half3 color = _AdditionalLightsBuffer[perObjectLightIndex].color.rgb;
half4 distanceAndSpotAttenuation = _AdditionalLightsBuffer[perObjectLightIndex].attenuation;
half4 spotDirection = _AdditionalLightsBuffer[perObjectLightIndex].spotDirection;
uint lightLayerMask = _AdditionalLightsBuffer[perObjectLightIndex].layerMask;
#else
float4 lightPositionWS = _AdditionalLightsPosition[perObjectLightIndex];
half3 color = _AdditionalLightsColor[perObjectLightIndex].rgb;
half4 distanceAndSpotAttenuation = _AdditionalLightsAttenuation[perObjectLightIndex];
half4 spotDirection = _AdditionalLightsSpotDir[perObjectLightIndex];
uint lightLayerMask = asuint(_AdditionalLightsLayerMasks[perObjectLightIndex]);
#endif
// Directional lights store direction in lightPosition.xyz and have .w set to 0.0.
// This way the following code will work for both directional and punctual lights.
float3 lightVector = lightPositionWS.xyz - positionWS * lightPositionWS.w;
float distanceSqr = max(dot(lightVector, lightVector), HALF_MIN);
half3 lightDirection = half3(lightVector * rsqrt(distanceSqr));
// full-float precision required on some platforms
float attenuation = DistanceAttenuation(distanceSqr, distanceAndSpotAttenuation.xy) * AngleAttenuation(spotDirection.xyz, lightDirection, distanceAndSpotAttenuation.zw);
Light light;
light.direction = lightDirection;
light.distanceAttenuation = attenuation;
light.shadowAttenuation = 1.0; // This value can later be overridden in GetAdditionalLight(uint i, float3 positionWS, half4 shadowMask)
light.color = color;
light.layerMask = lightLayerMask;
return light;
}
lightPositionWS:额外灯在世界空间下的坐标
color:额外灯颜色
distanceAndSpotAttenuation:距离衰减
spotDirection:距离衰减方向
有了这写数据,就可以计算额外灯的方向、距离衰减和角度衰减了。
我们在之后的文章中,来分析额外灯方向、距离衰减和角度衰减是怎么计算的。