Search Unity

Smoothing angle on a dynamically generated mesh

Discussion in 'Scripting' started by LukeB, Aug 4, 2010.

  1. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Hey everyone!

    I've ended up generating a mesh dynamically (using new Mesh() then setting its vertices/ uv), however it ends up appearing quite sharp around the edges. I'm guessing that this is something to do with the 'smoothing angle' that's automatically applied to imported meshes, however I can't find anything that would do it on a dynamically generated one. Can anyone please help?

    Thanks and all the best,
    Luke Briggs,
    KuleStar
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    if you create the mesh yourself, you decide yourself on where you want to create new vertices and where use the same for multiple triangles actually. Your code also decides the normal of the vertex.

    Through this control you define how smooth / hard edged it actually will be
     
  3. LukeB

    LukeB

    Joined:
    Aug 8, 2009
    Posts:
    88
    Thanks for your reply!

    The vertices are actually a duplicate of another mesh thats already around (I know going the route of generating it like this isn't exactly the best instead of instantiate - however there is a reason for it!), just the duplicated version looks much sharper around the edges. I've attached an image of the two meshes, with the one to the left the dynamically generated one - hopefully you'll be able to see what's going on!
     

    Attached Files:

  4. callahan.44

    callahan.44

    Joined:
    Jan 9, 2010
    Posts:
    694
    Looks like you have to calculate the vertex normals or your polygons aren't sharing the same vertexs.

    Calculating the normals is the average of the polygon normals that share that vertex, upto a cutoff point.
    (smoothing angle - If you want a defined sharp edge the smoothing will require additional vertexs)

    If the mesh was imported with smoothing angles, thats probably already done.

    Plenty of code samples about, like
    http://www.devmaster.net/forums/showthread.php?t=1783

    Unity has them as an array per vertex, mesh.normals
     
  5. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    http://en.wikipedia.org/wiki/Smoothing_group

    here is a good starting point to understanding it. For a spherical surface, I understand that the normal can either be decided by the angle in between vertices which is a hard normal, which can be decided by the sphere formula, in other words the actual angle the normal would be in a perfect mesh, perfectly aligned with the radius for example. I can't guarantee that because something I tested with marching cubes with duplicate normals and vertices everywhere, but in that case using the theoretical shape of an object rather than adjacentvertices made perfectly nice smooth normals.

    Additionally, the link in the previous post is out of date so here is the cached version:

    And the code it mentions is available online, GLM library something

    -Overview

    Normals are an important piece of geometry for rendering smooth surfaces. Generation of proper normals is very straightforward for continuous surfaces. To calculate a smooth normal for a vertex, average the facet normals of all the polygons that the vertex is in. However, this does not work when there are edges that are to be preserved in the model. This method will smooth out all edges. Therefore, it is desirable to selectively average normals according to a threshold for edge preservation.
    A good example is a model of a cube. It is desirable for the cube to have flat faces. However, if all the facet normals around a vertex are averaged, and the resulting average normal is used, the cube takes on a spherical look (see figure 1), which is probably not the desired effect.
    Another gotcha when creating smooth normals is duplicate vertices. These can be eliminated by 'welding' vertices that are closer than a tolerance to each other. Then the normals are recalculated. See figure 2 for an example of this.
    Different models require different edge tolerance angles. Sometimes the angle across some edges is the same as the angle between some geometry that should be smooth. This is a problem with my current algorithm. It could be dealt with by localizing the averaging according to user defined areas. See figure 3 for an example of problems of this kind (the base of the pawn is faceted, when it shouldn't be, but the tolerance must be 76° for the edge in the middle to appear).


    Implementation

    /* glmVertexNormals: Generates smooth vertex normals for a model.
    * First builds a list of all the triangles each vertex is in. Then
    * loops through each vertex in the the list averaging all the facet
    * normals of the triangles each vertex is in. Finally, sets the
    * normal index in the triangle for the vertex to the generated smooth
    * normal. If the dot product of a facet normal and the facet normal
    * associated with the first triangle in the list of triangles the
    * current vertex is in is greater than the cosine of the angle
    * parameter to the function, that facet normal is not added into the
    * average normal calculation and the corresponding vertex is given
    * the facet normal. This tends to preserve hard edges. The angle to
    * use depends on the model, but 90 degrees is usually a good start.
    *
    * model - initialized GLMmodel structure
    * angle - maximum angle (in degrees) to smooth across
    */
     
    Last edited: Mar 7, 2014
    JPhilipp likes this.