【问题标题】:How to draw multiple circles by using "LineRenderer"如何使用“LineRenderer”绘制多个圆
【发布时间】:2020-10-19 22:46:08
【问题描述】:

LineRenderer的画圈代码。

但我想画多个半径不同的圆,我用了“for循环”,但有一个圆而不是多个

public float ThetaScale = 0.01f;
public float radius = 3f;
private int Size;
private LineRenderer LineDrawer;
private float Theta = 0f;


void Start ()
{
  LineDrawer = GetComponent<LineRenderer>();
}

void Update ()
{
 Theta = 0f;
 Size = (int)((1f / ThetaScale) + 1f);
 LineDrawer.SetVertexCount(Size);

 for (int l = 0; l < 5; l++)
 {

    for(int i = 0; i < Size; i++)
    {
      Theta += (2.0f * Mathf.PI * ThetaScale);
      float x = l * radius * Mathf.Cos(Theta);
      float y = l * radius * Mathf.Sin(Theta);
      LineDrawer.SetPosition(i, new Vector3(x, 0, y));
    }

 }
 }

【问题讨论】:

  • 请定义“没有结果”,你的代码没有编译,结果不是你想要的,如果不是,为什么?那种东西^^

标签: unity3d


【解决方案1】:

在每个循环中,您总是会覆盖同一行渲染器中的相同位置索引。所以你永远只有最后一个圈子。

请注意,重复使用SetPoisition 也是相当昂贵的。正如 API 中所说,您应该处理数组,然后使用 SetPoisitions 一次分配所有位置。

有一点不太清楚:如果你使用一个LineRenderer,你不会得到独立的圈子,但它们总是会在某个时候连接起来。否则,您将需要 5 个单独的 LineRenderer 实例。

选项 A:5 个圆圈,但由于单个 LineRenderer 的一部分而相互连接

void Start ()
{
    LineDrawer = GetComponent<LineRenderer>();
    LineDrawer.loop = false;

    Theta = 0f;
    // Use one position more to close the circle
    Size = (int)((1f / ThetaScale) + 1f) + 1;

    LineDrawer.positionCount = 5 * Size;
    var positions = new Vector3[5 * Size];

    for (int l = 0; l < 5; l++)
    {
       for(int i = 0; i < Size; i++)
       {
           Theta += (2.0f * Mathf.PI * ThetaScale);
           float x = l * radius * Mathf.Cos(Theta);
           float y = l * radius * Mathf.Sin(Theta);

           positions[5 * l + i] = new Vector3(x, 0, y);
        }
    }

    LineDrawer.SetPositions(positions);
}

选项 B:5 个分隔的圆圈中的 5 个分隔 LineRenderers

// Drag 5 individual LineRenderer here via the Inspector
public LineRenderer[] lines = new LineRenderer[5];

void Start ()
{
    foreach(var line in lines)
    {
        line.loop = true;

        Theta = 0f;
        Size = (int)((1f / ThetaScale) + 1f);
 
        line.positionCount = Size;

        var positions = new Vector3[Size];

        for(int i = 0; i < Size; i++)
        {
            Theta += (2.0f * Mathf.PI * ThetaScale);
            float x = l * radius * Mathf.Cos(Theta);
            float y = l * radius * Mathf.Sin(Theta);

            positions[5 * l + i] = new Vector3(x, 0, y);
        }

        line.SetPositions(positions);
    }
}

【讨论】:

    【解决方案2】:

    您在这里和那里错过了一些细节。在这里,这将起作用:

    using UnityEngine;
    
    [ExecuteAlways]
    [RequireComponent( typeof(LineRenderer) )]
    public class CircularBehaviour : MonoBehaviour
    {
        [SerializeField][Min(3)] int _numSegments = 16;
        [SerializeField][Min(1)] int _numCircles = 5;
        [SerializeField] float _radius = 3f;
        LineRenderer _lineRenderer;
        void Awake ()
        {
            _lineRenderer = GetComponent<LineRenderer>();
            _lineRenderer.loop = false;
            _lineRenderer.useWorldSpace = false;
        }
        void Update ()
        {
            const float TAU = 2f * Mathf.PI;
            float theta = TAU / (float)_numSegments;
            int numVertices = _numSegments + 1;
            _lineRenderer.positionCount = numVertices * _numCircles;
            
            int vert = 0;
            for( int l=1 ; l<=_numCircles ; l++ )
            {
                float r = _radius * (float)l;
                for( int i=0 ; i<numVertices ; i++ )
                {
                    float f = theta * (float)i;
                    Vector3 v = new Vector3{ x=Mathf.Cos(f) , y=Mathf.Sin(f) } * r;
                    _lineRenderer.SetPosition( vert++ , v );
                }
            }
        }
    }
    

    但是

    正如@derHugo 解释的那样,这不是您要寻找的东西,因为所有圆圈都将被连接起来。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多