Search Unity

Shader - Avoid vertex Normals influence

Discussion in 'Shaders' started by MaT227, Apr 14, 2014.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hello everyone,

    I am creating a surface shader and I would like to avoid the vertex normal influence and only use the normal maps informations.
    Here is an example of the problem.



    How can I only use the normal data ? I don't want to handle this in the import settings.
    Thanks a lot.
     
  2. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    Tangent space normal maps (which Unity and basically every engine use) need vertex normals in order to be oriented properly with respect to the mesh surface. Object space normal maps don't have this requirement, so you could potentially use those.

    However, you probably just need to smooth your vertex normals: right now, they're split in places that make unnatural-looking facets. Have you tried playing with the smoothing angle in the imp or settings for your model?
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Hi Daniel,

    Thanks for your answer, I've tried to play with the smoothing angle and sometimes it fixes the issue but I don't want to be dependant of this.
    As you said it seems that Object space normal maps can do this.

    Is it possible to use Object space normals in a surface shader ?

    Edit: Here is an example of Object Space Normal integration
     
    Last edited: Apr 14, 2014
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    This is a problem where you should fix at your modelling application rather than messing with shaders. You should smooth your models normals in your modelling application, and use the highres mesh to bake the normals.
     
  5. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    The answers here beg the question 'Why would I want to use Tangent-Space normal maps?'
    This wiki article over at Polycount might help.
     
  6. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    This is a very good article thank you RC-1290. Unfortunately I don't want to modify the mesh.

    The aim is to avoid vertex normal influence for a tangent space normal map.
    But it seems impossible...
     
  7. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    I never tried, dont even know if it will look good or even will work, but you can also try to average and scale the vertex normals to a minimum before calculating tangent space rotation and only depend on tangents and binormals. Worth a try.
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    It's not possible to do - vertex normals, along with vertex tangents, are required for tangent space maps to work.

    In Unity, the normals - along with the tangents - are used to generate the bitangent/binormal. These three are used to create an orthogonal matrix that is used to rotate vectors (usually the light direction and the view direction) from object space into tangent space. These vectors need to be normalized to create a proper rotation matrix, so you can't scale the normals.

    These rotated vectors are then dot producted against the values from the tangent space normal map (which is already in tangent space) and lighting is calculated from that.

    So when you use tangent space normal maps, you are ignoring the vertex normals - all of the lighting information comes from the tangent space normal map - but you need the vertex normals in order to do anything useful with the tangent space normal map in the first place.



    Your issues look like your mesh has rubbish vertex normals. You'll need to properly smooth it in your 3D application and re-export it.
     
  9. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Here is an example of what I mean.
    This is from the Half-Life Model Viewer with a Dota 2 model. They are using Tangent Space Normals but there's is no influence of the vertex normal.



    How is it possible ?
     
  10. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    They have baked the normal map correctly and it is synced to the Source engine's tangent basis.
     
  11. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    So, it's seems to be impossible to use those normals with Unity, there will always be some discordance ?
     
    Last edited: Apr 15, 2014
  12. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    They are also avoiding normal map symmetry seams in their shader.
    This is another thing that is hard to fix.
     
  13. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    I feel like this might still not be clear, even though it's been alluded to a couple times: there is no such thing as tangent space without vertex normals and tangents. Those are the two vectors which define tangent space. Vertex normals will always influence tangent space because they define it.

    What you're seeing is vertex normals (and probably tangents) which don't match the basis for which the normal map was generated. The first thing you should try is setting both normals and tangents to "import" in model import settings. Unity does not have all the information available that the modelling program does, and is unlikely to be able to match your normal maps, especially with a made-up smoothing angle.

    If you're still having problems, you'll likely have to re-bake your normal maps. Farfarer probably knows more about doing this for Unity than anyone, and has kindly made a plugin for xNormal that should help you generate correct results.
     
  14. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Thank you all of you for your great explanations. I've made a huge step in Normal comprehension. I was missing the basis concept.