Advertisement
mattjeanes

Abyss Custom Sky v2

Jan 7th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. /*
  2. -----------------------------------------------------------
  3. ----------------Dr. Matt's Simple Day/Night ---------------
  4. -----------------------------------------------------------
  5. */
  6.  
  7. local skypaint
  8. local sun
  9.  
  10. hook.Add("InitPostEntity", "Abyss-SkyPaint", function()
  11.     if SERVER then
  12.         RunConsoleCommand("sv_skyname", "painted")
  13.         local skypaints=ents.FindByClass("env_skypaint")
  14.         if #skypaints==0 then
  15.             skypaint = ents.Create("env_skypaint")
  16.             skypaint:Spawn()
  17.             skypaint:Activate()
  18.         elseif #skypaints >= 1 then
  19.             skypaint=skypaints[1]
  20.         end
  21.         local suns=ents.FindByClass("env_sun")
  22.         if #suns >= 1 then
  23.             sun=suns[1]
  24.         end
  25.     end
  26. end)
  27.  
  28. local function NoFog()
  29.     render.FogMode( 1 )
  30.     render.FogStart( 0 )
  31.     render.FogEnd( 0  )
  32.     render.FogMaxDensity( 0 )
  33.     render.FogColor( 255, 255, 255 )
  34.     return true
  35. end
  36. hook.Add( "SetupWorldFog", "Abyss-SkyPaint", NoFog )
  37. hook.Add( "SetupSkyboxFog", "Abyss-SkyPaint", NoFog )
  38.  
  39. local function UpdateDusk(p)
  40.     if p > 160 and p < 200 then
  41.         skypaint:SetDuskIntensity( math.sqrt(((5/-20)*(p-160)+5)^2)*-1+5 )
  42.     elseif p > 340 and p < 380 then
  43.         skypaint:SetDuskIntensity( math.sqrt(((5/-20)*(p-340)+5)^2)*-1+5 )
  44.     else
  45.         skypaint:SetDuskIntensity( 0 )
  46.     end
  47. end
  48.  
  49. local function SetSunAngle(ang)
  50.     sun:SetKeyValue( "sun_dir", tostring( ang:Forward() ) )
  51.     UpdateDusk(ang.p)
  52. end
  53.  
  54. local function SetSkyColor(top,bottom)
  55.     skypaint:SetTopColor( top )
  56.     skypaint:SetBottomColor( bottom )
  57. end
  58.    
  59. local function UpdateSky(h,m)
  60.     local dawn={}
  61.     dawn.top=Vector(0.46, 0.47, 0.32)
  62.     dawn.bottom=Vector(0.10, 0.26, 0.31)
  63.  
  64.     local day={}
  65.     day.top=Vector( 0.2, 0.5, 1.0 )
  66.     day.bottom=Vector( 0.8, 1.0, 1.0 )
  67.    
  68.     local dusk={}
  69.     dusk.top=Vector(0.05, 0.05, 0.05)
  70.     dusk.bottom=Vector(0.23, 0.15, 0.08)
  71.    
  72.     local night={}
  73.     night.top=Vector( 0, 0, 0 )
  74.     night.bottom=Vector( 0, 0, 0 )
  75.  
  76.     local skys={night,night,night,night,night,dawn,dawn,day,day,day,day,day,day,day,day,day,day,dusk,dusk,night,night,night,night}
  77.     skys[0]=night
  78.     local currentsky=skys[h]
  79.     local nextsky=skys[h+1]
  80.     if h==23 then nextsky=skys[0] end
  81.    
  82.     local lerptime=m*(1+(2/3))/100
  83.     local top=LerpVector(lerptime, currentsky.top, nextsky.top)
  84.     local bottom=LerpVector(lerptime, currentsky.bottom, nextsky.bottom)
  85.     SetSkyColor(top,bottom)
  86. end
  87.  
  88. local function UpdateSun(time)
  89.     SetSunAngle(Angle((time*15)+90,0,0))
  90. end
  91.  
  92. local function round(num, idp)
  93.     local mult = 10^(idp or 0)
  94.     return math.floor(num * mult + 0.5) / mult
  95. end
  96.  
  97. local function UpdateLighting(h)
  98.     local L={"b","b","b","b","d","f","h","j","l","l","l","l","l","l","l","j","h","f","d","b","b","b","b"}
  99.     L[0]="b"
  100.     engine.LightStyle(0,L[h])
  101.     timer.Simple(0.5,function()
  102.         umsg.Start("UpdateLighting")
  103.         umsg.End()
  104.     end)
  105. end
  106.  
  107. usermessage.Hook("UpdateLighting",function()
  108.     render.RedownloadAllLightmaps()
  109. end)
  110.  
  111. if SERVER then
  112.     timer.Create("UpdateSky", 60, 0, function()
  113.         local h=tonumber(os.date("%H"))
  114.         local m=tonumber(os.date("%M"))
  115.         local t=h+(os.date("%M")*(1+(2/3))/100)
  116.         UpdateSky(h,m)
  117.         if IsValid(sun) then
  118.             UpdateSun(t)
  119.         end
  120.         UpdateLighting(h)
  121.     end)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement