surface.DrawTexturedRectUV

From Garry's Mod
Jump to: navigation, search
 surface.DrawTexturedRectUV( )

Contents

Description

Draws a textured rectangle with a repeated or partial texture.

u and v refer to texture coordinates.

  • (u, v) = (0, 0) is the top left
  • (u, v) = (1, 0) is the top right
  • (u, v) = (1, 1) is the bottom right
  • (u, v) = (0, 1) is the bottom left

Using a start point of (1, 0) and an end point to (0, 1), you can draw an image flipped horizontally, same goes with other directions. Going above 1 will tile the texture. Negative values are allowed as well.

Here's a helper image: http://i.imgur.com/SIKlpp4.png

NOTE

If you are using a .png image, you need supply the "noclamp" flag as second parameter for Material if you intend to use tiling.

NOTE

If you find that surface.DrawTexturedRectUV is getting your texture coordinates (u0, v0), (u1, v1) wrong and you're rendering with a material created with CreateMaterial, try adjusting them with the following code:

local du = 0.5 / 32 -- half pixel anticorrection
local dv = 0.5 / 32 -- half pixel anticorrection
local u0, v0 = (u0 - du) / (1 - 2 * du), (v0 - dv) / (1 - 2 * dv)
local u1, v1 = (u1 - du) / (1 - 2 * du), (v1 - dv) / (1 - 2 * dv)

Explanation:
surface.DrawTexturedRectUV tries to correct the texture coordinates by half a pixel (something to do with sampling) and computes the correction using IMaterial::GetMappingWidth()/GetMappingHeight(). If the material was created without a $basetexture, then GetMappingWidth()/GetMappingHeight() uses the width and height of the error material (which is 32x32).

NOTE

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

BUG

The UV offsets might require (sub-)pixel correction for accurate tiling results.

Issue Tracker: #3173

Arguments

number x

The X integer coordinate.

Arguments

number y

The Y integer coordinate.

Arguments

number width

The integer width of the rectangle.

Arguments

number height

The integer height of the rectangle.

Arguments

number startU

The U texture mapping of the rectangle origin.

Arguments

number startV

The V texture mapping of the rectangle origin.

Arguments

number endU

The U texture mapping of the rectangle end.

Arguments

number endV

The V texture mapping of the rectangle end.

Examples

Example

Demonstrates the function usage.

local mat = Material( "gui/tool.png" )
hook.Add( "HUDPaint", "DrawTexturedRectUV_example1", function()
	surface.SetDrawColor( color_white )
	surface.SetMaterial( mat )

	surface.DrawTexturedRect( 25, 25, 100, 100 )
	surface.DrawTexturedRectUV( 25, 130, 100, 100, 0, 0, 1, 1 ) -- Exactly same as above line

	-- Draws right half of the texture
	-- Note that we also change the width of the rectangle to avoid stetcing of the texture
	-- This is for demonstration purposes, you can do whatever it is you need
	surface.DrawTexturedRectUV( 130, 130, 50, 100, 0.5, 0, 1, 1 )
end )


Examples

Example

Paints repeated texture over a panel

function PANEL:Paint( w, h )
	-- Size of your texture, texW - width, texH - height
	local texW = 16
	local texH = 16

	surface.SetMaterial( Material( "icon16/box.png", "noclamp" ) )
	surface.SetDrawColor( color_white )
	surface.DrawTexturedRectUV( 0, 0, w, h, 0, 0, w / texW, h / texH )
end


Personal tools
Navigation