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

drawing lines procedurally along a meshes triangle perimeters

Discussion in 'Scripting' started by miloshcavitch, Mar 28, 2017.

  1. miloshcavitch

    miloshcavitch

    Joined:
    Jan 31, 2017
    Posts:
    13
    Hey all, relative unity noob here currently working on a procedural map generator. The art style i'm trying to achieve is similar to old vector graphics games from the 90s, similar to the games discussed on this forum thread:
    http://www.neogaf.com/forum/showthread.php?t=898121

    I want my geometry to be non translucent so im thinking the best way to do this is by creating textures procedurally. I want lines to be drawn from vertex to vertex for every triangle in my mesh, with the exception of the "hypotenuse" so the final result of 2 adjacent triangles will be an outlined square as opposed to 2 adjacent triangles. I'm thinking if i created the texture at the same time that I create the mesh is the most efficient way to do this.

    I've looked into procedural texture generation and it seems like what is most commonly done to draw on the UV of an entire mesh, as opposed to individual triangles. Anybody know how to draw on individual triangles?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The best way to handle this will be dependent on how you've generated your mesh. The simplest thing to do would be to make a texture that consists of a single triangle, and you can set every triangle worth of UVs to the same 3 UV points.

    This only works if your mesh generation creates 3 vertices for every triangle and doesn't share them between triangles. That would be generally not the recommended way to make meshes, but it's advisable for this scenario.

    It's so advisable for this scenario that, even if your mesh isn't built that way, I'd suggest altering your mesh after generation so that it is. Here's a function that will do that (untested, and it definitely generates garbage so don't run this every frame without optimizing it, but it should work). While it's at it it sets the UV array to match a single triangle in the texture. (A right triangle "leaning" against the left side of the image, to be specific - adjust as needed if the triangles end up looking lopsided)
    Code (csharp):
    1.  
    2.     Mesh GetTriangulatedMesh(Mesh m) {
    3.         int[] triangles = m.triangles;
    4.         Vector3[] oldVerts = m.vertices;
    5.         Vector3[] newVertices = new Vector3[triangles.Length];
    6.         Vector2[] newUV = new Vector2[triangles.Length];
    7.         for (int v = 0; v < triangles.Length; v++) {
    8.             newVertices [v] = oldVerts [triangles [v]];
    9.             if (v % 3 == 0)
    10.                 newUV [v] = new Vector2 (0, 0);
    11.             else if (v % 3 == 1)
    12.                 newUV [v] = new Vector2 (0, 1);
    13.             else
    14.                 newUV [v] = new Vector2 (1, 0);
    15.             triangles [v] = v;
    16.         }
    17.         m.Clear ();
    18.         m.vertices = newVertices;
    19.         m.uv = newUV;
    20.         m.triangles = triangles;
    21.         return m;
    22.     }
     
  3. miloshcavitch

    miloshcavitch

    Joined:
    Jan 31, 2017
    Posts:
    13
    awesome thanks! I'll let you know how it goes!