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

Procedurally generated flat shaded mesh

Discussion in 'Scripting' started by gorbit99, Oct 16, 2016.

  1. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    I'm creating a mesh procedurally, but for the sake of generating time I just simply used shared vertices, and now the whole mesh is smooth. Can I achieve flat shading without giving each triangle it's own vertices (I wouldn't mind a solution with a shader too). Thanks in advance, gorbit99
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    The model importer (this has little to do with your use-case, but bear with me) has a setting for Normals in which you can choose "Calculate" and then set a maximum angle at which to smooth vertices. From what I understand, this is actually accomplished by generating additional vertices (if they didn't exist already and you need them) or combining vertices (if they did exist and you don't need them) for all of the triangles in the model. If you import a box model with 8 verts, it'll look smoothed, but if you set the importer to Calculate the normals and then set the max angle to 0, it'll actually have 24 verts when used in Unity. The Unity cube primitive also has 24 verts.

    It may be possible to have a shader that makes the model appear flat, but it's important to know that "flat" is actually expressly defined as "having separate vertices per triangle", so going out of your way to avoid that scenario seems a little masochistic, lol.
     
    gorbit99 likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Does giving the mesh separate triangles really mess up generation time that much?

    The flat or smooth appearance is more to do with the normals then the underlying mesh topography. How are you setting the normals?
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Well, if you create objects at a large scale, then it will slow it down, but I can live with that I guess

    I'm not calculating them, I just call Mesh#RecalculateNormals()
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    From my understanding, it only has to do with normals if those extra vertices are actually there, otherwise the "1 normal per point" limitation doesn't really give you much choice in the matter. Could be wrong of course though, this isn't exactly an area I'm super-familiar with.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could go with UVs and a normal map. But this seems more complex then simply using the right number of vertices.

    Don't guess, profile.

    How often are you generating this mesh that adding a few more Vector3 gives you a slowdown problem?
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It's a terrain, so it generates once, but then almost never changes (or even if it is changing, only a small fraction of the whole terrain)