Look at Sun+Lights glare effect; Where did I go wrong?
I have been modifying... well, completely redesigning a script I found that caused a screen glare when the Player looked up at the sun, I'm aiming for it to do that for anything that has a light the player looks at (as long as the light is enabled; for spotlights if they are pointed at the player), I had been doing alright with this script for awhile, but I'm beginning to think this is a little larger than I can handle, plus I've run into an annoying block... the Color(s) stopped calculating correctly, and... well, if you copy the code below and paste it in a Localscript with all the variables accounted for, you'll see what I mean...
01 | repeat wait() until game.Players.LocalPlayer.Character -- [ Giving Time If A Player Handling Script Needs To Run Beforehand. ] |
03 | local GroupS = game:GetService( "GroupService" ) -- [ I like to get all the services just in case I'll need them later... ] |
04 | local WorkS = game:GetService( 'Workspace' ) |
05 | local People = game:GetService( 'Players' ) |
06 | local Lights = game:GetService( 'Lighting' ) |
07 | local SG = game:GetService( 'StarterGui' ) |
08 | local TeamS = game:GetService( 'Teams' ) |
11 | local HUD = script.Parent -- [ Simple screen GUI. ] |
12 | local HudScrn = HUD:FindFirstChild( "ViewMain" ) -- [ Frame called "ViewMain" . ] |
13 | local Flash = HudScrn:FindFirstChild( "Glare" ) -- [ Another frame called... Oh can't you guess... ] |
14 | local Cam = WorkS.CurrentCamera |
15 | local SunClr = Color 3. new( 1 , 1 , 1 ) |
19 | local VisorGlare = true |
22 | local Classes = { "Model" , "Tool" , "HopperBin" , "Accoutrement" , "Hat" } -- [ All classes that can contain a part with a Light. ] |
26 | User = People.LocalPlayer |
28 | local Body = User.Character or WorkS:FindFirstChild(User.Name) |
29 | local Head = Body:FindFirstChild( "Head" ) |
30 | local Mouse = User:GetMouse() |
33 | function CalcGlare(Clr) -- [ This calculates realistic glare brightness/coloration. ] |
34 | local newR = Clr.r+( 255 %Clr.r) |
35 | if newR > 1 then newR = 1 end |
36 | if newR < 0 then newR = 0 end |
37 | local newG = Clr.g+( 255 %Clr.g) |
38 | if newG > 1 then newG = 1 end |
39 | if newG < 0 then newG = 0 end |
40 | local newB = Clr.b+( 255 %Clr.b) |
41 | if newB > 1 then newB = 1 end |
42 | if newB < 0 then newB = 0 end |
43 | print (newR .. " " .. newG .. " " .. newB) |
45 | return Color 3. new(newR, newG, newB) -- [ Keeps returning incredibly stupid values, even with my if statements! ] |
48 | function CheckPart(Prt) -- [ Checks parts to see if they contain a Light. ] |
49 | for _, I in ipairs (Prt:GetChildren()) do |
50 | if I:IsA( "Light" ) then |
52 | return true , { I.Color, I.Parent } -- [ If the part contains a Light and the Light is enabled, returns the Light's color and its Parent. ] |
53 | else return false , nil end |
54 | elseif Classes [ I.ClassName ] then |
61 | local ShowGlare = coroutine.wrap( function () |
63 | local SightRay = Ray.new(Cam.CoordinateFrame.p, Cam.CoordinateFrame.lookVector) |
64 | local Part, Pos = WorkS:FindPartOnRayWithIgnoreList(SightRay, { Cam, Body } ) |
65 | local IsLight, Data = nil , nil |
67 | IsLight, Data = CheckPart(Part) -- [ The function returns a bool and a table. ] |
69 | local LightClr = Data [ 1 ] |
70 | local LightPrt = Data [ 2 ] |
71 | Glare = ((Cam.CoordinateFrame.lookVector - LightPrt.CFrame.p).magnitude * 0.3 )+ 0.7 |
72 | Flash.BackgroundColor 3 = CalcGlare(LightClr) |
75 | Glare = ((Cam.CoordinateFrame.lookVector - Lights:GetSunDirection()).magnitude * 0.3 )+ 0.7 |
76 | Flash.BackgroundColor 3 = SunClr |
78 | Flash.BackgroundTransparency = Part and 1 or Glare |
Please Help if you can!
(And yes, I do like to enclose my comments in *Brackets *[])