Python1320

Untitled

Dec 11th, 2010
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. if SERVER then
  2.     AddCSLuaFile"sh_material.lua"
  3.     return
  4. end
  5.  
  6. ------------------------------------------
  7. -- This code is designed to replace and restore materials
  8. -- And it does pretty bad job at it..
  9. ------------------------------------------
  10.  
  11. Mat_Replace_Replaced={}
  12.  
  13.  
  14. -------------
  15. -- Takes    Material
  16. -- Returns  Texture
  17. -------------
  18. local function GetTexture(mat)
  19.     return mat:GetMaterialTexture("$basetexture")
  20. end
  21. -------------
  22. -- Takes    Material,Texture
  23. -- Returns  -
  24. -------------
  25. local function SetTexture(from,to)
  26.    
  27.     from:SetMaterialTexture("$basetexture",to)
  28. end
  29.  
  30. -------------
  31. -- Takes    Material,Color
  32. -- Returns  -
  33. -------------
  34. local function SetColor(texture,to)
  35.     texture:SetMaterialVector("$color",Vector(tonumber(to.r) or 255,tonumber(to.g) or 255,tonumber(to.b) or 255))
  36. end
  37.  
  38. -------------
  39. -- Takes    Material
  40. -- Returns  Vector
  41. -------------
  42. local function GetColor(mat)
  43.     local col = mat:GetMaterialVector("$color")
  44.     col = Color(col.r or 255,col.g or 255,col.b or 255)
  45.     return col
  46. end
  47.  
  48. -------------
  49. -- Takes    String  (A Material)
  50. -- returns  Table/nothing
  51. -------------
  52. function Mat_Replace_Get(Target)
  53.     return Mat_Replace_Replaced[Target]
  54. end
  55.  
  56.  
  57. -------------
  58. -- Takes    String,String
  59. -- returns success
  60. -------------
  61. function Mat_Replace_Texture(Target,Replacer)
  62.  
  63.     local OriginalMaterial=Material(Target)
  64.     if OriginalMaterial:IsError() then return false end
  65.    
  66.     local rtype=type(Replacer)
  67.  
  68.     local NewTexture = nil
  69.  
  70.     if rtype=="string" then
  71.         NewTexture = GetTexture(Material(Replacer))
  72.     elseif rtype=="ITexture" then
  73.         NewTexture=Replacer
  74.     elseif rtype=="Material" then
  75.         NewTexture = GetTexture(Replacer)
  76.     end
  77.    
  78.     if NewTexture==nil then return false end
  79.    
  80.    
  81.     Mat_Replace_Replaced[Target]=Mat_Replace_Replaced[Target] or {}
  82.    
  83.    
  84.     Mat_Replace_Replaced[Target].OldTexture = Mat_Replace_Replaced[Target].OldTexture or GetTexture(OriginalMaterial)
  85.     Mat_Replace_Replaced[Target].NewTexture = NewTexture
  86.    
  87.     SetTexture(OriginalMaterial,NewTexture)
  88.    
  89.     return true
  90.    
  91. end
  92.  
  93. -------------
  94. -- Takes    String,Color (not alpha)
  95. -- returns success
  96. -------------
  97. function Mat_Replace_Color(Target,NewColor)
  98.  
  99.     local OriginalMaterial=Material(Target)
  100.  
  101.     if OriginalMaterial:IsError() then return false end
  102.    
  103.     Mat_Replace_Replaced[Target]=Mat_Replace_Replaced[Target] or {}
  104.    
  105.    
  106.     Mat_Replace_Replaced[Target].OldColor = Mat_Replace_Replaced[Target].OldColor or GetColor(OriginalMaterial)
  107.     Mat_Replace_Replaced[Target].NewColor = NewColor
  108.    
  109.     SetColor(OriginalMaterial,NewColor)
  110.    
  111.     return true
  112.    
  113. end
  114.  
  115.  
  116.  
  117.  
  118.  
  119. function Mat_Replace_RestoreAll()
  120.    
  121.     --print("Restoring textures...")
  122.     for name,tbl in pairs(Mat_Replace_Replaced) do
  123.         if !pcall(function()
  124.         --MsgN("\tRestoring "..name)
  125.        
  126.         if tbl.OldTexture then
  127.         --  Msg("\t\tBasetexture: ("..
  128.             --  tostring(tbl.NewTexture).."->"..tostring(tbl.OldTexture).."): ")
  129.             --MsgN(tostring(
  130.             Mat_Replace_Texture(name,tbl.OldTexture)
  131.             --))
  132.         end
  133.         if tbl.OldColor then
  134.             --Msg("\t\tColor ("..
  135.                 --table.ToString(tbl.NewColor).."->"..table.ToString(tbl.OldColor).."): ")
  136.             --MsgN(tostring(
  137.             Mat_Replace_Color(name,tbl.OldColor)
  138.             --))
  139.         end
  140.        
  141.         end) then print("Failed to restore: ".. tostring(name)) end
  142.     end
  143.    
  144. end
  145. hook.Add('ShutDown','MatRestorer',Mat_Replace_RestoreAll)
  146.  
  147.  
  148. -- Live examples
  149. hook.Add('InitPostEntity','Physgun_Replace',function()
  150.     local PhysBeam_Normal='sprites/physbeam'
  151.     local PhysBeam_Grabbed='sprites/physbeama'
  152.     local PhysBeam_EndGlow='sprites/physg_glow1'
  153.     local PhysBeam_GunGlow='sprites/physg_glow2'
  154.    
  155.     -- Lazor beam!
  156.     Mat_Replace_Texture(    PhysBeam_Normal,    [[Sprites/crystal_beam1]]   )
  157.     Mat_Replace_Color(      PhysBeam_Normal,    Color(0,.6,0)               )
  158.    
  159.     -- Ty but noty
  160.     --Mat_Replace_Texture(PhysBeam_EndGlow,[[Sprites/arrow]])
  161.    
  162.     -- Not so blue anymore eh?
  163.     Mat_Replace_Texture(    PhysBeam_Grabbed,   [[Sprites/bluelaser1]]      )
  164.     Mat_Replace_Color(      PhysBeam_Grabbed,   Color(0,.3,0)               )
  165.    
  166.     -- Ahhgahh, color match :p
  167.     Mat_Replace_Color(      PhysBeam_EndGlow,   Color(0,1,0)                )
  168.    
  169.    
  170.     -- Disable that shit
  171.     Mat_Replace_Color(      PhysBeam_GunGlow,   Color(0,0,0)                )
  172.  
  173. end)
Advertisement
Add Comment
Please, Sign In to add comment