[英]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.