Garry's Mod Wiki

surface.DrawTexturedRectUV

surface.DrawTexturedRectUV( number x, number y, number width, number height, number startU, number startV, number endU, number endV )

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:

image.png
If you are using a .png image, you need supply the "noclamp" flag as second parameter for Material if you intend to use tiling.
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).

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

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

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

Issue Tracker: 3173

Arguments

1 number x
The X integer coordinate.
2 number y
The Y integer coordinate.
3 number width
The integer width of the rectangle.
4 number height
The integer height of the rectangle.
5 number startU
The U texture mapping of the rectangle origin.
6 number startV
The V texture mapping of the rectangle origin.
7 number endU
The U texture mapping of the rectangle end.
8 number endV
The V texture mapping of the rectangle end.

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 )

Example

Paints repeated texture over a panel

local mat = Material( "icon16/box.png", "noclamp" ) function PANEL:Paint( w, h ) -- Size of your texture, texW - width, texH - height local texW = 16 local texH = 16 surface.SetMaterial( mat ) surface.SetDrawColor( color_white ) surface.DrawTexturedRectUV( 0, 0, w, h, 0, 0, w / texW, h / texH ) end

Page Links


Special Pages


Wikis

?

Render Time: 57ms

DB GetPage 4
Generate Html 29
SaveChanges (1) 9
Render Body 0
Render Sidebar 11