DOUBLE LAYER CLOUD TEST #3. altered the atmosphere and cloud blending into it. fixed fog ambient color being black.

This commit is contained in:
Xonk 2023-12-08 01:14:42 -05:00
parent 72709b914d
commit 3893c87607
4 changed files with 81 additions and 90 deletions

View File

@ -169,7 +169,7 @@ if (gl_FragCoord.x > 18.+257. && gl_FragCoord.y > 1. && gl_FragCoord.x < 18+257+
vec3 sky = texelFetch2D(colortex4,ivec2(gl_FragCoord.xy)-ivec2(257,0),0).rgb/150.0;
if(viewVector.y < -0.025) sky = sky * clamp( exp(viewVector.y) - 1.0,0.25,1.0) ;
// if(viewVector.y < -0.025) sky = sky * clamp( exp(viewVector.y) - 1.0,0.25,1.0) ;
vec4 clouds = renderClouds(mat3(gbufferModelView)*viewVector*1024.,vec2(fract(frameCounter/1.6180339887),1-fract(frameCounter/1.6180339887)), sunColor, moonColor, skyGroundCol/30.0);
sky = sky*clouds.a + clouds.rgb / 5.0;

View File

@ -94,8 +94,23 @@ vec3 sky_transmittance(vec3 position, vec3 direction, const float steps) {
vec3 calculateAtmosphere(vec3 background, vec3 viewVector, vec3 upVector, vec3 sunVector, vec3 moonVector, out vec2 pid, out vec3 transmittance, const int iSteps, float noise) {
const int jSteps = 4;
// clamp(sunVector.y*2.0,0.0,1.0)
float GroundDarkening = max(exp2(-15 * clamp(-viewVector.y,0.0,1.0)) * 0.7+0.3, clamp(sunVector.y*2.0,0.0,1.0)); // darken the ground in the sky.
// viewVector.y = viewVector.y - 0.05;
// float GroundDarkening = max(exp2(-15 * clamp(-viewVector.y,0.0,1.0)) * 0.7+0.3, clamp(sunVector.y*2.0,0.0,1.0)); // darken the ground in the sky.
float GroundDarkening2 = exp(-100 * pow(max(-viewVector.y*5,0.0),2)); // darken the ground in the sky.
float GroundDarkening = max(GroundDarkening2 * 0.7+0.3,clamp(sunVector.y*2.0,0.0,1.0));
// viewVector.y = max(viewVector.y,0.0);
vec3 viewPos = (sky_planetRadius + eyeAltitude) * upVector;
vec2 aid = rsi(viewPos, viewVector, sky_atmosphereRadius);
@ -112,7 +127,7 @@ vec3 calculateAtmosphere(vec3 background, vec3 viewVector, vec3 upVector, vec3 s
position += increment * (0.34*noise);
vec2 phaseSun = sky_phase(dot(viewVector, sunVector), 0.8);
vec2 phaseMoon = sky_phase(dot(viewVector, moonVector), sky_mieg);
vec2 phaseMoon = sky_phase(dot(viewVector, moonVector), 0.8);
vec3 scatteringSun = vec3(0.0);
vec3 scatteringMoon = vec3(0.0);
@ -123,6 +138,7 @@ vec3 calculateAtmosphere(vec3 background, vec3 viewVector, vec3 upVector, vec3 s
float high_sun = clamp(pow(sunVector.y+0.6,5),0.0,1.0) * 3.0; // make sunrise less blue, and allow sunset to be bluer
float low_sun = clamp(((1.0-abs(sunVector.y))*3.) - high_sun,1.0,2.0) ;
for (int i = 0; i < iSteps; ++i, position += increment) {
vec3 density = sky_density(length(position));
if (density.y > 1e35) break;
@ -133,8 +149,9 @@ vec3 calculateAtmosphere(vec3 background, vec3 viewVector, vec3 upVector, vec3 s
vec3 stepTransmittedFraction = clamp01((stepTransmittance - 1.0) / -stepOpticalDepth) ;
vec3 stepScatteringVisible = transmittance * stepTransmittedFraction * GroundDarkening ;
scatteringSun += sky_coefficientsScattering * (stepAirmass.xy * phaseSun ) * stepScatteringVisible * sky_transmittance(position, sunVector*0.5+0.1, jSteps) ;
scatteringMoon += sky_coefficientsScattering * (stepAirmass.xy * phaseMoon) * stepScatteringVisible * sky_transmittance(position, moonVector, jSteps);
scatteringSun += sky_coefficientsScattering * (stepAirmass.xy * phaseSun) * stepScatteringVisible * sky_transmittance(position, sunVector*0.5+0.1, jSteps) * GroundDarkening2;
scatteringMoon += sky_coefficientsScattering * (stepAirmass.xy * phaseMoon) * stepScatteringVisible * sky_transmittance(position, moonVector, jSteps) * GroundDarkening2;
// Nice way to fake multiple scattering.
scatteringAmbient += sky_coefficientsScattering * stepAirmass.xy * stepScatteringVisible * low_sun;

View File

@ -67,18 +67,14 @@ vec4 GetVolumetricFog(
vec3 LightColor,
vec3 AmbientColor
){
/// ------------- RAYMARCHING STUFF ------------- \\\
//project pixel position into projected shadowmap space
vec3 wpos = mat3(gbufferModelViewInverse) * viewPosition + gbufferModelViewInverse[3].xyz;
vec3 fragposition = mat3(shadowModelView) * wpos + shadowModelView[3].xyz;
fragposition = diagonal3(shadowProjection) * fragposition + shadowProjection[3].xyz;
// mat4 Custom_ViewMatrix = BuildShadowViewMatrix(LightDir);
// mat4 Custom_ProjectionMatrix = BuildShadowProjectionMatrix();
// vec3 fragposition = mat3(Custom_ViewMatrix) * wpos + Custom_ViewMatrix[3].xyz;
// fragposition = diagonal3(Custom_ProjectionMatrix) * fragposition + Custom_ProjectionMatrix[3].xyz;
//project view origin into projected shadowmap space
vec3 start = toShadowSpaceProjected(vec3(0.0));
@ -91,54 +87,45 @@ vec4 GetVolumetricFog(
float maxLength = min(length(dVWorld), far)/length(dVWorld);
dV *= maxLength;
dVWorld *= maxLength;
//apply dither
vec3 progress = start.xyz;
vec3 vL = vec3(0.);
float SdotV = dot(sunVec,normalize(viewPosition))*lightCol.a;
// float SdotV = dot(normalize(LightDir * mat3(gbufferModelViewInverse)), normalize(viewPosition))*lightCol.a;
float dL = length(dVWorld);
vec3 progress = start.xyz;
vec3 progressW = gbufferModelViewInverse[3].xyz + cameraPosition;
vec3 WsunVec = mat3(gbufferModelViewInverse) * sunVec * lightCol.a;
float SdotV = dot(sunVec,normalize(viewPosition))*lightCol.a;
/// ------------- COLOR/LIGHTING STUFF ------------- \\\
vec3 color = vec3(0.0);
vec3 absorbance = vec3(1.0);
//Mie phase + somewhat simulates multiple scattering (Horizon zero down cloud approx)
// float mie = phaseg(SdotV,0.7)*5.0 + 0.1;
float mie = fogPhase(SdotV) * 5.0;
float rayL = phaseRayleigh(SdotV);
vec3 rC = vec3(fog_coefficientRayleighR*1e-6, fog_coefficientRayleighG*1e-5, fog_coefficientRayleighB*1e-5);
vec3 mC = vec3(fog_coefficientMieR*1e-6, fog_coefficientMieG*1e-6, fog_coefficientMieB*1e-6);
// Makes fog more white idk how to simulate it correctly
vec3 LightSourceColor = LightColor;
#ifdef ambientLight_only
LightSourceColor = vec3(0.0);
#endif
vec3 skyCol0 = AmbientColor / 2.0;
// recolor change sun and sky color to a color, but make sure luminance is preserved.
vec3 skyCol0 = AmbientColor;
#ifdef PER_BIOME_ENVIRONMENT
BiomeFogColor(LightSourceColor);
BiomeFogColor(skyCol0);
#endif
// float upGradient = 1.0 - (normalize(wpos).y*0.5 + 0.5);
// skyCol0 *= exp(upGradient * -5.0)*1.5 + 0.5;
skyCol0 = max(skyCol0 + skyCol0*(normalize(wpos).y*0.9+0.1),0.0);
float upGradient = normalize(wpos).y*0.9+0.1;
skyCol0 = max(skyCol0 + skyCol0*upGradient,0.0);
float mu = 1.0;
float muS = mu;
float absorbance = 1.0;
float expFactor = 11.0;
vec3 WsunVec = mat3(gbufferModelViewInverse) * sunVec * lightCol.a;
vec3 progressW = gbufferModelViewInverse[3].xyz + cameraPosition;
float lightleakfix = clamp(pow(eyeBrightnessSmooth.y/240.,2) ,0.0,1.0);
float expFactor = 11.0;
for (int i=0;i<VL_SAMPLES;i++) {
float d = (pow(expFactor, float(i+dither)/float(VL_SAMPLES))/expFactor - 1.0/expFactor)/(1-1.0/expFactor);
float dd = pow(expFactor, float(i+dither)/float(VL_SAMPLES)) * log(expFactor) / float(VL_SAMPLES)/(expFactor-1.0);
@ -155,17 +142,13 @@ vec4 GetVolumetricFog(
sh = shadow2D(shadow, pos).x;
}
// #ifdef TEST
// lightleakfix = 1.0;
// #endif
#ifdef VL_CLOUDS_SHADOWS
sh *= GetCloudShadow_VLFOG(progressW, WsunVec);
#endif
float densityVol = cloudVol(progressW) * lightleakfix;
//Water droplets(fog)
float density = densityVol*mu*300.;
float density = densityVol*300.;
//Just air
vec2 airCoef = exp(-max(progressW.y - SEA_LEVEL, 0.0) / vec2(8.0e3, 1.2e3) * vec2(6.,7.0)) * 24 * Haze_amount;
@ -174,35 +157,21 @@ vec4 GetVolumetricFog(
vec3 rL = rC*airCoef.x;
vec3 m = (airCoef.y+density) * mC;
vec3 AtmosphericFog = skyCol0 * (rL*3.0 + m);
vec3 DirectLight = (LightSourceColor*sh) * (rayL*rL*3.0 + m*mie);
vec3 AmbientLight = skyCol0 * m;
// #ifdef TEST
// vec3 Lightning = vec3(0.0);
// #else
vec3 Lightning = Iris_Lightningflash_VLfog(progressW-cameraPosition, lightningBoltPosition.xyz) * m;
// #endif
vec3 AtmosphericFog = skyCol0 * (rL*3.0 + m);// + (LightSourceColor * sh) * (rayL*rL*3.0 + m*mie);
vec3 vL0 = (AtmosphericFog + AmbientLight + DirectLight + Lightning) * lightleakfix;
// vL0 = DirectLight;
// #if defined Cave_fog && defined TEST
// vL0 += cavefogCol;
// #endif
vec3 lighting = (AtmosphericFog + AmbientLight + DirectLight + Lightning) * lightleakfix;
vL += (vL0 - vL0 * exp(-(rL+m)*dd*dL)) / ((rL+m)+0.00000001)*absorbance;
absorbance *= dot(clamp(exp(-(rL+m)*dd*dL),0.0,1.0), vec3(0.333333));
color += max(lighting - lighting * exp(-(rL+m)*dd*dL),0.0) / max(rL+m, 0.00000001)*absorbance;
absorbance *= max(exp(-(rL+m)*dd*dL),0.0);
}
return vec4(vL,absorbance);
return vec4(color, dot(absorbance,vec3(0.333333)));
}
/*
/// experimental functions to render clouds and fog in 2 passes
float cloudCoverage(in vec3 pos, float minHeight, float maxHeight){
@ -356,3 +325,4 @@ vec4 renderVolumetrics(
}
return vec4(color, absorbance);
}
*/

View File

@ -51,14 +51,16 @@ float cloudCov(in vec3 pos, vec3 samplePos, float minHeight, float maxHeight){
vec2 SampleCoords0 = (samplePos.xz + cloud_movement) / 5000;
vec2 SampleCoords1 = (samplePos.xz - cloud_movement) / 500;
float thedistance = 1.0-clamp(1.0-length((pos-cameraPosition).xz)/15000,0,1);
/// when the coordinates reach a certain height, alter the sample coordinates
if(max(pos.y - (maxHeight + 80),0.0) > 0.0){
SampleCoords0 = -( (samplePos.zx + cloud_movement*2) / 15000);
SampleCoords1 = -( (samplePos.zx - cloud_movement*2) / 1500);
}
float CloudLarge = texture2D(noisetex, SampleCoords0 ).b;
float CloudSmall = texture2D(noisetex, SampleCoords1 ).r;
float CloudLarge = max(texture2D(noisetex, SampleCoords0 ).b+thedistance,thedistance);
float CloudSmall = max(texture2D(noisetex, SampleCoords1 ).r+thedistance,thedistance);
float coverage = abs(pow(CloudLarge,1)*2.0 - 1.2)*0.5 - (1.0-CloudSmall);
float FirstLayerCoverage = DailyWeather_Cumulus(coverage);
@ -167,7 +169,8 @@ vec3 DoCloudLighting(
float sunShadows,
vec3 sunScatter,
vec3 sunMultiScatter
vec3 sunMultiScatter,
float distantfog
// float moonShadows,
// vec3 moonScatter
@ -178,14 +181,11 @@ vec3 DoCloudLighting(
vec3 skyLight = skyLightCol;
skyLight *= exp2((skyScatter*skyScatter) * densityFaded * -35.0) * lesspowder;
// skyLight *= exp(skyScatter * -10);
skyLight *= mix(1.0, exp2((skyScatter*skyScatter) * densityFaded * -35.0) * lesspowder, distantfog);
vec3 sunLight = exp(sunShadows * -15 + powder ) * sunScatter;
sunLight += exp(sunShadows * -3) * sunMultiScatter * (powder*0.7+0.3);
// vec3 moonLighting = exp(MoonShadowing * -7 + powder) * moonContribution;
// return skyLight;
// return sunLight;
return skyLight + sunLight;
@ -212,7 +212,6 @@ vec4 renderClouds(
float shadowStep = 200.0;
vec3 dV_Sun = WsunVec*shadowStep;
float SdotV = dot(mat3(gbufferModelView)*WsunVec,normalize(FragPosition));
// if(dV_Sun.y/shadowStep < -0.1) dV_Sun = -dV_Sun;
float mieDay = phaseg(SdotV, 0.75);
float mieDayMulti = (phaseg(SdotV, 0.35) + phaseg(-SdotV, 0.35) * 0.5) ;
@ -222,9 +221,6 @@ vec4 renderClouds(
vec3 sunIndirectScattering = SunColor * phaseg(dot(mat3(gbufferModelView)*vec3(0,1,0),normalize(FragPosition)), 0.5);
// SkyColor *= clamp(dV_Sun.y/100.0,0.5,1.0);
SunColor = SunColor * clamp(dV_Sun.y ,0.0,1.0);
MoonColor *= clamp(-dV_Sun.y,0.0,1.0);
//////////////////////////////////////////
////// Raymarching stuff
@ -238,11 +234,13 @@ vec4 renderClouds(
vec3 dV_view = normalize(viewPos.xyz);
dV_view.y += 0.05;
dV_view *= 300/abs(dV_view.y)/maxIT_clouds;
float mult = length(dV_view);
// first cloud layer
float MinHeight_0 = Cumulus_height;
float MaxHeight_0 = 100 + MinHeight_0;
@ -256,9 +254,14 @@ vec4 renderClouds(
float allDensities = Cumulus_density;
vec3 forg = normalize(dV_view);
float distantfog = max(1.0 - clamp(exp2(pow(abs(forg.y),1.5) * -35.0),0.0,1.0),0.0);
// sunScattering *= distantfog;
// sunMultiScattering *= distantfog;
#ifdef Cumulus
for(int i = 0; i < maxIT_clouds; i++) {
// determine the base of each cloud layer
bool isUpperLayer = max(progress_view.y - MinHeight_1,0.0) > 0.0;
float CloudBaseHeights = isUpperLayer ? 200.0 + MaxHeight_0 : MaxHeight_0;
@ -286,10 +289,10 @@ vec4 renderClouds(
float upperLayerOcclusion = !isUpperLayer ? allDensities * 2.0 * GetCumulusDensity(progress_view + vec3(0.0,1.0,0.0) * max((MaxHeight_1 - 30.0) - progress_view.y,0.0), 0, MinHeight_0, MaxHeight_0) : 0.0;
float skylightOcclusion = max(exp2((upperLayerOcclusion*upperLayerOcclusion) * -5),0.5);
float skylightOcclusion = max(exp2((upperLayerOcclusion*upperLayerOcclusion) * -5), 0.5 + (1.0-distantfog)*0.5);
float skyScatter = clamp((CloudBaseHeights - 20 - progress_view.y) / 275.0,0.0,1.0);
vec3 Lighting = DoCloudLighting(muE, cumulus, SkyColor * skylightOcclusion, skyScatter, sunLight, sunScattering, sunMultiScattering);
vec3 Lighting = DoCloudLighting(muE, cumulus, SkyColor * skylightOcclusion, skyScatter, sunLight, sunScattering, sunMultiScattering, distantfog);
vec3 indirectSunlight = sunIndirectScattering * skylightOcclusion * exp(-20.0 * pow(abs(upperLayerOcclusion - 0.3),2)) * exp((cumulus*cumulus) * -10.0) ;
Lighting += indirectSunlight ;
@ -307,16 +310,17 @@ vec4 renderClouds(
//////////////////////////////////////////
////// fade off in the distance stuff
//////////////////////////////////////////
// return vec4(color, total_extinction);
////////////////////////////////////////
return vec4(color, total_extinction);
vec3 normView = normalize(dV_view);
// Assume fog color = sky gradient at long distance
vec4 fogColor = vec4(skyFromTex(normView, colortex4)/30.0, 0.0);
float fog = clamp(abs(max(cameraPosition.y, 255.0) + MaxHeight_0) / max(abs(MinHeight_0-cameraPosition.y),0.00001) * abs(normView.y/1.5),0,1);
float fog = clamp(abs(max(cameraPosition.y, 255.0) + MaxHeight_0) / max(abs(MinHeight_0-cameraPosition.y),0.00001) * abs(normView.y),0,1);
fog = max(1.0 - clamp(exp((fog*fog) * -100.0),0.0,1.0),0.0);
fog = max(1.0 - clamp(exp((fog*fog) * -35.0),0.0,1.0),0.0);
// fog = max(1.0 - clamp(exp2(fog * -10.0),0.0,1.0),0.0);
return mix(fogColor, vec4(color, total_extinction), fog);