Advertisement
CloneTrooper1019

Auto Exposure

Jun 23rd, 2019
3,921
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.95 KB | None | 1 0
  1. -- @ CloneTrooper1019, 2019
  2.  
  3. local Players = game:GetService("Players")
  4. local Lighting = game:GetService("Lighting")
  5. local RunService = game:GetService("RunService")
  6.  
  7. local player = Players.LocalPlayer
  8. local up = Vector3.new(0, 1, 0)
  9.  
  10. local function getBrightness(color)
  11.     local h, s, v = Color3.toHSV(color)
  12.     return v
  13. end
  14.  
  15. local function computeOcclusion(origin, dir)
  16.     local occlusion = 0
  17.    
  18.     local ray = Ray.new(origin, dir.Unit * 5000)
  19.     local ignoreList = {player.Character}
  20.    
  21.     while occlusion < 1 do
  22.         local hit = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList, false, true)
  23.        
  24.         if not hit then
  25.             break
  26.         end
  27.        
  28.         if hit.Transparency < 0.95 and hit.CastShadow then
  29.             local opacity = math.clamp(1 - hit.Transparency, 0, 1)
  30.             local blockDepth = (hit.Size.Magnitude * opacity) / 20
  31.             occlusion = math.min(1, occlusion + blockDepth)
  32.         end
  33.        
  34.         table.insert(ignoreList, hit)
  35.     end
  36.    
  37.     return occlusion
  38. end
  39.  
  40. local function update(deltaTime)
  41.     local camera = workspace.CurrentCamera
  42.    
  43.     if not camera then
  44.         return
  45.     end
  46.    
  47.     local origin = camera.CFrame.Position
  48.     local sunDir = Lighting:GetSunDirection()
  49.     local dayLight = math.clamp(sunDir.Y * 10, -0.5, 1)
  50.    
  51.     if dayLight < 0 then
  52.         sunDir = Lighting:GetMoonDirection()
  53.     end
  54.    
  55.     local indoorLevel = computeOcclusion(origin, up)
  56.     local sunOcclusion = computeOcclusion(origin, sunDir)
  57.    
  58.     local indoorLight = getBrightness(Lighting.Ambient)
  59.     local outdoorLight = getBrightness(Lighting.OutdoorAmbient)
  60.    
  61.     local strength = 1 - (outdoorLight + ((indoorLight - outdoorLight) * indoorLevel))
  62.     local target = sunOcclusion * strength * dayLight * 3
  63.    
  64.     local current = Lighting.ExposureCompensation
  65.    
  66.     if (current < target) then
  67.         current = math.min(target, current + deltaTime)
  68.     elseif (current > target) then
  69.         current = math.max(target, current - (deltaTime * 2))
  70.     end
  71.    
  72.     Lighting.ExposureCompensation = current
  73. end
  74.  
  75. RunService:BindToRenderStep("AutoExposure", 201, update)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement