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

[Tutorial] how to calculate vertex normals in shader graph

Discussion in 'Community Learning & Teaching' started by gamedevbill, Feb 22, 2020.

  1. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    Last edited: Jan 26, 2021
    Ciderhelm, lilacsky824 and shamsfk like this.
  2. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    shamsfk likes this.
  3. Zicore

    Zicore

    Joined:
    Jul 2, 2018
    Posts:
    10
    This is a really great tutorial! However I have a hard time to get it running in my experiment.
    Maybe you can give me some tips.

    I basically create a crater like shape with this sub graph:


    Using your Neighbors and NewNormal Subgraphs here.


    The resulting mesh looks like this:


    Calculating the normals doesn't work.

    I'm pretty sure it's because I'm using the World Position in my displacement part, and only add the Y part later, but I don't know how to incorporate your neighbor sub graph correctly.
     
  4. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    I just saw this question... Yeah, the world position is a good guess, but i would have thought it'd still work. I'll try to build a test project from your screenshot in a week or so. I'll update here once i have something.

    Thanks
    gdb
     
  5. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    Ok, I've looked into it. The issue is that your "DispalceSub" graph is not actually outputting a position. It outputs xyz=[0,offset,0]. You correctly calculate that offset for the neighbors, but are feeding in a value to the normal calculator that has x & z components both set to 0.

    What you need to do is change DisplaceSub to include both positions as an input. Take the PositionWorld at the front end (as you currently do), then at the end, take PositionObject as an additional input and add it in before returning.

    upload_2020-7-26_19-1-52.png

    This wraps your entire vertex displacement into one subgraph. Then to do the normal calculations, you'll have to have two "Neighbors" nodes. One that calculates neighbors in world space, and one in object space.

    Then it works. Hope this helps.
     
  6. Zicore

    Zicore

    Joined:
    Jul 2, 2018
    Posts:
    10
    Thanks! I'm sure this will help!
     
    gamedevbill likes this.
  7. gamedevbill

    gamedevbill

    Joined:
    May 25, 2018
    Posts:
    43
    I've made this tutorial into a video! Be sure to check it out in the start of the thread above!
     
  8. immeasurability

    immeasurability

    Joined:
    Apr 22, 2014
    Posts:
    125
    Hello how this possible to recalculate normals for blend shape?
     
  9. shailadevi

    shailadevi

    Joined:
    Aug 26, 2020
    Posts:
    1
    Hi,

    I'm a French speaker so I understood the basic principle but I could never get into the technical part, so I simply followed the video and performed the actions shown in the video. I went to the git and tested with the NormalCalculation folder, even though it's for HDRP (because I'm in URP personally) and I tried again with the NormalCalc_URP folder, taking a good look at the structure of the SubGraphNormalCorrection. So I took the neighbours and newnormal subshader and applied the nodal structure with my own mesh deformation (just a noise on the Y value of a ground to achieve a soft mountain topology).

    But I got no results. The mesh looks a little darker but still just as flat, and my normals, which I display using c# code ( OnDrawGyzmo() ), tell me that they're still those of the base plane, flat and pointing skywards.

    Thanks in advance for your answers