Search Unity

Question Is it possible to draw a circle / ellipse based on two points using a linerenderer?

Discussion in 'Scripting' started by unity_ngJ4kuo5PNtRow, Sep 21, 2021.

  1. unity_ngJ4kuo5PNtRow

    unity_ngJ4kuo5PNtRow

    Joined:
    Sep 21, 2021
    Posts:
    2
    Hi, I would like to ask if it is possible to generate and draw a circle or ellipse using a linerenderer based on two points in the scene. Specifically, these are the vertices of the mesh. For example, I would like to draw a circle / ellipse around the neck of a given humanoid.

    upload_2021-9-21_17-53-12.png

    I need this circle to enlarge and shrink based on the properties of the blendshapes. If I widen the neck with blendshape, I need this circle to enlarge as well. I can draw this circle by finding the id of all the vertices around the neck and entering these vertices into the LineRenderer. But it's a very impractical way that I don't want to use. So I would like to ask if it is possible to draw such a circle that would be connected only to two points and not to all / many.

    I will be very grateful for any advice.
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    You really can't just use two points, as the circle/ellipse can't know which way it should point to. You would need a vector direction so that you get the plane on which the circle/ellipse lies on.

    For a circle you can find diameter from those two points. Then just fit your circle between those two points and so that it aligns based on your plane normal.

    To fit an ellipse to you could just render a circle and then deform it based on the minor and major axis of the ellipse (i.e. the shortest diameter and the longest diameter.) You could use those two points as your major axis, and then supply a third center point to measure the minor axis. Either take it from your mesh or supply an arbitrary position yourself.

    To get the changing positions of the two reference vertices you need to extract position of those vertices from your mesh.

    I would skip the Line Renderer part until you have the math figured out to plot the positions for the line vertices.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,001
    You need at least 3 points to properly describe a circle in 2d or 3d space. That could be 3 points on the perimeter of the circle or two points on the perimeter and the center of the circle. Here I've written a method that can calculate the circle center and the radius based on 3 given points on a circle. Unfortunately the UA question where I actually wrote this code for can no longer be found ^^. Anyways, here's an example to see it in action:

    CalculateCircleCenter.gif
     
    exiguous and GroZZleR like this.