Legacy Documentation: Version 4.5.0

Script language:

  • JS
  • C#
  • Boo
Script language

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

GL

Namespace: UnityEngine

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

LINESMode for Begin: draw lines.
modelviewThe current modelview matrix.
QUADSMode for Begin: draw quads.
TRIANGLE_STRIPMode for Begin: draw triangle strip.
TRIANGLESMode for Begin: draw triangles.
wireframeShould rendering be done in wireframe?

Static Functions

BeginBegin drawing 3D primitives.
ClearClear the current render buffer.
ClearWithSkyboxClear the current render buffer with camera's skybox.
ColorSets current vertex color.
EndEnd drawing 3D primitives.
GetGPUProjectionMatrixCompute GPU projection matrix from camera's projection matrix.
InvalidateStateInvalidate the internally cached renderstates.
IssuePluginEventSend a user-defined event to a native code plugin.
LoadIdentityLoad the identity matrix to the current modelview matrix.
LoadOrthoHelper function to set up an ortho perspective transform.
LoadPixelMatrixSetup a matrix for pixel-correct rendering.
LoadProjectionMatrixLoad an arbitrary matrix to the current projection matrix.
MultiTexCoordSets current texture coordinate (v.x,v.y,v.z) to the actual texture unit.
MultiTexCoord2Sets current texture coordinate (x,y) for the actual texture unit.
MultiTexCoord3Sets current texture coordinate (x,y,z) to the actual texture unit.
MultMatrixMultiplies the current modelview matrix with the one specified.
PopMatrixRestores both projection and modelview matrices off the top of the matrix stack.
PushMatrixSaves both projection and modelview matrices to the matrix stack.
SetRevertBackfacingSelect whether to invert the backface culling (true) or not (false).
TexCoordSets current texture coordinate (v.x,v.y,v.z) for all texture units.
TexCoord2Sets current texture coordinate (x,y) for all texture units.
TexCoord3Sets current texture coordinate (x,y,z) for all texture units.
VertexSubmit a vertex.
Vertex3Submit a vertex.
ViewportSet the rendering viewport.