#ifndef UNITY_COMMON_LIGHTING_INCLUDED #define UNITY_COMMON_LIGHTING_INCLUDED //----------------------------------------------------------------------------- // Attenuation functions //----------------------------------------------------------------------------- // Ref: Moving Frostbite to PBR float SmoothDistanceAttenuation(float squaredDistance, float invSqrAttenuationRadius) { float factor = squaredDistance * invSqrAttenuationRadius; float smoothFactor = saturate(1.0f - factor * factor); return smoothFactor * smoothFactor; } #define PUNCTUAL_LIGHT_THRESHOLD 0.01 // 1cm (in Unity 1 is 1m) float GetDistanceAttenuation(float3 unL, float invSqrAttenuationRadius) { float sqrDist = dot(unL, unL); float attenuation = 1.0f / (max(PUNCTUAL_LIGHT_THRESHOLD * PUNCTUAL_LIGHT_THRESHOLD, sqrDist)); // Non physically based hack to limit light influence to attenuationRadius. attenuation *= SmoothDistanceAttenuation(sqrDist, invSqrAttenuationRadius); return attenuation; } float GetAngleAttenuation(float3 L, float3 lightDir, float lightAngleScale, float lightAngleOffset) { float cd = dot(lightDir, L); float attenuation = saturate(cd * lightAngleScale + lightAngleOffset); // smooth the transition attenuation *= attenuation; return attenuation; } #endif // UNITY_COMMON_LIGHTING_INCLUDED