Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Using GGX instead of Blinn-Phong

Discussion in 'General Graphics' started by G-Mika, May 5, 2015.

  1. G-Mika

    G-Mika

    Joined:
    Jul 17, 2012
    Posts:
    94
    I started a blog about advanced graphics programming in Unity, I just wrote a post on how to replace Blinn-Phong by GGX to get a more natural and softer specular highlight in Unity:

    http://gfxdevunity.blogspot.ca/2015/05/replacing-blinn-phong-by-ggx.html

    For some reasons they replaced GGX by blinn-phong, but most other engines uses GGX. It causes less specular aliasing and it looks a bit more natural.
     
    a2, ArthurT, wdw8903 and 3 others like this.
  2. mholub

    mholub

    Joined:
    Oct 3, 2012
    Posts:
    123
    I studied Unity CG include files today and accidentally found fragment of code in UnityStandardConfig.cginc which looks like this:
    Code (csharp):
    1.  
    2. #define UNITY_BRDF_GGX 0
    3.  
    And in UnityStandardBRDF.cginc
    Code (csharp):
    1.  
    2. #if UNITY_BRDF_GGX
    3.     half V = SmithGGXVisibilityTerm (nl, nv, roughness);
    4.     half D = GGXTerm (nh, roughness);
    5. #else
    6.     half V = SmithBeckmannVisibilityTerm (nl, nv, roughness);
    7.     half D = NDFBlinnPhongNormalizedTerm (nh, RoughnessToSpecPower (roughness));
    8. #endif
    9.  
    So the simplest way to use CGX is to copy Standard.shader and add one more line in CGINCLUDE block

    Code (csharp):
    1.  
    2. CGINCLUDE
    3.         #define UNITY_SETUP_BRDF_INPUT MetallicSetup
    4.         #define UNITY_BRDF_CGX 1
    5. ENDCG
    6.  
    Specular lighting changes when you do this. Does it work? I can't visually distinguish CGX and Blinn-Phong :)

    What is actually strange, when I define UNITY_BRDF_CGX, specular highlights look harder, not softer like on your pictures.

    CGX:
    CGX.png

    Blinn-Phong:
    Blinn-Phong.png
     
    Last edited: May 5, 2015
  3. G-Mika

    G-Mika

    Joined:
    Jul 17, 2012
    Posts:
    94
    Yes you are right, it's probably a simpler way to do it. I should have looked into the UnityStandardConfig.cginc file :)

    You seem to use a very low resolution texture, you might wan't to test on something more high res. It will really shows up within my test scene.

    I exaggerated one of the roof smoothness a bit to highlight the changes :
     

    Attached Files:

    minhdaubu2 and theANMATOR2b like this.
  4. minhdaubu2

    minhdaubu2

    Joined:
    Jun 10, 2014
    Posts:
    70
    hey, could you make the "Directional Specular" to GGX too? :D
     
    Last edited: May 7, 2015
  5. G-Mika

    G-Mika

    Joined:
    Jul 17, 2012
    Posts:
    94
    dnnkeeper, minhdaubu2 and OCASM like this.
  6. minhdaubu2

    minhdaubu2

    Joined:
    Jun 10, 2014
    Posts:
    70
  7. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    My guess is that they went with Normalized Blinn Phong so that they could have consistent visuals with their mobile PBR path where they use a much lower quality Blinn Phong.

    On a related note, if you want better compatibility with tools like Substance Designer and Painter I recommend you set the following:
    Code (CSharp):
    1. #define UNITY_BRDF_GGX 1
    2. #define UNITY_GLOSS_MATCHES_MARMOSET_TOOLBAG2 0
     
    Last edited: May 8, 2015
  8. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    I have two additional notes for further improving the mapping to the Substance tools.

    First, you should change the _Metallic property from:
    Code (CSharp):
    1. [Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    to:
    Code (CSharp):
    1. _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    Second, you need to make sure that your metalgloss maps' import settings are set to "Advanced" and check "Bypass sRGB Sampling".
     
    Last edited: May 8, 2015
    tswalk and minhdaubu2 like this.
  9. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,108
    so simple, yet so effective... thanks for posting this! makes a huge difference.
     
  10. minhdaubu2

    minhdaubu2

    Joined:
    Jun 10, 2014
    Posts:
    70
    where I can find those codes? i use unity 5.0.1p3
     
  11. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    @minhdaubu2
    Download the archive from the first post in this thread, then you can patch the shader file and the config header.
     
  12. minhdaubu2

    minhdaubu2

    Joined:
    Jun 10, 2014
    Posts:
    70
    I did it, but i only see that in "GGX-Standard.shader" :(
     
  13. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    @minhdaubu2
    In "GGX-Standard.shader" find the "_Metallic" property and change it to this:
    Code (CSharp):
    1. _Metallic("Metallic", Range(0.0, 1.0)) = 0.0
    In "UnityStandardConfig.cginc", find "UNITY_BRDF_GGX" and "UNITY_GLOSS_MATCHES_MARMOSET_TOOLBAG2" and change them to this:
    Code (CSharp):
    1. #define UNITY_BRDF_GGX 1
    2. #define UNITY_GLOSS_MATCHES_MARMOSET_TOOLBAG2 0
    Finally, any time you use a metalgloss map make sure you set its import settings to "Advanced" and check "Bypass sRGB Sampling".
     
    Last edited: May 9, 2015
  14. minhdaubu2

    minhdaubu2

    Joined:
    Jun 10, 2014
    Posts:
    70
    -Thank so much :rolleyes:
     
  15. modar

    modar

    Joined:
    Jul 18, 2013
    Posts:
    27
    @n00body what about Alloy shaders? do they natively agree with Substance Painter shader?
     
  16. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    @modar
    Yes, we use the exact same material inputs as Substance Designer/Painter, and even provide preview shaders for each tool to maximize the quality of the transfer.
     
  17. modar

    modar

    Joined:
    Jul 18, 2013
    Posts:
    27
    @n00body
    Great! so you are telling that I should get the same results of SP in unity if I use Alloy and the same skybox? sounds very tempting :)
     
  18. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    @modar
    It's not 100% the same, since Unity precomputes and stores its reflection probes while the Substance tools use importance sampling. However, we've found that it is a pretty close match.

    Check out the crash course video if you want to see how Alloy works:
     
    theANMATOR2b likes this.
  19. zyzyx

    zyzyx

    Joined:
    Jul 9, 2012
    Posts:
    222
    Now all we need is Alloy for mobile ;)
     
  20. n00body

    n00body

    Joined:
    Dec 17, 2010
    Posts:
    691
    @zyzyx
    Hopefully once OpenGL ES 3.0 hardware is more prevalent, and Unity has figured out how to handle mobile hardware sRGB limitations.
     
  21. dnnkeeper

    dnnkeeper

    Joined:
    Jul 7, 2013
    Posts:
    74
    Thank you G.Mika for this shaders. GGX looks gorgeous. I managed to replicate scene from UE4 with this. Here is some screenshots that shows the difference. Demo scene build and sources are available here: https://yadi.sk/d/g8lbg3frizHdF
     

    Attached Files:

    G-Mika likes this.
unityunity