简体   繁体   中英

How can I fix this imperfect circle I made using LineRenderer?

So I made this shape which I applied to a sprite via this script:

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);
        }
    }
}

As you can see in the screenshot, the start and end do not connect as they should. How can I fix this? I found this snippet of code on the entire internet but all give this result. Can somebody fix this or provide, perhaps, a spline solution? I think its overkill to go to a Shader solution (0 experience with shaders).

在此处输入图片说明

This solution could be a little bit complicated. But it'll works.
The idea is

1) Draw first segment as small segmented area.
2) Draw from seconds to last -1 segments as big segment.
3) Draw last segment as small segmented area too.

It makes seamless edge between the start segment and the end segment. And total segment's count is not too many.

total segment = segment + 2 * subsegment

This is sample code.

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) );
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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