Search Unity

Shader that lights up by a specific light source?

Discussion in 'Shaders' started by BakeMyCake, Jun 22, 2017.

  1. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    Hi guys, is there a way I can prevent one light source from contributing to my shader or material?

    I know you can layer game objects, but I have a mesh that has several materials. One of the materials needs to ignore all except one light source.
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    You could write a shader that uses a specific light for information passed via code, rather than using the global automatic lighting variables, i.e. in code set material/global shader properties pertaining to the position of the individual light source, light colour, intensity, etc. Then, in the shader itself use those variables to light the object, i.e.

    Code (CG):
    1. //Where _LightPosition is the world space position/direction of the light passed via code
    2. float3 lightDir = normalize (_LightPosition - worldPos);    //For a point/spotlight, or:
    3. //float3 lightDir = _LightPosition;    //Direction of directional light
    4. half diff = max (0, dot (normal, lightDir));    //The actual lighting function
    You would attach the code to the specific light source and set global shader variables such that the information works for any object.
     
    BakeMyCake likes this.