Skip to content

Instantly share code, notes, and snippets.

@DarkDesire
Last active August 28, 2016 01:43
Show Gist options
  • Save DarkDesire/65d251200d825fd87409 to your computer and use it in GitHub Desktop.
Save DarkDesire/65d251200d825fd87409 to your computer and use it in GitHub Desktop.
CreateLineMesh
public void CreateLineMesh(Vector3[] points)
{
Mesh mesh = new Mesh();
Vector2[] uvs = new Vector2[points.Length * 2];
Vector3[] vertices = new Vector3[points.Length * 2];
int[] triangles = new int[Mathf.RoundToInt(points.Length/4) * 2 * 3];
for(int v = 0; v < vertices.Length; v += 2)
{
vertices[v] = points[v/2] - Vector3.right * 0.05f;
uvs[v] = new Vector2(vertices[v].x, vertices[v].y);
}
for(int v = 1; v < vertices.Length; v += 2)
{
vertices[v] = vertices[v-1] + Vector3.right * 0.1f;
uvs[v] = new Vector2(vertices[v].x, vertices[v].y);
}
int CurrentVerticesIndex = 0;
for(int i = 0; i < triangles.Length; i+=6)
{
triangles[i] = CurrentVerticesIndex;
triangles[i+1] = CurrentVerticesIndex+1;
triangles[i+2] = CurrentVerticesIndex+3;
triangles[i+3] = CurrentVerticesIndex;
triangles[i+4] = CurrentVerticesIndex+3;
triangles[i+5] = CurrentVerticesIndex+2;
CurrentVerticesIndex += 2;
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.name = "LineMesh";
mesh.uv = uvs;
mesh.RecalculateNormals();
CurrentLine.mesh = mesh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment