Search Unity

[SOLVED] Obtaining Light scale inside Shaders

Discussion in 'Shaders' started by Deleted User, Aug 25, 2014.

  1. Deleted User

    Deleted User

    Guest

    Background
    I'm attempting to do realtime fake spherical area lights. In order to do that, I need to pass into my shaders a parameter for the light's size. Since light inputs are limited to what Unity already passes in, I need to re-purpose a channel for my own use. It appears that in Unity, the light transform scale isn't currently used for anything, so it seems like the best candidate right now.

    Question
    Is there any way to obtain a Point/Spot light game object's transform scale inside a surface shader and a deferred prepass override shader?
     
    Last edited by a moderator: Aug 25, 2014
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    What is "light scale", color vector magnitude? Anyway seems you may use "_LightColor0.a".
     
  3. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    _LightPos.w ???
     
  4. Deleted User

    Deleted User

    Guest

    For anyone who is curious, here is the answer. The approach is different depending on where you are trying to obtain it. For example:

    Forward shader passes
    http://forum.unity3d.com/threads/ge...ight-in-forward-add-mode.213430/#post-1433291

    Please note, it may not be super expensive but it certainly isn't free. To alleviate that, I strongly recommend doing this calculation in your vertex shaders and transferring the result to the pixel shader via interpolators.

    Deferred override shader
    range = rsqrt(_LightPos.w);

    This works since the constant "_LightPos" stores "1 / (range * range)" in its ".w" channel.