The equation for a sunset
I’ve always been a big fan of sunsets.
I get some really nice ones most evenings in the summer from my current flat but with winter approaching I wanted to try and make something virtual and consistent.
One of my goals was that after I got my quest 2 to try and sit coding with a sunset in the background. There are a number of issues on the road to realising that dream in its entirety however I got a long way with watching the sunset over the ocean.
Bellow, I present a shader (Code dedicated to programing the shader section of the graphics card pipeline) for a sunset over ocean waves.
Ocean waves in deep water are much easier to generate than shallow-water waves as you can get away modelling them as not interacting with anything. That permits an ontogenetic style where you can skip to the future without calculating the intermediate steps. The surface is based on the sums of sin waves which is not true to how water actually moves as it moves in Gerstner waves. So I based the waves on the most realistic waves I could find. The surface colour of the water mainly comes from the reflection of the sky above but also from the algae in it. This gives it a blue-green colour. Blue from the sky and green from the algae. I mixed the colours based on the fresnel effect so the water reflects more when at a steep angle.
I had some issues when the bounced ray went into the water but I allowed these to be approximated by reversing the direction of the ray and making it slightly darker. To keep it running at a reasonable speed I needed to keep the number of bounces to a minimum.
I can't claim to have coded it from scratch I used a mix of other peoples shaders where I thought a particular cloud model was better or a particular way of rendering the atmospheric interactions with the suns light. The atmosphere is based on this shader. It isn't quite a physical realisation of Rayleigh scattering but it is based on the idea that higher wavelengths of light (bluer light) is scattered more. If you’ve ever wondered why the sky is blue and sunsets are red it’s down to Rayleigh scattering.
This scattering means that you can see the suns light indirectly as it bounces off the atmosphere.
The same effect causes the remaining light, the light that has managed to pass through the atmosphere unscattered to contain fewer colours with higher frequency such as the blues and greens. That leaves you with only reds and oranges, the classic colours of a sunset.
Although not as important there is another effect that helps to make the sky look more realistic. Its effect doesn’t matter as much as Rayleigh scattering, especially at sunset but when the sun is high in the sky there is a white ring around where the sun actually is. This is called Mie scattering.
One element that I'm most impressed with is the clouds. Not the cirrus clouds that I added myself but the cumulus ones that came from this shader.
They are a 3D Perline noise which a ray is then passed through in a really intelligent way. The quality of the effect is quite magical if expensive.
Comments
Post a Comment