2023-08-03 00:23:29 -04:00
# include "/lib/settings.glsl"
# include "/lib/res_params.glsl"
2023-10-14 23:34:52 -04:00
2024-06-19 21:44:21 -04:00
// uniform int dhRenderDistance;
2023-10-14 23:34:52 -04:00
uniform float frameTimeCounter ;
2023-10-07 22:18:20 -04:00
# include "/lib/Shadow_Params.glsl"
2023-10-08 15:16:13 -04:00
2023-10-07 22:18:20 -04:00
flat varying vec3 averageSkyCol_Clouds ;
flat varying vec3 averageSkyCol ;
flat varying vec3 sunColor ;
flat varying vec3 moonColor ;
flat varying vec3 lightSourceColor ;
flat varying vec3 zenithColor ;
2023-08-03 00:23:29 -04:00
flat varying vec2 tempOffsets ;
2023-10-23 17:15:56 -04:00
2023-08-03 00:23:29 -04:00
flat varying float exposure ;
flat varying float avgBrightness ;
flat varying float rodExposure ;
flat varying float avgL2 ;
flat varying float centerDepth ;
2024-11-15 17:54:18 -05:00
#ifdef Daily_Weather
flat varying vec4 dailyWeatherParams0 ;
flat varying vec4 dailyWeatherParams1 ;
#endif
uniform int hideGUI ;
flat varying vec4 CurrentFrame_dailyWeatherParams0 ;
flat varying vec4 CurrentFrame_dailyWeatherParams1 ;
2024-02-10 18:10:46 -05:00
2023-08-03 00:23:29 -04:00
uniform sampler2D colortex4 ;
uniform sampler2D colortex6 ;
uniform sampler2D depthtex0 ;
2023-10-23 17:41:30 -04:00
uniform sampler2D depthtex1 ;
uniform sampler2D depthtex2 ;
2023-08-03 00:23:29 -04:00
uniform mat4 gbufferModelViewInverse ;
uniform vec3 sunPosition ;
uniform vec2 texelSize ;
uniform float sunElevation ;
uniform float eyeAltitude ;
uniform float near ;
2023-10-07 22:18:20 -04:00
// uniform float far;
2023-08-03 00:23:29 -04:00
uniform float frameTime ;
uniform int frameCounter ;
uniform float rainStrength ;
// uniform int worldTime;
2023-10-07 22:18:20 -04:00
vec3 sunVec = normalize ( mat3 ( gbufferModelViewInverse ) * sunPosition ) ;
2023-12-17 22:25:26 -05:00
2023-10-07 22:18:20 -04:00
// vec3 sunVec = normalize(LightDir);
2023-08-03 00:23:29 -04:00
# include "/lib/sky_gradient.glsl"
# include "/lib/util.glsl"
# include "/lib/ROBOBO_sky.glsl"
float luma ( vec3 color ) {
return dot ( color , vec3 ( 0.21 , 0.72 , 0.07 ) ) ;
}
2024-02-05 16:04:37 -05:00
vec3 rodSample ( vec2 Xi )
{
float r = sqrt ( 1.0 f - Xi . x * Xi . y ) ;
float phi = 2 * 3.14159265359 * Xi . y ;
2023-08-03 00:23:29 -04:00
2024-02-05 16:04:37 -05:00
return normalize ( vec3 ( cos ( phi ) * r , sin ( phi ) * r , Xi . x ) ) . xzy ;
}
2023-08-03 00:23:29 -04:00
//Low discrepancy 2D sequence, integration error is as low as sobol but easier to compute : http://extremelearning.com.au/unreasonable-effectiveness-of-quasirandom-sequences/
vec2 R2_samples ( int n ) {
vec2 alpha = vec2 ( 0.75487765 , 0.56984026 ) ;
return fract ( alpha * n ) ;
}
float tanh ( float x ) {
return ( exp ( x ) - exp ( - x ) ) / ( exp ( x ) + exp ( - x ) ) ;
}
float ld ( float depth ) {
return ( 2.0 * near ) / ( far + near - depth * ( far - near ) ) ; // (-depth * (far - near)) = (2.0 * near)/ld - far - near
}
uniform float nightVision ;
2024-02-10 18:10:46 -05:00
uniform int worldDay ;
void getWeatherParams (
inout vec4 weatherParams0 ,
inout vec4 weatherParams1 ,
float layer0_coverage ,
float layer1_coverage ,
float layer2_coverage ,
float uniformFog_density ,
float layer0_density ,
float layer1_density ,
float layer2_density ,
float cloudyFog_density
) {
weatherParams0 = vec4 ( layer0_coverage , layer1_coverage , layer2_coverage , uniformFog_density ) ;
weatherParams1 = vec4 ( layer0_density , layer1_density , layer2_density , cloudyFog_density ) ;
}
2023-08-03 00:23:29 -04:00
2024-05-22 00:01:17 -04:00
float hash11 ( float p )
{
p = fract ( p * .1031 ) ;
p *= p + 33.33 ;
p *= p + p ;
return fract ( p ) ;
}
2023-08-03 00:23:29 -04:00
void main ( ) {
gl_Position = ftransform ( ) * 0.5 + 0.5 ;
gl_Position . xy = gl_Position . xy * vec2 ( 18 . + 258 * 2 , 258 . ) * texelSize ;
gl_Position . xy = gl_Position . xy * 2 . - 1.0 ;
2023-10-07 22:18:20 -04:00
#ifdef OVERWORLD_SHADER
///////////////////////////////////
/// --- AMBIENT LIGHT STUFF --- ///
///////////////////////////////////
averageSkyCol_Clouds = vec3 ( 0.0 ) ;
averageSkyCol = vec3 ( 0.0 ) ;
vec2 sample3x3 [ 9 ] = vec2 [ ] (
vec2 ( - 1.0 , - 0.3 ) ,
vec2 ( 0.0 , 0.0 ) ,
vec2 ( 1.0 , - 0.3 ) ,
vec2 ( - 1.0 , - 0.5 ) ,
vec2 ( 0.0 , - 0.5 ) ,
vec2 ( 1.0 , - 0.5 ) ,
vec2 ( - 1.0 , - 1.0 ) ,
vec2 ( 0.0 , - 1.0 ) ,
vec2 ( 1.0 , - 1.0 )
) ;
// sample in a 3x3 pattern to get a good area for average color
2024-02-05 16:04:37 -05:00
2023-10-07 22:18:20 -04:00
int maxIT = 9 ;
2024-02-05 16:04:37 -05:00
// int maxIT = 20;
2023-10-07 22:18:20 -04:00
for ( int i = 0 ; i < maxIT ; i ++ ) {
2024-02-05 16:04:37 -05:00
vec3 pos = vec3 ( 0.0 , 1.0 , 0.0 ) ;
pos . xy += normalize ( sample3x3 [ i ] ) * vec2 ( 0.3183 , 0.9000 ) ;
2023-12-17 22:25:26 -05:00
2024-02-05 16:04:37 -05:00
averageSkyCol_Clouds += 1.5 * ( skyCloudsFromTex ( pos , colortex4 ) . rgb / maxIT / 150.0 ) ;
averageSkyCol += 1.5 * ( skyFromTex ( pos , colortex4 ) . rgb / maxIT / 150.0 ) ;
2023-10-07 22:18:20 -04:00
}
2024-02-16 21:28:13 -05:00
// maximum control of color and luminance
2024-07-08 20:56:41 -04:00
// vec3 minimumlight = vec3(0.5,0.75,1.0) * nightVision;
2024-11-15 17:54:18 -05:00
// averageSkyCol_Clouds = max( normalize(averageSkyCol_Clouds) * min(luma(averageSkyCol_Clouds) * 3.0,2.5) * (1.0-rainStrength*0.7), minimumlight);
vec3 minimumlight = vec3 ( 0.02 ) * 0.2 * MIN_LIGHT_AMOUNT + nightVision * 0.05 ;
averageSkyCol_Clouds = max ( normalize ( averageSkyCol_Clouds + 1e-6 ) * min ( luma ( averageSkyCol_Clouds ) * 3.0 , 2.5 ) , 0.0 ) ;
averageSkyCol = max ( averageSkyCol * PLANET_GROUND_BRIGHTNESS , 0.0 ) + max ( minimumlight , 0.02 * 0.2 ) ;
2023-10-07 22:18:20 -04:00
////////////////////////////////////////
/// --- SUNLIGHT/MOONLIGHT STUFF --- ///
////////////////////////////////////////
vec2 planetSphere = vec2 ( 0.0 ) ;
float sunVis = clamp ( sunElevation , 0.0 , 0.05 ) / 0.05 * clamp ( sunElevation , 0.0 , 0.05 ) / 0.05 ;
float moonVis = clamp ( - sunElevation , 0.0 , 0.05 ) / 0.05 * clamp ( - sunElevation , 0.0 , 0.05 ) / 0.05 ;
2023-12-27 15:04:36 -05:00
vec3 skyAbsorb = vec3 ( 0.0 ) ;
2023-10-07 22:18:20 -04:00
sunColor = calculateAtmosphere ( vec3 ( 0.0 ) , sunVec , vec3 ( 0.0 , 1.0 , 0.0 ) , sunVec , - sunVec , planetSphere , skyAbsorb , 25 , 0.0 ) ;
2024-03-12 17:16:30 -04:00
sunColor = sunColorBase / 4000.0 * skyAbsorb ;
2023-10-07 22:18:20 -04:00
moonColor = moonColorBase / 4000.0 ;
2024-11-15 17:54:18 -05:00
// lightSourceColor = sunVis >= 1e-5 ? sunColor * sunVis : moonColor * moonVis;
lightSourceColor = sunColor * sunVis + moonColor * moonVis ;
2023-10-07 22:18:20 -04:00
#endif
2024-02-10 18:10:46 -05:00
//////////////////////////////////
/// --- WEATHER PARAMETERS --- ///
//////////////////////////////////
#ifdef Daily_Weather
2024-05-22 00:01:17 -04:00
// this is horrid and i hate it
// store 8 values that control cloud parameters.
// as the day counter changes, switch to a different set of stored values.
#ifdef CHOOSE_RANDOM_WEATHER_PROFILE
2024-06-10 23:26:19 -04:00
int dayCounter = int ( clamp ( hash11 ( float ( mod ( worldDay , 1000 ) ) ) * 10.0 , 0 , 10 ) ) ;
2024-05-22 00:01:17 -04:00
#else
int dayCounter = int ( mod ( worldDay , 10 ) ) ;
#endif
vec4 weatherParameters_A [ 10 ] = vec4 [ ] (
vec4 ( DAY0_l0_coverage , DAY0_l1_coverage , DAY0_l2_coverage , DAY0_ufog_density ) ,
vec4 ( DAY1_l0_coverage , DAY1_l1_coverage , DAY1_l2_coverage , DAY1_ufog_density ) ,
vec4 ( DAY2_l0_coverage , DAY2_l1_coverage , DAY2_l2_coverage , DAY2_ufog_density ) ,
vec4 ( DAY3_l0_coverage , DAY3_l1_coverage , DAY3_l2_coverage , DAY3_ufog_density ) ,
vec4 ( DAY4_l0_coverage , DAY4_l1_coverage , DAY4_l2_coverage , DAY4_ufog_density ) ,
vec4 ( DAY5_l0_coverage , DAY5_l1_coverage , DAY5_l2_coverage , DAY5_ufog_density ) ,
vec4 ( DAY6_l0_coverage , DAY6_l1_coverage , DAY6_l2_coverage , DAY6_ufog_density ) ,
vec4 ( DAY7_l0_coverage , DAY7_l1_coverage , DAY7_l2_coverage , DAY7_ufog_density ) ,
vec4 ( DAY8_l0_coverage , DAY8_l1_coverage , DAY8_l2_coverage , DAY8_ufog_density ) ,
vec4 ( DAY9_l0_coverage , DAY9_l1_coverage , DAY9_l2_coverage , DAY9_ufog_density )
) ;
vec4 weatherParameters_B [ 10 ] = vec4 [ ] (
vec4 ( DAY0_l0_density , DAY0_l1_density , DAY0_l2_density , DAY0_cfog_density ) ,
vec4 ( DAY1_l0_density , DAY1_l1_density , DAY1_l2_density , DAY1_cfog_density ) ,
vec4 ( DAY2_l0_density , DAY2_l1_density , DAY2_l2_density , DAY2_cfog_density ) ,
vec4 ( DAY3_l0_density , DAY3_l1_density , DAY3_l2_density , DAY3_cfog_density ) ,
vec4 ( DAY4_l0_density , DAY4_l1_density , DAY4_l2_density , DAY4_cfog_density ) ,
vec4 ( DAY5_l0_density , DAY5_l1_density , DAY5_l2_density , DAY5_cfog_density ) ,
vec4 ( DAY6_l0_density , DAY6_l1_density , DAY6_l2_density , DAY6_cfog_density ) ,
vec4 ( DAY7_l0_density , DAY7_l1_density , DAY7_l2_density , DAY7_cfog_density ) ,
vec4 ( DAY8_l0_density , DAY8_l1_density , DAY8_l2_density , DAY8_cfog_density ) ,
vec4 ( DAY9_l0_density , DAY9_l1_density , DAY9_l2_density , DAY9_cfog_density )
) ;
2024-11-15 17:54:18 -05:00
CurrentFrame_dailyWeatherParams0 = weatherParameters_A [ dayCounter ] ;
CurrentFrame_dailyWeatherParams1 = weatherParameters_B [ dayCounter ] ;
vec4 rainyWeatherParameters_A [ 3 ] = vec4 [ ] (
// vec4(DAY0_l0_coverage, DAY0_l1_coverage, DAY0_l2_coverage, DAY0_ufog_density),
vec4 ( 1.3 , 0.0 , 0.0 , 0.0 ) ,
vec4 ( 0.5 , 0.0 , 0.0 , 0.0 ) ,
vec4 ( 0.0 , 0.0 , 0.0 , 0.0 )
) ;
vec4 rainyWeatherParameters_B [ 3 ] = vec4 [ ] (
// vec4(DAY7_l0_density, DAY7_l1_density, DAY7_l2_density, DAY7_cfog_density),
vec4 ( 0.1 , 0.0 , 0.0 , 0.0 ) ,
vec4 ( 0.1 , 0.0 , 0.0 , 0.0 ) ,
vec4 ( 0.0 , 0.0 , 0.0 , 0.0 )
) ;
// if(hideGUI == 1){
// CurrentFrame_dailyWeatherParams0 = rainyWeatherParameters_A[worldDay%2];
// CurrentFrame_dailyWeatherParams1 = rainyWeatherParameters_B[worldDay%2];
// } else {
// CurrentFrame_dailyWeatherParams0 = vec4(0.5,0.0,0.0,0.0);
// CurrentFrame_dailyWeatherParams1 = vec4(0.1,0.5,0.0,0.0);
// }
#if defined Daily_Weather
dailyWeatherParams0 = vec4 ( sqrt ( texelFetch2D ( colortex4 , ivec2 ( 1 , 1 ) , 0 ) . rgb / 1500.0 ) , 0.0 ) ;
dailyWeatherParams1 = vec4 ( texelFetch2D ( colortex4 , ivec2 ( 2 , 1 ) , 0 ) . rgb / 1500.0 , 0.0 ) ;
dailyWeatherParams0 . a = texelFetch2D ( colortex4 , ivec2 ( 3 , 1 ) , 0 ) . x / 1500.0 ;
dailyWeatherParams1 . a = texelFetch2D ( colortex4 , ivec2 ( 3 , 1 ) , 0 ) . y / 1500.0 ;
#endif
2024-06-10 23:26:19 -04:00
2024-02-10 18:10:46 -05:00
#endif
2023-08-03 00:23:29 -04:00
//////////////////////////////
/// --- EXPOSURE STUFF --- ///
//////////////////////////////
float avgLuma = 0.0 ;
float m2 = 0.0 ;
int n = 100 ;
vec2 clampedRes = max ( 1.0 / texelSize , vec2 ( 1920.0 , 1080 . ) ) ;
float avgExp = 0.0 ;
float avgB = 0.0 ;
vec2 resScale = vec2 ( 1920 . , 1080 . ) / clampedRes ;
const int maxITexp = 50 ;
float w = 0.0 ;
for ( int i = 0 ; i < maxITexp ; i ++ ) {
vec2 ij = R2_samples ( ( frameCounter % 2000 ) * maxITexp + i ) ;
vec2 tc = 0.5 + ( ij - 0.5 ) * 0.7 ;
2023-10-07 22:18:20 -04:00
vec3 sp = texture2D ( colortex6 , tc / 16 . * resScale + vec2 ( 0.375 * resScale . x + 4.5 * texelSize . x , .0 ) ) . rgb ;
2024-11-15 17:54:18 -05:00
avgExp += log ( sqrt ( luma ( sp ) ) ) ;
2023-08-03 00:23:29 -04:00
avgB += log ( min ( dot ( sp , vec3 ( 0.07 , 0.22 , 0.71 ) ) , 8e-2 ) ) ;
}
avgExp = exp ( avgExp / maxITexp ) ;
avgB = exp ( avgB / maxITexp ) ;
avgBrightness = clamp ( mix ( avgExp , texelFetch2D ( colortex4 , ivec2 ( 10 , 37 ) , 0 ) . g , 0.95 ) , 0.00003051757 , 65000.0 ) ;
float L = max ( avgBrightness , 1e-8 ) ;
float keyVal = 1.03 - 2.0 / ( log ( L * 4000 / 150 . * 8 . / 3.0 + 1.0 ) / log ( 10.0 ) + 2.0 ) ;
float expFunc = 0.5 + 0.5 * tanh ( log ( L ) ) ;
2024-11-15 17:54:18 -05:00
2024-11-15 17:58:24 -05:00
// float targetExposure = 1.0/log(L+1.05);
2024-11-15 18:52:43 -05:00
float targetExposure = ( EXPOSURE_DARKENING * 0.35 ) / log ( L + 1.0 + EXPOSURE_BRIGHTENING * 0.05 ) ;
2024-11-15 17:58:24 -05:00
// float targetExposure = 0.18/log2(L*2.5+1.045)*0.62; // choc original
2023-08-03 00:23:29 -04:00
avgL2 = clamp ( mix ( avgB , texelFetch2D ( colortex4 , ivec2 ( 10 , 37 ) , 0 ) . b , 0.985 ) , 0.00003051757 , 65000.0 ) ;
float targetrodExposure = max ( 0.012 / log2 ( avgL2 + 1.002 ) - 0.1 , 0.0 ) * 1.2 ;
2024-11-15 17:54:18 -05:00
exposure = max ( targetExposure , 0.0 ) ;
2024-06-19 21:44:21 -04:00
// exposure = mix(0.0, 1.0, min(targetExposure,1.0));
// exposure = 1;
2024-06-27 15:23:50 -04:00
float currCenterDepth = ld ( texture2D ( depthtex2 , vec2 ( 0.5 ) * RENDER_SCALE ) . r ) ;
2023-08-03 00:23:29 -04:00
centerDepth = mix ( sqrt ( texelFetch2D ( colortex4 , ivec2 ( 14 , 37 ) , 0 ) . g / 65000.0 ) , currCenterDepth , clamp ( DoF_Adaptation_Speed * exp ( - 0.016 / frameTime + 1.0 ) / ( 6.0 + currCenterDepth * far ) , 0.0 , 1.0 ) ) ;
centerDepth = centerDepth * centerDepth * 65000.0 ;
rodExposure = targetrodExposure ;
#ifndef AUTO_EXPOSURE
exposure = Manual_exposure_value ;
rodExposure = clamp ( log ( Manual_exposure_value * 2.0 + 1.0 ) - 0.1 , 0.0 , 2.0 ) ;
#endif
}