Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GL

Namespace: UnityEngine

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Low-level graphics library.

Use this class to manipulate active transformation matrices, issue rendering commands similar to OpenGL's immediate mode and do other low-level graphics tasks. Note that in almost all cases using Graphics.DrawMesh is more efficient than using immediate mode drawing.

GL immediate drawing functions use whatever is the "current material" set up right now. The material controls how the rendering is done (blending, textures, etc.), so unless you explicitly set it to something before using GL draw functions, the material can happen to be anything. Also, if you call any other drawing commands from inside GL drawing code, they can set material to something else, so make sure it's under control as well.

GL drawing commands execute immediately. That means if you call them in Update(), they will be executed before the camera is rendered (and the camera will most likely clear the screen, making the GL drawing not visible).

The usual place to call GL drawing is most often in OnPostRender() from a script attached to a camera

static var lineMaterial : Material;

static function CreateLineMaterial() { if( !lineMaterial ) { lineMaterial = new Material( "Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + " Blend SrcAlpha OneMinusSrcAlpha " + " ZWrite Off Cull Off Fog { Mode Off } " + " BindChannels {" + " Bind \"vertex\", vertex Bind \"color\", color }" + "} } }" ); lineMaterial.hideFlags = HideFlags.HideAndDontSave; lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave; } }

function OnPostRender() { CreateLineMaterial(); // set the current material lineMaterial.SetPass( 0 ); GL.Begin( GL.LINES ); GL.Color( Color(1,1,1,0.5) ); GL.Vertex3( 0, 0, 0 ); GL.Vertex3( 1, 0, 0 ); GL.Vertex3( 0, 1, 0 ); GL.Vertex3( 1, 1, 0 ); GL.Color( Color(0,0,0,0.5) ); GL.Vertex3( 0, 0, 0 ); GL.Vertex3( 0, 1, 0 ); GL.Vertex3( 1, 0, 0 ); GL.Vertex3( 1, 1, 0 ); GL.End(); }

Note: This class is almost always used when you need to draw a couple of lines or triangles, and don't want to deal with meshes. If you want to avoid surprises the usage pattern is this:

function OnPostRender() {
	 // Set your materials

GL.PushMatrix(); // yourMaterial.SetPass( ); // Draw your stuff GL.PopMatrix(); }

Where at the "// Draw your stuff" you should do SetPass() on some material previously declared, which will be used for drawing. If you dont call SetPass, then you'll get basically a random material (whatever was used before) which is not good. So do it.

Static Variables

LINES Mode for Begin: draw lines.
modelview The current modelview matrix.
QUADS Mode for Begin: draw quads.
TRIANGLE_STRIP Mode for Begin: draw triangle strip.
TRIANGLES Mode for Begin: draw triangles.
wireframe Should rendering be done in wireframe?

Static Functions

Begin Begin drawing 3D primitives.
Clear Clear the current render buffer.
ClearWithSkybox Clear the current render buffer with camera's skybox.
Color Sets current vertex color.
End End drawing 3D primitives.
GetGPUProjectionMatrix Compute GPU projection matrix from camera's projection matrix.
InvalidateState Invalidate the internally cached renderstates.
IssuePluginEvent Send a user-defined event to a native code plugin.
LoadIdentity Load the identity matrix to the current modelview matrix.
LoadOrtho Helper function to set up an ortho perspective transform.
LoadPixelMatrix Setup a matrix for pixel-correct rendering.
LoadProjectionMatrix Load an arbitrary matrix to the current projection matrix.
MultiTexCoord Sets current texture coordinate (v.x,v.y,v.z) to the actual texture unit.
MultiTexCoord2 Sets current texture coordinate (x,y) for the actual texture unit.
MultiTexCoord3 Sets current texture coordinate (x,y,z) to the actual texture unit.
MultMatrix Multiplies the current modelview matrix with the one specified.
PopMatrix Restores both projection and modelview matrices off the top of the matrix stack.
PushMatrix Saves both projection and modelview matrices to the matrix stack.
SetRevertBackfacing Select whether to invert the backface culling (true) or not (false).
TexCoord Sets current texture coordinate (v.x,v.y,v.z) for all texture units.
TexCoord2 Sets current texture coordinate (x,y) for all texture units.
TexCoord3 Sets current texture coordinate (x,y,z) for all texture units.
Vertex Submit a vertex.
Vertex3 Submit a vertex.
Viewport Set the rendering viewport.