OpenGL performance issue

Hi,

I’m having some framerate issues while using GL.LINES. I use the following code (attached to a camera) to draw a kind of wireframe cylinder:

void OnRenderObject(){

		GL.Begin(GL.LINES);
		GL.Color (Color.red);
		for (int i = 0; i < 361; i++){
			
			
			float cx = 18 * Mathf.Cos(i*Mathf.Deg2Rad);
			float cy = 18 * Mathf.Sin(i*Mathf.Deg2Rad);
			Vector3 v = new Vector3(cx,cy,0);
			GL.Vertex3(v.x,v.y,0);

			cx = 18 * Mathf.Cos(i*Mathf.Deg2Rad+Mathf.Deg2Rad);
			cy = 18 * Mathf.Sin(i*Mathf.Deg2Rad+Mathf.Deg2Rad);
			GL.Vertex3(cx,cy,0);

			GL.Vertex3(v.x,v.y,40);
			GL.Vertex3(cx,cy,40);

		}

		for (int i = 0; i<361; i+=30) {
			float cx = 18 * Mathf.Cos(i*Mathf.Deg2Rad);
			float cy = 18 * Mathf.Sin(i*Mathf.Deg2Rad);
			GL.Vertex3(cx,cy,0);

			cx = 18 * Mathf.Cos(i*Mathf.Deg2Rad+Mathf.Deg2Rad);
			cy = 18 * Mathf.Sin(i*Mathf.Deg2Rad+Mathf.Deg2Rad);
			GL.Vertex3(cx,cy,40);
		}

		
		GL.End();
...

Drawing just a single circle (the first two GL.Vertex3 calls) is fine, but adding the second circle drops the frame rate by around 5 fps, and drawing the lines in between drops it even more. Is there a solution for obtaining the same effect without the performance issues?

Also, some potentially relevant info: I’m running this code with an Oculus so I attached the above script to both of the two cameras that make up the stereoscopic effect. Is this needed, and could it be part of the frame rate problem? Thanks.

This doesn’t seem to be a OpenGL issue as much as it is an issue of calculating the same Vector3 coordinates every frame. Obviously doing the same hundreds of trigonometric calculations every second is going to cost you performance. Instead you should just generate the Vector3s and store them, similar to how you would in Mesh.

Even better, just use a mesh. While meshes are typically made up of triangles, Unity allows you to create a mesh that’s made up of lines or points using Mesh.SetIndicies and MeshTopology.