Garry's Mod Wiki

render.DrawSprite

  render.DrawSprite( Vector position, number width, number height, table color = Color( 255, 255, 255 ) )

Description

Draws a sprite in 3D space.

This is a rendering function that requires a 3d rendering context.

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

Arguments

1 Vector position
Position of the sprite.
2 number width
Width of the sprite.
3 number height
Height of the sprite.
4 table color = Color( 255, 255, 255 )
Color of the sprite. Uses the Color.

Example

Draw a sprite at the center of the map

local pos,material = Vector(0,0,0), Material( "sprites/splodesprite" ) --Define this sort of stuff outside of loops to make more efficient code. hook.Add( "HUDPaint", "paintsprites", function() cam.Start3D() -- Start the 3D function so we can draw onto the screen. render.SetMaterial( material ) -- Tell render what material we want, in this case the flash from the gravgun render.DrawSprite( pos, 16, 16, color_white) -- Draw the sprite in the middle of the map, at 16x16 in it's original colour with full alpha. cam.End3D() end )
Output: An orange star appears at 0,0,0 on the map.

Example

Function that displays a sprite at the given position, without the need of a specific rendering context

draw.VectorSprite(Vector position, number size, Color color, boolean constantSize)

local toDraw3d = {} local sprites3d = 0 local toDraw2d = {} local sprites2d = 0 local material = Material("sprites/light_ignorez") function draw.VectorSprite(position, size, color, constantSize) if (not isvector(position)) then error("bad argument #1 to draw.DrawVectorSprite (Vector expected, got " .. type(position) .. ")") end if (not isnumber(size)) then error("bad argument #2 to draw.DrawVectorSprite (number expected, got " .. type(size) .. ")") end if (not IsColor(color)) then error("bad argument #3 to draw.DrawVectorSprite (Color expected, got " .. type(color) .. ")") end local tbl = {position, size, color} if (constantSize) then sprites2d = sprites2d + 1 toDraw2d[sprites2d] = tbl else sprites3d = sprites3d + 1 toDraw3d[sprites3d] = tbl end end local render_SetMaterial = render.SetMaterial local render_DrawSprite = render.DrawSprite hook.Add("PreDrawEffects", "draw.VectorSprite", function() if (sprites3d ~= 0) then render_SetMaterial(material) for i = 1, sprites3d do local info = toDraw3d[i] toDraw3d[i] = nil -- Clear the table every frame render_DrawSprite(info[1], info[2], info[2], info[3]) end sprites3d = 0 end end) local surface_SetMaterial = surface.SetMaterial local surface_SetDrawColor = surface.SetDrawColor local surface_DrawTexturedRect = surface.DrawTexturedRect hook.Add("DrawOverlay", "draw.VectorSprite", function() if (sprites2d ~= 0) then surface_SetMaterial(material) for i = 1, sprites2d do local info = toDraw2d[i] toDraw2d[i] = nil local pos2d = info[1]:ToScreen() if pos2d.visible then surface_SetDrawColor(info[3]) surface_DrawTexturedRect(pos2d.x, pos2d.y, info[2], info[2]) end end sprites2d = 0 end end)