鸟语天空
生成矩形边框Mesh
post by:追风剑情 2024-5-16 16:07

工程截图

222222.png

生成边框Mesh脚本

using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
/// <summary>
/// 矩阵边框网格
/// </summary>
public class RectBorderMesh : MonoBehaviour
{
    public float borderWidth = 0.1f;
    public Color32 borderColor = new Color32(255, 255, 255, 255);
    [SerializeField]
    private MeshFilter meshFilter;
    [SerializeField]
    private MeshRenderer meshRenderer;
    private Vector2 size;

    private void Awake()
    {
        if (meshRenderer == null)
            meshRenderer = this.AddComponent<MeshRenderer>();
        size = transform.localScale;
        CreateMeshFilter();
    }

    // 创建MeshFilter
    private MeshFilter CreateMeshFilter()
    {
        if (meshFilter == null)
            meshFilter = this.AddComponent<MeshFilter>();
        meshFilter.mesh = CreateMesh();
        return meshFilter;
    }

    // 创建Mesh
    private Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.hideFlags = HideFlags.DontSave;
        mesh.vertices = CreateBorderVertices();
        mesh.triangles = CreateTriangles();
        mesh.normals = CreateNormals();
        mesh.uv = CreateUVCoordinates();
        mesh.colors32 = CreateColors();
        return mesh;
    }

    // 创建三角点索引
    private int[] CreateTriangles()
    {
        int[] triangles = new int[24] {
            0, 5, 6, 6, 1, 0,
            1, 6, 7, 7, 2, 1,
            2, 7, 8, 8, 3, 2,
            3, 8, 9, 9, 4, 3
        };
        return triangles;
    }

    // 创建顶点颜色
    private Color32[] CreateColors()
    {
        Color32[] colors = new Color32[10];
        for (int i = 0; i < colors.Length; i++)
            colors[i] = borderColor;
        return colors;
    }
    

    // 创建UV贴图坐标
    private Vector2[] CreateUVCoordinates()
    {
        Vector2[] uvs = new Vector2[10];
        uvs[0] = new Vector2(0, 0);
        uvs[1] = new Vector2(0.25f, 0);
        uvs[2] = new Vector2(0.5f, 0);
        uvs[3] = new Vector2(0.75f, 0);
        uvs[4] = new Vector2(1, 0);//内边框起始顶点
        uvs[5] = new Vector2(0, 1);
        uvs[6] = new Vector2(0.25f, 1);
        uvs[7] = new Vector2(0.5f, 1);
        uvs[8] = new Vector2(0.75f, 1);
        uvs[9] = new Vector2(1, 1);//外边框起始顶点
        return uvs;
    }

    // 创建顶点法线
    private Vector3[] CreateNormals()
    {
        Vector3[] normals = new Vector3[10];
        for (int i = 0; i < normals.Length; i++)
            normals[i] = new Vector3(0, 0, -1);
        return normals;
    }

    // 创建边框顶点
    private Vector3[] CreateBorderVertices()
    {
        List<Vector3> vertices = new List<Vector3>();
        vertices.AddRange(CreateInVertices());
        vertices.AddRange(CreateOutVertices());
        return vertices.ToArray();
    }

    // 创建内边框矩形顶点
    private Vector3[] CreateInVertices()
    {
        Vector3 center = transform.position;
        float halfWidth = size.x / 2.0f;
        float halfHeight = size.y / 2.0f;
        Vector3 leftUp = new Vector3(center.x - halfWidth, center.y + halfHeight, center.z);
        Vector3 leftBottom = new Vector3(center.x - halfWidth, center.y - halfHeight, center.z);
        Vector3 rightUp = new Vector3(center.x + halfWidth, center.y + halfHeight, center.z);
        Vector3 rightBottom = new Vector3(center.x + halfWidth, center.y - halfHeight, center.z);

        Vector3[] inVertices = new Vector3[5];
        inVertices[0] = leftUp;
        inVertices[1] = rightUp;
        inVertices[2] = rightBottom;
        inVertices[3] = leftBottom;
        inVertices[4] = leftUp;//首尾闭合

        return inVertices;
    }

    // 创建外边框矩形顶点
    private Vector3[] CreateOutVertices()
    {
        Vector3[] outVertices = CreateInVertices();
        outVertices[0].x -= borderWidth;
        outVertices[0].y += borderWidth;
        outVertices[1].x += borderWidth;
        outVertices[1].y += borderWidth;
        outVertices[2].x += borderWidth;
        outVertices[2].y -= borderWidth;
        outVertices[3].x -= borderWidth;
        outVertices[3].y -= borderWidth;
        outVertices[4] = outVertices[0];//首尾闭合
        return outVertices;
    }
}

运行效果
11111111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容