Advertisement
CloneTrooper1019

Roblox Lens Flare Effect

Jan 11th, 2018
1,636
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.82 KB | None | 0 0
  1. ------------------------------------------------------------------------------------------------------------------------
  2. -- @CloneTrooper1019, 2018 <3
  3. -- Lens Flare Effect
  4. -- A remake of Roblox's 2007 Lens Flare
  5. ------------------------------------------------------------------------------------------------------------------------
  6. -- Configuration
  7. ------------------------------------------------------------------------------------------------------------------------
  8.  
  9. -- Classic Style
  10.  
  11. local LENS_FLARE_CONFIGURATION =
  12. {  
  13.     Scale = 1/8;
  14.     Texture = "rbxasset://textures/whiteCircle.png";
  15.     Transparency = 0.9;
  16. }
  17.  
  18. -- Modern Style
  19.  
  20. --local LENS_FLARE_CONFIGURATION =
  21. --{
  22. -- 
  23. --  Scale = 1/3;
  24. --  Texture = "rbxasset://textures/shadowblurmask.png";
  25. --  Transparency = 0.7;
  26. --}
  27.  
  28. ------------------------------------------------------------------------------------------------------------------------
  29. -- Lenses
  30. ------------------------------------------------------------------------------------------------------------------------
  31. --[[
  32.     The LENSES array is used to control the individual lenses of the effect.
  33.    
  34.     * Color     - The color of the lense.
  35.     * Radius    - An arbitrary scaling factor for each lense.
  36.     * Distance  - A value between 0 and 1 indicating the position of the lense on the screen.
  37.                   ( A value of 0.5 will be at the center of the screen )
  38.                   ( A value of 1.0 will be directly on top of the sun )
  39.                   ( A value of 0.0 will be on the opposite end of the screen )
  40.    
  41. --]]
  42.  
  43. local LENSES =
  44. {
  45.     { Color = Color3.fromRGB(200, 255, 200), Radius = 1.00, Distance = 0.000 };
  46.     { Color = Color3.fromRGB(  0, 255,   0), Radius = 0.50, Distance = 0.200 };
  47.     { Color = Color3.fromRGB(255,   0,   0), Radius = 0.30, Distance = 0.225 };
  48.     { Color = Color3.fromRGB(255, 170,   0), Radius = 1.50, Distance = 0.250 };
  49.     { Color = Color3.fromRGB(255, 170,   0), Radius = 3.00, Distance = 0.250 };
  50.     { Color = Color3.fromRGB(  0, 255,   0), Radius = 0.50, Distance = 0.300 };
  51.     { Color = Color3.fromRGB(  0, 255,   0), Radius = 0.20, Distance = 0.600 };
  52.     { Color = Color3.fromRGB(  0, 255,   0), Radius = 0.40, Distance = 0.650 };
  53.     { Color = Color3.fromRGB(255,   0,   0), Radius = 0.20, Distance = 0.780 };
  54.     { Color = Color3.fromRGB(  0, 255,   0), Radius = 0.25, Distance = 0.900 };
  55.     { Color = Color3.fromRGB( 63,  63,  63), Radius = 0.15, Distance = 1.200 };
  56.     { Color = Color3.fromRGB( 63,  63,  63), Radius = 0.15, Distance = 1.500 };
  57. }
  58.  
  59. ------------------------------------------------------------------------------------------------------------------------
  60. -- Constants
  61. ------------------------------------------------------------------------------------------------------------------------
  62.  
  63. local RunService = game:GetService("RunService")
  64. local Lighting = game:GetService("Lighting")
  65.  
  66. local c = workspace.CurrentCamera
  67. local lensFlareNode = c:FindFirstChild("LensFlareNode")
  68. if lensFlareNode then
  69.     lensFlareNode:ClearAllChildren()
  70. else
  71.     lensFlareNode = Instance.new("Part")
  72.     lensFlareNode.Name = "LensFlareNode"
  73.     lensFlareNode.Transparency = 1
  74.     lensFlareNode.Anchored = true
  75.     lensFlareNode.CanCollide = false
  76.     lensFlareNode.Locked = true
  77.     lensFlareNode.Parent = c
  78. end
  79.  
  80. ------------------------------------------------------------------------------------------------------------------------
  81. -- Main
  82. ------------------------------------------------------------------------------------------------------------------------
  83.  
  84. local function as_Vector2(v3,...)
  85.     -- Roblox likes to kill my OCD.
  86.     return Vector2.new(v3.X,v3.Y),...
  87. end
  88.  
  89. local function isSunOccluded(sunDir)
  90.     local origin = c.CFrame.p + (c.CFrame.lookVector*2)
  91.     local ray = Ray.new(origin,origin + (sunDir * 10e6))
  92.     local ignore = {}
  93.     local occluded = false
  94.     while true do
  95.         local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,ignore)
  96.         if hit then
  97.             local t = hit.Transparency + hit.LocalTransparencyModifier
  98.             if t <= 0 then
  99.                 occluded = true
  100.                 break
  101.             else
  102.                 table.insert(ignore,hit)
  103.             end
  104.         else
  105.             break
  106.         end
  107.     end
  108.     return occluded
  109. end
  110.  
  111. local function createLenseBeam(lense,id)
  112.     local radius = lense.Radius * LENS_FLARE_CONFIGURATION.Scale
  113.    
  114.     local a0 = Instance.new("Attachment")
  115.     a0.Name = id .. "_A0"
  116.     a0.Parent = lensFlareNode
  117.     lense.A0 = a0
  118.    
  119.     local a1 = Instance.new("Attachment")
  120.     a1.Name = id .. "_A1"
  121.     a1.Parent = lensFlareNode
  122.     lense.A1 = a1
  123.    
  124.     local beam = Instance.new("Beam")
  125.     beam.Name = id
  126.     beam.Color = ColorSequence.new(lense.Color)
  127.     beam.Width0 = radius
  128.     beam.Width1 = radius
  129.     beam.TextureSpeed = 0
  130.     beam.LightEmission = 1
  131.     beam.Texture = LENS_FLARE_CONFIGURATION.Texture
  132.     beam.Attachment0 = a0
  133.     beam.Attachment1 = a1
  134.     beam.Transparency = NumberSequence.new(LENS_FLARE_CONFIGURATION.Transparency)
  135.     beam.Parent = lensFlareNode
  136.     lense.Beam = beam
  137.  
  138.     return beam
  139. end
  140.  
  141. local function updateLensFlare()
  142.     local vpSize = c.ViewportSize
  143.     local sunDir = Lighting:GetSunDirection()
  144.     local sunWrldSpace = sunDir * 10e6
  145.     local sunScrnSpace,inView = as_Vector2(c:WorldToViewportPoint(sunWrldSpace))
  146.     local sunScrnSpaceInv = c.ViewportSize - sunScrnSpace
  147.     local enabled = (inView and not isSunOccluded(sunDir))
  148.    
  149.     for i,lense in ipairs(LENSES) do
  150.         local beam = lense.Beam or createLenseBeam(lense,i)
  151.         beam.Enabled = enabled
  152.         if enabled then
  153.             local radius = lense.Radius * LENS_FLARE_CONFIGURATION.Scale
  154.             local lenseSP = sunScrnSpaceInv:lerp(sunScrnSpace,lense.Distance)
  155.             local lenseWP = c:ViewportPointToRay(lenseSP.X,lenseSP.Y,1).Origin
  156.             local lenseCF = CFrame.new(lenseWP,lenseWP - sunDir)
  157.             lense.A0.CFrame = lenseCF * CFrame.new(-radius/2,0,0)
  158.             lense.A1.CFrame = lenseCF * CFrame.new(radius/2,0,0)
  159.         end
  160.     end
  161. end
  162.  
  163. RunService:BindToRenderStep("UpdateLensFlare",201,updateLensFlare)
  164.  
  165. ------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement