Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Mobile Surface Shader using Metallic/Smoothness Texture Lighting?

Discussion in 'Shaders' started by hungrybelome, Sep 28, 2018.

  1. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Hi,

    I'm currently trying to edit the Mobile-BumpSpec shader to use metallic/smoothness inputs instead of specular/gloss. I've edited the shader to accept a Metallic/Smoothness texture like the Standard shader.

    Right now I am just setting o.Gloss to metallic and o.Specular to smoothness, and it kind of works for some models, but I'd rather have a correct lighting model that expects metallic/smoothness inputs.

    Right now I'm trying to edit the lighting model used in Mobile-BumpSpec:

    Code (CSharp):
    1. inline fixed4 LightingMobileBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
    2. {
    3.     fixed diff = max (0, dot (s.Normal, lightDir));
    4.     fixed nh = max (0, dot (s.Normal, halfDir));
    5.     fixed spec = pow (nh, s.Specular*128) * s.Gloss;
    6.  
    7.     fixed4 c;
    8.     c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * atten;
    9.     UNITY_OPAQUE_ALPHA(c.a);
    10.     return c;
    11. }
    I've looked at the StandardBDRF file to try and see if they have a lower LOD version using metallic/smooth, and I think they have this:

    Code (CSharp):
    1. // Old school, not microfacet based Modified Normalized Blinn-Phong BRDF
    2. // Implementation uses Lookup texture for performance
    3. //
    4. // * Normalized BlinnPhong in RDF form
    5. // * Implicit Visibility term
    6. // * No Fresnel term
    7. //
    8. // TODO: specular is too weak in Linear rendering mode
    9. half4 BRDF3_Unity_PBS (half3 diffColor, half3 specColor, half oneMinusReflectivity, half smoothness,
    10.     float3 normal, float3 viewDir,
    11.     UnityLight light, UnityIndirect gi)
    12. {
    13.     float3 reflDir = reflect (viewDir, normal);
    14.  
    15.     half nl = saturate(dot(normal, light.dir));
    16.     half nv = saturate(dot(normal, viewDir));
    17.  
    18.     // Vectorize Pow4 to save instructions
    19.     half2 rlPow4AndFresnelTerm = Pow4 (float2(dot(reflDir, light.dir), 1-nv));  // use R.L instead of N.H to save couple of instructions
    20.     half rlPow4 = rlPow4AndFresnelTerm.x; // power exponent must match kHorizontalWarpExp in NHxRoughness() function in GeneratedTextures.cpp
    21.     half fresnelTerm = rlPow4AndFresnelTerm.y;
    22.  
    23.     half grazingTerm = saturate(smoothness + (1-oneMinusReflectivity));
    24.  
    25.     half3 color = BRDF3_Direct(diffColor, specColor, rlPow4, smoothness);
    26.     color *= light.color * nl;
    27.     color += BRDF3_Indirect(diffColor, specColor, gi, grazingTerm, fresnelTerm);
    28.  
    29.     return half4(color, 1);
    30. }
    But I'm not sure where to get some of the arguments for that function, like oneMinusReflectivity.

    Any advice on how to make a mobile surface shader using a metallic/smooth texture? Is metallic/smooth lighting inherently more expensive than spec/gloss lighting and there is no way to get a cheap mobile version? I would think that the two would be comparably expensive based upon reading spec/gloss vs metallic workflow comparisons, but I really don't know.

    Thanks!
     
  2. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,466
    Metallic/smoothness shader are PBR shader, the various requirement to make pbr is what makes it expensive. One way around this is to bake shader into a lighting lut, like the old shadowgun used to do. Basically you calculate offline a texture indexed by ndotl (diffuse) and ndoth (specular) and then use those term in real time to get the PART of lighting (no fresnel due to requiring a 3d texture). So yeah having a full pbr lighting is more involved in mobile, and require knowing the concept behind it to know what trade off to do. But I think unity already has the standard shader compiling to mobile version already, and they are still way more expensive than simple ndotl, ndoth used in mobile bump.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,229
    Really metallic and smoothness are just a different way of storing specular and sharpness, along with a way to extrapolate a specular color.

    In PBR, the smoothness controls both the “gloss” and the specular sharpness, because that’s how it works in the real world. It makes sense if you think about it as a sharp specular reflection is reflecting the same total amount of light as a soft
    specular reflection, but the later is reflecting that same total amount of light from over a larger area.

    Metallic also controls the “gloss”, and also the diffuse and specular color. Dielectrics, or non metal surfaces, almost all have very similar “gloss” values, roughly a “gloss” value of 0.04. Yes, really that low. However the specular highlight is much brighter in PBR at the smoother ranges than the old blind phong ever is. The gloss also goes to 1.0 at glancing angles so it can be much, much brighter. That’s a metallic of 0.0. For a metallic of 1.0 the diffuse texture’s color is used as the specular color rather than using a gloss at all. The gloss value is effectively “1.0”. The actual diffuse color used for lighting is black.

    You can approximate the PBR shading much more cheaply than Unity’s Standard shader does, the LW renderer is using an approximation that’s almost half the cost of the Standard’s mobile “BRDF3” for very similar quality for example, or translate the metallic and smoothness values to very approximate gloss and spec power values using the old blinn phong.
     
    hungrybelome likes this.
  4. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Thanks so much for the detailed post once again, bgolus! That's a great primer on lighting models. I'll have to read more about this, and try to find some visualizations.

    "or translate the metallic and smoothness values to very approximate gloss and spec power values using the old blinn phong."

    Could you please elaborate on that, if possible?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,229
    Well, what you were doing already could be considered a basic approximation. Or using the smoothness for both gloss and spec. You can get slightly better results if you apply a power to the smoothness before using it as the spec. What power is going to depend on what you thing looks good. There are better approximations, I believe you'll find one if you look deeper in the BRDF3_Direct function for converting smoothness to specular power. Or search online for how to get specular power from smoothness or roughness. You'll find several different solutions.
     
    hungrybelome likes this.
  6. hungrybelome

    hungrybelome

    Joined:
    Dec 31, 2014
    Posts:
    336
    Thank you, that's a great starting point. I'll investigate all three suggestions. Thanks bgolus!
     
  7. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    692
    @hungrybelome did you ever make it anywhere with an approximated mobile PBR shader? I'm looking into the same thing right now.
     
    lekhamadhuribollineni likes this.
  8. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    692
    @bgolus do you know anything about the LWRP standard shader? Would it be feasible to port it to the built in render pipeline? We are really trying to reduce shader complexity in our project right now, and most everything uses the standard shader atm.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,229
  10. joshuacwilde

    joshuacwilde

    Joined:
    Feb 4, 2018
    Posts:
    692