Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there a better way to calculate a circle with the LineRenderer?

Discussion in 'Scripting' started by Vider5CC, Jun 9, 2014.

  1. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    I'm using this script to calculate a circle with the LineRenderer. The circle has the radius (5.0f, 0, 5.0f) and the camera is above and rotated to the circle. The problem is that you can see the segments if you look closely. I increased the segments variable to 75 (can't see a difference above 75) but I guess that these are different segments.

    Now my question: Is this a problem that only anti-aliasing can solve or can I improve the script?
    I'm especially not sure about this part:
    Code (CSharp):
    1.          
    2. x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius.x;
    3. y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius.y;
    4. z = Mathf.Cos (Mathf.Deg2Rad * angle) * radius.z;
    5.  

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Circle : MonoBehaviour{
    6.  
    7.     private Vector3 oldPosition;
    8.  
    9.     [HideInInspector]
    10.     public Vector3 offset = new Vector3(0, 0, 0);
    11.     private Vector3 oldOffset;
    12.  
    13.     public int segments = 75;
    14.     private int oldSegments;
    15.  
    16.     public Vector3 radius = new Vector3(5.0f, 0, 5.0f);
    17.     private Vector3 oldRadius;
    18.  
    19.     public Vector3 rotation = new Vector3(0, 0, 0);
    20.     private Vector3 oldRotation;
    21.  
    22.     private Vector3[] vertices;
    23.     private Vector3[] rotatedVertices;
    24.  
    25.     private LineRenderer line;
    26.    
    27.     void Start (){
    28.  
    29.         if(line == null)
    30.             line = gameObject.GetComponent<LineRenderer>();
    31.        
    32.         CreatePoints();
    33.     }
    34.    
    35.     void Update(){
    36.        
    37.         CheckCircle();
    38.     }
    39.    
    40.     void CheckCircle(){
    41.        
    42.            if((oldRadius         != radius)
    43.            || (oldRotation       != rotation)
    44.            || (oldPosition       != transform.position)
    45.            || (oldOffset         != offset)
    46.            || (oldSegments       != segments))
    47.            
    48.             CreatePoints();
    49.     }
    50.    
    51.     public void CreatePoints (){
    52.        
    53.         oldRadius        = radius;
    54.         oldRotation      = rotation;
    55.         oldPosition      = transform.position;
    56.         oldOffset        = offset;
    57.         oldSegments      = segments;
    58.        
    59.         float x = 0f;
    60.         float y = 0f;
    61.         float z = 0f;
    62.        
    63.         float angle = 20f;
    64.         vertices = new Vector3[segments + 1];
    65.  
    66.         for (int i = 0; i < (segments + 1); i++){
    67.            
    68.             x = Mathf.Sin (Mathf.Deg2Rad * angle) * radius.x;
    69.             y = Mathf.Cos (Mathf.Deg2Rad * angle) * radius.y;
    70.             z = Mathf.Cos (Mathf.Deg2Rad * angle) * radius.z;
    71.            
    72.             vertices[i] = new Vector3(x,y,z) + offset;
    73.            
    74.             angle += (360f / segments);
    75.         }
    76.        
    77.         line.SetVertexCount(segments + 1);
    78.  
    79.         // Use Matrix4x4 to rotate the circle
    80.         rotatedVertices = Matrix4x4Extensions.arrayRotate(vertices, rotation);
    81.  
    82.         for (int i = 0; i < (segments + 1); i++){
    83.            
    84.             line.SetPosition (i, rotatedVertices[i]);
    85.         }
    86.     }
    87. }
    88.  
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Not sure what you are using the circle for, but wouldn't it be easier to just use a texture of a circle?
     
  3. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Sorry, I forgot to mention that. I want to change the radius at runtime. So it could be an ellipse.
    I don't know how good that works with a texture and I need the vertices anyway.
     
  4. expressionpixel

    expressionpixel

    Joined:
    Oct 1, 2012
    Posts:
    20
    increase the amount of segments until it looks clean, also try adjusting the radius size and tweak the actual scale of the gameobject.
     
  5. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Like I said I believe that the segments variable over 75 doesn't make any difference. What do you mean with adjusting and tweaking? Something like radius/2 and setting the scale of the gameObject to 2?
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    It is much easier to use a gameObject with a texture and simply scaling that. Saves you a lot of resources as well. Make the x or y bigger than the other one for an elipse.
     
  7. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
  8. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Is the LineRenderer so resource intensive? What could I do if the texture is in a 3D space and I need to see the texture from every possible angle?
     
    Last edited: Jun 10, 2014
  9. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Z0leee87 likes this.
  10. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Then create a torus model or a mesh model and apply the texture on it.
    And you wont be able to view LineRenderer from all angles. LineRenderer is a double sided Quad and it is not possible to see it from all angles.
     
  11. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Hm, I though about that but wouldn't that be slower then the LineRenderer? I don't know how the LineRenderer works but I can really see it from each angle. It's like a torus. Maybe because the polygons always face the camera?
     
  12. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    Select the LineRenderer in scene view while in play mode then you can see all the quads.
    And LineRenderer may be a bit slow, as you are recreating geometry each frame.
     
  13. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    Ah thanks, the quads always face the camera.
    Do you mean my CreatePoints function (just called if something changes) or something in the core of the LineRenderer?
     
  14. msl_manni

    msl_manni

    Joined:
    Jul 5, 2011
    Posts:
    272
    If you are calling CreatePoints each frame then it will get slow. And the more the quads the slower it gets.
    If it is for only one object then there will not be a big difference, bit if you creating for many then ..........................
    And lastly if you are going for a certain look then ignore the rest. Some things can only be done in a certain way only.

    BTW did you check the asset properly.

    - 1 Projector-based
    - 3 Quad-based
    - 6 Particle-based

    In web demo you can even increase the size to see what type suits your need.
    Then you can create that one yourself, if you want to.
     
  15. Vider5CC

    Vider5CC

    Joined:
    Apr 19, 2014
    Posts:
    14
    I will use it with max. 20-25 objects but CreatePoints will be called like once per minute. (not all objects at the same time)
    Are the settings for the texture? Because right now I just use the default sprite for testing.