繁体   English   中英

如何修复使用LineRenderer制作的不完美圆?

[英]How can I fix this imperfect circle I made using LineRenderer?

因此,我制作了此形状,并通过以下脚本将其应用于精灵:

using UnityEngine;
using System.Collections;

public class CircleShapeGenerator : MonoBehaviour
{
    public int segments = 100;
    public float radius = 1;

    public Color c1 = new Color( 1, 1, 1, 0.1f );
    public Color c2 = new Color( 1, 1, 1, 0.1f );

    LineRenderer line;

    void Start ()
    {
        line = gameObject.AddComponent<LineRenderer>();

        line.material = new Material(Shader.Find("Particles/Additive"));
        line.SetWidth(0.05F, 0.05F);
        line.SetVertexCount (segments + 1);
        line.useWorldSpace = false;
    }
    void Update()
    {
        line.SetColors(c1, c2);

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            float x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius;
            float y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius;

            line.SetPosition( i, new Vector3( x,y,0) );

            angle += (360f / segments);
        }
    }
}

正如您在屏幕截图中所看到的,起点和终点未按应有的方式连接。 我怎样才能解决这个问题? 我在整个互联网上都找到了这段代码,但是所有人都给出了这个结果。 有人可以解决此问题或提供样条解决方案吗? 我认为使用Shader解决方案(使用Shader的0经验)过于刻板。

在此处输入图片说明

该解决方案可能有点复杂。 但这会起作用。
这个想法是

1)将第一个线段绘制为小分段区域。
2)从几秒钟到最后的-1段作为大段进行绘制。
3)也将最后一个段绘制为小段区域。

它使起点和终点之间具有无缝的边缘。 而且细分市场的总数并不太多。

total segment = segment + 2 * subsegment

这是示例代码。

using UnityEngine;
using System.Collections;

public class CircleShapeGenerator : MonoBehaviour {

    public int segments = 100;
    public int edgeSegments = 10;
    public float radius = 1f; 

    int vertCount;
    float increAngle, increAngleEdge;

    public Color c1 = new Color( 1, 1, 1, 1f );
    public Color c2 = new Color( 1, 1, 1, 1f );

    LineRenderer line;

    void Start ()
    {
        vertCount = segments + 2*edgeSegments - 2 + 1;
        increAngle = 360f / segments;
        increAngleEdge = increAngle/edgeSegments;

        line = gameObject.AddComponent<LineRenderer>();

        line.material = new Material(Shader.Find("Particles/Additive"));
        line.SetWidth(0.05F, 0.05F);
        line.SetVertexCount (vertCount);
        line.useWorldSpace = false;
    }

    void Update()
    {
        line.SetColors(c1, c2);

        //draw first segment
        float angle = 0;
        for (int i = 0; i < edgeSegments; i++)
        {
            float x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius;
            float y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius;

            line.SetPosition( i, new Vector3(x, y, 0) );
            angle += increAngleEdge;
        }

        //draw from seconds to last-1  segment
        angle -= increAngleEdge;
        for (int i = 0; i < segments-2; i++)
        {
            angle += increAngle;

            float x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius;
            float y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius;

            line.SetPosition( edgeSegments + i, new Vector3(x, y, 0) );
        }

        //draw last segment
        for (int i = 0; i < edgeSegments+1; i++)
        {
            angle += increAngleEdge;

            float x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius;
            float y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius;

            line.SetPosition( edgeSegments + segments - 2 + i, new Vector3(x, y, 0) );
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM