--C13 AEGIS_Lite function expansion 1.89
local _l_GlareStyleLUT = {
    {--[[0-preset]]1,
    --[[1-basethresholdStart]]0.1,
    --[[2-luminanceStart]]1.5,
    --[[3-luminanceEnd]]1.8,
    --[[4-basebloomGammaStart]]1,
    --[[5-basebloomGammaEnd]]1.6,
    --[[6-bloomLuminanceStart]]0.2,
    --[[7-bloomLuminanceEnd]]0.13,
    --[[8-starLuminanceStart]]0.05,
    --[[9-starLuminanceEnd]]0.3,
    --[[10-starSoftnessStart]]0.3,
    --[[11-starSoftnessEnd]]0,
    --[[12-gausRadiusStart]]0.6,
    --[[13-gausRadiusEnd]]1,
    --[[14-basethresholdEnd]]0.1,
    --[[15-glareBrightPass]]0,
    --[[16-glareShapeStarLengthStart]]0.1,
    --[[17-glareShapeStarLengthEnd]]0.1,
    --[[18-glareShapeSecondaryStarLengthStart]]0.28,
    --[[19-glareShapeSecondaryStarLengthEnd]]0.38,
    --[[20-glareShapeStarStreaks]]4,
    --[[21-glareShapeGhostLuminanceStart]]0.01,
    --[[22-glareShapeGhostLuminanceEnd]]0.01,
    --[[23-glareShapeGhostHaloLuminanceStart]]1.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]1.03,
    --[[25-glareShapeGhostDistortionStart]]10,
    --[[26-glareShapeGhostDistortionEnd]]10,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.104,
    --[[30-glareAnamorphic]]0
    }, 
    {--[[0-preset]]2,
    --[[1-basethresholdStart]]3,
    --[[2-luminanceStart]]5,
    --[[3-luminanceEnd]]1,
    --[[4-basebloomGammaStart]]1.3,
    --[[5-basebloomGammaEnd]]2.1,
    --[[6-bloomLuminanceStart]]0.5,
    --[[7-bloomLuminanceEnd]]0.12,
    --[[8-starLuminanceStart]]0.0,
    --[[9-starLuminanceEnd]]0,
    --[[10-starSoftnessStart]]0,
    --[[11-starSoftnessEnd]]0,
    --[[12-gausRadiusStart]]0.9,
    --[[13-gausRadiusEnd]]1.5,
    --[[14-basethresholdEnd]]0.5,
    --[[15-glareBrightPass]]1,
    --[[16-glareShapeStarLengthStart]]0,
    --[[17-glareShapeStarLengthEnd]]0,
    --[[18-glareShapeSecondaryStarLengthStart]]0,
    --[[19-glareShapeSecondaryStarLengthEnd]]0,
    --[[20-glareShapeStarStreaks]]0,
    --[[21-glareShapeGhostLuminanceStart]]0.01,
    --[[22-glareShapeGhostLuminanceEnd]]0.01,
    --[[23-glareShapeGhostHaloLuminanceStart]]1.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]1.03,
    --[[25-glareShapeGhostDistortionStart]]10,
    --[[26-glareShapeGhostDistortionEnd]]10,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.204,
    --[[30-glareAnamorphic]]0
    },
    {--[[0-preset]]3,
    --[[1-basethresholdStart]]0.1,
    --[[2-luminanceStart]]1,
    --[[3-luminanceEnd]]2,
    --[[4-basebloomGammaStart]]1.5,
    --[[5-basebloomGammaEnd]]1.9,
    --[[6-bloomLuminanceStart]]0.1,
    --[[7-bloomLuminanceEnd]]0.1,
    --[[8-starLuminanceStart]]0,
    --[[9-starLuminanceEnd]]0,
    --[[10-starSoftnessStart]]0,
    --[[11-starSoftnessEnd]]0,
    --[[12-gausRadiusStart]]1,
    --[[13-gausRadiusEnd]]1,
    --[[14-basethresholdEnd]]1,
    --[[15-glareBrightPass]]1,
    --[[16-glareShapeStarLengthStart]]0,
    --[[17-glareShapeStarLengthEnd]]0,
    --[[18-glareShapeSecondaryStarLengthStart]]0,
    --[[19-glareShapeSecondaryStarLengthEnd]]0,
     --[[20-glareShapeStarStreaks]]0,
    --[[21-glareShapeGhostLuminanceStart]]0.01,
    --[[22-glareShapeGhostLuminanceEnd]]0.01,
    --[[23-glareShapeGhostHaloLuminanceStart]]1.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]1.03,
    --[[25-glareShapeGhostDistortionStart]]10,
    --[[26-glareShapeGhostDistortionEnd]]10,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.04,
    --[[30-glareAnamorphic]]0
    },
    {--[[0-preset]]4,
    --[[1-basethresholdStart]]3,
    --[[2-luminanceStart]]1.5,
    --[[3-luminanceEnd]]2.0,
    --[[4-basebloomGammaStart]]1.4,
    --[[5-basebloomGammaEnd]]1.8,
    --[[6-bloomLuminanceStart]]0.34,
    --[[7-bloomLuminanceEnd]]0.23,
    --[[8-starLuminanceStart]]0.05,
    --[[9-starLuminanceEnd]]0.03,
    --[[10-starSoftnessStart]]0.3,
    --[[11-starSoftnessEnd]]0.0,
    --[[12-gausRadiusStart]]0.6,
    --[[13-gausRadiusEnd]]1,
    --[[14-basethresholdEnd]]1,
    --[[15-glareBrightPass]]1,
    --[[16-glareShapeStarLengthStart]]0.601,
    --[[17-glareShapeStarLengthEnd]]0.401,
    --[[18-glareShapeSecondaryStarLengthStart]]1.28,
    --[[19-glareShapeSecondaryStarLengthEnd]]1.038,
    --[[20-glareShapeStarStreaks]]6,
    --[[21-glareShapeGhostLuminanceStart]]3.5,
    --[[22-glareShapeGhostLuminanceEnd]]0.025,
    --[[23-glareShapeGhostHaloLuminanceStart]]1.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]5.03,
    --[[25-glareShapeGhostDistortionStart]]0.0,
    --[[26-glareShapeGhostDistortionEnd]]10.3,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.04,
    --[[30-glareAnamorphic]]1
   },
   {--[[0-preset]]5,
    --[[1-basethresholdStart]]2.5,
    --[[2-luminanceStart]]2,
    --[[3-luminanceEnd]]3,
    --[[4-basebloomGammaStart]]1.0,
    --[[5-basebloomGammaEnd]]1.4,
    --[[6-bloomLuminanceStart]]2.34,
    --[[7-bloomLuminanceEnd]]1.53,
    --[[8-starLuminanceStart]]0.05,
    --[[9-starLuminanceEnd]]0.45,
    --[[10-starSoftnessStart]]0.3,
    --[[11-starSoftnessEnd]]0.0,
    --[[12-gausRadiusStart]]0.6,
    --[[13-gausRadiusEnd]]1.4,
    --[[14-basethresholdEnd]]0.5,
    --[[15-glareBrightPass]]0,
    --[[16-glareShapeStarLengthStart]]0.601,
    --[[17-glareShapeStarLengthEnd]]0.601,
    --[[18-glareShapeSecondaryStarLengthStart]]0.28,
    --[[19-glareShapeSecondaryStarLengthEnd]]0.601,
    --[[20-glareShapeStarStreaks]]6,
    --[[21-glareShapeGhostLuminanceStart]]0.0000000,
    --[[22-glareShapeGhostLuminanceEnd]]0.0000000,
    --[[23-glareShapeGhostHaloLuminanceStart]]10.0,
    --[[24-glareShapeGhostHaloLuminanceEnd]]10.0,
    --[[25-glareShapeGhostDistortionStart]]0.0,
    --[[26-glareShapeGhostDistortionEnd]]10.3,
    --[[27-glareShapeGhostSharpenessStart]]1,
    --[[28-glareShapeGhostSharpenessEnd]]1,
    --[[29-glareShapeAfterimageLength]]0.04,
    --[[30-glareAnamorphic]]1
},
{--[[0-preset]]6,
    --[[1-basethresholdStart]]1,
    --[[2-luminanceStart]]3,
    --[[3-luminanceEnd]]4,
    --[[4-basebloomGammaStart]]1.5,
    --[[5-basebloomGammaEnd]]1.6,
    --[[6-bloomLuminanceStart]]0.1,
    --[[7-bloomLuminanceEnd]]0.3,
    --[[8-starLuminanceStart]]0,
    --[[9-starLuminanceEnd]]0,
    --[[10-starSoftnessStart]]0,
    --[[11-starSoftnessEnd]]0,
    --[[12-gausRadiusStart]]1,
    --[[13-gausRadiusEnd]]1,
    --[[14-basethresholdEnd]]0.3,
    --[[15-glareBrightPass]]1,
    --[[16-glareShapeStarLengthStart]]0,
    --[[17-glareShapeStarLengthEnd]]0,
    --[[18-glareShapeSecondaryStarLengthStart]]0,
    --[[19-glareShapeSecondaryStarLengthEnd]]0,
     --[[20-glareShapeStarStreaks]]0,
    --[[21-glareShapeGhostLuminanceStart]]0.01,
    --[[22-glareShapeGhostLuminanceEnd]]0.01,
    --[[23-glareShapeGhostHaloLuminanceStart]]1.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]1.03,
    --[[25-glareShapeGhostDistortionStart]]10,
    --[[26-glareShapeGhostDistortionEnd]]10,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.1,
    --[[30-glareAnamorphic]]0
},
 {--[[0-preset]]7,
    --[[1-basethresholdStart]]3,
    --[[2-luminanceStart]]5,
    --[[3-luminanceEnd]]1,
    --[[4-basebloomGammaStart]]1.3,
    --[[5-basebloomGammaEnd]]2.1,
    --[[6-bloomLuminanceStart]]0.75,
    --[[7-bloomLuminanceEnd]]0.12,
    --[[8-starLuminanceStart]]0.0,
    --[[9-starLuminanceEnd]]1,
    --[[10-starSoftnessStart]]0,
    --[[11-starSoftnessEnd]]0,
    --[[12-gausRadiusStart]]0.9,
    --[[13-gausRadiusEnd]]0.5,
    --[[14-basethresholdEnd]]0.5,
    --[[15-glareBrightPass]]1,
    --[[16-glareShapeStarLengthStart]]0,
    --[[17-glareShapeStarLengthEnd]]0.1,
    --[[18-glareShapeSecondaryStarLengthStart]]0,
    --[[19-glareShapeSecondaryStarLengthEnd]]0,
    --[[20-glareShapeStarStreaks]]2,
    --[[21-glareShapeGhostLuminanceStart]]0.0051,
    --[[22-glareShapeGhostLuminanceEnd]]0.001,
    --[[23-glareShapeGhostHaloLuminanceStart]]0.03,
    --[[24-glareShapeGhostHaloLuminanceEnd]]0.03,
    --[[25-glareShapeGhostDistortionStart]]0.2,
    --[[26-glareShapeGhostDistortionEnd]]10,
    --[[27-glareShapeGhostSharpenessStart]]0,
    --[[28-glareShapeGhostSharpenessEnd]]0,
    --[[29-glareShapeAfterimageLength]]0.204,
    --[[30-glareAnamorphic]]1
}
}
local _l_glarestyleLUTresult
local _l_glareStyleCPP = LUT:new(_l_GlareStyleLUT)

local ColorWheelExt = ac.ColorCorrectionModulationRgb { color = rgb(1,1,1) }
ac.weatherColorCorrections[#ac.weatherColorCorrections + 1] = ColorWheelExt


local humidity
local smog
--local ambientTempNormal
local rainIntensity = 0
local _fog_temp_hsv = hsv(0,0,0)
local _fog_pure_hsv = hsv(0,0,0)
local fogColor = rgb(0,0,0)
local sunColor = rgb(0,0,0)
local newFog = rgb(0,0,0)
local fogDistance
local fog
local sim
local baseFogMulti
local altitude = 0
local extremefog=0
local trackFogAdjustment = 0

local _l_fogDistance_LUT_humidity = { 
    -- 0 humidity 1 distance  2 blend   3density   4height    5exponent
        {0,        70000,   0.60,        0.25,     200000,      1.20},
        {0.1,      68000,   0.65,        0.25,     200000,      1.00},
        {0.2,      67000,   0.75,        0.25,     200000,      0.98},
        {0.4,      65000,   0.80,        0.25,     200000,      0.95},
        {0.5,      62000,   0.80,        0.30,     200000,      0.89},
        {0.6,      60000,   0.80,        0.40,     200000,      0.87},
        {0.7,      59000,   0.80,        0.50,     200000,      0.85},
        {1,        55000,   0.98,        0.60,     200000,      0.80}
        
        
    }

local _l_fogDistance_LUT_humidity_result
local _l_fogDistance_humidity_CPP = LUT:new(_l_fogDistance_LUT_humidity)


local _l_fogDistance_mist_LUT = {
    -- 0 fog  1 distance   2 blend  3density    4height         5exponent
        {0,        70000,    0.60,        0.25,     150000,      1.20},
        {0.1,      66000,    0.60,        0.40,     200000,      1.00},
        {0.2,      64000,    0.70,        0.50,     200000,      0.90},
        {0.4,      60000,    0.70,        0.60,     200000,      0.90},
        {0.5,      50000,    0.86,        0.30,     200000,      0.86},
        {0.6,      43000,    0.98,        0.25,     201000,      0.85},
        {0.7,      40000,    0.99,        0.20,     201000,      0.84},
        {0.8,      35000,    0.99,        0.15,     201000,      0.72},
        {1,        24000,    0.999,       0.02,     201000,      0.70}
        
        
    }

local _l_fogDistance_LUT_mist_result
local _l_fogDistance_mistCPP = LUT:new(_l_fogDistance_mist_LUT)

local _l_fogDistance_rain_LUT = {
    -- 0 rain 1 distance   2 blend      3density    4height         5exponent
        {0,        70000,    0.30,        0.25,     200000,      1.20},
        {0.03,     68000,    0.35,        0.25,     210000,      0.98},
        {0.1,      60000,    0.77,        0.33,     215000,      0.95},
        {0.2,      58000,    0.93,        0.34,     217000,      0.85},
        {0.4,      50000,    0.94,        0.35,     219000,      0.83},
        {0.5,      45000,    0.94,        0.36,     220000,      0.80},
        {0.6,      45000,    0.99,        0.37,     221000,      0.80},
        {0.7,      45000,    0.99,        0.38,     222000,      0.80},
        {0.8,      45000,    0.99,        0.49,     223000,      0.80},
        {0.9,      45000,    0.99,        0.59,     224000,      0.80},
        {1,        45000,    0.999,       0.60,     226000,      0.80}
        
        
    }

local _l_fogDistance_LUT_rain_result
local _l_fogDistance_rainCPP = LUT:new(_l_fogDistance_rain_LUT)

local tunnelFogDistanceAdpation = 0
local thresholdOccluded = 1
local rainFogControl = 0
local fogHorizonHeight = 1
local fogHorizonIntensity = 1
local fogHorizonExponent = 1
local baseFogDistance = 100000
local fogExponent = 1
local fogBoost = 1


function C13_CalculateFogExternal(fogXORBadness,badness,skyLevel)
if sim == nil then
    sim = ac.getSim()
end

local isPowerOfTheSun = (pure.script.ui.getValue("Power of the sun") or dashcam)
if isPowerOfTheSun then
    
    fogBoost = pure.mod.dayCurve(1.3,2,0.3)
    
else
    
    fogBoost = 1
end
--ac.resetGlowBrightness()
--ac.setGlowBrightness(0)

rainFogControl = math.saturateN(rainIntensity/0.4)

trackFogAdjustment = 1 + PURE__getTrackAdjustment_FOG_SHAPE()/10
thresholdOccluded =  math.saturateN((pure.camera.getOcclusion())/0.8)
--ac.debug("altitude",sim.baseAltitude)
altitude = sim.baseAltitude
fog = pure.world.getMist()
extremefog = math.saturateN((fog-0.8)/0.2)
baseFogMulti = math.clamp(math.max(pure.mod.dayCurve(1.2,1,1.2),1*(fog/1.2)),0,1)

--if(pure.script.ui.getValue("C13 Fog")) then
    humidity =  pure.world.getHumidity()  --math.saturateN(ac.getAirHumidity()*100)
    --humidity = math.saturateN(ac.getAirHumidity()*100)
    smog = pure.world.getSmog()
    
    rainIntensity = sim.rainIntensity
    --humidity = math.lerp(absoluteHumidity,humidity,fog)
    --local highhumidity = math.saturateN(humidity-0.4)/0.6
    --local isfog = fog > 0.1
    fogColor = pure.light.getFog() 
    --local baseFogDistance = math.lerp(33000,15000,humidity * baseFogMulti)
    --local baseFogDistance = math.lerp(33000,15000,humidity * baseFogMulti)
    _l_fogDistance_LUT_humidity_result = _l_fogDistance_humidity_CPP:get(humidity)
    _l_fogDistance_LUT_mist_result = _l_fogDistance_mistCPP:get(fog)      
    _l_fogDistance_LUT_rain_result = _l_fogDistance_rainCPP:get(rainIntensity)    


    baseFogDistance = math.min(math.lerp(_l_fogDistance_LUT_humidity_result[1],_l_fogDistance_LUT_mist_result[1],rainFogControl),_l_fogDistance_LUT_mist_result[1],_l_fogDistance_LUT_rain_result[1])
    tunnelFogDistanceAdpation = math.clamp((100000/baseFogDistance)*10000,0,7000) * (1- thresholdOccluded)
    --fogDistance = math.clamp(math.min(math.lerp(baseFogDistance,10000,fog*fog*humidity),math.lerp(baseFogDistance,13000,badness), math.lerp(baseFogDistance,6500, pure.mod.night(0) * fog)) - 4000 * extremefog,6000,40000)
    --fogDistance = math.clamp(math.min(baseFogDistance,math.lerp(baseFogDistance,13000,badness)) + tunnelFogDistanceAdpation ,6000,40000)
    fogDistance = math.clamp(baseFogDistance+tunnelFogDistanceAdpation,6000,100000)

    local basefogHeight = math.max(_l_fogDistance_LUT_humidity_result[4],_l_fogDistance_LUT_mist_result[4],_l_fogDistance_LUT_rain_result[4])
    basefogHeight = basefogHeight - 2 * tunnelFogDistanceAdpation
    --basefogHeight = basefogHeight * pure.mod.dayCurve(0.6,1)
    fogDistance = fogDistance * pure.mod.dayCurve(0.8,1)
    
    if trackFogAdjustment > 1 then
    fogDistance = math.clamp(fogDistance/trackFogAdjustment,6000,100000)
    end
    --ac.setFogBacklitMultiplier(math.lerp(0.09,0.2,fog))
    --ac.setFogDensity(1.1-0.5 * fog * highhumidity)
    --local baseDensity = (1.3 + 0.5 * humidity) * baseFogMulti
    
    local baseDensity = math.min(_l_fogDistance_LUT_mist_result[3],math.lerp(_l_fogDistance_LUT_humidity_result[3],_l_fogDistance_LUT_rain_result[3],rainIntensity))
    --ac.setFogDensity(math.clamp(baseDensity - (baseDensity * 0.9 * fog)  + 0.1 * smog,0.1,2 ))
    ac.setFogDensity(math.clamp(baseDensity  + 0.1 * smog,0.1,2 ))
    if pure.system.isCSPGammaFix() then
    ac.setFogDistance((fogDistance/(1 + 5 * fog)) - (altitude))
    else
        ac.setFogDistance(fogDistance)
    end
    --ac.setFogDistance(pure.script.ui.getValue("Fdistance"))
    fogExponent = math.clamp(pure.mod.dayCurve(1.3,1.2 - 0.1 * fog) - 0.3 * humidity - 0.85 * (math.saturateN(fog-0.5)/0.5) + 0.3 * (1-pure.camera.getOcclusion()),0.7,1.5)
    fogExponent = math.clamp(math.min(_l_fogDistance_LUT_humidity_result[5],_l_fogDistance_LUT_mist_result[5],_l_fogDistance_LUT_rain_result[5]) + 0.3 * (1-pure.camera.getOcclusion()),0.6,1.3)
    ac.setFogExponent(fogExponent)
    --ac.setFogExponent(pure.script.ui.getValue("hExponent"))
    local fogBlend =  0.5--math.lerp(math.clamp(0.7 * baseFogMulti + 0.5 * (math.saturateN(fog-0.5)/0.5) + 0.5 * rainIntensity + 0.25 * humidity,0,1.0),1,fog/0.83)
    fogBlend = math.max(_l_fogDistance_LUT_humidity_result[2],_l_fogDistance_LUT_mist_result[2],math.clamp(_l_fogDistance_LUT_rain_result[2],0,math.saturateN(0.8 + badness+fog)))*baseFogMulti
    --fogBlend =math.clamp(fogBlend,0,math.saturateN(0.8 + badness+fog))
    ac.setFogBlend(fogBlend)
    --ac.setFogBlend(pure.script.ui.getValue("blExp"))
    --ac.setFogHeight( math.lerp(30000,50000,math.saturateN(0.2*humidity + 1*fog + 0.3*rainIntensity) * baseFogMulti ) - altitude*10)
    ac.setFogHeight(basefogHeight)
    --ac.setFogHeight(pure.script.ui.getValue("hHeight"))
    --ac.setFogColor(newFog)
    ac.setFogColor(C13_setFogColor())
    if ac.getPatchVersionCode() > 2943 then
    ac.setNearbyFog(C13_setFogColor() * 1.2,baseFogDistance/(math.max(100*(fog*fog*fog),2*humidity))+ 1000 * (1- thresholdOccluded) ,math.lerp(0,10,humidity) ,1+ math.clamp(baseDensity  + 0.1 * smog,0.1,2 ),3 * fogExponent * fogExponent - 0.3 * fog + 2 *(1-thresholdOccluded))
    end
    ac.setFogBacklitExponent(pure.mod.dayCurve(3,10-2*humidity,0.5) - 1 * fog - 0.2 * humidity)
    --ac.setFogBacklitExponent(50)
    --ac.setFogBacklitMultiplier(0.02 + (pure.mod.dayCurve(0.2,0.01,0.5) * humidity + 0.2 * fog) *(1- (pure.world.getSunCover()- rainIntensity)))
    ac.setFogBacklitMultiplier(0.02 + (pure.mod.dayCurve(0.2,0.01,0.5) * humidity + 0.2 * fog))
    --ac.setFogBacklitMultiplier(0)
    if ac.getPatchVersionCode() > 2943 then
    --ac.setFogAtmosphere(math.lerp(0,0.1,math.saturateN(humidity-fog)))
    ac.setFogAtmosphere(pure.mod.dayCurve(0.2,0.1) + math.lerp(0,0.1,math.saturateN(humidity-fog)))
    end

    --ac.setSkyFogMultiplier(pure.script.ui.getValue("hMultiplier"))

    --fogHorizonIntensity = math.clamp(0.4 + 0.5 * humidity + 0.1 * smog + 2 * rainIntensity + 0.3 * badness,1,1.4) 
    --fogHorizonHeight = math.clamp(1.6 - 0.2 * fog - 0.3 * rainIntensity,0.4,1.1)
    --fogHorizonExponent = math.clamp(1 - 0.5 * rainIntensity - 0.4 * humidity,0.0,1.3) 
    --ac.setHorizonFogMultiplier(fogHorizonIntensity,fogHorizonExponent,fogHorizonHeight)
    --ac.setHorizonFogMultiplier(pure.script.ui.getValue("hMultiplier"),pure.script.ui.getValue("hExponent"),pure.script.ui.getValue("hHeight"))
    pure.config.set("clouds2D.brightness",pure.mod.dayCurve(pure.mod.nightCurve(0.1,1,20),1.4 )- 0.3 * math.clamp(rainIntensity,0,0.5),true)
    pure.config.set("clouds2D.contrast",math.lerp(math.clamp(skyLevel,0,1.6 - 0.3 * pure.world.getOvercast()) + 0.4 * rainIntensity,0.8,pure.mod.night(0)),true)
    pure.config.set("light.distant_ambient.saturation",math.lerp(1, 2,1-pure.world.getOvercast()+badness),true)
    pure.config.set("light.distant_ambient.level",math.lerp(1,2,1-pure.world.getOvercast()),true)
    pure.config.set("light.distant_ambient.distance",math.lerp(1,3,1-pure.world.getOvercast()),true)
    pure.config.set("light.sky.level",math.clamp(pure.mod.dayCurve(1,2,0.2) - (0.8 + 0.4 * trackFogAdjustment) * humidity,0.8 - (0.4*math.saturateN(trackFogAdjustment-1)),2) ,true)
    
end


--local fogColorMorning = rgb(20.8, 18.0, 18.9)/30
local fogColorMorning = rgb(247, 166, 141)/100
local fogColorMorning2 = rgb(116, 169, 248)/200
local fogColorMidday = rgb(116, 169, 248)/15
local fogColorMidday2 = rgb(106, 159, 348)/15
local fogColorMist= rgb(220, 229, 242)/15
local fogColorRain= rgb(80, 109, 157)/100
local fogColorNight = rgb(3.8, 4.3, 4.6)/100
local finalFog = rgb(0,0,0)
local morningFog = rgb(0,0,0)
local finalFogHsv = hsv(0,0,0)
local mistBoost = 1
local mistFogMidday = rgb(0,0,0)
function C13_setFogColor()

    morningFog = math.lerp(fogColorMorning2,fogColorMorning, math.saturateN(fog*5))
    --ac.debug("lcs",pure.system.isCSPGammaFix())
    if pure.system.isCSPGammaFix() then
        mistFogMidday = math.lerp(fogColorMidday2,fogColorMidday,math.pow(fog,0.5))
        finalFog = pure.mod.dayCurve(morningFog,mistFogMidday,1)
    else
        finalFog = pure.mod.dayCurve(morningFog,fogColorMidday,1)
    end
    
    --finalFog = math.lerp(finalFog,math.lerp(fogColorRain,fogColor,1-pure.mod.sun(0)),pure.world.getSunCover())
    finalFog = math.lerp(finalFog,math.lerp(fogColorRain,fogColor,1-pure.mod.sun(0)),math.pow(rainIntensity,0.2))
    
    mistBoost = 1 + 5 * C13_BellCurve(fog,0.5,0.125)
    --ac.debug("mistBoost",mistBoost)
    local fogColorMistAdapted = fogColorMist * (1 - 0.7 * math.pow(fog,1/2))
    finalFog = math.lerp(finalFog,math.lerp(fogColorMistAdapted*mistBoost,fogColor,1-pure.mod.sun(0)),(pure.mod.dayCurve(fog*0.8,fog)))
    finalFog = math.lerp(finalFog,fogColor,(1-pure.mod.twilight(0)))

    RGBToHSV_To(finalFogHsv,finalFog)
    finalFogHsv.v = math.clamp(finalFogHsv.v,0,math.clamp(20-15*pure.world.getCloudCoverage() - 13 * (math.saturateN(fog-0.6)/0.4),4,20))
    finalFogHsv.v = finalFogHsv.v * pure.script.weather.getVariable("fogWeatherBrightness")
    --ac.debug("fogWeatherBrightness",pure.script.weather.getVariable("fogWeatherBrightness"))
    --ac.debug("cloudCoverage",pure.world.getCloudCoverage())
    if pure.system.isCSPGammaFix() then
        finalFogHsv.s = math.clamp(finalFogHsv.s,0,0.9-0.3*pure.world.getCloudCoverage())
    else
    finalFogHsv.s = math.clamp(finalFogHsv.s,0,0.6-0.3*pure.world.getCloudCoverage())
    end
    HSVToRGB_To(finalFog,finalFogHsv.h,finalFogHsv.s,finalFogHsv.v)

    finalFog = finalFog * 1 + 0.05 * __PURE__fog_get_cam_position_NLP() * pure.mod.night(0)
   
    --finalFog = math.lerp(finalFog,fogColor,pure.mod.nightCurve(1,1))
    --finalFog = finalFog * 1
    --finalFog = math.lerp(fogColor,finalFog,pure.mod.sun(0))

    --ac.setNearbyFog(rgb(0,0,0), pure.script.ui.getValue("nFogDistance"), pure.script.ui.getValue("nFogHeight"), pure.script.ui.getValue("nFogTransparency"), pure.script.ui.getValue("nFogExp"))

    return finalFog * fogBoost

end

function C13_BellCurve(x, midpoint, width)
    local coefficient = 1 / math.sqrt(2 * math.pi * 0.16)
    local exponent = -((x - midpoint)^2) / (2 * width^2)
    return coefficient * math.exp(exponent)
end


local ImpactVisionDT
local car
local hit = 0
local nHit = 0
local G = 0
local scalarG = 0
local Gy = 0
local collisionDepth = 0
local speedLast = 0
--local dofMax = 200
local normDofMax=0
local lowDOf=64
local highDof=16
local slowFocus = 1
local noDofShelf = 50
local fastFocus = 20
local chAbbLadTisDef= vec2(0.002600,0.001900)
local chAbbLadTisHit= vec2(0.052600,0.0501900)
local dispX
local dispY
local chAbFinal
local glareThresholdStart = 0
local glareThresholdEnd = 0
local glareLuminanceStart = 0
local glareLuminanceEnd = 0
local glareBloomGammaStart = 0
local glareBloomGammaEnd=0
local glareBloomLuminanceStart=0
local glareBloomLuminanceEnd = 0
local glareStarLuminanceStart=0
local glareStareLuminanceEnd=0
local glareStarSoftnessStart=0
local glareStarSoftnessEnd=0
local glareGaussRadiusStart=0
local glareGaussRadiusEnd=0
local glarebrightPass=0
local glareShapeStarLengthStart=0
local glareShapeStarLengthEnd=0
local glareShapeStarSecondaryLengthStart=0
local glareShapeStarSecondaryLengthEnd=0
--local glareShapeAnamorphic = false
local glareShapeGhostLuminanceStart = 0
local glareShapeGhostLuminanceEnd = 0
local glareShapeGhostHaloLuminanceStart = 0
local glareShapeGhostHaloLuminanceEnd=0
local glareShapeGhostDistortionStart=0
local glareShapeGhostDistortionEnd = 0
local glareShapeGhostSharpenessStart=0
local glareShapeGhostSharpenessEnd=0
local glareShapeAfterimageLength = 0
local glareShapeStarStreaks = 0
local glareAnamorphic = 0
local glarePreset=1
local dashcam = false
local impactVis = false
local callbackBloom = 0
local callbackgenRange = 0
local callbackGausRadious=0
local callbackStarLum = 0
local callbackStarSoftness = 0
local normalisedCBEcrossover = 1
--local normalisedCBEcrossover

function C13_setCBEcxExternal(cbec)
    normalisedCBEcrossover = cbec
    --ac.debug("normalisedCBEcrossoverExternal",cbec)
end

function C13_setGlareExternal(index)
    --ac.debug("indexExternal",index)
    _l_glarestyleLUTresult = _l_glareStyleCPP:get(index)
    glareThresholdStart  = _l_glarestyleLUTresult[1]
    glareThresholdEnd = _l_glarestyleLUTresult[14]
    glareLuminanceStart  = _l_glarestyleLUTresult[2]
    glareLuminanceEnd  = _l_glarestyleLUTresult[3]
    glareBloomGammaStart  = _l_glarestyleLUTresult[4]
    glareBloomGammaEnd = _l_glarestyleLUTresult[5]
    glareBloomLuminanceStart = _l_glarestyleLUTresult[6]
    glareBloomLuminanceEnd  = _l_glarestyleLUTresult[7]
    glareStarLuminanceStart = _l_glarestyleLUTresult[8]
    glareStareLuminanceEnd = _l_glarestyleLUTresult[9]
    glareStarSoftnessStart = _l_glarestyleLUTresult[10]
    glareStarSoftnessEnd = _l_glarestyleLUTresult[11]
    glareGaussRadiusStart = _l_glarestyleLUTresult[12]
    glareGaussRadiusEnd = _l_glarestyleLUTresult[13]
    glarebrightPass = _l_glarestyleLUTresult[15]
    glareShapeStarLengthStart =_l_glarestyleLUTresult[16]
    glareShapeStarLengthEnd=_l_glarestyleLUTresult[17]
    glareShapeStarSecondaryLengthStart=_l_glarestyleLUTresult[18]
    glareShapeStarSecondaryLengthEnd=_l_glarestyleLUTresult[19]

    glareShapeStarStreaks = _l_glarestyleLUTresult[20]

    --glareShapeAnamorphic = _l_glarestyleLUTresult[20]
    glareShapeGhostLuminanceStart = _l_glarestyleLUTresult[21]
    glareShapeGhostLuminanceEnd = _l_glarestyleLUTresult[22]
    glareShapeGhostHaloLuminanceStart = _l_glarestyleLUTresult[23]
    glareShapeGhostHaloLuminanceEnd=_l_glarestyleLUTresult[24]
    glareShapeGhostDistortionStart=_l_glarestyleLUTresult[25]
    glareShapeGhostDistortionEnd = _l_glarestyleLUTresult[26]
    glareShapeGhostSharpenessStart=_l_glarestyleLUTresult[27]
    glareShapeGhostSharpenessEnd=_l_glarestyleLUTresult[28]
    glareShapeAfterimageLength = _l_glarestyleLUTresult[29]
    glareAnamorphic = _l_glarestyleLUTresult[30]
    

      --ac.debug("glareGaussRadiusEnd",glareGaussRadiusEnd)
end


function C13_GlareExternal(ppObj)

    --ac.debug("glareLuminanceStart",glareLuminanceStart)
    --ac.debug("ppobj.glareLuminance",ppObj.glareLuminance)

    
    
    ppObj.glareLuminance = math.lerp(glareLuminanceStart,glareLuminanceEnd,normalisedCBEcrossover)
    ppObj.glareThreshold = math.lerp(glareThresholdStart,glareThresholdEnd,normalisedCBEcrossover)
    --ac.debug("params.glareThreshold",ppObj.glareThreshold)
    --callbackBloom = math.lerp(0.24,0.33,normalisedCBEcrossover)
    callbackBloom = math.lerp(glareBloomLuminanceStart,glareBloomLuminanceEnd,normalisedCBEcrossover)
    ppObj.glareShapeBloomLuminance = callbackBloom

    callbackgenRange = math.lerp(0.7,0.001,normalisedCBEcrossover)
    ppObj.glareGenerationRangeScale =callbackgenRange

    callbackGausRadious = math.lerp(glareGaussRadiusStart,glareGaussRadiusEnd,normalisedCBEcrossover)
    ppObj.glareBloomGaussianRadiusScale =callbackGausRadious

   callbackStarLum = math.lerp(glareStarLuminanceStart,glareStareLuminanceEnd,normalisedCBEcrossover)
   ppObj.glareShapeStarLuminance =callbackStarLum

   callbackStarSoftness= math.lerp(glareStarSoftnessStart,glareStarSoftnessEnd,normalisedCBEcrossover)
   ppObj.glareStarSoftness =callbackStarSoftness

   ppObj.glareBloomLuminanceGamma = math.lerp(glareBloomGammaStart,glareBloomGammaEnd,normalisedCBEcrossover)

   ppObj.glareBrightPass = glarebrightPass

   ppObj.glareShapeStarLength = math.lerp(glareShapeStarLengthStart,glareShapeStarLengthEnd,normalisedCBEcrossover)

   ppObj.glareShapeStarSecondaryLength = math.lerp(glareShapeStarSecondaryLengthStart,glareShapeStarSecondaryLengthEnd,normalisedCBEcrossover)

  

   ppObj.glareShapeGhostLuminance = math.lerp(glareShapeGhostLuminanceStart,glareShapeGhostLuminanceEnd,normalisedCBEcrossover)

   ppObj.glareShapeGhostHaloLuminance = math.lerp(glareShapeGhostHaloLuminanceStart,glareShapeGhostHaloLuminanceEnd,normalisedCBEcrossover)

   ppObj.glareShapeGhostDistortion = math.lerp(glareShapeGhostDistortionStart,glareShapeGhostDistortionEnd,normalisedCBEcrossover)

   ppObj.glareShapeGhostSharpeness = math.lerp(glareShapeGhostSharpenessStart,glareShapeGhostSharpenessEnd,normalisedCBEcrossover)

   ppObj.glareShapeAfterimageLength = glareShapeAfterimageLength

   ppObj.glareShapeStarStreaks = glareShapeStarStreaks

   if(glareAnamorphic == 1) then
    ppObj.glareAnamorphic = true
   else
    ppObj.glareAnamorphic = false
   end
   dashcam = pure.script.ui.getValue("Dashcam mode")
    if(dashcam) then
        ppObj.lensDistortionEnabled = true
    else
        ppObj.lensDistortionEnabled= false
    end
   --ac.debug("glareAnamorphic",params.glareAnamorphic)
   
   

   return false
end

--local _l_glarestyleLUTresult

function C13_CheckCamera()
    if (sim.cameraMode~=ac.CameraMode.Car and sim.cameraMode~=ac.CameraMode.Cockpit and sim.cameraMode~=ac.CameraMode.Drivable) then
        --ac.debug("camera",sim.cameraMode)
        return false
    end
    return true
end



local newExposure = 0.6
local oldExposure = 0.6
local expTimeout = 0.3
local changing
function C13_CameraExposureExternal(exposure,dt)

       
    newExposure = exposure
    if (math.abs(oldExposure - newExposure) < 0.1 and not changing) then
        oldExposure = oldExposure
    else
        if expTimeout > 0.03 and not changing then
                if newExposure < oldExposure then
                expTimeout = expTimeout - 0.2 * dt 
                
                else
                    expTimeout = expTimeout - 0.2 * dt
                    --ac.debug("expTimeout",expTimeout)
                end
        
        elseif math.abs(oldExposure-newExposure) < 0.01 then
            expTimeout = math.applyLag(expTimeout, 0.3,0.9,dt)
            if(expTimeout > 0.299) then
              changing = false
            end
        
        elseif changing then
            if newExposure > oldExposure then
                oldExposure = math.applyLag(oldExposure,newExposure,0.8,dt)
            else
                oldExposure = math.applyLag(oldExposure,newExposure,0.7,dt)
            end
        else
            changing = true
          --[[  if newExposure > oldExposure then
                oldExposure = math.applyLag(oldExposure,newExposure,0.8,dt)
            else
                oldExposure = math.applyLag(oldExposure,newExposure,0.7,dt)
            end
            ]]
        end
    end
    --[[if math.abs(oldExposure-newExposure) < 0.01 then
        expTimeout = math.applyLag(expTimeout, 0.2,0.9,dt)
        if(expTimeout > 0.185) then
          changing = false
        end

    end]]
    --ac.debug("changing",changing)
    --ac.debug("expTimeout",expTimeout)
    --ac.debug("oldExposure",oldExposure)
    --ac.debug("newExposure",newExposure)

   return oldExposure
end


