Garry's Mod Wiki


  IMaterial, number Material( string materialName, string pngParameters = nil )


Either returns the material with the given name, or loads the material interpreting the first argument as the path.

When using .png or .jpg textures, try to make their sizes Power Of 2 (1, 2, 4, 8, 16, 32, 64, etc). While images are no longer scaled to Power of 2 sizes since February 2019, it is a good practice for things like icons, etc.
Server-side, the Material function can consistently return an invalid material (with '__error') depending on the file type loaded; however, .vtf and .vmt files appear unaffected.
This function is very expensive when used in rendering hooks or in operations requiring very frequent calls. It is better to store the Material in a variable (like in the examples).


1 string materialName
The material name or path. The path is relative to the materials/ folder. You do not need to add materials/ to your path.

To retrieve a Lua material created with CreateMaterial, just prepend a ! to the material name.

Since paths are relative to the materials folder, resource paths like ../data/MyImage.jpg will work since .. translates to moving up a parent directory in the file tree.

Note that the maps folder is excluded, so things like map thumbnails can't be made into materials.

2 string pngParameters = nil
A string containing space separated keywords which will be used to add material parameters.

See Material Parameters for more information.

This feature only works when importing .png or .jpeg image files.


1 IMaterial
Generated material.
2 number
How long it took for the function to run.


Creates a PNG material with noclamp and smooth parameters set and then draws on screen.

In this example the .png file is located in materials/vgui/wave.png.

local wave = Material( "vgui/wave.png", "noclamp smooth" ) hook.Add( "HUDPaint", "HUDPaint_DrawATexturedBox", function() surface.SetMaterial( wave ) surface.SetDrawColor( 255, 255, 255, 255 ) surface.DrawTexturedRect( 50, 50, 128, 128 ) end )


Acquires and uses one of the Post-Processing Materials to make the screen darker and more saturated.

local mat_color = Material( "pp/colour" ) -- used outside of the hook for performance hook.Add("RenderScreenspaceEffects", "ColorExample", function() render.UpdateScreenEffectTexture() mat_color:SetTexture( "$fbtexture", render.GetScreenEffectTexture() ) mat_color:SetFloat( "$pp_colour_addr", 0 ) mat_color:SetFloat( "$pp_colour_addg", 0 ) mat_color:SetFloat( "$pp_colour_addb", 0 ) mat_color:SetFloat( "$pp_colour_mulr", 0 ) mat_color:SetFloat( "$pp_colour_mulg", 0 ) mat_color:SetFloat( "$pp_colour_mulb", 0 ) mat_color:SetFloat( "$pp_colour_brightness", 0 ) mat_color:SetFloat( "$pp_colour_contrast", 0.5 ) mat_color:SetFloat( "$pp_colour_colour", 5 ) render.SetMaterial( mat_color ) render.DrawScreenQuad() end )