Search Unity

Question Get normal after vertex displacement by sampling displacement function at neighboring points

Discussion in 'Shader Graph' started by keenanwoodall, Mar 14, 2020.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    I'm working on a shader that twists a mesh.
    2020-03-14_18-23-15.gif

    To recalculate the normals in the shader I'm calculating the position 3 times. The first time is used for the actual vertex position and the next two offset the position a tiny amount along the tangent and bitangent vector before getting sent into the twist function. This should allow me to approximate the final normal by getting the cross product of the direction of the two offset positions from the actual position after the twist is applied to them.

    The shader graph looks a lil more complicated than it actually is because I want the twist to be relative to an "origin" transform in the scene. To do this I have a script that is setting two matrices on the material for converting a point from world space to/from an "origin" transform's local space.

    You can see how it works below:
    2020-03-14_18-28-59.gif

    Here's the full shader:
    upload_2020-3-14_18-36-14.png

    Each blue circle is showing the nodes that are twisting an input point. It's the exact same nodes for the 3 points. All it's doing is making the points relative to the origin transform, twisting them, and then converting them back to object space.

    To the right of the blue circles the normal is the cross product of the direction from the twisted vertex point to the two points that were slightly offset before being twisted.

    I feel like I'm quite close to calculating the normals correctly, but it isn't quite right. If I zoom in you can clearly see it's not playing nice with lighting.
    upload_2020-3-14_18-40-11.png

    Any help would be appreciated and I'm happy to give more info if needed. There's some subgraphs I made for the shader but I don't think they're relevant since the vertex is clearly getting twisted correctly and the function is the same for each sample point