简体   繁体   English

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

[英]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). 我认为使用Shader解决方案(使用Shader的0经验)过于刻板。

在此处输入图片说明

This solution could be a little bit complicated. 该解决方案可能有点复杂。 But it'll works. 但这会起作用。
The idea is 这个想法是

1) Draw first segment as small segmented area. 1)将第一个线段绘制为小分段区域。
2) Draw from seconds to last -1 segments as big segment. 2)从几秒钟到最后的-1段作为大段进行绘制。
3) Draw last segment as small segmented area too. 3)也将最后一个段绘制为小段区域。

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

暂无
暂无

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

相关问题 如何使用 linerenderer 绘制一个圆圈并将其作为 object 的子 object 的圆圈应该画出来? - How can I draw a circle using linerenderer and make it the circle as object child of the object is should draw around? 我如何使用衬纸机.startColor和衬纸机.startWidth? - How can I use linerenderer.startColor and linerenderer.startWidth? 如何使用 LineRenderer 根据 object 周围的半径大小绘制圆? - How can I use LineRenderer to draw a circle depending on radius size around an object? 如何在鼠标点击ogameobject时禁用/启用LineRenderer? - How can I disable/enable LineRenderer on mouse click on ogameobject? 如何将 object 移动到 linerenderer 线位置? - How can I move an object over linerenderer lines positions? 我在LineRenderer SetWidth上收到警告,它已过时我应该如何解决? - I'm getting warning on LineRenderer SetWidth that it's obsolete how should i fix it? 如何从已绘制的任何点/线中清除 linerenderer 并使用标志返回点和线? - How can I clear a linerenderer from any points/lines that have beem drawn and return the points and lines back using a flag? 如何在 linerenderer 的两条连接线的每个角上绘制/显示文本? - How can I draw/display a text on each corner of two connected line of linerenderer? 如何将所有 linerenderer 测试 function 放在游戏对象父级下? - How can I put all the linerenderer test function under a gameobject parent? 我无法统一绘制 lineRenderer - i cant draw a lineRenderer in unity
相关标签
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM