new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / postprocess / sunbeams.lua

  1.  
  2. local _Material                 = Material( "pp/sunbeams" )
  3.  
  4. /*---------------------------------------------------------
  5.    Register the convars that will control this effect
  6. ---------------------------------------------------------*/  
  7. local pp_sunbeams                       = CreateClientConVar( "pp_sunbeams",                            "0",            true, false )
  8. local pp_sunbeams_darken        = CreateClientConVar( "pp_sunbeams_darken",             "0.95",         false, false )
  9. local pp_sunbeams_multiply  = CreateClientConVar( "pp_sunbeams_multiply",       "1.0",          false, false )
  10. local pp_sunbeams_sunsize       = CreateClientConVar( "pp_sunbeams_sunsize",            "0.075",        false, false )
  11.  
  12.  
  13. function DrawSunbeams( darken, multiply, sunsize, sunx, suny )
  14.  
  15.         if ( !render.SupportsPixelShaders_2_0() ) then return end
  16.        
  17.  
  18.         _Material:SetMaterialFloat( "$darken", darken )
  19.         _Material:SetMaterialFloat( "$multiply", multiply )
  20.         _Material:SetMaterialFloat( "$sunx", sunx )
  21.         _Material:SetMaterialFloat( "$suny", suny )
  22.         _Material:SetMaterialFloat( "$sunsize", sunsize )
  23.        
  24.         render.SetMaterial( _Material )
  25.         render.DrawScreenQuad()
  26.        
  27. end
  28.  
  29. local function DrawInternal()
  30.  
  31.         if ( !pp_sunbeams:GetBool() ) then return end
  32.         if ( !GAMEMODE:PostProcessPermitted( "sunbeams" ) ) then return end
  33.         if ( !render.SupportsPixelShaders_2_0() ) then return end
  34.        
  35.         local sun = util.GetSunInfo()
  36.        
  37.         if (!sun) then return end
  38.         if (sun.obstruction == 0) then return end
  39.        
  40.         local sunpos = EyePos() + sun.direction * 4096
  41.         local scrpos = sunpos:ToScreen()
  42.        
  43.         local dot = (sun.direction:Dot( EyeVector() ) - 0.8) * 5
  44.         if (dot <= 0) then return end
  45.        
  46.         DrawSunbeams( pp_sunbeams_darken:GetFloat(),
  47.                                   pp_sunbeams_multiply:GetFloat() * dot * sun.obstruction,
  48.                                   pp_sunbeams_sunsize:GetFloat(),
  49.                                   scrpos.x / ScrW(),
  50.                                   scrpos.y / ScrH()
  51.                                   );
  52.  
  53. end
  54.  
  55. hook.Add( "RenderScreenspaceEffects", "RenderSunbeams", DrawInternal )
  56.  
  57.  
  58. /*
  59. // Control Panel
  60. */
  61. local function BuildControlPanel( CPanel )
  62.  
  63.         CPanel:AddControl( "Header", { Text = "#Sun Beams", Description = "#Sun Beams" }  )
  64.         CPanel:AddControl( "CheckBox", { Label = "#Enable", Command = "pp_sunbeams" }  )
  65.        
  66.         CPanel:AddControl( "Slider", { Label = "#Multiply", Command = "pp_sunbeams_multiply", Type = "Float", Min = "0", Max = "1" }  )
  67.         CPanel:AddControl( "Slider", { Label = "#Darken", Command = "pp_sunbeams_darken", Type = "Float", Min = "0", Max = "1" }  )
  68.         CPanel:AddControl( "Slider", { Label = "#Sun Size", Command = "pp_sunbeams_sunsize", Type = "Float", Min = "0.01", Max = "0.25" }  )   
  69.  
  70. end
  71.  
  72. /*
  73. // Tool Menu
  74. */
  75. local function AddPostProcessMenu()
  76.  
  77.         spawnmenu.AddToolMenuOption( "PostProcessing", "PPShader", "SunBeams", "#Sun Beams", "", "", BuildControlPanel, { SwitchConVar = "pp_sunbeams" } )
  78.  
  79. end
  80.  
  81. hook.Add( "PopulateToolMenu", "AddPostProcessMenu_SunBeams", AddPostProcessMenu )
  82.