This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi! :) Can anybody recommend a nice sky shader tutorial with source code? Best would be an example with GLSL shaders ... Greets!

##### Share on other sites
I can't provide a tutorial, but I can describe the workings of a basic sky shader. I'll work in the HSV colour-space because the setting calls for it:

You have, more or less, two stages to worry about.
The first stage is to determine colour of the sky without a sun. The 'ambient' lightness of the sky will be determined by the time of day and the diffuse lightness by the viewing angle. Notice that the darkest point of the sky is usually directly up (the exception being when the sun is nearby). The lightness of the sky, then, can be calculated using
v = scaled_dimensionless_time_of_day * (1 - dot(view, float3(0, 1, 0)));
where 'view' is the view-vector, and 'y is up'. I'd recommend you clamp this value to be positive.
Then, you have the hue to consider. Directly up is more-or-less constantly true-blue. The colour at the horizon depends on the time of day, and it goes roughly, depening on the air quality & humidity, through the rainbow from blue at midday to red at sunrise/sunset. The intermediate hues interpolate between the two. So to calculate a hue value, you could use:
h = dot(view, float3(0, 1, 0)) * 2/3;
This will be a linear rainbow with the cos of the angle, so you'll likely want to raise the dot-product to some power (say 2-16) to bias the 'blueness' so it doesn't look like an everlasting sunset. So something like:
h = (1 - pow(1 - dot(view, float3(0, 1, 0)), 8)) * 2/3;
This looks like it could be optimised somewhat.
How you caluclate the saturation is up to you, but I'd say that the clearer the day, the higher the saturation. You'll also want to tune it according to the rest of your world. It may well change throughout the course of the day, but fixing
s = 0.7
will probably look fine.

The second consideration is positioning of the sun. The closer the viewing angle matches with the sun's direction, the lighter the sky will be at that point:
sun = dot(view, sun_direction)
This will also want to be exponentiated, but less so than the hue (perhaps a factor of 2-4), and clamped to [0,1].

These two colour values will need to be added (after converting HSV to RGB, if you use the formulas above) and clamped again.
This covers the basics - there are many ways to expand on this. I recommend you keep the cloud rendering separate.

Bear in mind that I pulled the theory and forumlas from the top of my head, so my values (or entire train of though) may be off. I'd recommend looking at a tutorial or two to get an idea of potential pitfalls that I won't have forseen before plunging yourself into a coding frenzy.

Regards

##### Share on other sites
I always through up a link to this when people ask this. And people ask this alot; try searching the forums. Or google.

##### Share on other sites
Hi!

@TheAdmiral: Thanks a lot! Was a good starting point for me. Together with the link that Funkymunky provided I should be able to code a nice sky. Thanks again. :)

##### Share on other sites
Hi!

@TheAdmiral: Thanks a lot! Was a good starting point for me. Together with the link that Funkymunky provided I should be able to code a nice sky. Thanks again. :)

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 9
• 22
• 9
• 31
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007482
• ### Who's Online (See full list)

There are no registered users currently online

×