Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Mesh triangles inexplicably offset by value along normals when importing FBX

Discussion in 'General Graphics' started by exitshadow, Aug 17, 2023.

  1. exitshadow

    exitshadow

    Joined:
    Nov 28, 2020
    Posts:
    7
    Hello!

    I just found this extremely strange issue when working with Unity Toon Shader. Some faces were rendered black, so I assumed it was a normal problem. Returning to Blender, the normals of the model were absolutely pristine. Somehow, the weird shading issue seems to be vaguely linked to sharp edges, but I couldn’t figure out how exactly. I fiddled with the mesh for a while, adding weighted normals—that seemed to fix the issue, but only partially. I so figured that there must have a problem in the FBX exporter or importer.

    Looking at the faulty mesh in question in Wireframe mode, something very strange appears:


    As you can see, the wireframe selector, but not the mesh itself, seems to be offset by some value along the normal. It is very strange because the other meshes that have been exported and imported at the same time do not present that issue, as it’s possible to see in the image.

    In the import parameters, forcing mesh compression to medium solves the shading problem, but not this wireframe offset thing.

    I’m not even too sure it’s linked, but since it’s present only on the mesh that had shading defects, it could be.

    Has anyone ever encountered this? I would be really interested to have an explanation and know if there is something I could do about it in the future when modelling assets.
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,630
    This is just a guess:

    Most toon shaders do the outline by rendering the mesh a second time, offseting its vertices along the normal vectors and reversing culling. This happens in the vertex shader stage. Now, I've never used Unity's Toon shader but looking at pictures of it I'd bet they do it this way (alternative would be to do outlines in a fullscreen post-process pass, but that's probably not flexible enough for a generic shader).

    So when you select the mesh and check its wireframe, you're probably getting the original mesh without any vertex offsetting since the wireframe shader does not perform any offsetting. However, the original mesh uses a different vertex shader that offsets the vertices, so wireframe view and the original mesh don't match (this is ok).

    Check your face normals in Blender, if faces are being drawn black that probably means their normals are flipped, so the toon shader is rendering them on top obscuring the colors that should show inside the outline.
     
    Last edited: Aug 22, 2023
  3. exitshadow

    exitshadow

    Joined:
    Nov 28, 2020
    Posts:
    7
    Thank you, this all makes sense.

    Yes, that’s what I thought at first, so I turned off the outlines. Now maybe it still keeps executing, just not doing anything with it. The strange thing is that the vertex offset thing only occurs in that mesh and they all share the same shaders—the original one from their original material, and the Toon one. Even, the same material.

    The problem is that I have checked the faces orientation over and over again. It is in my pipeline production to always work with backface culling during process and face orientation debug view before export. The meshes are clean, or I am taking crazy pills!

    There is obviously something that is going on that I am not seeing, that’s for sure, but it seems to be on some data that Unity is reading and that Blender might not care about, or is correcting by itself. I tried reimport the exported FBXs in Blender and couldn’t find any obvious anomaly such as flipped faces or doubled vertices with a flip. However when I entirely remodel the mesh (remodelling part of it doesn’t fix it!), the issue disappears so definitely it’s something to do with the vertices data, but they just show like normal in Blender.

    Maybe that could be a bug of Blender itself. I am continuing to investigate.