【Unity 椭圆虚线绘制】Graphic绘制

【Unity 椭圆虚线绘制】Graphic绘制

【Unity 椭圆虚线绘制】Graphic绘制-盘古CG网
【Unity 椭圆虚线绘制】Graphic绘制
此内容为免费阅读,请登录后查看
0
免费阅读
 
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
using UnityEngine.UI;
using XUGL;
 
[ExecuteInEditMode]
public class EllipseRenderer : Graphic
{ 

    [Header("���߿���")]
    [Tooltip("ÿ�� dash ��ϸ")]
    public float dashLength = 6f;    
    [Tooltip("ÿ�� dash �����ĵ���")] 
    public float dashSpacing = 15f;   
    [Tooltip("dash ��ʵ�ʳ���")] 
    public float thickness = 2f;   

    [Header("����")]
    [Tooltip("Խ��ԽԲ��")] 
    public int segments = 360;
    protected override void OnPopulateMesh(VertexHelper vh)
    {
        vh.Clear();

        float radiusX = base.rectTransform.rect.width / 2.0f;
        float radiusY = base.rectTransform.rect.height / 2.0f;
        Vector2 center = rectTransform.rect.center;

        UIVertex v = UIVertex.simpleVert;
        v.color = color;
         
        float circumference = Mathf.PI * (3f * (radiusX + radiusY) - Mathf.Sqrt((3f * radiusX + radiusY) * (radiusX + 3f * radiusY)));
        int totalDashes = Mathf.FloorToInt(circumference / dashSpacing);
         
        int calculationSegments = segments * 4;
        float angleStep = 360f / calculationSegments;

        List<Vector2> points = new List<Vector2>();
        for (int i = 0; i <= calculationSegments; i++)
        {
            float angleDeg = i * angleStep;
            float rad = Mathf.Deg2Rad * angleDeg;
            points.Add(new Vector2(Mathf.Cos(rad) * radiusX, Mathf.Sin(rad) * radiusY) + center);
        }

        float accumulatedDistance = 0;
        Vector2 prevPoint = points[0];

        for (int i = 1; i < points.Count; i++)
        {
            Vector2 currentPoint = points[i];
            float segmentLength = Vector2.Distance(prevPoint, currentPoint);
             
            while (accumulatedDistance + segmentLength >= dashSpacing)
            {
                float remaining = dashSpacing - accumulatedDistance;
                float t = remaining / segmentLength;
                Vector2 dashCenter = Vector2.Lerp(prevPoint, currentPoint, t);
                 
                float angle = Mathf.Atan2(radiusY * Mathf.Cos(Mathf.Deg2Rad * (i * angleStep)),
                                         -radiusX * Mathf.Sin(Mathf.Deg2Rad * (i * angleStep)));
                Vector2 dir = new Vector2(-Mathf.Sin(angle), Mathf.Cos(angle));
                Vector2 normal = new Vector2(-dir.y, dir.x) * (thickness / 2f);
                 
                Vector2 dashStart = dashCenter - dir * (dashLength / 2f);
                Vector2 dashEnd = dashCenter + dir * (dashLength / 2f);
                 
                int idx = vh.currentVertCount;
                v.position = dashStart - normal;
                vh.AddVert(v);
                v.position = dashStart + normal;
                vh.AddVert(v);
                v.position = dashEnd + normal;
                vh.AddVert(v);
                v.position = dashEnd - normal;
                vh.AddVert(v);

                vh.AddTriangle(idx, idx + 1, idx + 2);
                vh.AddTriangle(idx, idx + 2, idx + 3);
                 
                segmentLength -= remaining;
                prevPoint = dashCenter + dir * (dashLength / 2f);
                accumulatedDistance = 0;
            }

            accumulatedDistance += segmentLength;
            prevPoint = currentPoint;
        }
    }
}

 

 

by-牛牛新媒体群-木子李

动力支持
© 版权声明
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容