Search Unity

Question How to recalculate normals by dx and dy nodes in ShaderGraph

Discussion in 'Shaders' started by naileye, Mar 12, 2023.

  1. naileye

    naileye

    Joined:
    Oct 18, 2015
    Posts:
    6
    Hello everyone,

    I am trying to figure out how to recalculate smooth normal vectors in ShaderGraph using the dx and dy nodes. I have knocked my head on the table over and over again, but I still can't figure out how to do it. Can someone help me with that?

    I wanted the result to be smooth, not flat, like it is now.

    Thank you in advance.

    snipped-formula.png

    upload_2023-3-12_16-23-18.png
     
  2. naileye

    naileye

    Joined:
    Oct 18, 2015
    Posts:
    6
  3. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    ddx and ddy only ever look one pixel to the left/right and one pixel up/down. You can read more about how they work here: http://www.aclockworkberry.com/shader-derivative-functions/

    The faceted/low-poly look in your screenshot is the expected result if you try to use them to construct normals (it's worth noting some games actually do want that look). The only info they will give you is the pixels directly next to you on the same triangle, which only faces a single direction. You can't get info about other triangles from them, so can't create anything smoother.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    To follow up on this a bit, the partial derivatives are going to calculate the normal of the actual geometry surface, one triangle at a time. @burningmime mentioned the partial derivative functions are comparing values one pixel away in either axis, but on top of that it's only comparing values from the current triangle. Since the mesh that's being rendered is made up of flat triangles, and you're getting the derivative of the surface position, flat faceted normals is all you can actually get from partial derivatives.