Created
October 21, 2023 07:55
-
-
Save shivaduke28/89ef34f656f1bffca2823ede0654f488 to your computer and use it in GitHub Desktop.
Unity PointLight range and attenuation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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