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

Is this a problem with normals

Discussion in 'General Graphics' started by MonkeyCrime, Dec 4, 2014.

  1. MonkeyCrime

    MonkeyCrime

    Joined:
    Jun 13, 2013
    Posts:
    32
    I'm playing around with my own procedural mesh generator (perhaps for top-down shooter). Because I want to use a texture atlas for roads, river beds etc I've split everything up into discrete triangles. I thought I was recalulating my normals correctly by finding all triangles that share the same vertex position, summing and then averaging their normals and applying that value to the vertex in question.

    But, for the centre vertex, I seem to get this weird lighting issue that the attached image hopefully shows. The centre vertex normals seem to be wrong and I get this weird "X" effect. The "bad normals 2" image shows the wireframe; 4 tris per quad, laid out in a grid with a heightmap raising each corner of the quad.

    I know I should be able to use sub meshes etc and apply different materials but I haven't figured out how to make this work with my planned approach to making hills, carving riverbeds, placing roads etc so I've done it this way for now.

    Any thoughts on what I'm doing wrong? I really want to get my own code working just so I can say I did it, but I'm frustrated at the moment.
     

    Attached Files:

  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    Normally if you build a Mesh at runtime in Unity you can just call 'RecalculateNormals' to calculate the normals from the faces/verts etc... It might be useful if you posted the code that calculates the normals.

    For face normals you can create them using a cross vector of 2 joined edges. One thing you may be doing wrong is forgetting to normalize your vertex normal (the one that gets calculated by summing the shared face normals and averaging them).
     
  3. MonkeyCrime

    MonkeyCrime

    Joined:
    Jun 13, 2013
    Posts:
    32
    My code is pretty messy at the moment due to all the things I've been trying. For each face I get the vector cross and then normalize it by dividing the normal by it's magnitude.

    Over the weekend I tried another approach by removing the centre vertex and going back to a more typical two-triangle quad approach. Once I understood how to create the submeshes and assign them I managed to get what I wanted; a system where I can have the terrain with one texture and rivers/hills/roads etc using different textures. Still some issues with normals where the submeshes meet but I think I know what to do.
     

    Attached Files: