CODE_BLUE

Blue's Shadows

Oct 2nd, 2017
1,404
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. --Global table
  5. if BSHADOWS == nil then
  6. BSHADOWS = {}
  7.  
  8. --The original drawing layer
  9. BSHADOWS.RenderTarget = GetRenderTarget("bshadows_original", ScrW(), ScrH())
  10.  
  11. --The shadow layer
  12. BSHADOWS.RenderTarget2 = GetRenderTarget("bshadows_shadow",  ScrW(), ScrH())
  13.  
  14. --The matarial to draw the render targets on
  15. BSHADOWS.ShadowMaterial = CreateMaterial("bshadows","UnlitGeneric",{
  16.     ["$translucent"] = 1,
  17.     ["$vertexalpha"] = 1,
  18.     ["alpha"] = 1
  19. })
  20.  
  21. --When we copy the rendertarget it retains color, using this allows up to force any drawing to be black
  22. --Then we can blur it to create the shadow effect
  23. BSHADOWS.ShadowMaterialGrayscale = CreateMaterial("bshadows_grayscale","UnlitGeneric",{
  24.     ["$translucent"] = 1,
  25.     ["$vertexalpha"] = 1,
  26.     ["$alpha"] = 1,
  27.     ["$color"] = "0 0 0",
  28.     ["$color2"] = "0 0 0"
  29. })
  30.  
  31. --Call this to begin drawing a shadow
  32. BSHADOWS.BeginShadow = function()
  33.  
  34.     --Set the render target so all draw calls draw onto the render target instead of the screen
  35.     render.PushRenderTarget(BSHADOWS.RenderTarget)
  36.  
  37.     --Clear is so that theres no color or alpha
  38.     render.OverrideAlphaWriteEnable(true, true)
  39.     render.Clear(0,0,0,0)
  40.     render.OverrideAlphaWriteEnable(false, false)
  41.  
  42.     --Start Cam2D as where drawing on a flat surface
  43.     cam.Start2D()
  44.  
  45.     --Now leave the rest to the user to draw onto the surface
  46. end
  47.  
  48. --This will draw the shadow, and mirror any other draw calls the happened during drawing the shadow
  49. BSHADOWS.EndShadow = function(intensity, spread, blur, opacity, direction, distance, _shadowOnly)
  50.    
  51.     --Set default opcaity
  52.     opacity = opacity or 255
  53.     direction = direction or 0
  54.     distance = distance or 0
  55.     _shadowOnly = _shadowOnly or false
  56.  
  57.     --Copy this render target to the other
  58.     render.CopyRenderTargetToTexture(BSHADOWS.RenderTarget2)
  59.  
  60.     --Blur the second render target
  61.     if blur > 0 then
  62.         render.OverrideAlphaWriteEnable(true, true)
  63.         render.BlurRenderTarget(BSHADOWS.RenderTarget2, spread, spread, blur)
  64.         render.OverrideAlphaWriteEnable(false, false)
  65.     end
  66.  
  67.     --First remove the render target that the user drew
  68.     render.PopRenderTarget()
  69.  
  70.     --Now update the material to what was drawn
  71.     BSHADOWS.ShadowMaterial:SetTexture('$basetexture', BSHADOWS.RenderTarget)
  72.  
  73.     --Now update the material to the shadow render target
  74.     BSHADOWS.ShadowMaterialGrayscale:SetTexture('$basetexture', BSHADOWS.RenderTarget2)
  75.  
  76.     --Work out shadow offsets
  77.     local xOffset = math.sin(math.rad(direction)) * distance
  78.     local yOffset = math.cos(math.rad(direction)) * distance
  79.  
  80.     --Now draw the shadow
  81.     BSHADOWS.ShadowMaterialGrayscale:SetFloat("$alpha", opacity/255) --set the alpha of the shadow
  82.     render.SetMaterial(BSHADOWS.ShadowMaterialGrayscale)
  83.     for i = 1 , math.ceil(intensity) do
  84.         render.DrawScreenQuadEx(xOffset, yOffset, ScrW(), ScrH())
  85.     end
  86.  
  87.     if not _shadowOnly then
  88.         --Now draw the original
  89.         BSHADOWS.ShadowMaterial:SetTexture('$basetexture', BSHADOWS.RenderTarget)
  90.         render.SetMaterial(BSHADOWS.ShadowMaterial)
  91.         render.DrawScreenQuad()
  92.         print("Drew Shadow")
  93.     end
  94.  
  95.     cam.End2D()
  96. end
  97.  
  98. --This will draw a shadow based on the texture you passed it.
  99. BSHADOWS.DrawShadowTexture = function(texture, intensity, spread, blur, opacity, direction, distance, shadowOnly)
  100.  
  101.     --Set default opcaity
  102.     opacity = opacity or 255
  103.     direction = direction or 0
  104.     distance = distance or 0
  105.     shadowOnly = shadowOnly or false
  106.  
  107.     --Copy the texture we wish to create a shadow for to the shadow render target
  108.     render.CopyTexture(texture, BSHADOWS.RenderTarget2)
  109.  
  110.     --Blur the second render target
  111.     if blur > 0 then
  112.         render.PushRenderTarget(BSHADOWS.RenderTarget2)
  113.         render.OverrideAlphaWriteEnable(true, true)
  114.         render.BlurRenderTarget(BSHADOWS.RenderTarget2, spread, spread, blur)
  115.         render.OverrideAlphaWriteEnable(false, false)
  116.         render.PopRenderTarget()
  117.     end
  118.  
  119.     --Now update the material to the shadow render target
  120.     BSHADOWS.ShadowMaterialGrayscale:SetTexture('$basetexture', BSHADOWS.RenderTarget2)
  121.  
  122.     --Work out shadow offsets
  123.     local xOffset = math.sin(math.rad(direction)) * distance
  124.     local yOffset = math.cos(math.rad(direction)) * distance
  125.  
  126.     --Now draw the shadow
  127.     BSHADOWS.ShadowMaterialGrayscale:SetFloat("$alpha", opacity/255) --Set the alpha
  128.     render.SetMaterial(BSHADOWS.ShadowMaterialGrayscale)
  129.     for i = 1 , math.ceil(intensity) do
  130.         render.DrawScreenQuadEx(xOffset, yOffset, ScrW(), ScrH())
  131.     end
  132.     if not shadowOnly then
  133.         --Now draw the original
  134.         BSHADOWS.ShadowMaterial:SetTexture('$basetexture', texture)
  135.         render.SetMaterial(BSHADOWS.ShadowMaterial)
  136.         render.DrawScreenQuad()
  137.     end
  138. end
  139. 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.

×