Skip to content

Instantly share code, notes, and snippets.

@shivaduke28
Created October 21, 2023 07:55
Show Gist options
  • Save shivaduke28/89ef34f656f1bffca2823ede0654f488 to your computer and use it in GitHub Desktop.
Save shivaduke28/89ef34f656f1bffca2823ede0654f488 to your computer and use it in GitHub Desktop.
Unity PointLight range and attenuation
#include "AutoLight.cginc"
#if defined(POINT)
// 1/range*range for Unity's PointLight
float UnityPointLightRangeInvSqr()
{
float3 unitLS = float3(unity_WorldToLight._m00, unity_WorldToLight._m01, unity_WorldToLight._m02);
return dot(unitLS, unitLS);
}
// https://forum.unity.com/threads/light-dist ance-in-shader.509306/#post-3326818
float UnityPointLightAttenuation(float3 positionWS)
{
float3 positionLS = mul(unity_WorldToLight, unityShadowCoord4(positionWS, 1)).xyz;
float3 distanceLSSqr = dot(positionLS, positionLS);
return saturate(1.0 / (1.0 + 25.0 * distanceLSSqr) * saturate((1 - sqrt(distanceLSSqr)) * 5.0));
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment