Garry's Mod Wiki

Vector:ToScreen

  table Vector:ToScreen()

Description

Returns where on the screen the specified position vector would appear. A related function is gui.ScreenToVector, which converts a 2D coordinate to a 3D direction.

Should be called from a 3D rendering environment or after cam.Start3D or it may not work correctly.
Errors in a render hook can make this value incorrect until the player restarts their game.

Issue Tracker: 462
cam.Start3D or 3D context cam.Start with non-default parameters incorrectly sets the reference FOV for this function, causing incorrect return values. This can be fixed by creating and ending a default 3D context (cam.Start3D with no arguments).

Issue Tracker: 1404

Returns

Example

Draw some text on certain entities/positions in-world.

hook.Add( "HUDPaint", "ToScreenExample", function() -- Get a list of all props and draw a marker on screen for each prop for _, ent in ipairs( ents.FindByClass( "prop_*" ) ) do local point = ent:GetPos() + ent:OBBCenter() -- Gets the position of the entity, specifically the center local data2D = point:ToScreen() -- Gets the position of the entity on your screen -- The position is not visible from our screen, don't draw and continue onto the next prop if ( not data2D.visible ) then continue end -- Draw a simple text over where the prop is draw.SimpleText( "Prop here", "Default", data2D.x, data2D.y, Color( 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER ) end end )
Output:
image.png