在Unity中创建2D圆形网格

时间:2018-05-30 14:04:57

标签: c# unity3d mesh

我目前有一个“CreateMesh”脚本,可以作为具有网格渲染器和网格过滤器的对象的组件放置,并且在网格的维度中使用多边形对撞机创建2D网格,给出“ MeshType“变量设置为”tri“或”box“(分别用于三角形和矩形网格。)我还想添加创建圆形网格的能力但是从一些研究中我已经意识到这并不是那么简单正如我初想的那样。但是我还没有发现任何有用的东西。

这是我对盒子和三角形网格的代码:

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);

}

所以基本上我想要一个我可以在update方法中调用的函数,它只是创建一个圆形网格物体。 E.g:

void Update () {
   if (meshType == "tri")
   {
       TriangleMesh(width, height);
   }
   if (meshType == "box")
   {
       BoxMesh(width, height);
   }
   if (meshType == "circle")
   {
       CircleMesh(radius);
   }
}

1 个答案:

答案 0 :(得分:2)

我设法找到的解决方案涉及创建n个边的正多边形,其中n值很大。我有一个名为PolyMesh的函数,它创建一个具有n个边和给定半径的正多边形网格。

生成顶点

对于具有n个边的正多边形的每个顶点,相对于多边形中心的坐标由x = r*i*sin(θ)y = r*i*cos(θ)给出,因此x = r*i*sin(2π/2)y = r*i*cos(2π/2)。我从0迭代到n-1。因此,我们可以有一个分配了顶点的列表,然后转换为数组:

    //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();

生成三角形

n个边的给定正多边形可以从同一点分割成n-2个三角形。所以我们可以按如下方式生成每个三角形:

    //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();

生成法线

由于这是一个2d对象,我们可以将每个法线都设为-Vector3.forward,如下所示:

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

生成对撞机

我们可以使用具有相同半径的圆形对撞机,但为了使此函数适用于n值较小的多边形,我们必须使用PolygonCollider2D。由于顶点在顶点数组中已经按顺序排列,我们可以简单地将它们用作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);

完整代码应如下所示:

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