• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Adam D. · Apr 26, 2010 at 09:38 PM · lineglbezier

Create a circle or ellipse with the GL class

I am trying to create a simple circle. I don't want to use the LineRenderer because I need the circle to be a 1px line at all times. So I am using the GL class.

I am uncertain how to create a Quadratic Bezier Curve using the GL class.

Here is the modified code from the Wiki:

var numberOfPoints = 60; var lineColor = Color.white; var drawLines = true;

private var lineMaterial : Material; private var linePoints : Vector2[]; private var lineWidth = 1;

function Awake () { //create line material color lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" + "SubShader { Pass {" + " BindChannels { Bind \"Color\",color }" + " Blend SrcAlpha OneMinusSrcAlpha" + " ZWrite Off Cull Off Fog { Mode Off }" + "} } }"); lineMaterial.hideFlags = HideFlags.HideAndDontSave; lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; }

function Start () { linePoints = new Vector2[numberOfPoints];

 // Plot points on a circle
 var radians : float = 360.0/(numberOfPoints-1)*Mathf.Deg2Rad;
 var p = 0.0;
 for (i = 0; i < numberOfPoints; i++) {
     linePoints[i] = Vector2(0.5 + .25*Mathf.Cos(p), 0.5 + 0.25*Mathf.Sin(p));
     p += radians;
     //print(p);
 }

}

function OnPostRender () { if (!drawLines || !linePoints || linePoints.Length < 2) {return;}

 var cam = camera; // Cache GetComponent call in local variable for speed
 var nearClip = cam.nearClipPlane+0.01; // Add a bit, else there's flickering when the camera moves
 var pWidth = (1.0/Screen.width)*((Screen.width+0.0)/Screen.height)*0.22;
 var end = linePoints.Length-1;

 lineMaterial.SetPass(0);
 GL.Begin(GL.LINES);
 GL.Color(lineColor);

 for (i = 0; i &lt; end; i++) {
     var v1 = cam.ViewportToWorldPoint(Vector3(linePoints[i].x, linePoints[i].y, nearClip));
     var v2 = cam.ViewportToWorldPoint(Vector3(linePoints[i+1].x, linePoints[i+1].y, nearClip));
     GL.Vertex(v1);
     GL.Vertex(v2);
 }
 GL.End();

}

function OnApplicationQuit () { DestroyImmediate(lineMaterial); }

@script RequireComponent(Camera)

Any help would be awesome. Thanks in advance!

Comment
Barrett-Fox

People who like this

1 Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cyclops · Apr 26, 2010 at 09:48 PM 0
Share

I've never used the LineRenderer before, but I see it has an option to set lineWidth, which I presume you could set to 1. I'm not clear why that won't do what you want?

avatar image Cyclops · Apr 26, 2010 at 09:55 PM 0
Share

Okay, I just tried creating a LineRenderer, and it looked like setting it to a width of 0.005 was very thin, whether it was a single pixel I can't tell :)

avatar image Adam D. · Apr 26, 2010 at 10:07 PM 0
Share

Right, but I am essentially zooming in and out (camera) and the LineRenderer won't remain constant as the camera gets close. Make sense?

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Eric5h5 · Apr 27, 2010 at 12:05 AM

Have a look at this vector line script on the wiki, which has an example that plots a circle (and then animates it in a funky way, but you can leave that part out of course).

Comment
The-BOOM
Adam D.
Cyclops
Barrett-Fox

People who like this

4 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Adam D. · Apr 27, 2010 at 12:24 AM 0
Share

Thanks Eric! I did find that script, and boiled it down to draw a circle (I have updated my original question, with the modified wiki script). I am struggling getting this to work in world space, instead of camera space. I basically want to supply a center transform point, and I want the circle to be drawn around the center point, using an outer transform point to get the radius. Think of "the Moons orbit path around the Earth."

I have tried converting some of the Vector2 points to Vector3, but things got messy fast.

Thanks!

avatar image Eric5h5 · Apr 27, 2010 at 12:35 AM 0
Share

@Adam D. You can use WorldToViewportSpace in order to supply the center transform point, as well as the outer transform point. Actually in this case using screen space would be more convenient for plotting circles since you don't have to compensate for the aspect ratio to get a perfect circle.

avatar image Adam D. · Apr 27, 2010 at 01:07 AM 0
Share

Thanks, I'll give it a go... My math is rusty, so we'll see ;) And I'm still a bit confused on how to create my array of points in world space like you are doing here: linePoints[i] = Vector2(0.5 + .25*Mathf.Cos(p), 0.5 + 0.25*Mathf.Sin(p));

avatar image Adam D. · Apr 27, 2010 at 02:15 AM 0
Share

Got it working thanks! Replaced "0.5 + .25" with the transform.position of my target object. Works great.

avatar image AhmedMTawfik · Aug 04, 2020 at 06:35 PM 0
Share

Link is dead; any replacements?

avatar image Eno-Khaon AhmedMTawfik · Aug 04, 2020 at 07:25 PM 1
Share

Same place, new location:

http://wiki.unity3d.com/index.php?title=VectorLine

avatar image AhmedMTawfik Eno-Khaon · Aug 05, 2020 at 07:17 PM 0
Share

Thanks for the link!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to render GL.Line in front of UI canvas? 0 Answers

Bezier curver using LineRenderer 0 Answers

GL.Line help? Cannot see line drawn... draw grid using gl.line 4 Answers

How to create a Mini Map at runtime 2 Answers

optimal race line 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges