Python1320

MatExt

Aug 3rd, 2011
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 KB | None | 0 0
  1.  
  2. -- Material Extensions / SkyBox modder
  3. local sky={
  4.     ["up"]=true,
  5.     ["dn"]=true,
  6.     ["lf"]=true,
  7.     ["rt"]=true,
  8.     ["ft"]=true,
  9.     ["bk"]=true,
  10. }
  11. -- Initialize our sky.
  12. for side,_ in pairs(sky) do
  13.     local path = 'skybox/matext' .. side
  14.     if SERVER then
  15.         resource.AddSingleFile('materials/'..path..'.vmt')
  16.     else
  17.         sky[ side ] = Material( path )
  18.         if sky[ side ]:IsError() then
  19.             ErrorNoHalt("Skybox initialization error: "..path.." is not found\n")
  20.         end
  21.     end
  22. end
  23. local mp_sky = CreateConVar("mp_sky","sky_day02_02",{FCVAR_REPLICATED,FCVAR_NOTIFY,FCVAR_GAMEDLL,FCVAR_ARCHIVE})
  24. local skybox_basemat = "matext"
  25. if SERVER then
  26.  
  27.     AddCSLuaFile"matext.lua"
  28.     local skyname=GetConVarString"sv_skyname"
  29.     skyname = skyname == "matext"  and "sky_day02_02" or skyname
  30.     RunConsoleCommand("mp_sky",skyname)
  31.  
  32.     -- Set our sky override
  33.     timer.Simple(0,function()
  34.         RunConsoleCommand("sv_skyname",skybox_basemat)
  35.     end)
  36.     timer.Simple(1,function()
  37.         RunConsoleCommand("sv_skyname",skybox_basemat)
  38.     end)
  39.     RunConsoleCommand("sv_skyname",skybox_basemat)
  40.    
  41.    
  42.     cvars.AddChangeCallback("sv_skyname",function(_,_,new)
  43.         if new ~= skybox_basemat then
  44.             RunConsoleCommand("sv_skyname",skybox_basemat)
  45.         end
  46.     end)
  47.    
  48.     return
  49. end
  50.  
  51. ------------------------------
  52.  
  53.  
  54.  
  55. local META=getmetatable(Material(""))
  56.  
  57. function META:GetTexture()
  58.     return self:GetMaterialTexture("$basetexture")
  59. end
  60. function META:SetTexture(to)
  61.     self:SetMaterialTexture("$basetexture",to)
  62. end
  63.  
  64. function META:GetHDRTexture()
  65.     return self:GetMaterialTexture("$hdrbaseTexture") or self:GetMaterialTexture("$hdrcompressedTexture")
  66. end
  67. function META:SetHDRTexture(to)
  68.     if not to then
  69.         return self:SetMaterialString("$hdrbaseTexture",""),self:SetMaterialString("$hdrcompressedTexture","")
  70.     end
  71.     return self:SetMaterialTexture("$hdrbaseTexture",to),self:SetMaterialTexture("$hdrcompressedTexture",to)
  72. end
  73.  
  74. function META:GetTextureColor()
  75.     return self:GetMaterialVector("$color")
  76. end
  77. function META:SetTextureColor(color)
  78.     self:SetMaterialVector("$color", color)
  79. end
  80.  
  81. local G = _G
  82. module("materials",package.seeall)
  83.  
  84. ReplacedData = {}
  85.  
  86. function ReplaceTexture(path, to)
  87.    
  88.     local mat = Material(path)
  89.    
  90.     if mat:IsError() then return false end
  91.        
  92.     local typ = type(to)
  93.     local tex
  94.    
  95.     if typ == "string" then
  96.         tex = Material(to):GetMaterialTexture("$basetexture")
  97.     elseif typ == "ITexture" then
  98.         tex = to
  99.     elseif typ == "Material" then
  100.         tex = to:GetMaterialTexture("$basetexture")
  101.     else return false end
  102.        
  103.     ReplacedData[path] = ReplacedData[path] or {}
  104.     ReplacedData[path].OldTexture = ReplacedData[path].OldTexture or mat:GetMaterialTexture("$basetexture")
  105.     ReplacedData[path].NewTexture = tex
  106.    
  107.     mat:SetTexture(tex)
  108.    
  109.     return true
  110.    
  111. end
  112.  
  113.  
  114. function SetColor(path, color)
  115.    
  116.     local mat = Material(path)
  117.    
  118.     if not mat:IsError() then
  119.         ReplacedData[path] = ReplacedData[path] or {}
  120.         ReplacedData[path].OldColor = ReplacedData[path].OldColor or mat:GetMaterialVector("$color")
  121.         ReplacedData[path].NewColor = color
  122.        
  123.         mat:SetMaterialVector("$color", color)
  124.        
  125.         return true
  126.     end
  127.    
  128.     return false
  129. end
  130.  
  131.  
  132. function RestoreAll()
  133.     local i=0
  134.     for name, tbl in pairs(ReplacedData) do
  135.         if
  136.             !pcall(function()
  137.                 if tbl.OldTexture then
  138.                     ReplaceTexture(name, tbl.OldTexture)
  139.                 end
  140.                
  141.                 if tbl.OldColor then
  142.                     SetColor(name, tbl.OldColor)
  143.                 end
  144.                 i=i+1
  145.             end)
  146.         then
  147.             ErrorNoHalt("[MatExt] Failed to restore material data: " .. tostring(name).."\n")
  148.         end
  149.     end
  150.     if i>0 then
  151.         Msg"[MatExt] "print("Restored "..i.." textures.")
  152.     end
  153. end
  154.  
  155. hook.Add('ShutDown','materials',RestoreAll)
  156.  
  157.  
  158. function SetSkyColor(vector)
  159.     for side,skymat in pairs(sky) do
  160.         skymat:SetMaterialVector('$color', vector)
  161.     end
  162. end
  163.  
  164. G.SetSkyColor = SetSkyColor
  165. G.FadeSkyBox  = SetSkyColor
  166.  
  167. function SetSky(target)
  168.    
  169.     for side,skymat in pairs(sky) do
  170.  
  171.         local newmat = Material("skybox/".. target ..side)
  172.         local hdr = skymat:GetHDRTexture()
  173.        
  174.         if newmat:IsError() then return false end
  175.        
  176.         skymat:SetTexture( newmat:GetTexture() )
  177.  
  178.         if not pcall(function()
  179.         if hdr then -- Todo: Can clear texture if new one does not have it?
  180.             skymat:SetHDRTexture( hdr ) --  Clear
  181.         end
  182.         end)
  183.         then print(side,skymat,hdr) end
  184.         -- TODO: ADD MATERIAL SCALING!!!
  185.        
  186.     end
  187.    
  188.     return true
  189. end
  190.  
  191. G.ChangeSky = SetSky
  192. G.SetSky = SetSky
  193.  
  194. -- This should not be here but it's there anyways
  195. local function Assign()
  196.  
  197.     --if not MATEXT_DEBUG and not string.find(game.GetMap(),"metastruct",1,true) then return end
  198.  
  199.     local new = mp_sky:GetString()
  200.     if not SetSky( new ) then
  201.         ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
  202.         ErrorNoHalt("[MatExt] Reverting to sky_day02_02\n")
  203.         SetSky "sky_day02_02"
  204.     end
  205.     /*
  206.     cvars.AddChangeCallback("mp_sky",function(_,_,new)
  207.         Msg"[MatExt] " print("Changing sky to ".. new .. "." )
  208.         if !SetSky( new ) then
  209.             ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
  210.         end
  211.     */
  212.     local mp_sky=GetConVar"mp_sky"
  213.     local oldval=mp_sky:GetString()
  214.     timer.Create('MatExt_mp_sky',1.33,0,function()
  215.         if mp_sky:GetString()==oldval then return end
  216.         oldval=mp_sky:GetString()
  217.        
  218.         if !SetSky( oldval ) then
  219.             ErrorNoHalt("[MatExt] Could not change sky to "..new.."\n")
  220.         end    
  221.     end)
  222. end
  223. timer.Simple(0,function()
  224.     Assign()
  225.     timer.Simple(0,Assign) -- to make sure..
  226.  end)
Advertisement
Add Comment
Please, Sign In to add comment