CODE_BLUE

Blue's Masks

Oct 2nd, 2017
293
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --This code can be improved alot.
  2. --Feel free to improve, use or modify in anyway altough credit would be apreciated.
  3.  
  4. if BMASKS == nil then
  5. BMASKS = {} --Global table, access the functions here
  6.  
  7. BMASKS.Materials = {} --Cache materials so they dont need to be reloaded
  8. BMASKS.Masks = {} --A table of all active mask objects, you should destroy a mask object when done with it
  9.  
  10. --The material used to draw the render targets
  11. BMASKS.MaskMaterial = CreateMaterial("!bluemask","UnlitGeneric",{
  12.     ["$translucent"] = 1,
  13.     ["$vertexalpha"] = 1,
  14.     ["$alpha"] = 1,
  15. })
  16.  
  17. --Creates a mask with the specified options
  18. --Be sure to pass a unique maskName for each mask, otherwise they will override each other
  19. BMASKS.CreateMask = function(maskName, maskPath, maskProperties)
  20.     local mask = {}
  21.  
  22.     --Set mask name
  23.     mask.name = maskName
  24.  
  25.     --Load materials
  26.     if BMASKS.Materials[maskPath] == nil then
  27.         BMASKS.Materials[maskPath] = Material(maskPath, maskProperties)
  28.     end
  29.  
  30.     --Set the mask material
  31.     mask.material = BMASKS.Materials[maskPath]
  32.  
  33.     --Create the render target
  34.     mask.renderTarget = GetRenderTargetEx("BMASKS:"..maskName, ScrW(), ScrH(), RT_SIZE_FULL_FRAME_BUFFER, MATERIAL_RT_DEPTH_NONE, 2, CREATERENDERTARGETFLAGS_UNFILTERABLE_OK, IMAGE_FORMAT_RGBA8888)
  35.  
  36.     BMASKS.Masks[maskName] = mask
  37.  
  38.     return maskName
  39. end
  40.  
  41. --Call this to begin drawing with a mask.
  42. --After calling this any draw call will be masked until you call EndMask(maskName)
  43. BMASKS.BeginMask = function(maskName)
  44.     --FindMask
  45.     if BMASKS.Masks[maskName] == nil then
  46.         print("Cannot begin a mask without creating it first!")
  47.         return false
  48.     end
  49.  
  50.     --Store current render target
  51.     BMASKS.Masks[maskName].previousRenderTarget = render.GetRenderTarget()
  52.    
  53.     --Confirgure drawing so that we write to the masks render target
  54.     render.PushRenderTarget(BMASKS.Masks[maskName].renderTarget)
  55.     render.OverrideAlphaWriteEnable( true, true )
  56.     render.Clear( 0, 0, 0, 0 )
  57. end
  58.  
  59. --Ends the mask and draws it
  60. --Not calling this after calling BeginMask will cause some really bad effects
  61. --This done return the render target used, using this you can create other effects such as drop shadows without problems
  62. --Passes true for dontDraw will result in it not being render and only returning the texture of the result (which is ScrW()xScrH())
  63. BMASKS.EndMask = function(maskName, x, y, sizex, sizey, opacity, rotation, dontDraw)
  64.  
  65.     dontDraw = dontDraw or false
  66.     rotation = rotation or 0
  67.     opacity = opacity or 255
  68.  
  69.     --Draw the mask
  70.     render.OverrideBlendFunc( true, BLEND_ZERO, BLEND_SRC_ALPHA, BLEND_DST_ALPHA, BLEND_ZERO )
  71.     surface.SetDrawColor(Color(255,255,255,opacity))
  72.     surface.SetMaterial(BMASKS.Masks[maskName].material)
  73.     if rotation == nil or rotation == 0 then
  74.         surface.DrawTexturedRect(x, y, sizex, sizey)
  75.     else
  76.         surface.DrawTexturedRectRotated(x, y, sizex, sizey, rotation)
  77.     end
  78.     render.OverrideBlendFunc(false)
  79.     render.OverrideAlphaWriteEnable( false )
  80.     render.PopRenderTarget()
  81.  
  82.     --Update material
  83.     BMASKS.MaskMaterial:SetTexture('$basetexture', BMASKS.Masks[maskName].renderTarget)
  84.  
  85.     --Clear material for upcoming draw calls
  86.     draw.NoTexture()
  87.  
  88.     --Only draw if they want is to
  89.     if not dontDraw then
  90.         --Now draw finished result
  91.         surface.SetDrawColor(Color(255,255,255,255))
  92.         surface.SetMaterial(BMASKS.MaskMaterial)
  93.         render.SetMaterial(BMASKS.MaskMaterial)
  94.         render.DrawScreenQuad()
  95.     end
  96.  
  97.     return BMASKS.Masks[maskName].renderTarget
  98. end
  99. end
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×