glacius3000,
thanks, that was the good file !
A little side note : to get a rough approximate of the normalized distance instead of the squared distance, one could do:
float att = 1 - dot(tolight, tolight) * _LightPos.w;
att = 1 - att * att;
to avoid to use the sqrt() function, as “float att = tolight * _LightPos.w;” doesn’t really give a distance…
But actually, I found out I had to square the dot to get more light (as it was my final goal), so it was even easier => att = att * att …!
kebrus,
I had found out it’s a texture lookup by reading here :
So here the Easiest way to change Point Light attenuation ! (for the Deferred Path)
0- Note: I’m using Unity 4.5.0f6 ; some things may change in future versions
1- Download the built-in shader package matching your Unity version from here : http://unity3d.com/unity/download/archive/
2- Extract “DefaultResources/Internal-PrePassLighting.shader” to “/Assets/Resources” or in a subfolder ; personally, I put it in “Resources/Shaders”.
3- Restart Unity, else the shader isn’t used by the Editor renderer (as explained here : http://docs.unity3d.com/Manual/RenderTech-DeferredLighting.html )
4- 1st option : change the calculation in this function “half4 CalculateLight (v2f i)” of Internal-PrePassLighting.shader .
EDIT: with Unity 5, it must be done in UnityDeferredCalculateLightParams() in UnityDeferredLibrary.cginc
You also have to go to Edit/ProjectSettings/Graphics and set Deferred to Custom shader and then link your custom Internal-DeferredShading shader.
If like me, you want the point light to have less attenuation, you add the last line here :
#if defined (POINT) || defined (POINT_COOKIE)
float3 tolight = wpos - _LightPos.xyz;
half3 lightDir = -normalize (tolight);
float att = dot(tolight, tolight) * _LightPos.w;
att = att * att;
Instead of “att * att”, you can put something else ; “sqrt(att)” will make everything more dark, and “att * att * att” will make everything even more clear
5- 2nd option : change the lookup texture
This option is interesting if you want something more complex. But it won’t work for cookie light (it’d need some other stuff, I didn’t look in details).
a- in Internal-PrePassLighting.shader, under :
_LightTextureB0 ("", 2D) = "" {}
add :
_LightTextureB02 ("", 2D) = "" {}
then replace this line :
sampler2D _LightTextureB0;
by this one :
sampler2D _LightTextureB0, _LightTextureB02;
then lastly, for the point light, change this line :
float atten = tex2D (_LightTextureB0, att.rr).UNITY_ATTEN_CHANNEL;
to :
float atten = tex2D (_LightTextureB02, att.rr).UNITY_ATTEN_CHANNEL;
Then in a monobehavior C# script, in its OnStart() function, add this :
//=== Point Light Attenutation
Texture2D m_AttenTex = new Texture2D(256, 1, TextureFormat.ARGB32, false, true);
m_AttenTex.filterMode = FilterMode.Bilinear;
m_AttenTex.wrapMode = TextureWrapMode.Clamp;
Color[] AttenColor = new Color[256];
for (int i = 0; i < 256; ++i)
{
float v;
if (i < 255)
{
v = i / 255.0f;
v = 1.0f / (1.0f + 25.0f * v);
}
else
v = 0.0f;
AttenColor[i] = new Color(v, v, v, v);
}
m_AttenTex.SetPixels(AttenColor);
m_AttenTex.Apply();
Shader.SetGlobalTexture("_LightTextureB02", m_AttenTex);
This is exactly the same attenuation texture than the default one created by Unity (except they probably don’t have 256 pixels in it, but 16 or 32, I guess), so tune it to your liking.
To match the above example to lower the attenuation, here what it’d give :
v = 1.0f / (1.0f + 25.0f * v * v);
Note: if don’t use linear space lighting (ie: you use default gamma space), you may need to use this call instead :
new Texture2D(256, 1, TextureFormat.ARGB32, false);