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. Dismiss Notice

Specular highlights on legacy shaders not round?

Discussion in 'Shaders' started by michal_gjk, Aug 13, 2016.

  1. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Hello.

    Is any of the non-standard shaders able to generate a nice sharp and most importantly ROUND Phong highlight?

    Anyone I try is basically highlighting the geometry. You can clearly see edges on spheres in the material editor.

    It basically looks like a per-vertex highlight.

    Standard shaders don't seem to have that problem.


    Just in case it's not clear let me underscore I'm talking about the stock object which you can see at the bottom of the material editor. Plain albedo (no texture) and no bumpmap with about 90% shinines.

    FWIW. quality: fantastic. 4 pixel lights. ver. 5.4.0f3. Win10.


    If not can you point me to the exact bit of code that makes the difference in the standard shader?
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    I did notice that they don't seem to normalize the surface normals in the legacy shaders for some reason. You would need to create your own lighting model in a surface shader and do that, which isn't too hard. If you need help with that just ask.
     
  3. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Thank you for your reply.

    I think I can handle that. Could you just point me to the bit of code in the standard shader that normalizes the surfaces normals? I could only find the bit where they normalize the bumpmap ones but that's obviously not what we're after.

    Would appreciate some guidance.
     
  4. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    For those who may also be annoyed be unity's implementation of LightingBlinnPhong () you need to normalize s.Normal SurfaceOutput s.

    s.Normal = normalize(s.Normal);

    I can understand why they chose not to pass it normalized already but the fact that their shaders' lighting functions don't normalize it is a bug IMO. and it's been there for years I presume.

    Their custom LightingMobileBlinnPhong() in the .shader files also leave normals unnormalized....
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    It's an optimization. Normalize isn't free, so it's cheaper to only normalize the vector on each vertex, though obviously doesn't make for as nice looking highlights.
     
  6. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    then if anything that should be a shader variant with normalization as an option. Otherwise practically anyone not using standard shaders needs to dive into shader development from the get go to be able to get any acceptable image quality.

    Are you 100% sure it was a conscious decision and not an omission ?

    Is it mentioned anywhere officially?
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    Not really.

    And Unity has kind of been a "mobile first" platform for a long time, which means usually choosing optimization over accuracy. I think it's a bit of an omission to not have the normal be normalized for surface shaders in 5.0, but I suspect it was kept that way to keep the functionality identical to previous versions of Unity. This has been reported as a bug several times over the last few years and it's never been fixed so I can only assume it's intentional on their part.
     
  8. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    OK. Thanks for the reply.
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    Also, another point to it being intentional ... I'm pretty sure the standard shader does the same thing, or used to, depending on the platform. Basically on any decent PC it's doing the normalization in the fragment, but on really old PCs or mobile it doesn't. If I remember correctly there was even a note with something like "Can we fit normalization into the shader model 2.0 instruction count?"
     
  10. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Well it's done at the very beginning of LightingStandard(Specular) and it's not #ifdef'd so it applies to everything.

    Which figures. With the amount of code standard shaders have to get through one normalization is the least of their problems.
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,208
    Look at the Simplified version of the standard shader, that's the one that runs on other platforms.
     
  12. michal_gjk

    michal_gjk

    Joined:
    Aug 13, 2016
    Posts:
    69
    Gottcha!

    Unfortunately I see no easy way to force it on to see it in action.