Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Surface Shader - Calling Standard Lighting Function From Custom Lighting Function

Discussion in 'Shaders' started by Ewanuk, Apr 29, 2016.

  1. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    I'm trying to create a special kind of light by putting special conditions into the light's cookie. If it's a certain kind of light, do my own calculation, otherwise, use the Standard lighting calculations.

    I've created a Surface Shader using the "Create > Shader > Standard Surface Shader" Menu item from the project tab.

    In the shader, I've created a custom lighting function:

    Code (CSharp):
    1.  
    2. #pragma surface surf Custom
    3. inline half4 LightingCustom(SurfaceOutput s, half3 lightDir, half atten)
    4. {
    5.     if (_LightColor0 or _LightTexture0 meets a condition)
    6.     {
    7.         //Do a custom lighting calculation
    8.     }
    9.     else
    10.     {
    11.          //Use the Standard lighting calculation(s)
    12.     }
    13. }
    My question is, how do I call Unity's standard lighting calculations? Is it that simple? Would I be better off using a multi-compile statement and setting the conditions another way?


    Edit: I should note, I would love to just modify DefaultResourcesExtra/Standard.shader, but I'm not sure where to even begin there. I only need a sub-set of the standard shader's input power (Normal Maps and Emission). So creating my own surface shader seemed like a simpler option.
     
    Lipoly likes this.
  2. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    Here you are.
    Code (ShaderLab):
    1. CGPROGRAM
    2.  
    3. #pragma surface surf Custom fullforwardshadows
    4. #include "UnityPBSLighting.cginc"
    5.  
    6. // ...
    7. // ...
    8. // ...
    9.  
    10. inline half4 LightingCustom(SurfaceOutputStandard s, half3 lightDir, UnityGI gi)
    11. {
    12.     return LightingStandard(s, lightDir, gi);
    13. }
    14.  
    15. inline void LightingCustom_GI(SurfaceOutputStandard s, UnityGIInput data, inout UnityGI gi)
    16. {
    17.     LightingStandard_GI(s, data, gi);
    18. }
    19.  
    20. ENDCG
    21.  
     
  3. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    Thank you!
     
  4. cmiz

    cmiz

    Joined:
    Apr 28, 2017
    Posts:
    1
    Apologies for the necrobump, but I am trying to do something very similar. I am not finding a way to access _LightTexture0 to do a texture lookup (and more importantly, I also need the light space uv coordinates). Any advice?