Search Unity

Rotating Vertices of a procedural mesh

Discussion in 'Scripting' started by jospor, Mar 25, 2014.

  1. jospor

    jospor

    Joined:
    Oct 23, 2013
    Posts:
    10
    I'm trying out some things with procedural meshes, I have these vertices:

    Code (csharp):
    1.  
    2. vertices.Add(new Vector3(0, 0, 1));
    3. vertices.Add(new Vector3(1, 0, 1));
    4. vertices.Add(new Vector3(1, -1, 0));
    5. vertices.Add(new Vector3(0, -1, 0));
    6.  
    This forms a plane leaning towards positive Z, or north. If I wanted to "convert" these vertices to make it lean towards another direction instead of North, like East(Positive X), how would I do that the best way? So far I have been "hardcoding" it for different directions but there must be a better way of doing this.

    Thanks.
     
  2. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    3,786
  3. jospor

    jospor

    Joined:
    Oct 23, 2013
    Posts:
    10
    I found a function on stackoverflow that I modified a bit and it works for my needs, I'll post it here incase anyone else needs it.
    Code (csharp):
    1.  
    2.     public static void RotateVertices(int direction, ref List<Vector3> vertices)
    3.     {
    4.         if (direction < 0 || direction > 3)
    5.             direction = 0;
    6.         Quaternion qAngle = Quaternion.AngleAxis( (direction * 90), Vector3.up );
    7.  
    8.         List<Vector3> ret = new List<Vector3>();
    9.  
    10.         Vector3 offset;
    11.         if (direction == 0)
    12.             offset = Vector3.zero;
    13.         else if (direction == 1)
    14.             offset = new Vector3(1, 0, 0);
    15.         else if (direction == 2)
    16.             offset = new Vector3(1, 0, -1);
    17.         else
    18.             offset = new Vector3(0, 0, -1);
    19.  
    20.         foreach(Vector3 vertex in vertices)
    21.         {
    22.             ret.Add(offset + (qAngle * vertex));
    23.         }
    24.  
    25.         vertices = ret;
    26.     }
    27.  
    Direction 0 is North, which is my default and 1 is East, 2 South and 3 West.
     
unityunity