<p>I believe that Space Engine as a base algorithm still uses Precomputed Atmospheric Scattering by E. Bruneton. It is released on <a href="https://github.com/ebruneton/precomputed_atmospheric_scattering" rel="nofollow noreferrer">github</a>. So it is good starting point if you want to implement atmospheric scattering.</p>

<p>For beginner this algorithm is pretty advanced stuff written in C++11, and modern OpenGL, so such knowledge is required. In short, to avoid a cost of solving rendering equation for scattering in participating medium (single as well as multiple scattering) it uses precomputed lookup textures where it is possible and equations for Rayleigh and Mie scattering based on physical model.</p>

<p>There is also <a href="https://www.shadertoy.com/view/lslXDr" rel="nofollow noreferrer">simpler demo</a> computed in real time on shadertoy. This is probably better place to start. It involves raycasting a sphere and integrating Rayleigh and Mie scattering along a ray in real time. It is based on <a href="https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html" rel="nofollow noreferrer">GPU Gems article</a>, which provides some good information how it works.</p>

CC BY-SA 4.0
2019-10-13T12:47:16.817Z
2019-10-13T13:24:35.587Z
2019-10-13T13:24:35.587Z
mdkdy
<p>I'm working on a game set in space with procedurally generated planets, and I'm trying to write a shader that puts an atmosphere over a planet given the color of the atmosphere and the strength of the atmosphere.</p>

<p>It looks ok at low strengths:</p>

<p><a href="https://i.sstatic.net/Ndp1Y.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Ndp1Y.png" alt="enter image description here"></a></p>

<p>But at high strengths the planet looks really bright:</p>

<p><a href="https://i.sstatic.net/GomKM.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/GomKM.png" alt="enter image description here"></a></p>

<p>And neither of them look realistic. For example in space engine the atmosphere really affects every part of the planet including the oceans without necessarily looking very bright:</p>

<p><a href="https://i.sstatic.net/5FBOW.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/5FBOW.jpg" alt="enter image description here"></a></p>

<p>Here's the relevant part of the shader code I'm using (in glsl):</p>

<pre><code>//Final atmosphere color to use
vec4 atmo_pixel_color;

if(r &gt; radius_percent)
{
    //This is in space, cut off the color quickly
    float atmo_alpha = clamp(0.5 - pow((r - radius_percent), 2) * 600, 0, 1);
    atmo_pixel_color = atmo_color * atmo_strength;
    atmo_pixel_color.w = atmo_alpha;
}
else
{
    //Atmosphere is thicker near the edges of the planet
    float thickness = pow((r / radius_percent), 2) * 0.1f;
    atmo_pixel_color = atmo_color * (atmo_strength + thickness);
}

gl_FragColor = planet_color + atmo_pixel_color;
</code></pre>

<p>r is the distance from the center of the planet. radius_percent is the percentage of the texture that is in the planet (above that is empty space). planet_color is the final base color of the planet itself, or empty black if the pixel is in space.</p>

<p>I have very little experience doing graphics programming and I didn't really know where to start to research this.</p>

<p>What kind of algorithm should I be using here to make the atmosphere look correct?</p>

<glsl><color><2d>
Shader code for putting an atmosphere over a planet
Earlh21
# Shader code for putting an atmosphere over a planet

(Asked by: username_0 on 2019-10-10)

I'm working on a game set in space with procedurally generated planets, and I'm trying to write a shader that puts an atmosphere over a planet given the color of the atmosphere and the strength of the atmosphere.

It looks ok at low strengths:

[![enter image description here](https://i.sstatic.net/Ndp1Y.png)](https://i.sstatic.net/Ndp1Y.png)

But at high strengths the planet looks really bright:

[![enter image description here](https://i.sstatic.net/GomKM.png)](https://i.sstatic.net/GomKM.png)

And neither of them look realistic. For example in space engine the atmosphere really affects every part of the planet including the oceans without necessarily looking very bright:

[![enter image description here](https://i.sstatic.net/5FBOW.jpg)](https://i.sstatic.net/5FBOW.jpg)

Here's the relevant part of the shader code I'm using (in glsl):

```
//Final atmosphere color to use
vec4 atmo_pixel_color;

if(r > radius_percent)
{
    //This is in space, cut off the color quickly
    float atmo_alpha = clamp(0.5 - pow((r - radius_percent), 2) * 600, 0, 1);
    atmo_pixel_color = atmo_color * atmo_strength;
    atmo_pixel_color.w = atmo_alpha;
}
else
{
    //Atmosphere is thicker near the edges of the planet
    float thickness = pow((r / radius_percent), 2) * 0.1f;
    atmo_pixel_color = atmo_color * (atmo_strength + thickness);
}

gl_FragColor = planet_color + atmo_pixel_color;

```

r is the distance from the center of the planet. radius\_percent is the percentage of the texture that is in the planet (above that is empty space). planet\_color is the final base color of the planet itself, or empty black if the pixel is in space.

I have very little experience doing graphics programming and I didn't really know where to start to research this.

What kind of algorithm should I be using here to make the atmosphere look correct?

**Tags:** glsl, color, 2d


---

## 1 answers

### Answer by username_1 on 2019-10-13. Score: 2

I believe that Space Engine as a base algorithm still uses Precomputed Atmospheric Scattering by E. Bruneton. It is released on [github](https://github.com/ebruneton/precomputed_atmospheric_scattering). So it is good starting point if you want to implement atmospheric scattering.

For beginner this algorithm is pretty advanced stuff written in C++11, and modern OpenGL, so such knowledge is required. In short, to avoid a cost of solving rendering equation for scattering in participating medium (single as well as multiple scattering) it uses precomputed lookup textures where it is possible and equations for Rayleigh and Mie scattering based on physical model.

There is also [simpler demo](https://www.shadertoy.com/view/lslXDr) computed in real time on shadertoy. This is probably better place to start. It involves raycasting a sphere and integrating Rayleigh and Mie scattering along a ray in real time. It is based on [GPU Gems article](https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html), which provides some good information how it works.


---

