2023-09-11 11:27:24 +00:00
|
|
|
uniform vec3 uSunPos;
|
|
|
|
uniform vec3 uRayOrigin;
|
|
|
|
uniform float uSunIntensity;
|
|
|
|
uniform float uPlanetRadius;
|
|
|
|
uniform float uAtmosphereRadius;
|
|
|
|
uniform vec3 uRayleighScattering;
|
|
|
|
uniform float uMieScattering;
|
|
|
|
uniform float uRayleighScaleHeight;
|
|
|
|
uniform float uMieScaleHeight;
|
|
|
|
uniform float uMiePreferredDirection;
|
2023-08-10 21:53:51 +00:00
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
in vec3 v_direction;
|
|
|
|
out vec4 fragcolor;
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
void main() {
|
|
|
|
vec3 color = atmosphere(
|
2023-09-11 11:27:24 +00:00
|
|
|
normalize(v_direction), // normalized ray direction
|
|
|
|
uRayOrigin, // ray origin
|
|
|
|
uSunPos, // position of the sun
|
|
|
|
uSunIntensity, // intensity of the sun
|
|
|
|
uPlanetRadius, // radius of the planet in meters
|
|
|
|
uAtmosphereRadius, // radius of the atmosphere in meters
|
|
|
|
uRayleighScattering, // Rayleigh scattering coefficient
|
|
|
|
uMieScattering, // Mie scattering coefficient
|
|
|
|
uRayleighScaleHeight, // Rayleigh scale height
|
|
|
|
uMieScaleHeight, // Mie scale height
|
|
|
|
uMiePreferredDirection // Mie preferred scattering direction
|
2023-08-10 21:53:51 +00:00
|
|
|
);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Apply exposure.
|
2023-08-10 21:53:51 +00:00
|
|
|
color = 1.0 - exp(-1.0 * color);
|
|
|
|
|
|
|
|
fragcolor = vec4(color, 1);
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// [src] https://github.com/wwwtyro/glsl-atmosphere by wwwtyro (Unlicensed)
|
|
|
|
// For more information, please refer to <http://unlicense.org>
|
2023-08-10 21:53:51 +00:00
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
#define PI 3.141592
|
|
|
|
#define iSteps 16
|
|
|
|
#define jSteps 8
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
vec2 rsi(vec3 r0, vec3 rd, float sr) {
|
2023-08-10 22:46:04 +00:00
|
|
|
// ray-sphere intersection that assumes
|
|
|
|
// the sphere is centered at the origin.
|
|
|
|
// No intersection when result.x > result.y
|
2023-08-10 21:53:51 +00:00
|
|
|
float a = dot(rd, rd);
|
|
|
|
float b = 2.0 * dot(rd, r0);
|
|
|
|
float c = dot(r0, r0) - (sr * sr);
|
|
|
|
float d = (b*b) - 4.0*a*c;
|
|
|
|
if (d < 0.0) return vec2(1e5,-1e5);
|
|
|
|
return vec2(
|
|
|
|
(-b - sqrt(d))/(2.0*a),
|
|
|
|
(-b + sqrt(d))/(2.0*a)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-10 14:46:07 +00:00
|
|
|
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
|
2023-08-10 22:46:04 +00:00
|
|
|
// Normalize the sun and view directions.
|
2023-08-10 21:53:51 +00:00
|
|
|
pSun = normalize(pSun);
|
|
|
|
r = normalize(r);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the step size of the primary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
vec2 p = rsi(r0, r, rAtmos);
|
|
|
|
if (p.x > p.y) return vec3(0,0,0);
|
|
|
|
p.y = min(p.y, rsi(r0, r, rPlanet).x);
|
|
|
|
float iStepSize = (p.y - p.x) / float(iSteps);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Initialize the primary ray time.
|
2023-08-10 21:53:51 +00:00
|
|
|
float iTime = 0.0;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Initialize accumulators for Rayleigh and Mie scattering.
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 totalRlh = vec3(0,0,0);
|
|
|
|
vec3 totalMie = vec3(0,0,0);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Initialize optical depth accumulators for the primary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
float iOdRlh = 0.0;
|
|
|
|
float iOdMie = 0.0;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the Rayleigh and Mie phases.
|
2023-08-10 21:53:51 +00:00
|
|
|
float mu = dot(r, pSun);
|
|
|
|
float mumu = mu * mu;
|
|
|
|
float gg = g * g;
|
|
|
|
float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
|
|
|
|
float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Sample the primary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
for (int i = 0; i < iSteps; i++) {
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the primary ray sample position.
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the height of the sample.
|
2023-08-10 21:53:51 +00:00
|
|
|
float iHeight = length(iPos) - rPlanet;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
|
2023-08-10 21:53:51 +00:00
|
|
|
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
|
|
|
|
float odStepMie = exp(-iHeight / shMie) * iStepSize;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Accumulate optical depth.
|
2023-08-10 21:53:51 +00:00
|
|
|
iOdRlh += odStepRlh;
|
|
|
|
iOdMie += odStepMie;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the step size of the secondary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Initialize the secondary ray time.
|
2023-08-10 21:53:51 +00:00
|
|
|
float jTime = 0.0;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Initialize optical depth accumulators for the secondary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
float jOdRlh = 0.0;
|
|
|
|
float jOdMie = 0.0;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Sample the secondary ray.
|
2023-08-10 21:53:51 +00:00
|
|
|
for (int j = 0; j < jSteps; j++) {
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the secondary ray sample position.
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate the height of the sample.
|
2023-08-10 21:53:51 +00:00
|
|
|
float jHeight = length(jPos) - rPlanet;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Accumulate the optical depth.
|
2023-08-10 21:53:51 +00:00
|
|
|
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
|
|
|
|
jOdMie += exp(-jHeight / shMie) * jStepSize;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Increment the secondary ray time.
|
2023-08-10 21:53:51 +00:00
|
|
|
jTime += jStepSize;
|
|
|
|
}
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate attenuation.
|
2023-08-10 21:53:51 +00:00
|
|
|
vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Accumulate scattering.
|
2023-08-10 21:53:51 +00:00
|
|
|
totalRlh += odStepRlh * attn;
|
|
|
|
totalMie += odStepMie * attn;
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Increment the primary ray time.
|
2023-08-10 21:53:51 +00:00
|
|
|
iTime += iStepSize;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-10 22:46:04 +00:00
|
|
|
// Calculate and return the final color.
|
2023-08-10 21:53:51 +00:00
|
|
|
return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
|
|
|
|
}
|