Unity中的Mesh网格编程——绘制基本2D图形

Posted Hello Bug.

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中的Mesh网格编程——绘制基本2D图形相关的知识,希望对你有一定的参考价值。

一:前言

利用Mesh可以绘制任意样式的图形,创建一个新的Mesh并设置vertices和triangles,因为任何图形都是由若干个三角形组成的
注意:三角形的顶点顺序必须是顺时针,顺时针表示正面,逆时针表示背面,而Unity在渲染时默认只渲染正面,背面是看不见的


二:绘制基本图形

——三角形

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        DrawTriangle();
    }

    /// <summary>
    /// 绘制三角形
    /// </summary>
    void DrawTriangle()
    {
        GameObject go = new GameObject("Triangle");
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[3]
        {
            new Vector3(0, 0, 0),
            new Vector3(0, 0, 1),
            new Vector3(1, 0, 0)
        };
        mesh.triangles = new int[3]
        {
            0, 1, 2
        };
        mf.mesh = mesh;
    }
}

 

 

——正方形

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        DrawSquare();
    }

    /// <summary>
    /// 绘制正方形
    /// </summary>
    void DrawSquare()
    {
        GameObject go = new GameObject("Square");
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[]
        {
            new Vector3(0, 0, 0),
            new Vector3(0, 0, 1),
            new Vector3(1, 0, 1),
            new Vector3(1, 0, 0)
        };
        mesh.triangles = new int[]
        {
            0, 1, 2,
            0, 2, 3
        };
        mf.mesh = mesh;
    }
}

 

 

——圆形


由三角函数求得A点坐标:(cosθ*r,sinθ*r)
绘制圆需要的参数:原点位置,半径,分成多少段(分成的段越多圆形越平滑)

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        DrawCircle();
    }

    /// <summary>
    /// 绘制圆形
    /// </summary>
    void DrawCircle()
    {
        int radius = 1;
        int segments = 100;
        float deltaAngle = Mathf.Deg2Rad * 360f / segments;
        float curAngle = 0;

        Vector3[] vertices = new Vector3[segments + 1];
        vertices[0] = Vector3.zero;
        for (int i = 1; i < vertices.Length; i++)
        {
            float x = Mathf.Cos(curAngle) * radius;
            float z = Mathf.Sin(curAngle) * radius;
            vertices[i] = new Vector3(x, 0, z);
            curAngle += deltaAngle;
        }
        int trianglesAmount = segments;
        int[] triangles = new int[segments * 3];
        for (int i = 0; i < trianglesAmount - 1; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 2;
            triangles[i * 3 + 2] = i + 1;
        }
        triangles[triangles.Length - 3] = 0;
        triangles[triangles.Length - 2] = 1;
        triangles[triangles.Length - 1] = segments;

        GameObject go = new GameObject("Circle");
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mf.mesh = mesh;
    }
}

 

 

——圆环

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        DrawAnnulus();
    }

    /// <summary>
    /// 绘制圆环
    /// </summary>
    void DrawAnnulus()
    {
        int innerRadius = 1;
        int outerRadius = 2;
        int segments = 100;
        float deltaAngle = Mathf.Deg2Rad * 360f / segments;
        float curAngle = 0;

        Vector3[] vertices = new Vector3[segments * 2];
        for (int i = 0; i < segments; i++)
        {
            float innerX = Mathf.Cos(curAngle) * innerRadius;
            float innerZ = Mathf.Sin(curAngle) * innerRadius;
            float outerX = Mathf.Cos(curAngle) * outerRadius;
            float outerZ = Mathf.Sin(curAngle) * outerRadius;
            vertices[i * 2] = new Vector3(innerX, 0, innerZ);
            vertices[i * 2 + 1] = new Vector3(outerX, 0, outerZ);
            curAngle += deltaAngle;
        }
        int trianglesAmount = segments * 2;
        int[] triangles = new int[trianglesAmount * 3];
        for (int i = 0; i < trianglesAmount - 2; i += 2)
        {
            triangles[i / 2 * 6] = i;
            triangles[i / 2 * 6 + 1] = i + 3;
            triangles[i / 2 * 6 + 2] = i + 1;
            triangles[i / 2 * 6 + 3] = i;
            triangles[i / 2 * 6 + 4] = i + 2;
            triangles[i / 2 * 6 + 5] = i + 3;
        }
        triangles[triangles.Length - 6] = vertices.Length - 2;
        triangles[triangles.Length - 5] = 1;
        triangles[triangles.Length - 4] = vertices.Length - 1;
        triangles[triangles.Length - 3] = vertices.Length - 2;
        triangles[triangles.Length - 2] = 0;
        triangles[triangles.Length - 1] = 1;


        GameObject go = new GameObject("Annulus");
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mf.mesh = mesh;
    }
}

 

 

——扇形

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        DrawSector();
    }

    /// <summary>
    /// 绘制扇形
    /// </summary>
    void DrawSector()
    {
        int angle = 120;
        int radius = 1;
        int segments = 100;
        float deltaAngle = Mathf.Deg2Rad * angle / segments;
        float curAngle = Mathf.Deg2Rad * (90 - angle / 2);

        Vector3[] vertices = new Vector3[segments + 2];
        vertices[0] = Vector3.zero;
        for (int i = 1; i < vertices.Length; i++)
        {
            float x = Mathf.Cos(curAngle) * radius;
            float z = Mathf.Sin(curAngle) * radius;
            vertices[i] = new Vector3(x, 0, z);
            curAngle += deltaAngle;
        }
        int trianglesAmount = segments;
        int[] triangles = new int[trianglesAmount * 3];
        for (int i = 0; i < trianglesAmount; i++)
        {
            triangles[i * 3] = 0;
            triangles[i * 3 + 1] = i + 2;
            triangles[i * 3 + 2] = i + 1;
        }

        GameObject go = new GameObject("Sector");
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mf.mesh = mesh;
    }
}

 

以上是关于Unity中的Mesh网格编程——绘制基本2D图形的主要内容,如果未能解决你的问题,请参考以下文章

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程