网格Mesh

 

/****************************************************
    文件:PrimiteBase.cs
	作者:Edision
    邮箱: 424054763@qq.com
    日期:#CreateTime#
	功能:Mesh  基类
*****************************************************/

using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
[ExecuteInEditMode]
public class PrimitiveBase : MonoBehaviour
{
    public Vector3 offset;
    protected MeshFilter meshFilter;
    public Transform cacheTransfrom;

    protected Vector3[] vertices;
    public int[] triangles;
    protected Vector2[] uvs;

    private void Awake()
    {
        Init();
        InitMesh();
    }
    void Init()
    {
        cacheTransfrom = this.transform;
        meshFilter = GetComponent<MeshFilter>();
        meshFilter.sharedMesh = new Mesh();
    }

    protected virtual void InitMesh() { }
    protected virtual void UpdateShape() { }

    protected void UpdateMesh()
    {
        meshFilter.sharedMesh.vertices = vertices;
        meshFilter.sharedMesh.triangles = triangles;
        meshFilter.sharedMesh.uv = uvs;
    }

    /// <summary>
    /// 面板信息改变,自动调用
    /// </summary>
    private void OnValidate()
    {
        InitMesh();
    }
}

/****************************************************
    文件:RectMesh.cs
	作者:Edision
    邮箱: 424054763@qq.com
    日期:#CreateTime#
	功能:矩形网格
*****************************************************/

using UnityEngine;

[RequireComponent(typeof(MeshFilter),typeof(MeshRenderer))][ExecuteInEditMode]
public class RectMesh : MonoBehaviour
{
    public enum PivotAlign
    {
        Left,
        Center,
        Right,
    }
    public PivotAlign widthAlign = PivotAlign.Center;
    public PivotAlign lengthAlign = PivotAlign.Center;

    public float m_Width;
    public float m_Length;

    public Vector3 offset;
    private MeshFilter meshFilter;
    public Transform cacheTransfrom;

    private Vector3[] vertices;
    private int[] triangles;
    private Vector2[] uvs;

    private void Awake()
    {
        Init();
    }
    void Init()
    {
        cacheTransfrom = this.transform;
        meshFilter = GetComponent<MeshFilter>();
        meshFilter.sharedMesh = new Mesh();
    }
    void InitMesh()
    {
        triangles = new int[] { 0, 2, 3, 0, 3, 1 };

        uvs = new Vector2[4];
        uvs[0].x = 0;
        uvs[0].y = 0;

        uvs[1].x = 1;
        uvs[1].y = 0;

        uvs[2].x = 0;
        uvs[2].y = 1;

        uvs[3].x = 1;
        uvs[3].y = 1;

        UpdateShape();
    }

    void UpdateShape()
    {
        Vector3 localPos = offset;
        float w2 = m_Width * 0.5f;
        float l2 = m_Length * 0.5f;//小写L2  不是数字12。


        vertices = new Vector3[4];
        float x0, z0, x1, z1, x2, z2, x3, z3;
        x0 = z0 = x1 = z1 = x2 = z2 = x3 = z3 = 0;

        switch (widthAlign)
        {
            case PivotAlign.Left:
                x0 = 0;
                x1 = m_Width;
                x2 = 0;
                x3 = m_Width;
                break;
            case PivotAlign.Center:
                x0 = -w2;
                x1 = w2;
                x2 = -w2;
                x3 = w2;
                break;
            case PivotAlign.Right:
                x0 = m_Width;
                x1 = 0;
                x2 = -m_Width;
                x3 = 0;
                break;
        }
        switch (lengthAlign)
        {
            case PivotAlign.Left:
                z0 = 0;
                z1 = 0;
                z2 = m_Length;
                z3 = m_Length;
                break;
            case PivotAlign.Center:
                z0 = -l2;
                z1 = -l2;
                z2 = l2;
                z3 = l2;
                break;
            case PivotAlign.Right:
                z0 = -m_Length;
                z1 = -m_Length;
                z2 = 0;
                z3 = 0;
                break;
        }

        vertices[0].x = localPos.x + x0;
        vertices[0].y = localPos.y;
        vertices[0].z = localPos.z + z0;

        vertices[1].x = localPos.x + x1;
        vertices[1].y = localPos.y;   
        vertices[1].z = localPos.z + z1;

        vertices[2].x = localPos.x + x2;
        vertices[2].y = localPos.y;
        vertices[2].z = localPos.z + z2;

        vertices[3].x = localPos.x + x3;
        vertices[3].y = localPos.y;
        vertices[3].z = localPos.z + z3;

        UpdateMesh();
    }

    void UpdateMesh()
    {
        meshFilter.sharedMesh.vertices = vertices;
        meshFilter.sharedMesh.triangles = triangles;
        meshFilter.sharedMesh.uv = uvs;
    }

    /// <summary>
    /// 面板信息改变,自动调用
    /// </summary>
    private void OnValidate()
    {
        InitMesh();
    }
}

/****************************************************
    文件:RingMesh.cs
	作者:Edision
    邮箱: 424054763@qq.com
    日期:#CreateTime#
	功能:曲线Mesh
*****************************************************/

using UnityEngine;

public class RingMesh : PrimitiveBase
{
    //每节
    public int segment = 1;
    public float ringWidth;
    public float _ringRadius;

    public float _angleBegin;
    public float _angleEnd;

    protected override void InitMesh()
    {
        segment = segment > 0 ? segment : 1;

        triangles = new int[segment * 2 * 3];
        for (int i = 0; i < segment; i++)
        {
            int k = i * 6;
            int j = i * 2;

            triangles[k] = j;
            triangles[k + 1] = j + 2;
            triangles[k + 2] = j + 3;
            triangles[k + 3] = j;
            triangles[k + 4] = j + 3;
            triangles[k + 5] = j + 1;
        }
        int vertextCount = segment * 2 + 2;
        uvs = new Vector2[vertextCount];
        float singleUV = 1f / segment;
        float uvY = 0f;
        for (int i = 0; i < vertextCount; i+=2)
        {
            uvs[i].x = 0f;
            uvs[i + 1].x = 1f;
            uvs[i].y = uvY;
            uvs[i + 1].y = uvY;
            uvY += singleUV;
        }
        UpdateShape();
    }

    protected override void UpdateShape()
    {
        int vertexCount = segment * 2 + 2;
        vertices = new Vector3[vertexCount];

        float angle = _angleBegin * Mathf.Deg2Rad;
        float wHalf = ringWidth * 0.5f;
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);

        float minRingRadius = _ringRadius - wHalf;
        float maxRingRadius = _ringRadius + wHalf;
        Vector3 localPos = offset;

        float x = cos * minRingRadius + localPos.x;
        float y = localPos.y;
        float z = sin * minRingRadius + localPos.z;

        vertices[0].x = x;
        vertices[0].y = y;
        vertices[0].z = z;

        x = cos * maxRingRadius + localPos.x;
        z = sin * maxRingRadius + localPos.z;

        vertices[1].x = x;
        vertices[1].y = y;
        vertices[1].z = z;

        float singleAngle = (_angleEnd - _angleBegin) / segment * Mathf.Deg2Rad;
        for (int i = 0; i < segment; i++)
        {
            angle += singleAngle;
            sin = Mathf.Sin(angle);
            cos = Mathf.Cos(angle);
            x = cos * minRingRadius + localPos.x;
            y = localPos.y;
            z = sin * minRingRadius + localPos.z;
            vertices[i * 2 + 2] = new Vector3(x, y, z);

            x = cos * maxRingRadius + localPos.x;
            y = localPos.y;
            z = sin * maxRingRadius + localPos.z;
            vertices[i * 2 + 3] = new Vector3(x, y, z);
        }
        UpdateMesh();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值