Ray-marched Rayleigh/Mie Single Scattering
Integrate sunlight scattered once toward the eye along the view ray through an exponential-density atmosphere, with an inner loop for sun transmittance; the textbook way to get blue skies and red sunsets in a shader.
Also known as Nishita sky, analytic sky ray march
In the family Atmosphere, water & volumes, atmosphere
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | |
| Babylon.js 9.38.0 | Official add-on | @babylonjs/materials SkyMaterial (rayleigh, mieCoefficient, mieDirectionalG) docs |
| Godot 4.7 | Built in | Sky.sky_material = PhysicalSkyMaterial (rayleigh_coefficient, mie_coefficient) docs Analytic sky shader with Rayleigh and Mie terms. |
| Unity | Built in | HDRP Volume > Add Override > Sky > Physically Based Sky (PhysicallyBasedSkyCommon.hlsl evaluates Rayleigh air and Mie aerosol scattering) docs HDRP only. |
| Unreal 5.7 | Built in | Sky Atmosphere component > Rayleigh Scattering / Mie Scattering settings docs |
| Blender 5.2 | Built in | Sky Texture node > Sky Type = Single Scattering docs Single Scattering is legacy; Multiple Scattering is the default. |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
Intersect the view ray with the atmosphere shell, split it into N samples and at each sample accumulate the local Rayleigh and Mie densities, which decay exponentially with altitude using separate scale heights. A second short march from the sample toward the sun gives the optical depth to the light; transmittance is exp of minus the sum of beta times optical depth over both legs. Contribution at each sample is that transmittance times density, and the final color is multiplied by the sun power and by the Rayleigh and Henyey-Greenstein phase functions evaluated at the angle to the sun. Rayleigh coefficients scale roughly with inverse wavelength to the fourth power, which makes the blue sky and red dusk. Higher scattering orders are ignored, so a constant ambient floor is often added.
When to choose it
Reach for it when
- need a blue sky and red sunset in a shader
- cheap atmosphere without precomputed tables
- haze and sun glow around the sun in a scene
- procedural sky that responds to sun position
Choose it for a compact sky shader with no tables, when you need sunset colors, and when you can afford roughly a hundred texture-less samples per pixel (or render to a small sky-dome texture once per frame).
Prefer Hosek-Wilkie Sky Model or Preetham Analytic Daylight Model for a per-pixel-free analytic sky, Precomputed Atmospheric Scattering or Scalable Production Sky and Atmosphere (Hillaire) when you need multiple scattering and aerial perspective at lower cost per pixel.
Field marks
- Single scattering only: the sky around the horizon after sunset goes unrealistically black or too saturated
- Banding or hard rings with too few view samples when the ray grazes the horizon
- Sun below the horizon without a planet-shadow test lights the ground incorrectly
- Mie asymmetry g near 0.99 creates aliasing at the sun disc without more samples
- Looking down onto ground needs a separate ground color; the integral just stops at the planet
Presets
Rayleigh/Mie sky, low sun, 16x6 samples
{
"R_PLANET": 6371000,
"R_ATMO": 6471000,
"H_R": 8000,
"H_M": 1200,
"G": 0.76,
"N_VIEW": 16,
"N_LIGHT": 6
}Show GLSL (67 lines)
// GLSL ES 3.0, Shadertoy-style. Single-scattering Rayleigh + Mie sky from the ground.
const float R_PLANET = 6371e3;
const float R_ATMO = 6471e3;
const vec3 BETA_R = vec3(5.8e-6, 13.5e-6, 33.1e-6);
const float BETA_M = 21e-6;
const float H_R = 8e3;
const float H_M = 1.2e3;
const float G = 0.76;
const float SUN_POWER = 22.0;
const int N_VIEW = 16;
const int N_LIGHT = 6;
vec2 raySphere(vec3 o, vec3 d, float r) {
float b = dot(o, d);
float h = b * b - (dot(o, o) - r * r);
if (h < 0.0) return vec2(1e9, -1e9);
h = sqrt(h);
return vec2(-b - h, -b + h);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec3 ro = vec3(0.0, R_PLANET + 2.0, 0.0);
vec3 rd = normalize(vec3(uv.x, uv.y + 0.18, -1.0));
float a = 0.10 + 0.06 * sin(iTime * 0.4);
vec3 sun = normalize(vec3(0.25, sin(a), -cos(a)));
float tmax = raySphere(ro, rd, R_ATMO).y;
vec2 pl = raySphere(ro, rd, R_PLANET);
if (pl.x > 0.0) tmax = min(tmax, pl.x);
float mu = dot(rd, sun);
float phaseR = 3.0 / (16.0 * 3.14159265) * (1.0 + mu * mu);
float g2 = G * G;
float phaseM = 3.0 / (8.0 * 3.14159265) * ((1.0 - g2) * (1.0 + mu * mu))
/ ((2.0 + g2) * pow(1.0 + g2 - 2.0 * G * mu, 1.5));
float ds = tmax / float(N_VIEW);
float odR = 0.0, odM = 0.0;
vec3 sumR = vec3(0.0), sumM = vec3(0.0);
for (int i = 0; i < N_VIEW; i++) {
vec3 p = ro + rd * (ds * (float(i) + 0.5));
float h = length(p) - R_PLANET;
float dR = exp(-h / H_R) * ds;
float dM = exp(-h / H_M) * ds;
odR += dR; odM += dM;
float tl = raySphere(p, sun, R_ATMO).y;
float dl = tl / float(N_LIGHT);
float lR = 0.0, lM = 0.0;
for (int j = 0; j < N_LIGHT; j++) {
vec3 q = p + sun * (dl * (float(j) + 0.5));
float hq = length(q) - R_PLANET;
lR += exp(-hq / H_R) * dl;
lM += exp(-hq / H_M) * dl;
}
vec3 tau = BETA_R * (odR + lR) + vec3(BETA_M * 1.1) * (odM + lM);
vec3 att = exp(-tau);
sumR += att * dR;
sumM += att * dM;
}
vec3 col = SUN_POWER * (sumR * BETA_R * phaseR + sumM * BETA_M * phaseM);
if (pl.x > 0.0) col += vec3(0.02, 0.025, 0.02);
col = 1.0 - exp(-col * 0.7);
col = pow(col, vec3(1.0 / 2.2));
fragColor = vec4(col, 1.0);
}
Attribution: written for this index; scattering coefficients are the commonly quoted Earth values used in Rayleigh/Mie tutorials
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| rayleigh-mie-sky-sunset | Tesla T4 1920x1080 | 42.4× baseline (heavy) · 1.39 ms / 1.46 ms | opengl-timer-query · 40 frames · GPU Weight Atlas · Colab T4 sweep |
| rayleigh-mie-sky-sunset | NVIDIA L4 1920x1080 | 40.1× baseline (heavy) · 0.637 ms / 0.640 ms · 2.2× faster on L4 | opengl-timer-query · Cross-architecture validation |
| rayleigh-mie-sky-sunset | NVIDIA A100-SXM4-40GB 1920x1080 | 26.2× baseline (heavy) · 0.754 ms / 0.754 ms · 1.9× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
From the citation graph (Crossref reference lists): cited by Scalable Production Sky and Atmosphere (Hillaire), Hosek-Wilkie Sky Model, Infinigen (Procedural Photorealistic Worlds).
Crosses
- Henyey-Greenstein Phase Function: Supplies the anisotropic Mie phase term evaluated at the view-sun angle.
- Screen-space Volumetric Light Scattering (God Rays): Sky color gives the sun color and intensity used by the screen-space ray accumulation.
Sources
- primaryTomoyuki Nishita, Takao Sirai, Katsumi Tadamura, Eihachiro Nakamae (1993). Display of the earth taking into account atmospheric scattering. Proceedings of the 20th annual conference on Computer graphics and interactive techniques.Verified via crossref · 2026-11-29doi:10.1145/166117.166140
- implementationwwwtyro (2016). wwwtyro/glsl-atmosphere. Renders sky colors with Rayleigh and Mie scattering.GitHub API · license Unlicense · 2026-11-29
- tutorialScratchapixel. Simulating the Colors of the Sky. scratchapixel.com.Verified via live page · 2026-11-29