Garry's Mod Wiki

surface.DrawPoly

  surface.DrawPoly( table vertices )

Description

Draws a textured polygon (secretly a triangle fan) with a maximum of 4096 vertices. Only works properly with convex polygons. You may try to render concave polygons, but there is no guarantee that things wont get messed up.

Unlike most surface library functions, non-integer coordinates are not rounded.

You must reset the drawing color and texture before calling the function to ensure consistent results. See examples below.
This is a rendering function that requires a 2d rendering context.

This means that it will only work in 2d Rendering Hooks.

Arguments

1 table vertices
A table containing integer vertices. See the PolygonVertex structure.

The vertices must be in clockwise order.

Example

Draws a red triangle in the top left corner of the screen.

local triangle = { { x = 100, y = 200 }, { x = 150, y = 100 }, { x = 200, y = 200 } } hook.Add("HUDPaint", "PolygonTest", function() surface.SetDrawColor( 255, 0, 0, 255 ) draw.NoTexture() surface.DrawPoly( triangle ) end )
Output:

Example

Draws a yellow polygon in the top left corner of the screen.

local triangle = { { x = 100, y = 200 }, { x = 150, y = 100 }, { x = 200, y = 200 }, { x = 150, y = 300 }, { x = 100, y = 300 }, } hook.Add("HUDPaint", "PolygonExample", function() surface.SetDrawColor( 255, 255, 0, 255 ) draw.NoTexture() surface.DrawPoly( triangle ) end )
Output:
drawpoly_example.png

Example

A helper function to draw a circle using surface.DrawPoly.

function draw.Circle( x, y, radius, seg ) local cir = {} table.insert( cir, { x = x, y = y, u = 0.5, v = 0.5 } ) for i = 0, seg do local a = math.rad( ( i / seg ) * -360 ) table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } ) end local a = math.rad( 0 ) -- This is needed for non absolute segment counts table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } ) surface.DrawPoly( cir ) end hook.Add("HUDPaint", "PolygonCircleTest", function() surface.SetDrawColor( 0, 0, 0, 200) draw.NoTexture() draw.Circle( ScrW() / 2, ScrH() / 2, 200, math.sin( CurTime() ) * 20 + 25 ) --Usage: --draw.Circle( x, y, radius, segments ) end )