7

I currently have a "CreateMesh" script that can be put as a component of an object with a Mesh Renderer, and a Mesh Filter, and a 2D mesh is created with a polygon collider in the dimensions of the mesh given a "MeshType" variable is set to either "tri" or "box" (for a triangle and rectangle mesh respectively.) I want to also add the ability to create a circular mesh however from some research I've realised this isn't as simple as I first thought. However I'm yet to find anything that's helping.

This is the code I have for the box and triangle meshes:

public float width = 5f;
public float height = 5f;

public string meshType;

public PolygonCollider2D polyCollider;

void Start()
{
    polyCollider = GetComponent<PolygonCollider2D>();
}

// Update is called once per frame
void Update () {
    if (meshType == "tri")
    {
        TriangleMesh(width, height);
    }
    if (meshType == "box")
    {
        BoxMesh(width, height);
    }

}

void TriangleMesh(float width, float height)
{
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;

    //Verticies
    Vector3[] verticies = new Vector3[3]
    {
        new Vector3(0,0,0), new Vector3(width, 0, 0), new Vector3(0, height, 0)
    };

    //Triangles
    int[] tri = new int[3];

    tri[0] = 0;
    tri[1] = 2;
    tri[2] = 1;

    //normals
    Vector3[] normals = new Vector3[3];

    normals[0] = -Vector3.forward;
    normals[1] = -Vector3.forward;
    normals[2] = -Vector3.forward;

    //UVs
    Vector2[] uv = new Vector2[3];

    uv[0] = new Vector2(0, 0);
    uv[0] = new Vector2(1, 0);
    uv[0] = new Vector2(0, 1);

    //initialise
    mesh.vertices = verticies;
    mesh.triangles = tri;
    mesh.normals = normals;
    mesh.uv = uv;

    //setting up collider
    polyCollider.pathCount = 1;

    Vector2[] path = new Vector2[3]
    {
        new Vector2(0,0), new Vector2(0, height), new Vector2(width, 0)
    };

    polyCollider.SetPath(0, path);

}

void BoxMesh(float width, float height)
{
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;

    //Verticies
    Vector3[] verticies = new Vector3[4]
    {
        new Vector3(0,0,0), new Vector3(0, height, 0), new Vector3(width, height, 0), new Vector3(width, 0, 0)
    };

    //Triangles
    int[] tri = new int[6];

    tri[0] = 0;
    tri[1] = 1;
    tri[2] = 3;

    tri[3] = 1;
    tri[4] = 2;
    tri[5] = 3;

    //normals
    Vector3[] normals = new Vector3[4];

    normals[0] = -Vector3.forward;
    normals[1] = -Vector3.forward;
    normals[2] = -Vector3.forward;
    normals[3] = -Vector3.forward;

    //UVs
    Vector2[] uv = new Vector2[4];

    uv[0] = new Vector2(0, 0);
    uv[1] = new Vector2(0, 1);
    uv[2] = new Vector2(1, 1);
    uv[3] = new Vector2(1, 0);

    //initialise
    mesh.vertices = verticies;
    mesh.triangles = tri;
    mesh.normals = normals;
    mesh.uv = uv;

    //setting up collider
    polyCollider.pathCount = 1;

    Vector2[] path = new Vector2[4]
    {
        new Vector2(0,0), new Vector2(0, height), new Vector2(width, height), new Vector2(width, 0)
    };

    polyCollider.SetPath(0, path);

}

So essentially I want a function that I could call in the update method that would simply create a circular mesh. E.g:

void Update () {
   if (meshType == "tri")
   {
       TriangleMesh(width, height);
   }
   if (meshType == "box")
   {
       BoxMesh(width, height);
   }
   if (meshType == "circle")
   {
       CircleMesh(radius);
   }
}
1
  • I've managed to solve the problem since posting this question i'll produce an answer when I get chance
    – Tom Ryan
    May 30, 2018 at 16:05

2 Answers 2

11

The solution I've managed to find involves creating a regular polygon of n sides with a large value of n. I have a function called PolyMesh which creates a regular polygon mesh with n sides and a given radius.

Generating the vertices

For each vertex of a regular polygon with n sides the coordinates relative to the centre of the polygon are given by x = r*i*sin(θ) and y = r*i*cos(θ) so therefore x = r*i*sin(2π/2) and y = r*i*cos(2π/2). Where i iterates from 0 to n-1. We can therefore have a list which has vertices assigned to it and then is converted to an array afterwards:

    //verticies
    List<Vector3> verticiesList = new List<Vector3> { };
    float x;
    float y;
    for (int i = 0; i < n; i ++)
    {
        x = radius * Mathf.Sin((2 * Mathf.PI * i) / n);
        y = radius * Mathf.Cos((2 * Mathf.PI * i) / n);
        verticiesList.Add(new Vector3(x, y, 0f));
    }
    Vector3[] verticies = verticiesList.ToArray();

Generating the triangles

A given regular polygon of n sides can be split into n-2 triangles from the same point. So we can generate each triangle as follows:

    //triangles
    List<int> trianglesList = new List<int> { };
    for(int i = 0; i < (n-2); i++)
    {
        trianglesList.Add(0);
        trianglesList.Add(i+1);
        trianglesList.Add(i+2);
    }
    int[] triangles = trianglesList.ToArray();

Generating the Normals

Since this is a 2d object we can have every normal as -Vector3.forward like so:

    //normals
    List<Vector3> normalsList = new List<Vector3> { };
    for (int i = 0; i < verticies.Length; i++)
    {
        normalsList.Add(-Vector3.forward);
    }
    Vector3[] normals = normalsList.ToArray();

Generating the collider

We could just use a circle collider with the same radius but in order to make this function work for a polygon of a smaller value of n we must use a PolygonCollider2D. Since the vertices are already in order in the vertices array we can simply use them as the paths for our PolygonCollider2D.

    //polyCollider
    polyCollider.pathCount = 1;

    List<Vector2> pathList = new List<Vector2> { };
    for (int i = 0; i < n; i++)
    {
        pathList.Add(new Vector2(verticies[i].x, verticies[i].y));
    }
    Vector2[] path = pathList.ToArray();

    polyCollider.SetPath(0, path);

The complete code should look like this:

public PolygonCollider2D polyCollider;

void Start()
{
    polyCollider = GetComponent<PolygonCollider2D>();
}

void PolyMesh(float radius, int n)
{
    MeshFilter mf = GetComponent<MeshFilter>();
    Mesh mesh = new Mesh();
    mf.mesh = mesh;

    //verticies
    List<Vector3> verticiesList = new List<Vector3> { };
    float x;
    float y;
    for (int i = 0; i < n; i ++)
    {
        x = radius * Mathf.Sin((2 * Mathf.PI * i) / n);
        y = radius * Mathf.Cos((2 * Mathf.PI * i) / n);
        verticiesList.Add(new Vector3(x, y, 0f));
    }
    Vector3[] verticies = verticiesList.ToArray();

    //triangles
    List<int> trianglesList = new List<int> { };
    for(int i = 0; i < (n-2); i++)
    {
        trianglesList.Add(0);
        trianglesList.Add(i+1);
        trianglesList.Add(i+2);
    }
    int[] triangles = trianglesList.ToArray();

    //normals
    List<Vector3> normalsList = new List<Vector3> { };
    for (int i = 0; i < verticies.Length; i++)
    {
        normalsList.Add(-Vector3.forward);
    }
    Vector3[] normals = normalsList.ToArray();

    //initialise
    mesh.vertices = verticies;
    mesh.triangles = triangles;
    mesh.normals = normals;

    //polyCollider
    polyCollider.pathCount = 1;

    List<Vector2> pathList = new List<Vector2> { };
    for (int i = 0; i < n; i++)
    {
        pathList.Add(new Vector2(verticies[i].x, verticies[i].y));
    }
    Vector2[] path = pathList.ToArray();

    polyCollider.SetPath(0, path);
}

An introduction to meshes

3

I have less than 50 reputation and so I can't just comment on @Tom Ryan's answer.

With that said, beware that his solution doesn't include the UVs for the mesh. Here is that addition:

//uvs
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++)
{
    uvs[i] = new Vector2(vertices[i].x / (radius*2) + 0.5f, vertices[i].y / (radius*2) + 0.5f);
}

// Later...
mesh.uv = uvs;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.