Search Unity

Vertex shader breaking the mesh apart

Discussion in 'Shaders' started by petey, Nov 18, 2017.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hi all,

    Just having a little play with vertex offset in a shader.
    Is there any way to keep the mesh from breaking up like this?


    Here's my graph in shader forge. It's pretty straight forward but there don't appear to be many settings in shader forge, or mesh import that seem to have a big effect.


    Thanks,
    Pete
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Hard edges. Unify your normals.
     
    petey and bgolus like this.
  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey thanks man!
    Did a couple of quick tests with different imported normals -


    Do you know if there is any way to change a meshes normals at runtime?
    I'm trying to use this shader for an effect that wont work with the object's imported normals.

    I was thinking of using it for an effect like this -

    So I was thinking of copying the mesh at runtime and applying this shader.


    Thanks!
    Pete
     
    Last edited: Nov 19, 2017
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    You can change the normals at runtime, but it can be a relatively expensive operation. You have to manually iterate over all of the vertices in the mesh and find the ones with the same position, then average the normals and reapply. You're much better off making duplicate mesh assets with different import settings, or doing what some of the toon shader assets do and bake the averaged normal into the tangent or a free UV channel. The benefit of using the mesh tangent is it works on skinned meshes, but you can't use normal maps as easily.
     
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Hey thanks!
    So you bake into the spare UV channel (or tangent) then write that data to the mesh normals at runtime rather than having to calculate them at runtime?
    If so that sounds okay, I have easy access to that channel data from Houdini, and I'm not using normal maps.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    No, just change the shader to use the appropriate data on the mesh to do the push rather than the normal. Don't bother doing anything to the mesh from script. That's just wasting memory and CPU time.
     
    petey likes this.
  7. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,824
    Ahh awesome thanks!