unity MeshDraw

未完无休止待续。。。。


using UnityEngine;

public class MeshDraw
{
    private static Vector3[] vertices;
    private static int[] rectangle = new int[6] { 0, 3, 1, 0, 2, 3 };
    private static Vector2[] rectangleUvs = new Vector2[4] { new Vector2(1, 1), new Vector2(0, 1), new Vector2(1, 0), new Vector2(0, 0)};
    public static Mesh DrawRect(Vector3 center, float width, float height)
    {
        var widthHelf = width * 0.5f;
        var heightHelf = height * 0.5f;

        vertices = new Vector3[4]{
            new Vector3( widthHelf + center.x, center.y,  heightHelf + center.z),
            new Vector3(-widthHelf + center.x, center.y,  heightHelf + center.z),
            new Vector3( widthHelf + center.x, center.y, -heightHelf + center.z),
            new Vector3(-widthHelf + center.x, center.y, -heightHelf + center.z)
        };

        return GetMesh(vertices, rectangle, rectangleUvs);
    }

    private static int circleVerticesCount;
    private static float circleAngleDegre = 360f;
    private static float circleAngleRad;
    public static Mesh DrawCircle(Vector3 center, float Radius, int Segments)
    {
        circleVerticesCount = Segments + 1;
        vertices = new Vector3[circleVerticesCount];
        vertices[0] = center;
        circleAngleRad = Mathf.Deg2Rad * circleAngleDegre;
        float angledelta = circleAngleRad / Segments;
        for (int i = 1; i < circleVerticesCount; i++)
        {
            float cosA = Mathf.Cos(circleAngleRad);
            float sinA = Mathf.Sin(circleAngleRad);

            vertices[i] = new Vector3(center.x+Radius * cosA, center.y, center.z+Radius * sinA);
            circleAngleRad -= angledelta;
        }

        int triangle_count = Segments * 3;
        int[] triangles = new int[triangle_count];
        for (int i = 0, vi = 1; i <= triangle_count - 1; i += 3, vi++)
        {
            triangles[i] = 0;
            triangles[i + 1] = vi;
            triangles[i + 2] = vi + 1;
        }
        triangles[triangle_count - 3] = 0;
        triangles[triangle_count - 2] = circleVerticesCount - 1;
        triangles[triangle_count - 1] = 1; 

        Vector2[] uvs = new Vector2[circleVerticesCount];
        for (int i = 0; i < circleVerticesCount; i++)
        {
            uvs[i] = new Vector2(vertices[i].x / Radius * 0.5f + 0.5f, vertices[i].z / Radius * 0.5f + 0.5f);
        }

        return GetMesh(vertices,triangles,uvs);
    }

    private static Mesh GetMesh(Vector3[] vertices, int[] triangles, Vector2[]uv)
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uv;
        return mesh;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值