Skip to content

Instantly share code, notes, and snippets.

@meepen
Last active February 22, 2021 17:40
Show Gist options
  • Save meepen/a2bb9c61e2cca03e2a37b2d305b1e0e0 to your computer and use it in GitHub Desktop.
Save meepen/a2bb9c61e2cca03e2a37b2d305b1e0e0 to your computer and use it in GitHub Desktop.
Loads non power of 2 PNG into a Garry's Mod texture without fucking over scaling.
local function wait_png(matstr)
local html = vgui.Create("DHTML")
html:AddFunction("console", "memeify", function()
html.done = true
end)
local mat = Material(matstr)
local rw, rh = mat:GetInt "$realwidth", mat:GetInt "$realheight"
local f = file.Open("materials/"..matstr, "rb", "GAME")
local cont = f:Read(f:Size())
f:Close()
f = file.Open(matstr:gsub("/","_"), "wb", "DATA")
f:Write(cont)
f:Close()
html:SetPos(ScrW() - 1, ScrH() - 1)
local mapw, maph = 1, 1
while (mapw < rw) do
mapw = mapw * 2
end
while (maph < rh) do
maph = maph * 2
end
html:SetSize(mapw, maph)
html:SetHTML([[
<style>
html {
overflow:hidden;
margin: -8px -8px;
}
</style>
<html>
<img onload="console.memeify();" src="asset://garrysmod/data/]]..matstr:gsub("/","_")..[[" />
</html>
]])
html:SetVisible(true)
local co = love.__love_co
local transplant = CreateMaterial("LoveEMU_"..rand.."transplant_png_"..png_count..matstr, "UnlitGeneric", {
["$translucent"] = 1,
["$nolod"] = 1,
["$ignorez"] = 1,
["$vertexalpha"] = 0,
})
png_count = png_count + 1
love.__love_incallback = true
local resume = function()
love.__love_incallback = false
love.__love_resume(co, transplant, rw, rh, mat)
end
hook.Add("Think", "LoveEMU_TransplantPNG", function()
if (IsValid(html) and html.done and html:GetHTMLMaterial()) then
local tex = html:GetHTMLMaterial():GetTexture "$basetexture"
transplant:SetTexture("$basetexture", tex)
tex:Download()
html:Remove()
-- lol fuck memory we can't destroy or reuse it
local rt = GetRenderTargetEx("LoveEMU_"..rand.."transplant_rt_"..png_count..matstr,
mapw, maph, RT_SIZE_NO_CHANGE, MATERIAL_RT_DEPTH_NONE, 1, CREATERENDERTARGETFLAGS_UNFILTERABLE_OK, IMAGE_FORMAT_RGBA8888)
render.PushRenderTarget(rt)
render.OverrideAlphaWriteEnable(true, true)
cam.Start2D()
render.Clear(0, 0, 0, 0, false, false)
surface.SetMaterial(transplant)
surface.SetDrawColor(255,255,255,255)
surface.DrawTexturedRectUV(0, 0, rw, rh, 0, 0, rw / mapw, rh / maph)
render.OverrideAlphaWriteEnable(true, false)
file.Write("loveemu_pot_"..matstr:gsub("/","_"), render.Capture {
format = "png",
alpha = true,
x = 0,
y = 0,
w = mapw,
h = maph
})
cam.End2D()
mat = Material("data/loveemu_pot_"..matstr:gsub("/","_"), "noclamp")
render.PopRenderTarget(rt)
resume()
end
end)
return love.__love_yield()
end
@masonticehurst
Copy link

masonticehurst commented Aug 8, 2017

Am I stupid?
[ERROR] RunString:40: attempt to index global 'love' (a nil value)

Code;

self.img_bg = vgui.Create( "DImage", self )
self.img_bg:SetSize( w, h * 10 )
self.img_bg:SetImage( wait_png( "proximity/levels_hexagons.png" ) )

Edit:
Also tried using the good old surface.SetMaterial/surface.DrawTexturedRect but same error :(
Attempt also made with "love" as an empty table, png_count as any integer, and rand as a math.random return

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