new post | browse code | authors | help | about

LuaBin 2.0

Viewing file root / lua / postprocess / morph.lua

  1.  
  2. local mat_Morph                 = Material( "pp/morph/refract" )
  3. local mat_Brush                 = Material( "pp/morph/brush1" )
  4. local mat_BrushOut              = Material( "pp/morph/brush_outline" )
  5. local mat_White                 = Material( "white" )
  6. local tex_MorphBuffer   = render.GetMorphTex0()
  7.  
  8. Morph = {}
  9.  
  10. Morph.Clear             = true
  11. Morph.Drawing           = false
  12. Morph.BrushSize         = 64
  13.  
  14. // This morph was so awesome. It was just like in photoshop or something
  15. // But it turned out that it only worked on my Geforce 7800GTX
  16. // So I had to make it dumb :(
  17.  
  18. // Maybe one day it'll be awesome again!
  19.  
  20.  
  21. /*---------------------------------------------------------
  22.    Register the convars that will control this effect
  23. ---------------------------------------------------------*/  
  24. local pp_morph                  = CreateClientConVar( "pp_morph", "0", false, false )
  25. local pp_morph_scale    = CreateClientConVar( "pp_morph_scale", "0.2", false, false )
  26.  
  27.  
  28. local function MorphFrameVisible()
  29.  
  30.         if ( !Morph.Frame ) then return false end
  31.         return Morph.Frame:IsVisible()
  32.  
  33. end
  34.  
  35.  
  36. /*---------------------------------------------------------
  37.    Draws the morph - you can call this from a function if you need to.
  38. ---------------------------------------------------------*/
  39. function DrawMorph( size )
  40.  
  41.         if ( !render.SupportsPixelShaders_2_0() ) then return end
  42.         if ( !render.SupportsHDR() ) then return end
  43.  
  44.         // Copy the backbuffer to the screen effect texture
  45.  
  46.         // Clear the morph buffer
  47.         if (Morph.Clear) then
  48.        
  49.                 render.ClearRenderTarget( tex_MorphBuffer, Color( 128, 128, 128, 255 ) )
  50.                 Morph.Clear = false
  51.                
  52.         end
  53.        
  54.         local x, y = gui.MousePos()
  55.        
  56.         // Here's the situation with this.
  57.         // I had it set up awesome, when it drew it just used the additive blend mode and it added/subtracted just fine.
  58.         // Then I found out it didn't work on ATI cards so I had to complicate everything.
  59.  
  60.         if (Morph.Drawing && MorphFrameVisible()) then
  61.                        
  62.                 local OldRT = render.GetRenderTarget();
  63.                 render.SetRenderTarget( tex_MorphBuffer )
  64.                 render.SetMaterial( mat_Brush )
  65.                
  66.                 // Interpolate the two positions
  67.                 local vStart    = Vector ( Morph.oldX, Morph.oldY, 0 )
  68.                 local vEnd              = Vector ( x, y, 0 )
  69.                 local vDiff     = vEnd-vStart
  70.                 local fLen              = vDiff:Length()
  71.                 local vNorm             = vDiff:GetNormalized()
  72.                
  73.                 color = Color( 128, 128, 128, 16 )
  74.                
  75.                 local r = (Morph.oldX - x)*10
  76.                 local g = (Morph.oldY - y)*10
  77.                
  78.                 color.r = color.r + math.Clamp( r, -127, 127 )
  79.                 color.g = color.g + math.Clamp( g, -127, 127 )
  80.                
  81.                
  82.                 if ( math.abs(r) > 5 || math.abs(g) > 5 ) then
  83.                
  84.                         for i=0, fLen, 16 do
  85.                
  86.                                 render.DrawQuadEasy( vStart + vNorm*i, Vector( 0, 0, -1 ), Morph.BrushSize, Morph.BrushSize, color )
  87.                
  88.                         end            
  89.                
  90.                 end
  91.                
  92.                 render.SetRenderTarget( OldRT )
  93.        
  94.         end
  95.        
  96.         Morph.oldX = x
  97.         Morph.oldY = y
  98.        
  99.         mat_Morph:SetMaterialFloat( "$refractamount", size )
  100.         mat_Morph:SetMaterialTexture( "$normalmap", tex_MorphBuffer )
  101.         mat_Morph:SetMaterialTexture( "$basetexture", tex_MorphBuffer )
  102.  
  103.        
  104.        
  105.         render.SetMaterial( mat_Morph )
  106.        
  107.         render.DrawScreenQuad()
  108.        
  109.         // Draw the handle
  110.         if ( vgui.IsHoveringWorld() && MorphFrameVisible()) then
  111.                 render.SetMaterial( mat_BrushOut )
  112.                 render.DrawQuadEasy( Vector( Morph.oldX, Morph.oldY, 0 ), Vector( 0, 0, -1 ), Morph.BrushSize, Morph.BrushSize, color_white )
  113.         end
  114.        
  115. end
  116.  
  117. /*---------------------------------------------------------
  118.    Hooked function to draw the morph
  119. ---------------------------------------------------------*/
  120. local function DrawInternal()
  121.  
  122.         if ( !pp_morph:GetBool() ) then return end
  123.         if ( !GAMEMODE:PostProcessPermitted( "morph" ) ) then return end
  124.  
  125.         DrawMorph( pp_morph_scale:GetFloat() );
  126.  
  127. end
  128.  
  129. hook.Add( "RenderScreenspaceEffects", "DrawMorph", DrawInternal )
  130.  
  131. /*---------------------------------------------------------
  132.    Mouse button down
  133. ---------------------------------------------------------*/  
  134. local function MouseDown( mouse )
  135.  
  136.         if (!MorphFrameVisible()) then return end
  137.  
  138.         // This makes it so that even if you're hovering over another panel when you
  139.         // release the mouse - it will still send the release event to this panel
  140.         vgui.GetWorldPanel():MouseCapture( true )
  141.        
  142.         Morph.Drawing = true
  143.  
  144. end
  145.  
  146.  
  147. /*---------------------------------------------------------
  148.    Mouse button released
  149. ---------------------------------------------------------*/  
  150. local function MouseUp( mouse )
  151.  
  152.         if (!MorphFrameVisible()) then return end
  153.        
  154.         vgui.GetWorldPanel():MouseCapture( false )
  155.        
  156.         Morph.Drawing = false
  157.        
  158. end
  159.  
  160. hook.Add( "GUIMousePressed", "MorphMouseDown", MouseDown )
  161. hook.Add( "GUIMouseReleased", "MorphMouseUp", MouseUp )
  162.  
  163.  
  164. /*---------------------------------------------------------
  165.    This is a console command that opens the morph window
  166. ---------------------------------------------------------*/  
  167. local function MorphFrame( player, command, arguments )
  168.  
  169.         if (Morph.Frame) then
  170.        
  171.                 Morph.Frame:SetVisible( true )
  172.        
  173.         return end
  174.  
  175.        
  176.         local frame = vgui.Create( "Frame" )
  177.         frame:SetName( "Morph" )       
  178.  
  179.        
  180.         // Brush Size Slider
  181.        
  182.         local brushsize = function( panel, message, param1, param2 )
  183.        
  184.                 if (message != "SliderMoved") then return end
  185.                 Morph.BrushSize = param1
  186.        
  187.         end
  188.        
  189.         local slider = vgui.Create( "Slider", frame, "MorphSlider" )
  190.         slider:PostMessage( "SetLower", "f", "8" )
  191.         slider:PostMessage( "SetHigher", "f", "256" )
  192.         slider:PostMessage( "SetValue", "f", Morph.BrushSize )
  193.         slider:SetActionFunction( brushsize )
  194.        
  195.        
  196.         // Refract Size Slider
  197.        
  198.         local refractsize = function( panel, message, param1, param2 )
  199.        
  200.                 if (message != "SliderMoved") then return end
  201.                
  202.                 RunConsoleCommand( "pp_morph_scale", param1 )
  203.        
  204.         end
  205.        
  206.         local slider = vgui.Create( "Slider", frame, "RefractSlider" )
  207.         slider:PostMessage( "SetInteger", "b", "0" )
  208.         slider:PostMessage( "SetLower", "f", "-1.0" )
  209.         slider:PostMessage( "SetHigher", "f", "1.0" )
  210.         slider:PostMessage( "SetValue", "f", pp_morph_scale:GetFloat() )
  211.        
  212.         slider:SetActionFunction( refractsize )
  213.        
  214.        
  215.         // Clear Button
  216.        
  217.         local clear = function( panel, message, param1, param2 )
  218.                
  219.                 if (message != "Command") then return end
  220.                 Morph.Clear = true
  221.                
  222.         end
  223.        
  224.         local button = vgui.Create( "Button", frame, "ClearButton" )
  225.         button:SetActionFunction( clear )
  226.        
  227.        
  228.         // Screenshot Button
  229.        
  230.         local screenshot = function( panel, message, param1, param2 )
  231.                
  232.                 if (message != "Command") then return end
  233.                
  234.                 frame:SetVisible( false )
  235.                
  236.                 timer.Simple( 0.1, LocalPlayer().ConCommand, LocalPlayer(), "jpeg\n" )
  237.                 timer.Simple( 0.2, frame.SetVisible, frame, true )
  238.                
  239.         end
  240.        
  241.         local button = vgui.Create( "Button", frame, "ScreenshotButton" )
  242.         button:SetActionFunction( screenshot )
  243.        
  244.         frame:LoadControlsFromFile( "resource/ui/Morph.res" )  
  245.         frame:SetKeyBoardInputEnabled( false )
  246.         frame:SetMouseInputEnabled( true )
  247.         frame:SetVisible( true )
  248.  
  249.         Morph.Frame = frame    
  250.        
  251.         // Turn morphing on
  252.         RunConsoleCommand( "pp_morph", "1" )
  253.  
  254. end
  255.  
  256. concommand.Add( "pp_morph_open", MorphFrame )
  257.  
  258. /*
  259. // Control Panel
  260. */
  261. local function BuildControlPanel( CPanel )
  262.  
  263.         CPanel:AddControl( "Header", { Text = "#Morph", Description = "#Morph_Information" }  )
  264.         CPanel:AddControl( "CheckBox", { Label = "#Morph_Toggle", Command = "pp_morph" }  )
  265.         CPanel:Button( "#Morph_Open", "pp_morph_open" )
  266.  
  267. end
  268.  
  269. /*
  270. // Tool Menu
  271. */
  272. local function AddPostProcessMenu()
  273.  
  274.         spawnmenu.AddToolMenuOption( "PostProcessing", "PPShader", "Morph", "#Morph", "", "", BuildControlPanel, { SwitchConVar = "pp_morph" } )
  275.  
  276. end
  277.  
  278. hook.Add( "PopulateToolMenu", "AddPostProcessMenu_Morph", AddPostProcessMenu )