Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Material Extensions / SkyBox modder
- local sky={
- ["up"]=true,
- ["dn"]=true,
- ["lf"]=true,
- ["rt"]=true,
- ["ft"]=true,
- ["bk"]=true,
- }
- -- Initialize our sky.
- for side,_ in pairs(sky) do
- local path = 'skybox/matext' .. side
- if SERVER then
- resource.AddSingleFile('materials/'..path..'.vmt')
- else
- sky[ side ] = Material( path )
- if sky[ side ]:IsError() then
- ErrorNoHalt("Skybox initialization error: "..path.." is not found\n")
- end
- end
- end
- local mp_sky = CreateConVar("mp_sky","sky_day02_02",{FCVAR_REPLICATED,FCVAR_NOTIFY,FCVAR_GAMEDLL,FCVAR_ARCHIVE})
- local skybox_basemat = "matext"
- if SERVER then
- AddCSLuaFile"matext.lua"
- local skyname=GetConVarString"sv_skyname"
- skyname = skyname == "matext" and "sky_day02_02" or skyname
- RunConsoleCommand("mp_sky",skyname)
- -- Set our sky override
- timer.Simple(0,function()
- RunConsoleCommand("sv_skyname",skybox_basemat)
- end)
- timer.Simple(1,function()
- RunConsoleCommand("sv_skyname",skybox_basemat)
- end)
- RunConsoleCommand("sv_skyname",skybox_basemat)
- cvars.AddChangeCallback("sv_skyname",function(_,_,new)
- if new ~= skybox_basemat then
- RunConsoleCommand("sv_skyname",skybox_basemat)
- end
- end)
- return
- end
- ------------------------------
- local META=getmetatable(Material(""))
- function META:GetTexture()
- return self:GetMaterialTexture("$basetexture")
- end
- function META:SetTexture(to)
- self:SetMaterialTexture("$basetexture",to)
- end
- function META:GetHDRTexture()
- return self:GetMaterialTexture("$hdrbaseTexture") or self:GetMaterialTexture("$hdrcompressedTexture")
- end
- function META:SetHDRTexture(to)
- if not to then
- return self:SetMaterialString("$hdrbaseTexture",""),self:SetMaterialString("$hdrcompressedTexture","")
- end
- return self:SetMaterialTexture("$hdrbaseTexture",to),self:SetMaterialTexture("$hdrcompressedTexture",to)
- end
- function META:GetTextureColor()
- return self:GetMaterialVector("$color")
- end
- function META:SetTextureColor(color)
- self:SetMaterialVector("$color", color)
- end
- local G = _G
- module("materials",package.seeall)
- ReplacedData = {}
- function ReplaceTexture(path, to)
- local mat = Material(path)
- if mat:IsError() then return false end
- local typ = type(to)
- local tex
- if typ == "string" then
- tex = Material(to):GetMaterialTexture("$basetexture")
- elseif typ == "ITexture" then
- tex = to
- elseif typ == "Material" then
- tex = to:GetMaterialTexture("$basetexture")
- else return false end
- ReplacedData[path] = ReplacedData[path] or {}
- ReplacedData[path].OldTexture = ReplacedData[path].OldTexture or mat:GetMaterialTexture("$basetexture")
- ReplacedData[path].NewTexture = tex
- mat:SetTexture(tex)
- return true
- end
- function SetColor(path, color)
- local mat = Material(path)
- if not mat:IsError() then
- ReplacedData[path] = ReplacedData[path] or {}
- ReplacedData[path].OldColor = ReplacedData[path].OldColor or mat:GetMaterialVector("$color")
- ReplacedData[path].NewColor = color
- mat:SetMaterialVector("$color", color)
- return true
- end
- return false
- end
- function RestoreAll()
- local i=0
- for name, tbl in pairs(ReplacedData) do
- if
- !pcall(function()
- if tbl.OldTexture then
- ReplaceTexture(name, tbl.OldTexture)
- end
- if tbl.OldColor then
- SetColor(name, tbl.OldColor)
- end
- i=i+1
- end)
- then
- ErrorNoHalt("[MatExt] Failed to restore material data: " .. tostring(name).."\n")
- end
- end
- if i>0 then
- Msg"[MatExt] "print("Restored "..i.." textures.")
- end
- end
- hook.Add('ShutDown','materials',RestoreAll)
- function SetSkyColor(vector)
- for side,skymat in pairs(sky) do
- skymat:SetMaterialVector('$color', vector)
- end
- end
- G.SetSkyColor = SetSkyColor
- G.FadeSkyBox = SetSkyColor
- function SetSky(target)
- for side,skymat in pairs(sky) do
- local newmat = Material("skybox/".. target ..side)
- local hdr = skymat:GetHDRTexture()
- if newmat:IsError() then return false end
- skymat:SetTexture( newmat:GetTexture() )
- if not pcall(function()
- if hdr then -- Todo: Can clear texture if new one does not have it?
- skymat:SetHDRTexture( hdr ) -- Clear
- end
- end)
- then print(side,skymat,hdr) end
- -- TODO: ADD MATERIAL SCALING!!!
- end
- return true
- end
- G.ChangeSky = SetSky
- G.SetSky = SetSky
- -- This should not be here but it's there anyways
- local function Assign()
- --if not MATEXT_DEBUG and not string.find(game.GetMap(),"metastruct",1,true) then return end
- local new = mp_sky:GetString()
- if not SetSky( new ) then
- ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
- ErrorNoHalt("[MatExt] Reverting to sky_day02_02\n")
- SetSky "sky_day02_02"
- end
- /*
- cvars.AddChangeCallback("mp_sky",function(_,_,new)
- Msg"[MatExt] " print("Changing sky to ".. new .. "." )
- if !SetSky( new ) then
- ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
- end
- */
- local mp_sky=GetConVar"mp_sky"
- local oldval=mp_sky:GetString()
- timer.Create('MatExt_mp_sky',1.33,0,function()
- if mp_sky:GetString()==oldval then return end
- oldval=mp_sky:GetString()
- if !SetSky( oldval ) then
- ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
- end
- end)
- end
- timer.Simple(0,function()
- Assign()
- timer.Simple(0,Assign) -- to make sure..
- end)
Advertisement
Add Comment
Please, Sign In to add comment