Search Unity

Correctly passing a surf variable to custom Lighting<name> function

Discussion in 'Shaders' started by moosefetcher, Jul 6, 2019.

  1. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    74
    I'm finally getting specular working in my custom LightingPlanetSurface function but I can't seem to pass a particular variable into the function. I have the specular working fine, it's just when I try to include variables passed in from the surf function that the shine disappears.
    In my surf function, I've got a _groundFlag variable that is set to 1 when a pixel is land and 0 when water.
    Here's how my lighting function is set up:

    1. struct SurfaceOutputCustom
    2. {
    3. fixed3 Albedo;
    4. fixed3 Normal;
    5. fixed3 Emission;
    6. half Specular;
    7. fixed Gloss;
    8. fixed Alpha;
    9. float4 flareColour;
    10. float4 grainierColour;
    11. float4 atmosphereShadowColour;
    12. float distanceFactor;
    13. float groundFlag;
    14. };
    15. half4 LightingPlanetSurface (SurfaceOutputCustom s, half3 lightDir, half3 viewDir, half atten) {
    16. …. function details here
    17. }
    And my surf function is set up like this:
    1. void surf (Input IN, inout SurfaceOutputCustom o) {
    2. …_groundFlag variable decided here
    3. // and I set the o.groundFlag variable like this...
    4. o.groundFlag = _groundFlag;
    5. }
    The _groundFlag variable is set to a valid value; I use it for other calculations in the surf function.
    So - why isn't it getting passed into the lighting function? When I include s.groundFlag in my specular calculations (even when I explicitly set the value to 1 or 0) the specular lighting disappears.
    Any pointers, much appreciated, thanks.
     
  2. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    74
    I think I figured it out. One of my specular calculations was multiplying a half4 by a float and it looks like that was breaking it. So all sorted now.