Search Unity

Generate mesh along a curved line

Discussion in 'Scripting' started by Marceta, Sep 9, 2013.

  1. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    I have made my bezier/curver road tool, wich make curved lines with some nodes. Now what i want to do and i really dont have idea how to do it is to generate mesh along that curved line i created. Any suggestions how could i generate that mesh wich is following road curve?

    So this is how it looks now. Curved line is LineRenderer, but i want to generate real mesh instead of that LineRenderer.

     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    The technique is usually called lofting

    In short, you sample along the 'path' every few steps, at each step you record the offsets of the 'shape' that you are lofting, local to the step position, then assemble all these resulting vert positions into a mesh using the Mesh class
    http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html

     
  3. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
    Thanks for answer. Its nice way to do it.
    So i tried and i did half of thing, im not very good with mesh class. So i have made Step points on path and made their offsets and i have added them to vertices holder of mesh, my question is now how i will hande uv and triangles?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    if you have the vert indices doing something sensible like
    Code (csharp):
    1. 0 1
    2. 2 3
    3. 4 5
    then the tris array goes 012213 234435 456657 etc..
    For uvs, you probably want all the left verts to have u=0, and right side u=1 (or viceversa);
    Then v starts at 0 for the first pair, then increases by the step distance, so for those first 6 verts, if the strip is 2 units long, and each path step is every 1 unit
    Code (csharp):
    1. 0,0  1,0
    2. 0,1  1,1
    3. 0,2  1,2
    or if you want the entire length of the strip to equal one texture cycle, you have to divide each v by the total length of the strip

    Code (csharp):
    1. 0,0/length  1,0/length
    2. 0,1/length  1,1/length
    3. 0,2/length  1,2/length
    FYI the procedural assets project on the asset store (is free) has an example of all this, probably a good thing to read through
     
  5. Marceta

    Marceta

    Joined:
    Aug 5, 2013
    Posts:
    177
  6. gabreho

    gabreho

    Joined:
    Feb 23, 2014
    Posts:
    2