- Home /
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 < 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!
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?
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 :)
Right, but I am essentially zoo$$anonymous$$g in and out (camera) and the LineRenderer won't remain constant as the camera gets close. $$anonymous$$ake sense?
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).
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, ins$$anonymous$$d 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 $$anonymous$$oons orbit path around the Earth."
I have tried converting some of the Vector2 points to Vector3, but things got messy fast.
Thanks!
@$$anonymous$$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.
Thanks, I'll give it a go... $$anonymous$$y 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*$$anonymous$$athf.Cos(p), 0.5 + 0.25*$$anonymous$$athf.Sin(p));
Got it working thanks! Replaced "0.5 + .25" with the transform.position of my target object. Works great.
Same place, new location:
http://wiki.unity3d.com/index.php?title=VectorLine
Your answer
Follow this Question
Related Questions
GL.LINES cannot switch colors 1 Answer
optimal race line 0 Answers
How to render GL.Line in front of UI canvas? 0 Answers
GL.Line help? Cannot see line drawn... draw grid using gl.line 4 Answers
Bezier curver using LineRenderer 0 Answers