mirror of
https://github.com/X0nk/Bliss-Shader.git
synced 2025-01-03 16:13:30 +08:00
f82426c609
it has begun
158 lines
12 KiB
GLSL
158 lines
12 KiB
GLSL
const float sunAngularSize = 0.533333;
|
|
const float moonAngularSize = 0.516667;
|
|
|
|
//Sky coefficients and heights
|
|
|
|
#define airNumberDensity 2.5035422e25
|
|
#define ozoneConcentrationPeak 8e-6
|
|
const float ozoneNumberDensity = airNumberDensity * ozoneConcentrationPeak;
|
|
#define ozoneCrossSection vec3(4.51103766177301e-21, 3.2854797958699e-21, 1.96774621921165e-22)
|
|
|
|
#define sky_planetRadius 6731e3
|
|
|
|
#define sky_atmosphereHeight 110e3
|
|
#define sky_scaleHeights vec2(8.0e3, 1.2e3)
|
|
|
|
#define sky_mieg 0.80 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
#define sky_coefficientRayleighR 5.8 //[0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4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.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0 ]
|
|
|
|
#define sky_coefficientRayleigh vec3(sky_coefficientRayleighR*1e-6, sky_coefficientRayleighG*1e-5, sky_coefficientRayleighB*1e-5)
|
|
|
|
|
|
#define sky_coefficientMieR 3.0 //[0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0 ]
|
|
#define sky_coefficientMieG 3.0 //[0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0 ]
|
|
#define sky_coefficientMieB 3.0 //[0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0 ]
|
|
|
|
#define sky_coefficientMie vec3(sky_coefficientMieR*1e-6, sky_coefficientMieG*1e-6, sky_coefficientMieB*1e-6) // Should be >= 2e-6
|
|
const vec3 sky_coefficientOzone = (ozoneCrossSection * (ozoneNumberDensity * 1.e-6)); // ozone cross section * (ozone number density * (cm ^ 3))
|
|
|
|
const vec2 sky_inverseScaleHeights = 1.0 / sky_scaleHeights;
|
|
const vec2 sky_scaledPlanetRadius = sky_planetRadius * sky_inverseScaleHeights;
|
|
const float sky_atmosphereRadius = sky_planetRadius + sky_atmosphereHeight;
|
|
const float sky_atmosphereRadiusSquared = sky_atmosphereRadius * sky_atmosphereRadius;
|
|
|
|
#define sky_coefficientsScattering mat2x3(sky_coefficientRayleigh, sky_coefficientMie)
|
|
const mat3 sky_coefficientsAttenuation = mat3(sky_coefficientRayleigh, sky_coefficientMie * 1.11, sky_coefficientOzone); // commonly called the extinction coefficient
|
|
|
|
#define sun_illuminance 128000.0 //[10000.0 20000.0 30000.0 40000.0 50000.0 60000.0 70000.0 80000.0 90000.0 100000.0 110000.0 120000.0 130000.0 140000.0 160000.0]
|
|
#define moon_illuminance 60.0 //[0.0 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0 1000.0 10000.0 100000.0]
|
|
|
|
#define sunColorR 1.0 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
#define sunColorG 0.9 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
#define sunColorB 0.81 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
|
|
#define sunColorBase (vec3(sunColorR,sunColorG,sunColorB) * sun_illuminance)
|
|
//#define sunColorBase blackbody(5778) * sun_illuminance
|
|
#define moonColorR 1.0 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
#define moonColorG 0.9 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
#define moonColorB 0.81 //[0.0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99 1.0 ]
|
|
|
|
#define moonColorBase (vec3(moonColorR, moonColorG, moonColorB) * moon_illuminance ) //Fake Purkinje effect
|
|
|
|
float sky_rayleighPhase(float cosTheta) {
|
|
const vec2 mul_add = vec2(0.1, 0.28) * rPI;
|
|
return cosTheta * mul_add.x + mul_add.y; // optimized version from [Elek09], divided by 4 pi for energy conservation
|
|
}
|
|
|
|
float sky_miePhase(float cosTheta, const float g) {
|
|
float gg = g * g;
|
|
return (gg * -0.25 + 0.25) * rPI * pow(-(2.0 * g) * cosTheta + (gg + 1.0), -1.5);
|
|
}
|
|
|
|
vec2 sky_phase(float cosTheta, const float g) {
|
|
return vec2(sky_rayleighPhase(cosTheta), sky_miePhase(cosTheta, g));
|
|
}
|
|
|
|
vec3 sky_density(float centerDistance) {
|
|
vec2 rayleighMie = exp(centerDistance * -sky_inverseScaleHeights + sky_scaledPlanetRadius);
|
|
|
|
// Ozone distribution curve by Sergeant Sarcasm - https://www.desmos.com/calculator/j0wozszdwa
|
|
float ozone = exp(-max(0.0, (35000.0 - centerDistance) - sky_planetRadius) * (1.0 / 5000.0))
|
|
* exp(-max(0.0, (centerDistance - 35000.0) - sky_planetRadius) * (1.0 / 15000.0));
|
|
return vec3(rayleighMie, ozone);
|
|
}
|
|
|
|
vec3 sky_airmass(vec3 position, vec3 direction, float rayLength, const float steps) {
|
|
float stepSize = rayLength * (1.0 / steps);
|
|
vec3 increment = direction * stepSize;
|
|
position += increment * 0.5;
|
|
|
|
vec3 airmass = vec3(0.0);
|
|
for (int i = 0; i < steps; ++i, position += increment) {
|
|
airmass += sky_density(length(position));
|
|
}
|
|
|
|
return airmass * stepSize;
|
|
}
|
|
vec3 sky_airmass(vec3 position, vec3 direction, const float steps) {
|
|
float rayLength = dot(position, direction);
|
|
rayLength = rayLength * rayLength + sky_atmosphereRadiusSquared - dot(position, position);
|
|
if (rayLength < 0.0) return vec3(0.0);
|
|
rayLength = sqrt(rayLength) - dot(position, direction);
|
|
|
|
return sky_airmass(position, direction, rayLength, steps);
|
|
}
|
|
|
|
vec3 sky_opticalDepth(vec3 position, vec3 direction, float rayLength, const float steps) {
|
|
return sky_coefficientsAttenuation * sky_airmass(position, direction, rayLength, steps);
|
|
}
|
|
vec3 sky_opticalDepth(vec3 position, vec3 direction, const float steps) {
|
|
return sky_coefficientsAttenuation * sky_airmass(position, direction, steps);
|
|
}
|
|
|
|
vec3 sky_transmittance(vec3 position, vec3 direction, const float steps) {
|
|
return exp2(-sky_opticalDepth(position, direction, steps) * rLOG2);
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
vec3 viewPosition = (sky_planetRadius + eyeAltitude) * upVector;
|
|
|
|
vec2 aid = rsi(viewPosition, viewVector, sky_atmosphereRadius);
|
|
if (aid.y < 0.0) {transmittance = vec3(1.0); return vec3(0.0);}
|
|
|
|
pid = rsi(viewPosition, viewVector, sky_planetRadius * 0.998);
|
|
bool planetIntersected = pid.y >= 0.0;
|
|
|
|
vec2 sd = vec2((planetIntersected && pid.x < 0.0) ? pid.y : max(aid.x, 0.0), (planetIntersected && pid.x > 0.0) ? pid.x : aid.y);
|
|
|
|
float stepSize = (sd.y - sd.x) * (1.0 / iSteps);
|
|
vec3 increment = viewVector * stepSize;
|
|
vec3 position = viewVector * sd.x + viewPosition;
|
|
position += increment * (0.34*noise);
|
|
vec2 phaseSun = sky_phase(dot(viewVector, sunVector ), sky_mieg);
|
|
vec2 phaseMoon = sky_phase(dot(viewVector, moonVector), sky_mieg);
|
|
|
|
vec3 scatteringSun = vec3(0.0);
|
|
vec3 scatteringMoon = vec3(0.0);
|
|
vec3 scatteringAmbient = vec3(0.0);
|
|
transmittance = vec3(1.0);
|
|
|
|
for (int i = 0; i < iSteps; ++i, position += increment) {
|
|
vec3 density = sky_density(length(position));
|
|
if (density.y > 1e35) break;
|
|
vec3 stepAirmass = density * stepSize;
|
|
vec3 stepOpticalDepth = sky_coefficientsAttenuation * stepAirmass;
|
|
|
|
vec3 stepTransmittance = exp2(-stepOpticalDepth * rLOG2);
|
|
vec3 stepTransmittedFraction = clamp01((stepTransmittance - 1.0) / -stepOpticalDepth);
|
|
vec3 stepScatteringVisible = transmittance * stepTransmittedFraction;
|
|
|
|
scatteringSun += sky_coefficientsScattering * (stepAirmass.xy * phaseSun ) * stepScatteringVisible * sky_transmittance(position, sunVector, jSteps);
|
|
scatteringMoon += sky_coefficientsScattering * (stepAirmass.xy * phaseMoon) * stepScatteringVisible * sky_transmittance(position, moonVector, jSteps);
|
|
// Nice way to fake multiple scattering.
|
|
scatteringAmbient += sky_coefficientsScattering * stepAirmass.xy * stepScatteringVisible;
|
|
|
|
transmittance *= stepTransmittance;
|
|
}
|
|
|
|
vec3 scattering = scatteringSun * sunColorBase + scatteringAmbient * background + scatteringMoon*moonColorBase;
|
|
|
|
return scattering;
|
|
}
|