Using surface.DrawTexturedRectUV to draw material as a background tile

Let’s say I have a background image/material, take this one as an example (it’s 15x15 if that matters):

How would I go about using it in a significantly larger panel (e.g. 800x600) as a background tile - is it possible to use some already existing function in GLua? I found surface/DrawTexturedRect, but I find it’s documentation very vague as I have only very little knowledge in textures/materials.

Of course, I could make a custom paint function which would draw the tile manually, but something tells me it’d be terribly slow - EDIT: going to try it before I continue complaining about it’s speed - EDIT: I tried and it was lagging my derma quite a bit - tried 5x5 and 15x15 sized tile textures.

function ff:Paint(w, h)
local x = 0;
local y = 0;


while (y < h) do
	x = 0;

	while (x < w) do
		surface.DrawTexturedRect(x, y, self.MatW, self.MatH);
		x = x + self.MatW;

	y = y + self.MatH;

return true;


First of all your texture must be power of two, otherwise the quality will fuck up.

I don’t see you mention even trying to use surface.DrawTexturedRectUV to draw the background.
Anyways, what you basically want to do is something like this:

local texW = 16 -- Size of your texture, W - width, H - height
local texH = 16

surface.DrawTexturedRectUV( 0, 0, w, h, 0, 0, w / texW, h / texH )

That should do the trick.

Well - despite mentioning “surface.DrawTexturedRect”, the link actually points to surface/DrawTexturedRectUV. I just forgot to add the “UV” :v:
Thanks for your help! However, it seems it doesn’t work as expected. I implemented the code you’ve sent and it ends up like this:

Oh it’s actually impossible now I believe, it seems the function is broken, it never repeats the image.

Time to join the Next Version Moaners Club.
Thanks for your help nonetheless.

EDIT: moan

It actually works, thanks to Azurblau. Documented and added an example: