{
 "id": "rayleigh-mie-single-scattering",
 "title": "Ray-marched Rayleigh/Mie Single Scattering",
 "aliases": [
  "Nishita sky",
  "analytic sky ray march"
 ],
 "domain": "volumes",
 "subdomain": "atmosphere",
 "stage": "volume",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu"
 ],
 "year_introduced": 1993,
 "inputs": [
  "material-params"
 ],
 "outputs": [
  "radiance",
  "hdr-image"
 ],
 "style": "photoreal",
 "dynamic": "dynamic",
 "summary": "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.",
 "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_use": "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).\n\nPrefer `hosek-wilkie-sky` or `preetham-sky` for a per-pixel-free analytic sky, `precomputed-atmospheric-scattering` or `hillaire-sky-atmosphere` when you need multiple scattering and aerial perspective at lower cost per pixel.",
 "cost": {
  "class": "medium",
  "notes": "Cost grows as view samples times light samples per pixel; render to a low-res dome or cubemap to bring it down."
 },
 "failure_modes": [
  "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1145/166117.166140",
   "type": "paper",
   "title": "Display of the earth taking into account atmospheric scattering",
   "authors": [
    "Tomoyuki Nishita",
    "Takao Sirai",
    "Katsumi Tadamura",
    "Eihachiro Nakamae"
   ],
   "year": 1993,
   "venue": "Proceedings of the 20th annual conference on Computer graphics and interactive techniques",
   "url": "https://doi.org/10.1145/166117.166140",
   "ids": {
    "doi": "10.1145/166117.166140"
   },
   "license": null,
   "extra": {
    "csl_type": "proceedings-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166140",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166140",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166140",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166140",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1145/166117.166140",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "nishita1993display"
  },
  {
   "role": "implementation",
   "key": "gh:wwwtyro/glsl-atmosphere",
   "type": "code",
   "title": "wwwtyro/glsl-atmosphere",
   "description": "Renders sky colors with Rayleigh and Mie scattering.",
   "authors": [
    "wwwtyro"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/wwwtyro/glsl-atmosphere",
   "ids": {
    "repo": "wwwtyro/glsl-atmosphere"
   },
   "license": "Unlicense",
   "extra": {
    "stars": 634,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2020-05-20T12:41:08Z",
    "language": "GLSL",
    "homepage": null,
    "license_name": "The Unlicense"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/wwwtyro/glsl-atmosphere",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "wwwtyro2016wwwtyro"
  },
  {
   "role": "tutorial",
   "key": "url:https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html",
   "type": "tutorial",
   "title": "Simulating the Colors of the Sky",
   "authors": [
    "Scratchapixel"
   ],
   "year": null,
   "venue": "scratchapixel.com",
   "url": "https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html",
   "ids": {},
   "license": null,
   "extra": {
    "http_status": 200,
    "live": true,
    "archive": {
     "url": "http://web.archive.org/web/20260712001022/https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html",
     "timestamp": "20260712001022"
    },
    "page_title": "Simulating the Colors of the Sky",
    "host": "scratchapixel.com",
    "title_confirmed_by_page": true,
    "title_unconfirmed": false
   },
   "provenance": {
    "title": {
     "authority": "http",
     "endpoint": "GET https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "http",
     "endpoint": "GET https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "http",
     "endpoint": "GET https://www.scratchapixel.com/lessons/procedural-generation-virtual-worlds/simulating-sky/simulating-colors-of-the-sky.html → 200",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "research-draft",
     "endpoint": "title_hint/author_hint, checked against page title where available",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "scratchapixelsimulating"
  }
 ],
 "presets": [
  {
   "id": "rayleigh-mie-sky-sunset",
   "title": "Rayleigh/Mie sky, low sun, 16x6 samples",
   "runtime": "glsl-fragment",
   "params": {
    "R_PLANET": 6371000,
    "R_ATMO": 6471000,
    "H_R": 8000,
    "H_M": 1200,
    "G": 0.76,
    "N_VIEW": 16,
    "N_LIGHT": 6
   },
   "code": "// GLSL ES 3.0, Shadertoy-style. Single-scattering Rayleigh + Mie sky from the ground.\nconst float R_PLANET = 6371e3;\nconst float R_ATMO = 6471e3;\nconst vec3 BETA_R = vec3(5.8e-6, 13.5e-6, 33.1e-6);\nconst float BETA_M = 21e-6;\nconst float H_R = 8e3;\nconst float H_M = 1.2e3;\nconst float G = 0.76;\nconst float SUN_POWER = 22.0;\nconst int N_VIEW = 16;\nconst int N_LIGHT = 6;\n\nvec2 raySphere(vec3 o, vec3 d, float r) {\n  float b = dot(o, d);\n  float h = b * b - (dot(o, o) - r * r);\n  if (h < 0.0) return vec2(1e9, -1e9);\n  h = sqrt(h);\n  return vec2(-b - h, -b + h);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  vec3 ro = vec3(0.0, R_PLANET + 2.0, 0.0);\n  vec3 rd = normalize(vec3(uv.x, uv.y + 0.18, -1.0));\n  float a = 0.10 + 0.06 * sin(iTime * 0.4);\n  vec3 sun = normalize(vec3(0.25, sin(a), -cos(a)));\n\n  float tmax = raySphere(ro, rd, R_ATMO).y;\n  vec2 pl = raySphere(ro, rd, R_PLANET);\n  if (pl.x > 0.0) tmax = min(tmax, pl.x);\n\n  float mu = dot(rd, sun);\n  float phaseR = 3.0 / (16.0 * 3.14159265) * (1.0 + mu * mu);\n  float g2 = G * G;\n  float phaseM = 3.0 / (8.0 * 3.14159265) * ((1.0 - g2) * (1.0 + mu * mu))\n               / ((2.0 + g2) * pow(1.0 + g2 - 2.0 * G * mu, 1.5));\n\n  float ds = tmax / float(N_VIEW);\n  float odR = 0.0, odM = 0.0;\n  vec3 sumR = vec3(0.0), sumM = vec3(0.0);\n  for (int i = 0; i < N_VIEW; i++) {\n    vec3 p = ro + rd * (ds * (float(i) + 0.5));\n    float h = length(p) - R_PLANET;\n    float dR = exp(-h / H_R) * ds;\n    float dM = exp(-h / H_M) * ds;\n    odR += dR; odM += dM;\n    float tl = raySphere(p, sun, R_ATMO).y;\n    float dl = tl / float(N_LIGHT);\n    float lR = 0.0, lM = 0.0;\n    for (int j = 0; j < N_LIGHT; j++) {\n      vec3 q = p + sun * (dl * (float(j) + 0.5));\n      float hq = length(q) - R_PLANET;\n      lR += exp(-hq / H_R) * dl;\n      lM += exp(-hq / H_M) * dl;\n    }\n    vec3 tau = BETA_R * (odR + lR) + vec3(BETA_M * 1.1) * (odM + lM);\n    vec3 att = exp(-tau);\n    sumR += att * dR;\n    sumM += att * dM;\n  }\n  vec3 col = SUN_POWER * (sumR * BETA_R * phaseR + sumM * BETA_M * phaseM);\n  if (pl.x > 0.0) col += vec3(0.02, 0.025, 0.02);\n  col = 1.0 - exp(-col * 0.7);\n  col = pow(col, vec3(1.0 / 2.2));\n  fragColor = vec4(col, 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index; scattering coefficients are the commonly quoted Earth values used in Rayleigh/Mie tutorials",
   "execution_fidelity": "faithful-pass",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "rayleigh-mie-single-scattering",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5055,
      "std": 0.2629,
      "unique_colors": 363
     },
     "image": "/specimens/rayleigh-mie-sky-sunset.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "precomputed-atmospheric-scattering",
  "hillaire-sky-atmosphere",
  "henyey-greenstein-phase-function",
  "preetham-sky",
  "hosek-wilkie-sky"
 ],
 "extends": [
  "henyey-greenstein-phase-function",
  "volume-ray-marching"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "henyey-greenstein-phase-function",
   "why": "Supplies the anisotropic Mie phase term evaluated at the view-sun angle."
  },
  {
   "id": "volumetric-light-scattering-god-rays",
   "why": "Sky color gives the sun color and intensity used by the screen-space ray accumulation."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/volumes/rayleigh-mie-single-scattering.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Nishita et al., 1993",
 "url": "https://texelatlas.vercel.app/t/rayleigh-mie-single-scattering/",
 "execution_fidelity": "faithful-pass",
 "fidelity_label": "Faithful Pass",
 "benchmark_eligible": true,
 "gpu_weight": {
  "weight_x": 42.39,
  "tier": "heavy",
  "p50_ms": 1.3947,
  "p95_ms": 1.4573,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "rayleigh-mie-sky-sunset",
  "execution_fidelity": "faithful-pass",
  "fidelity_label": "Faithful Pass",
  "benchmark_eligible": true,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 40.06,
   "tier": "heavy",
   "p50_ms": 0.637,
   "p95_ms": 0.64,
   "speedup_x": 2.19,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 26.17,
   "tier": "heavy",
   "p50_ms": 0.7537,
   "p95_ms": 0.7542,
   "speedup_x": 1.85,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}