Skip to content

Instantly share code, notes, and snippets.

@mattkrins
Last active December 1, 2022 18:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattkrins/5455b96631cc2ebdf0e577a71d1a3d54 to your computer and use it in GitHub Desktop.
Save mattkrins/5455b96631cc2ebdf0e577a71d1a3d54 to your computer and use it in GitHub Desktop.
This is a simple Garry's Mod helper function to hot-load images from a web URL for use in your rendering operations.
local WebMaterials = {}
function surface.GetURL(url, w, h, time)
if !url or !w or !h then return Material("error") end
if WebMaterials[url] then return WebMaterials[url] end
local WebPanel = vgui.Create( "HTML" )
WebPanel:SetAlpha( 0 )
WebPanel:SetSize( tonumber(w), tonumber(h) )
WebPanel:OpenURL( url )
WebPanel.Paint = function(self)
if !WebMaterials[url] and self:GetHTMLMaterial() then
WebMaterials[url] = self:GetHTMLMaterial()
self:Remove()
end
end
timer.Simple( 1 or tonumber(time), function() if IsValid(WebPanel) then WebPanel:Remove() end end ) // In case we do not render
return Material("error")
end
@mattkrins
Copy link
Author

Example Image:

image
https://cloud.githubusercontent.com/assets/2367602/24143894/673bda7a-0e7f-11e7-9d4c-53a7174672af.png

Example Code:

local function TESTsurfaceGetURL()
	local UrlMaterial = surface.GetURL("https://cloud.githubusercontent.com/assets/2367602/24143894/673bda7a-0e7f-11e7-9d4c-53a7174672af.png", 200, 200, 5)
	local Frame = vgui.Create( "DFrame" )
	Frame:SetTitle( "Test panel" )
	Frame:SetSize( 300, 300 )
	Frame:MakePopup()
	Frame.Paint = function( s, w, h )
		draw.RoundedBox( 0, 0, 0, w, h, Color( 231, 76, 60, 150 ) )
		surface.SetDrawColor(color_white)
		surface.SetMaterial( UrlMaterial )
		surface.DrawTexturedRect(0, 25, 200, 200)
	end
end

TESTsurfaceGetURL()

Result:

image

@garryspins
Copy link

cache the error material
and dont use HTML for this, use an http fetch request and put the body in a file, then use Material("../data/image.png") or whatever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment