Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Matcap shader with normalmap

Discussion in 'Shader Graph' started by herrkjeldsen, Sep 6, 2019.

  1. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    Hi! I am using a basic matcap unlit shader graph which seems to work well enough. I would like to include a baked normal map to the calculation as well, but for the life of me I can't figure out how to combine the view space and normal map :confused:

    I would be super grateful if anyone has a tip or a solution of some kind :D
     

    Attached Files:

  2. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    The left one is a regular normal-mapped pbr shader, while the right one is the matcap shader.

    Unity_2019-09-06_12-42-09.png

    I think I have managed to convert the normal map to view space properly, but the normals along sharp edges and such do not behave the same as the regular LWRP Lit shader :confused:

    I'm so bad at this :eek:

    Also attached is the test scene and some sample geo
     

    Attached Files:

    Last edited: Sep 6, 2019
  3. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    Unity_2019-09-06_12-57-15.png Unity_2019-09-06_12-58-53.png

    It is perhaps more visible from the back
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    There's no need to assign a Sampler State node for textures that are material properties. It'll use that texture's existing sampler state by default.

    There's no need to blend the tangent space surface normal and the normal map. Just use the output of the normal map's Sample Texture 2D node into the tangent > view space Transform node.

    Try normalizing the vector after the Transform node before the Remap node.

    I would recommend using an uncompressed texture for you matcap. The compression artifacts can be magnified a bit, and may be leading to some of the "boxy" shapes you see on the matcap model.

    Make sure the matcap texture is limited to a sphere with all values outside of that copied from the edge using a dilate or blur of the original texture, and make sure it's set to wrap clamped.
     
    herrkjeldsen likes this.
  5. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    Hi bgolus! Excellent help, thank you very much! Also I very much appreciate the tips on optimizing and removing clutter!

    The normalizing certainly made an improvement!
    Unity_2019-09-09_10-07-05.jpg

    If I remove the tangent vector blend it goes all wonky, though, but I'm not quite sure why :eek:


    It's so weird, because it's almost as if the imported normals aren't taken into account and it tries to generate them anew or something. But that's not quite it either I suppose, as i get some very visible seams as well :confused:
    Unity_2019-09-09_10-07-31.png
    Unity_2019-09-09_10-16-34.png

    Anyway, I'm super thankful for pointers, it is a great help! :)
     

    Attached Files:

  6. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Yeah, something for sure is going wonky. The bad normals when not using the blend, and the seams when using the blend are both indicative of the tangent space of your normal map and the matrix being used by Shader Graph not matching. That bug report you linked to is likely related, but it might be worthwhile reporting the issue independently since this is technically a "different" bug so fixing one might not fix the other.

    As for why you can't find the test scene mentioned, that's because it's private. When you upload a bug report to Unity via the editor you have the option to upload all or part of your current project to Unity so they can use it to test with. Often times those uploaded projects are literally the game that person is working on, so they're not ever downloadable publicly via the issue tracker.
     
    herrkjeldsen likes this.
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Tried to reproduce the problem you're seeing locally with 2019.2 and LWRP 6.5.3. Curiously it works perfectly if I use the normal map output, but is broken when I use the blend.
     
    herrkjeldsen likes this.
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    One random thing I noticed. The Normal Vector node set to tangent space shows something like this (copied from your image above, but shows similarly for me):
    upload_2019-9-9_12-15-58.png

    The tangent space normal vector for any mesh should always be exactly (0,0,1). Any other value is wrong, so the fact the preview is showing colors other than a solid blue means that node is doing something wrong.

    Tangent space is defined as a matrix made of the interpolated vertex tangent (the x flow of the mesh UVs), bitangent (a cross product of the tangent and normal), and vertex normal. Since the vertex normal is the third part of the matrix, then the tangent space normal is always (0,0,1).
     
    herrkjeldsen likes this.
  10. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    Yeah that is weird :D Very useful to see it's not just at my end, though.

    You have been super helpful bgolus, and I really appreciate you taking the time! Thanks a bunch! :):) Also the tangent vector preview explanation was great :D

    I'll have a go with 6.4.3 and noodle around some more. Maybe even HDRP just for funsies and see if that makes a difference.
     
  11. Finch07

    Finch07

    Joined:
    Jul 27, 2013
    Posts:
    4
    @herrkjeldsen Can you post your updated shadergraph? I'm trying to solve the same problem
     
  12. herrkjeldsen

    herrkjeldsen

    Joined:
    Nov 30, 2012
    Posts:
    9
    Hi Finch! I don't think we progressed any further unfortunately, but I couldn't find the shadergraph in question to verify :D I'd love to revisit the approach some day though, with all the updates that have happened in the shader graph and render pipeline. I'd be very interested to hear of your progress too!