Around The World, Part 31: Clear blue sky

1 Comment

Around The World will be played from a third-person chase camera, so most of the time, about half the screen will be filled with sky. How do we make that sky look good?

You may already seen some pretty skies in previous screenshots, but I wasn’t completely satisfied and decided to start over. I’ll do this in three phases:

  1. Make a clear sky that works up to and including sunset.
  2. Implement a night sky with stars.
  3. Add clouds.

This post is about the first step, clear skies. That may sound dull, but in fact even a clear sky has a surprising amount of colour variation, especially when the sun is low. I want those pretty sunsets!

Now if you’re into computer graphics at all, you may be aware that rendering realistic skies efficiently is a solved problem. So I won’t be creating anything ground-breaking today. I am however going to implement this from scratch, because it helps me understand the implementation and tweak it to my needs.

Also, it’s fun, dammit. I can barely go outside anymore without gazing at the sky, and wondering how I’m going to implement it. Especially clouds. Clouds! But I’m getting ahead of myself.

Godot’s built-in options

First off, the game will have dynamic weather and a day-night cycle, so anything like a static sky box is out of the question. It needs to update in real time.

Godot offers two built-in options for this: a fast one named ProceduralSkyMaterial and a slightly slower one named PhysicalSkyMaterial.

This is what ProceduralSkyMaterial looks like:

A bland, grayish blue sky with a fuzzy sun in the middle

That’s quite bland when the sun is high (here, 45°), and it doesn’t get better at sunset:

A bland, grayis blue sky with a fuzzy sun near the horizon

This implementation is just too simple; it’s basically a gradient towards the horizon, and a radial gradient around the sun. That’s not going to cut it.

How about its big brother, PhysicalSkyMaterial? It’s based on an analytical model of Earth’s atmosphere by Preetham et al. (1999), which means it’s somewhat realistic but above all still very efficient. Here’s what it looks like:

A blueish gray sky with a fuzzy sun in the middle, and some subtle reddening near the horizon

And at dusk:

A dark, almost black sky, with a reddening sun near the horizon

It’s better, but still not great. Tweaking the parameters (especially boosting the brightness) got me some slight improvements, but it still felt underwhelming.

QuickSky

My game’s art style uses low-poly models and coarse, pixelated textures; is not based on maximum realism. So we don’t necessarily need a very realistic sky. Artistic control is arguably more important. It needs to be pretty.

So I set out to do something like Godot’s ProceduralSkyMaterial: just slap some gradients and equations together until it looks good. I called this approach “QuickSky” and here’s how far I got with it:

A blue sky that fades towards pale cyan at the horizon, with a bright white sun in the middle

A dark blue sky that transitions through green and orange towards red at the horizon, with a yellow sun just above the horizon

I don’t think this is bad at all for how simple it is:

  • Start with a uniform sky-blue colour.
  • Towards the horizon, fade exponentially to a paler, light blue.
  • Draw the sun disk in white, with a white halo around it.
  • Pass the result through a Rayleigh outscattering function. This is just an orange colour raised to some power, with a higher exponent closer to the horizon. Tune down this effect the higher the sun is in the sky.

It’s yet another step forward, but I felt it had reached its limits. To make it look better (in all directions, under all conditions) I would need to add more tweaks, more parameters, all under artistic control. But in this case, I found that nature itself is the best artist: it does make pretty sunsets! So I would prefer to just do what it does. More realism after all!

SingleScatteringSky

The sky gets most of its colour from an effect called Rayleigh scattering. When sunlight travels through the atmosphere, it sometimes hits an air molecule and gets bounced (“scattered”) in a different direction. Shorter wavelengths get scattered more often than longer wavelengths, so blue light bounces around a lot more, whereas red more often travels straight on. This is why the sky is blue.

This is often implemented in shaders on the GPU by marching along the view ray and computing what happens at each point along the ray:

Scattering diagram

So that’s what I implemented: break up the view ray into a number of same-sized intervals, and compute how much direct sunlight would be scattered into the view ray along that segment. This is only “single scattering”: we assume that each photon is scattered no more than once until it hits something.

Even with such a simple approach, we already get a blue sky:

A blue sky that fades towards a slightly brighter blue at the horizon

It’s very bright because I have set the sunlight strength to 20.0, where 1.0 is full white. This is needed to get a good balance in brightness between the sun and the rest of the sky, otherwise the sky will be way too dark later.

Obviously, the real sky doesn’t look like this; there are some effects that I haven’t taken into account yet. First, before the sunlight reaches the view ray, it has already travelled through a part of the atmosphere, and so some of it will have scattered out in other directions. We can simulate that by marching another ray from each point along the view ray in the direction of the sun, tallying up how much atmosphere is encountered along the way, and applying attenuation accordingly.

This has a darkening effect, but mostly on the ground which is no longer lit from below:

A similar thing happens to light after it has been scattered into the view ray, while it travels along the view ray to the camera. Technically, this light has already been scattered once, and so we shouldn’t scatter it again, so I guess my implementation may not be energy-preserving, or something? But you can’t argue with results:

This approach is already enough to get reasonable sunsets:

All these ray marches take into account that the world and the atmosphere are round, so you automatically get the effect of a “planet shadow” where the sky is still dark opposite the sunrise.

But wait, there’s more!

Mie scattering

Besides molecules, the atmosphere also contains larger particles called aerosols, and these also cause scattering. This is called Mie scattering and it differs from Rayleigh scattering in some key ways:

  • It’s strongly directional, meaning that a scattered ray is much more likely to be scattered in a forwards direction than backwards. The physical processes are complex, but thankfully they can be approximated well by simple formulas like the Cornette-Shanks phase function.

  • It happens mostly in the lower part of the atmosphere, and barely above 5 km or so.

  • It doesn’t depend on wavelength, scattering all colours in the same way.

  • It also performs some absorption, meaning light just disappears.

The strong directionality of Mie scattering is what causes a bright halo around the sun; this is sunlight being scattered almost straight ahead. Its presence in the lower atmosphere is responsible for the colour of the horizon, which was still quite blown out when using just Rayleigh scattering.

Here’s the result of adding Mie scattering and absorption in all places where Rayleigh scattering is also being applied:

And the obligatory sunset:

Very nice! You can clearly see how Mie scattering causes the area around the sun to be bright, while Rayleigh outscattering colours it orange. Some of the colours are a bit oversaturated, but I think this can be fixed later with tone mapping (which is not enabled on these screenshots right now).

Ozone

In this classic paper by Nishita, Dobashi et al (1996), I found this interesting tidbit:

For visible wavelengths, absorption in the ozone layer is negligible compared to absorption by air molecules and aerosols.

However, ozone does strongly affect the sky colour during twilight, as pointed out by Peter Kutz and cited by a more modern classic, Hillaire (2020). So let’s follow Hillaire’s following of Bruneton and add absorption in the ozone layer:

Compared to the version without ozone, this gives the twilight sky a much more natural purple hue, rather than greenish. This makes sense given Hillaire’s coefficients: green is absorbed most strongly.

Final tweaks

I suggested previously that Rayleigh scattering (unlike Mie scattering) happens equally in all directions, but it turns out that’s only mostly true. Rather, it has a slight preference for the forwards and backwards directions. Let’s implement that. The effect is too subtle to show in screenshots, but it does help break up the uniform horizon a bit.

Also, I darkened the sun disk a bit around the edges, an effect called limb darkening. Without tone mapping, the effect is only visible just before the sun disappears, and even then it’s pretty subtle — the reddening at the bottom is due to atmospheric scattering:

Performance optimizations

So far, I’ve been showing static pictures, but how does this rendering technique fare in a real-time game? Actually, not bad at all! Remember that we’re doing a march along the view ray, and for each step in this march, we spawn a secondary ray march towards the sun. Even when setting the former to 64 steps and the latter to 8, causing 64 × 8 = 512 evaluations, my RX 7600 did not take more than a few milliseconds per frame (out of a budget of 16 ms to reach a smooth 60 frames per second).

However, there was some low hanging fruit to bring this number down further, and not needlessly heat up PCs or the planet:

  • Most of the action happens in the lower regions of the atmosphere. So it makes sense to focus our efforts there. By using smaller steps early on in the ray march, and exponentially larger ones higher up, the step counts could go down to 8 × 3 = 24 without notable loss in quality.

  • The atmosphere is generally considered to end at an altitude of 100 km, but Rayleigh scattering has a scale height of only 8 km, meaning at 8 km there’s only 1/e = 37% as much of it as at sea level, and at 24 km it’s less than 5%. So it makes sense to end our atmosphere earlier so we can focus even more samples where it matters. I chose a cutoff of 40 km, where the Rayleigh density is down to a mere 0.7%. This let me bring down the number of samples to 6 × 3 = 18.

  • The sky is pretty smooth, without sharp edges. Godot has built-in machinery to render it at ¼th resolution, then scale it up. Let’s use that. (This is not applied to the sun disk, which we want to remain sharp.)

  • For the Mie phase function, Cornette-Shanks turns out to be overkill. The slightly simpler and thus slightly faster Henyey-Greenstein function does the job just as well; the difference is barely visible.

It’s hard to even measure frame time after these changes, but I think it’s around 0.1 ms.

It would be possible to get rid of the secondary raymarches, by either:

  • Precomputing them in a lookup table (2D texture), as in Nishita, Dobashi et al (1996). The drawback is that the LUT needs to be updated whenever the parameters change (e.g. as a result of changing weather).

  • Finding an analytical approximation, as Sean O’Neill did in GPU Gems 2. However, his approximation is only valid for one particular set of parameters as well, and I don’t think they match mine.

Since the secondary raycast is only doing 3 iterations and we’re already running so quickly, I didn’t bother trying either of these.

Morgenstimmung

Let’s apply a tone mapper to get rid of the oversaturated colours, and see a sunrise in motion. I’m using the ACES tone mapper here, because the more modern AgX barely leaves any contrast intact:

I think that’s pretty good for a day’s work! Next up: a starry night, probably?

Read the whole story
jlvanderzwan
13 hours ago
reply
Don't know why the last video isn't loading in NB, but trust me, it's very “Naaaaaaaaants ingonyaaaaaaama bagithi Baba!”
Share this story
Delete

Girl Talk's Night Ripper REMIXED by The Hood Internet - Track 1 "Once Again"

1 Share
From: The Hood Internet
Duration: 2:35
Views: 14,084

Night Ripper was unleashed into the world 20 years ago, and when it hit, it did the same thing to us as it did to many: rewired our brains to forever change our associations of all the songs sampled within.

We’d already loved 2ManyDJs' "As Heard on Radio Soulwax Pt. 2," Danger Mouse's "The Grey Album," the many Z-Trip mixes that bridged genres— and the advent of this Girl Talk album helped unearth even more OGs: "The Lesson(s)" by Double Dee & Steinski, or Italian releases like Club House and Pink Project. It also absolutely opened the doors to people's reception of our bootleg mp3 blog The Hood Internet, which later evolved into live DJ sets, plus all sorts of remix, editing, and composition projects.

The Hood Internet's initial approach was largely an already-been-done A vs B re-imagining of two songs together as a full "single" or DJ edit, sourced from our own vast collections of music at that moment in time. I'm trying not to use the word mashup, but that's what we made. (Picture that Winnie the Pooh meme where his usual appearance is next to the word "mashup," and then wearing the tuxedo he's next to the word "blend.") Whereas Girl Talk's approach was FRENETIC: 15 to 20 samples happening within a single track, all anchored together. It made sense that he'd state he was "not a DJ," and that the tracks weren't mashups. Night Ripper was a pop sound collage built for the dance floor. 
 
Can you even remix something like that? Eh, why not. As a nod to its 20th bday, we grabbed the samples Girl Talk used on select tracks from the album, and created remixed versions of each track that keep the original tempo and rearrange the samples into a brand new configuration. And then made music videos for each of them!

Read the whole story
jlvanderzwan
1 day ago
reply
Share this story
Delete

Marcus Aurelius: Philosopher King

1 Comment
PERSON:
Read the whole story
jlvanderzwan
1 day ago
reply
I mean, they're both happy with the outcome, and a war was avoided, so philosophy did improve everyone's lives.
Share this story
Delete

The Science Behind Oatmeal and Diabetes

1 Comment

Before Insulin, There Was Oatmeal

Before the discovery of insulin, the lives of many people with diabetes were saved or prolonged by Carl von Noorden’s oatmeal diet. As I discuss in my video Is Oatmeal Good for People with Diabetes?, he published his findings in 1903, which were received with a great deal of skepticism. But the critics were overcome in the following years by the weight of the evidence.

Acclaimed doctor James B. Herrick began to try the oatmeal diet on his patients. Initially very doubtful, he became astonished by the results, which led to the 1909 proclamation that no case of juvenile or adolescent diabetes should be deprived of the benefits of the oatmeal cure.

The great Elliott Joslin, founder of the oldest and largest diabetes clinic in the world, described the effects of oatmeal as “sometimes magical,” calling the oatmeal cure an unsolved mystery, referred to back then as “one of the greatest puzzles in diabetes.” They did have some clues, though. They found that animal protein had to be strictly excluded, as it annihilates the favorable action of oatmeal-type diets.

And now we know, more than a century later, that, indeed, animal protein intake intensifies insulin resistance, which is the cause of prediabetes and type 2 diabetes, whereas plant-based foods enhance insulin sensitivity, which is the opposite.

 

Is Oatmeal Good for People with Diabetes?

We’ve long known that higher consumption of whole grains, including oats, is associated with a lower risk of diabetes. As I discuss in my video How Does Oatmeal Help with Blood Sugars?, more than a dozen randomized controlled trials found that oats significantly improved both short-term and long-term blood sugar control, in addition to lowering cholesterol levels.

We think the benefits arise from a fermentable fiber in oats called beta-glucan. We know one of the underlying cholesterol-lowering mechanisms of oatmeal consumption might be its microbiome-manipulating ability––in other words, having a beneficial effect on our intestinal bacteria.

 

What’s So Great About the Fiber in Oatmeal?

A little fiber goes a long way. Our good gut flora uses fiber to make short-chain fatty acids that have anti-inflammatory effects. There are dozens of randomized controlled trials showing the types of fiber found in oats and beans can improve long-term blood sugar control in people with diabetes. Why? Because the gut bacteria selectively promoted by dietary fiber intake can help alleviate type 2 diabetes.

The oat fiber itself has been shown to act as a prebiotic, boosting the growth of beneficial bacteria like Lactobacillus and Bifidobacteria. So, between the lack of animal protein, lack of animal fat, and bursting at the seams with prebiotic fiber, it’s no wonder that oatmeal diets grew to become part of the clinical routine in the treatment of diabetes.

 

How Soon Can You See Results?

As I discuss in my video Oatmeal Diet Put to the Test for Diabetes Treatment, several studies have suggested that a few days of eating oatmeal could have beneficial effects for about a month afterward. In a randomized, controlled, crossover trial, not only did insulin needs drop by about 40% in just two days, compared to just restricting calories alone with a hypocaloric diabetic diet, but a measure of long-term blood sugar control taken four weeks later reflected the benefit.

Other new studies have shown the same thing. Two days of oatmeal significantly reduced the required amount of insulin and improved blood sugar levels, with beneficial effects noted for up to four weeks. Consider this: Patients with uncontrolled type 2 diabetes on the two-day oatmeal diet experienced a 40% reduction of insulin dose, accompanied by almost normalization of average blood sugars. Although the intervention only lasted for two days, researchers observed a lasting significant reduction of insulin dosage and ameliorated mean blood sugars for weeks after the participants were dismissed from the study—and that was after they resumed their regular diets.

chart showing the reduction in blood sugar and insulin dosage after 2 days of eating oatmeal, immediately after, and 4 weeks later

Put people on a diet packed with oats, beans, fruits, vegetables, and nuts, and the number of their gut fiber-feeders churning out beneficial short-chain fatty acids shoots up, and fasting diabetic blood sugars drop by about 25% within one month. The more fiber-feeders they fostered, the better their blood sugar control. When the fiber-promoted short-chain fatty acid producers were present in greater diversity and abundance, participants had better improvement in their hemoglobin A1c levels (which is a measure of longer-term blood sugar control).

charts showing how high-fiber diets can alter the gut microbiome and improve blood sugars in those with type 2 diabetes

 

Are There Any Downsides to Oatmeal?

If you try an oatmeal diet, your physician should be ready to rapidly deprescribe your blood sugar drugs or else you could become dangerously overmedicated. Oatmeal interventions should not be performed in patients who might have difficulties in reporting symptoms of low blood sugar. The downside of trying oatmeal days is that it may work a little too well, so it must be done under close medical supervision.

 

The Glycemic Index of Oatmeal

Whole grains are good, but intact whole grains are better. The wholiest of grains: groats.

Oat groats have their inedible outer husks removed during processing. They can then be sliced into two to four pieces to make steel-cut (also known as pinhead or Irish) oats, which are considered a low-glycemic-index food, averaging under 55.

Oat groats can also be coarsely ground into Scottish oatmeal or steamed and flattened into “old-fashioned” rolled oats, which have a glycemic index of 55.

Instant oats are steamed longer and rolled even more thinly. Scoring 79, it’s considered a high-glycemic-index food, but not as bad as some breakfast cereals, which can get into the 80s or 90s.

 

Jazzing Up Oatmeal

Oatmeal is a classic whole-grain breakfast, and there are plenty of ways to enjoy it.

  • Of course, fruit and nuts are popular additions. A berry banana oatmeal bowl is quick and easy, and cinnamon baked apples make for a cozy breakfast. Assemble overnight oats or baked carrot cake oatmeal the night before to simplify your morning.
  • To add another type of fiber for your gut flora, mash cannellini beans into your oatmeal—my friend Paul swears you can’t even see or taste them.
  • I like to start my mornings with what I call my BROL bowl. BROL stands for barley, rye, oats, and lentils. I use oat groats (also called hull-less and hulled oats). I premix all the ingredients in a 1:1:1:1 ratio and then cook one scoop of dry BROL and two scoops of water in an electric pressure cooker. That makes a base with great texture. 

Once you have your BROL base, pick your toppings. When I feel like something sweet, my go-to is a chocolate-covered-cherry sensation. I make it with frozen dark red cherries, cocoa powder, dates, and walnuts or pumpkin seeds.

You can easily turn that BROL bowl savory; here’s a version made with sautéed greens. Google “savory oatmeal” for all sorts of interesting dishes involving mushrooms, herbs, curry, roasted vegetables—you name it!



Read the whole story
jlvanderzwan
2 days ago
reply
I'm mildly annoyed by the lack of information on whether oat milks preserve any benefits or whether they've been processed so much that thay're all gone
Share this story
Delete

The people’s atheist

1 Comment

Painting of two figures praying in a field at dusk with a pitchfork and wheelbarrow nearby, evoking a serene, rural setting.

Jean Meslier, a rural French priest, argued that religion is designed to produce subjects who will automatically obey

- by Nidal Taibi

Read on Aeon

Read the whole story
jlvanderzwan
2 days ago
reply
"Power does not hold only by force, taxation or law. It holds because it gets itself blessed. It holds because it succeeds in passing off injustice as order, domination as hierarchy, suffering as something meritorious."
Share this story
Delete

How the New York Times changed its coverage of trans people

1 Share

analysis of 3,242 articles over the last 12 years shows a clear shift from affirming to skeptical framing starting in 2022

#
Read the whole story
jlvanderzwan
2 days ago
reply
Share this story
Delete
Next Page of Stories