Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if SERVER then
- AddCSLuaFile"sh_material.lua"
- return
- end
- ------------------------------------------
- -- This code is designed to replace and restore materials
- -- And it does pretty bad job at it..
- ------------------------------------------
- Mat_Replace_Replaced={}
- -------------
- -- Takes Material
- -- Returns Texture
- -------------
- local function GetTexture(mat)
- return mat:GetMaterialTexture("$basetexture")
- end
- -------------
- -- Takes Material,Texture
- -- Returns -
- -------------
- local function SetTexture(from,to)
- from:SetMaterialTexture("$basetexture",to)
- end
- -------------
- -- Takes Material,Color
- -- Returns -
- -------------
- local function SetColor(texture,to)
- texture:SetMaterialVector("$color",Vector(tonumber(to.r) or 255,tonumber(to.g) or 255,tonumber(to.b) or 255))
- end
- -------------
- -- Takes Material
- -- Returns Vector
- -------------
- local function GetColor(mat)
- local col = mat:GetMaterialVector("$color")
- col = Color(col.r or 255,col.g or 255,col.b or 255)
- return col
- end
- -------------
- -- Takes String (A Material)
- -- returns Table/nothing
- -------------
- function Mat_Replace_Get(Target)
- return Mat_Replace_Replaced[Target]
- end
- -------------
- -- Takes String,String
- -- returns success
- -------------
- function Mat_Replace_Texture(Target,Replacer)
- local OriginalMaterial=Material(Target)
- if OriginalMaterial:IsError() then return false end
- local rtype=type(Replacer)
- local NewTexture = nil
- if rtype=="string" then
- NewTexture = GetTexture(Material(Replacer))
- elseif rtype=="ITexture" then
- NewTexture=Replacer
- elseif rtype=="Material" then
- NewTexture = GetTexture(Replacer)
- end
- if NewTexture==nil then return false end
- Mat_Replace_Replaced[Target]=Mat_Replace_Replaced[Target] or {}
- Mat_Replace_Replaced[Target].OldTexture = Mat_Replace_Replaced[Target].OldTexture or GetTexture(OriginalMaterial)
- Mat_Replace_Replaced[Target].NewTexture = NewTexture
- SetTexture(OriginalMaterial,NewTexture)
- return true
- end
- -------------
- -- Takes String,Color (not alpha)
- -- returns success
- -------------
- function Mat_Replace_Color(Target,NewColor)
- local OriginalMaterial=Material(Target)
- if OriginalMaterial:IsError() then return false end
- Mat_Replace_Replaced[Target]=Mat_Replace_Replaced[Target] or {}
- Mat_Replace_Replaced[Target].OldColor = Mat_Replace_Replaced[Target].OldColor or GetColor(OriginalMaterial)
- Mat_Replace_Replaced[Target].NewColor = NewColor
- SetColor(OriginalMaterial,NewColor)
- return true
- end
- function Mat_Replace_RestoreAll()
- --print("Restoring textures...")
- for name,tbl in pairs(Mat_Replace_Replaced) do
- if !pcall(function()
- --MsgN("\tRestoring "..name)
- if tbl.OldTexture then
- -- Msg("\t\tBasetexture: ("..
- -- tostring(tbl.NewTexture).."->"..tostring(tbl.OldTexture).."): ")
- --MsgN(tostring(
- Mat_Replace_Texture(name,tbl.OldTexture)
- --))
- end
- if tbl.OldColor then
- --Msg("\t\tColor ("..
- --table.ToString(tbl.NewColor).."->"..table.ToString(tbl.OldColor).."): ")
- --MsgN(tostring(
- Mat_Replace_Color(name,tbl.OldColor)
- --))
- end
- end) then print("Failed to restore: ".. tostring(name)) end
- end
- end
- hook.Add('ShutDown','MatRestorer',Mat_Replace_RestoreAll)
- -- Live examples
- hook.Add('InitPostEntity','Physgun_Replace',function()
- local PhysBeam_Normal='sprites/physbeam'
- local PhysBeam_Grabbed='sprites/physbeama'
- local PhysBeam_EndGlow='sprites/physg_glow1'
- local PhysBeam_GunGlow='sprites/physg_glow2'
- -- Lazor beam!
- Mat_Replace_Texture( PhysBeam_Normal, [[Sprites/crystal_beam1]] )
- Mat_Replace_Color( PhysBeam_Normal, Color(0,.6,0) )
- -- Ty but noty
- --Mat_Replace_Texture(PhysBeam_EndGlow,[[Sprites/arrow]])
- -- Not so blue anymore eh?
- Mat_Replace_Texture( PhysBeam_Grabbed, [[Sprites/bluelaser1]] )
- Mat_Replace_Color( PhysBeam_Grabbed, Color(0,.3,0) )
- -- Ahhgahh, color match :p
- Mat_Replace_Color( PhysBeam_EndGlow, Color(0,1,0) )
- -- Disable that shit
- Mat_Replace_Color( PhysBeam_GunGlow, Color(0,0,0) )
- end)
Advertisement
Add Comment
Please, Sign In to add comment