C# 如何修复使用LineRenderer生成的不完美圆?

C# 如何修复使用LineRenderer生成的不完美圆?,c#,unity3d,C#,Unity3d,因此,我制作了这个形状,通过这个脚本应用于精灵: 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

因此,我制作了这个形状,通过这个脚本应用于精灵:

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);
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类CircleShapeGenerator:MonoBehavior
{
公共整数段=100;
公共浮动半径=1;
公共颜色c1=新颜色(1,1,1,0.1f);
公共颜色c2=新颜色(1,1,1,0.1f);
线条渲染器线条;
无效开始()
{
line=gameObject.AddComponent();
line.material=新材质(Shader.Find(“粒子/添加剂”);
线条设置宽度(0.05F,0.05F);
line.SetVertexCount(段+1);
line.useWorldSpace=false;
}
无效更新()
{
行。设置颜色(c1、c2);
浮动角度=20f;
对于(int i=0;i<(段+1);i++)
{
浮点数x=数学正弦(数学Deg2Rad*角度)*半径;
浮动y=数学坐标(数学Deg2Rad*角度)*半径;
行设置位置(i,新向量3(x,y,0));
角度+=(360f/段);
}
}
}
正如您在屏幕截图中所看到的,开始和结束没有像它们应该的那样连接。我怎样才能解决这个问题?我在整个互联网上都找到了这段代码,但都给出了这个结果。有人能解决这个问题,或者提供样条线解决方案吗?我认为使用着色器解决方案(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) );
        }
    }
}
使用UnityEngine;
使用系统集合;
公共类CircleShapeGenerator:MonoBehavior{
公共整数段=100;
公共整数边段=10;
公共浮子半径=1f;
整数计数;
浮动增量角度,增量角度边缘;
公共颜色c1=新颜色(1,1,1,1f);
公共颜色c2=新颜色(1,1,1,1f);
线条渲染器线条;
无效开始()
{
顶点计数=分段+2*边分段-2+1;
增量角=360f/段;
增量角度边缘=增量角度/边缘分段;
line=gameObject.AddComponent();
line.material=新材质(Shader.Find(“粒子/添加剂”);
线条设置宽度(0.05F,0.05F);
line.SetVertexCount(垂直计数);
line.useWorldSpace=false;
}
无效更新()
{
行。设置颜色(c1、c2);
//画第一段
浮动角度=0;
对于(int i=0;i
虽然这些线段的端点相等,但切线不相等,因此它们的边指向的方向略有不同。如果你不打算放大,你可以忽略这个问题。如果是,您可以使用更多的线段,插入额外的“端点”线段,或者使用您自己的网格/精灵,通过设计避免任何连续性问题。在交点处绘制圆(直径将是线的宽度,中心将是您的线段坐标)可以做到这一点。否则,我同意@rutter的观点,即您需要构建自己的网格。