Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Look at Sun+Lights glare effect; Where did I go wrong?

Asked by
Xetrax 85
9 years ago

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...

repeat wait() until game.Players.LocalPlayer.Character --[ Giving Time If A Player Handling Script Needs To Run Beforehand. ]

local GroupS = game:GetService("GroupService")  --[ I like to get all the services just in case I'll need them later... ]
local WorkS = game:GetService('Workspace')
local People = game:GetService('Players')
local Lights = game:GetService('Lighting')
local SG = game:GetService('StarterGui')
local TeamS = game:GetService('Teams')

--[Constants]:
local HUD = script.Parent   --[ Simple screen GUI. ]
local HudScrn = HUD:FindFirstChild("ViewMain")  --[ Frame called "ViewMain". ]
local Flash = HudScrn:FindFirstChild("Glare")   --[ Another frame called... Oh can't you guess... ]
local Cam = WorkS.CurrentCamera
local SunClr = Color3.new(1, 1, 1)

--[ Variables ]:
local User = nil
local VisorGlare = true
local Glare = nil
local Dist = nil
local Classes = {"Model", "Tool", "HopperBin", "Accoutrement", "Hat"}   --[ All classes that can contain a part with a Light. ]

--[ PreSetup ]:
if User == nil then
    User = People.LocalPlayer
end
local Body = User.Character or WorkS:FindFirstChild(User.Name)
local Head = Body:FindFirstChild("Head")
local Mouse = User:GetMouse()   

--[ Code ]:
function CalcGlare(Clr) --[ This calculates realistic glare brightness/coloration. ]
    local newR = Clr.r+(255%Clr.r)
    if newR > 1 then newR = 1 end
    if newR < 0 then newR = 0 end
    local newG = Clr.g+(255%Clr.g)
    if newG > 1 then newG = 1 end
    if newG < 0 then newG = 0 end
    local newB = Clr.b+(255%Clr.b)
    if newB > 1 then newB = 1 end
    if newB < 0 then newB = 0 end
    print(newR .." ".. newG .." ".. newB)

    return Color3.new(newR, newG, newB) --[ Keeps returning incredibly stupid values, even with my if statements! ]
end

function CheckPart(Prt) --[ Checks parts to see if they contain a Light. ]
    for _, I in ipairs(Prt:GetChildren()) do
        if I:IsA("Light") then
            if (I.Enabled) then
                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. ]
            else return false, nil end
        elseif Classes[I.ClassName] then
            CheckPart(I)
        end
    end
    return nil
end

local ShowGlare = coroutine.wrap(function ()
    while true do
        local SightRay = Ray.new(Cam.CoordinateFrame.p, Cam.CoordinateFrame.lookVector)
        local Part, Pos = WorkS:FindPartOnRayWithIgnoreList(SightRay, {Cam, Body})
        local IsLight, Data = nil, nil
        if (Part) then
            IsLight, Data = CheckPart(Part) --[ The function returns a bool and a table. ]
            if (IsLight) then
                local LightClr = Data[1]
                local LightPrt = Data[2]
                Glare = ((Cam.CoordinateFrame.lookVector - LightPrt.CFrame.p).magnitude * 0.3)+0.7
                Flash.BackgroundColor3 = CalcGlare(LightClr)
            end
        else
            Glare = ((Cam.CoordinateFrame.lookVector - Lights:GetSunDirection()).magnitude * 0.3)+0.7
            Flash.BackgroundColor3 = SunClr
        end
        Flash.BackgroundTransparency = Part and 1 or Glare
        wait()
    end
end)

--[ Setup ]:
ShowGlare()

--[ Xetrax ]--

Please Help if you can! (And yes, I do like to enclose my comments in *Brackets *[])

0
If the way I comment bothers you, I'll edit this so all the comments are in the preferred style... Xetrax 85 — 9y

Answer this question