Garry's Mod Wiki

util.PixelVisible

  number util.PixelVisible( Vector position, number size, pixelvis_handle_t PixVis )

Description

Returns the visibility of a square that is always pointed at the camera in the world-space.

This is typically used for in-game sprites or "billboards". (render.DrawSprite)

Arguments

1 Vector position
The center of the visibility test.
2 number size
The size of the square to check for visibility.
3 pixelvis_handle_t PixVis
The PixVis handle created with util.GetPixelVisibleHandle.
Don't use the same handle twice per tick or it will give unpredictable results.

Returns

1 number
Visibility percentage, in range of [0-1]. 0 when none of the area is visible, 0.5 when half the area is visible, 1 when all of it is visible, etc.

Example

Demonstrates approximately what this function does.

local PixVis local material = Material( "sprites/splodesprite" ) -- Position and size of the pixvis thing local vPos = Vector( 0, 0, 0 ) local vSize = 16 hook.Add( "HUDPaint", "TestPixelVisibility", function() if ( !PixVis ) then PixVis = util.GetPixelVisibleHandle() end local visible = util.PixelVisible( vPos, vSize, PixVis ) local pos2D = vPos:ToScreen() draw.SimpleTextOutlined( "Visibility: " .. math.floor( visible * 100 ), "Default", pos2D.x, pos2D.y, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 2, color_black ) end ) hook.Add( "PreDrawOpaqueRenderables", "TestPixelVisibility", function() -- Approximate visualization of the pixel visibility area render.SetMaterial( material ) render.DrawSprite( vPos, vSize * 1.5, vSize * 1.5, color_white ) end )
Output: