Surface Shader - Calling Standard Lighting Function From Custom Lighting Function

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:

#pragma surface surf Custom
inline half4 LightingCustom(SurfaceOutput s, half3 lightDir, half atten)
{
    if (_LightColor0 or _LightTexture0 meets a condition)
    {
        //Do a custom lighting calculation
    }
    else
    {
         //Use the Standard lighting calculation(s)
    }
}

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.

1 Like

Here you are.

CGPROGRAM

#pragma surface surf Custom fullforwardshadows
#include "UnityPBSLighting.cginc"

// ...
// ...
// ...

inline half4 LightingCustom(SurfaceOutputStandard s, half3 lightDir, UnityGI gi)
{
    return LightingStandard(s, lightDir, gi);
}

inline void LightingCustom_GI(SurfaceOutputStandard s, UnityGIInput data, inout UnityGI gi)
{
    LightingStandard_GI(s, data, gi);
}

ENDCG
4 Likes

Thank you!

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?