Extending surface info in surface shader

I’m trying to write a custom illumination model for a surface shader.
However I find no way to pass a custom value calculated in the “surf()” function to the custom illumination model.

If it was possible to just extend the SurfaceOutput struct, then I could just add there the extra info I need, but I could not find out if this is possible.

Note that I can’t just use shader properties, since the value is calculated during the shader, in the surf() function.

Right now I cheated by using the SurfaceOutput.alpha to store a float and then retrieve it in the illumination function. However later I want to make a transparent version of the shader, so I need to find a better solution.

If I understand you correctly, it seems like this is what you’re looking for:

Thanks, but I’ve read that link before writing this thread :slight_smile:

Anyway, I’ve found a solution, which is actually very obvious…

Any shader variable declared outside the surf() function can be used to calculate a value inside the surf(), store the result and read the result in the lighting function.

Hey, I just looked over that page again and realized it didn’t contain the snippet I thought it did. You can definitely declare a global variable, but you can (and probably should) also create a struct to pass into your custom lighting model, like so (only including the relevant code):

#pragma surface surf Custom

struct SurfaceOutputCustom{
    fixed3 Albedo;
    fixed Alpha;
    fixed3 Emissions;
    fixed3 Normal;
    fixed Specular;
    fixed OtherVariable;
};

void surf (Input IN, inout SurfaceOutputCustom o){
    o.OtherVariable = 0.5;
}

inline fixed4 LightingCustom(SurfaceOutputCustom s, half3 lightDir, half atten){
    fixed4 c = fixed4(s.OtherVariable, 1.0, 1.0, 1.0);
    return c;
}

Ahh, I see, very useful!!

Weird that’s something important as that seems missing from the documentation.

Thanks :slight_smile: