render/DrawSprite

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Added an example: debug.DrawVectorSprite())
m (Completed example description for debug.DrawVectorSprite())
Line 38: Line 38:
 
}}
 
}}
 
{{Example
 
{{Example
|Description=Debug function that displays a sprite at the given position
+
|Description=Debug function that displays a sprite at the given position, without the need of a specific rendering context
  
 
debug.DrawVectorSprite( {{Type|Vector}} position, {{Type|number}} size, {{Struct|Color}} color, {{Type|boolean}} constantSize )
 
debug.DrawVectorSprite( {{Type|Vector}} position, {{Type|number}} size, {{Struct|Color}} color, {{Type|boolean}} constantSize )

Revision as of 18:12, 27 October 2017

 render.DrawSprite( )

Contents

Description

Draws a sprite in 3d space.

NOTE

This is a rendering function that requires a 3D rendering context.
This means that it will only work in hooks with a 3D rendering context.

Arguments

Vector position

Position of the sprite.

Arguments

number width

Width of the sprite.

Arguments

number height

Height of the sprite.

Arguments

table color

Color of the sprite. Uses the Color structure.

Examples

Example

Draw a sprite at the center of the map

local pos,material,white = Vector(0,0,0), Material( "sprites/splodesprite" ),Color(255,255,255,255) --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, 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.

Examples

Example

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

debug.DrawVectorSprite( Vector position, number size, Color structure color, boolean constantSize )

do
	local toDraw3d = {}
	local toDraw2d = {}
	local material = Material( "sprites/light_ignorez" )
	
	local render_SetMaterial = render.SetMaterial
	local render_DrawSprite = render.DrawSprite
	hook.Add( "PreDrawEffects", "debug.DrawVectorSprite()", function()
		for i = 1,#toDraw3d do
			local info = toDraw3d[i]
			render_SetMaterial( material )
			render_DrawSprite( info.position, info.size, info.size, info.color )
		end
		toDraw3d = {}
	end )
	
	local surface_SetMaterial = surface.SetMaterial
	local surface_SetDrawColor = surface.SetDrawColor
	local surface_DrawTexturedRect = surface.DrawTexturedRect
	hook.Add( "DrawOverlay", "debug.DrawVectorSprite()", function()
		if #toDraw2d~=0 then
			surface_SetMaterial( material )
			for i = 1,#toDraw2d do
				local info = toDraw2d[i]
				local pos2d = info.position:ToScreen()
				if pos2d.visible then
					surface_SetDrawColor( info.color )
					surface_DrawTexturedRect( pos2d.x, pos2d.y, info.size, info.size )
				end
			end
			toDraw2d = {}
		end
	end )
	
	function debug.DrawVectorSprite( position, size, color, constantSize )
		local toDraw = constantSize and toDraw2d or toDraw3d
		toDraw[#toDraw+1] = {
			position = position,
			size = size,
			color = color,
		}
	end
end

Output:

When calling the following code:
local c = Color( 255,0,0 )
hook.Add( "Think", "test_sprite", function()
	for i,veh in ipairs( ents.FindByClass( 'prop_vehicle_*' ) ) do
		debug.DrawVectorSprite( veh:GetPos(), 20, c )
	end
end )
you will have a red sprite at the position of every vehicle and every chair.
Personal tools
Navigation