RimWorld

RimWorld

Realistic Lighting and Shadows
moo 27 Jul @ 9:29pm
blending
I'll supply a drawing, as this is very similar work to some shader blending I did.
https://pasteboard.co/qShsBgTW9Zbc.png

Basically, at the instant you want to have sunlight breach/sink under the horizon, shadow strength must be aligned with light level.

Basically, your sums of all factors however you mix the inputs / weights need to equal the same, to have a consistent result.

-

On that topic, here's that shader code (HLSL)

https://pastebin.com/A8c2krLS

It's a long winded file full of comments caused by testing, but essentially it's a triplaner implementation (so x/y/z and it's facing to you) along with an implementation of texture blending by height (so basically, sand>grass>tundra>snowcap or biomes), and texture blending by slope (so grass > rock), and LOD by distance.

Basically, it's a work of fricken art.

The fragment section is the most important for me to point out to you - where the blends (near line 1949) are a combination of three parts, which is two textures and the cliff. The weights of the whole equation must always add up to 1. What goes up in weight in some part of the equation, must consequentially be matched in the equation

If I had miscalculated the weights (so we are over/under 1 when it comes down to it), then it would look off once the final mix happened as some texture was applied too strongly

Here's a picture of that shader code working with that blending (ignore the texture tiling, and ignore the very terrible landscaping it's really bumpy)

I also forgot, I had variable height-band blending implemented

https://pasteboard.co/1OJyRElH1tKP.png

In a long winded way, hopefully that helps frame the solution for you. Collected weights must always equal 1.
< >
Showing 1-13 of 13 comments
Sjaandi  [developer] 27 Jul @ 9:53pm 
The game only lets me have one sky dome color (I don't get to have light coming from any single "direction" - it's all ambient when drawn).

So what I'm doing right now for shadow darkness is I'm adding up all the direct light sources and diving by all the total light sources, and that fraction tells me how black the shadows should be.

The reason right now that shadows are almost perfectly black at sunrise is that the sun is providing about 3000 lux, and the sky is providing about 25 lux (which is a realistic ratio). However, this means that 3000 / 3025 gives almost perfectly black shadows, and it's only later when the sun is high in the sky that the ratio nears 100,000 / 110,000 that the shadows "ease up" a bit and you can see through them.

In real life, we normally aren't looking down at the ground from the sky (like we do in Rimworld), we're inside the shadows so our eyes adapt to the decreased light, so the shadows seem lighter than they really are.

But realism does not always match players' expectations, which is why I added a setting for Max Shadow Strength that lets players select a multiplier for how dark the shadows should be, so if they want shadows that are only 50% as black as they "should" be, they can choose that instead.
moo 27 Jul @ 9:58pm 
That might be it though. Extend the length of the blend from 25 lux up to 3K, and match that change to shadow strength decreasing towards the target?
moo 27 Jul @ 9:59pm 
Equally, match that colour blend from the nighttime skybox to the daytime one?
moo 27 Jul @ 10:01pm 
So... 25 - 3K over an hour or whatever(?). In the same timeframe, shadow maximum goes 100 > target and colour goes from night to day. Or.... 25-3K-100K, shadow goes proportial
Last edited by moo; 27 Jul @ 10:02pm
Sjaandi  [developer] 27 Jul @ 10:13pm 
I could easily add a blend that would "fix" the problem, but then I would ironically have the opposite issue where players complain they can't tell when the sun rises due to the transition being too smooth (I did have a few complaints about this with the previous model).

I also am trying to design the code in such a way that it becomes trivial to add support for different weather conditions later that can dim the sum/sky or change its color, and the more "patches" I layer on top, the further the code goes from "just working" to more brittle and harder to extend.

I've been recently learning about cicrumsolar light casting - and that won't remove the abrupt transition - but it might smooth it a bit as the sun would be a bit dimmer at first (perhaps only 1000) lux and the sky would be brighter (around 100-200) giving a ratio more like midday.

Realistically, though, the sky should transition differently depending on biome. If you are in a desert or cold mountain mountain air, the sky actually should transition very similar to how the mod currently displays (and if you watch videos of sunrises in such biomes, the mod is actually quite realistic). However, for toxic wastelands or humid tropical rainforests, the transition should be a lot smoother, with the possibility of the sky even outshining the sun in extreme cases, completely smoothing the transition!
moo 27 Jul @ 10:18pm 
A tight blend might be the answer to both problems. It's always going to take some fiddling with it.

And for the weather conditions, just weight the strength of the sun/shadow/colour against the weather. Rain in game actually (at least, I'm pretty sure) has a ramp up/down which is perfect for aligning to a weight for the final effect. I think snow does as well.

Fog, any sudden weather condition not so much - but this is something that you can probably spread a transition in multiple ticks
Last edited by moo; 27 Jul @ 10:19pm
moo 27 Jul @ 10:25pm 
As I think about this, the atmospheric colour should always be affected. Not jsut at 5am or whenever daybreak is, but even before and after that as the sun hits the upper atmosphere, the transition from night to day colouring is constant.
moo 27 Jul @ 10:28pm 
For the shadows, it's all about matching the darkness of that shadow at daybreak to the ambient colour, and then it transitions to whatever the target is, lightening up as time continues and the sun's reflected energy begins to put light into what was once shadowed spaces.
moo 27 Jul @ 10:29pm 
Basically, 100% shadow a tick before daybreak (which is colour matched or strength matched or whatever to the ambient), and degrading quickly to the target as the sun rises
Sjaandi  [developer] 27 Jul @ 10:29pm 
I'm not saying it's hard - but there is a lot of nuance that I have to take into account.

For example - I have to use static classes, which means saving state is harder between ticks. Not impossible - but I don't get the easy compartmentalized and saved state that instance variables would give me.

Right now - I'm completely overriding all of vanilla on pretty much anything that involves the sun, moon, or sky as vanilla assumes the sun travels the same arc every day, with a very slight variation for latitude and time of year.

I'm not exactly sure what information I have to work with from vanilla yet for weather - so how easy it will be depends on how much information that's already there that I can just use versus how much I have to build from scratch.
Sjaandi  [developer] 27 Jul @ 10:37pm 
A lot of what you are thinking about the mod already simulates.

If you look right before sunrise, the sky actually does start becoming pinkish and brightens up a lot (exponentially, in fact).

It's just that the sun is SOOO much brighter than the sky and Rimoworld's faster time scale means that what should be a 2-minute transition takes place in 1.5s, which is why it feels so abrupt even though proportionally it is correctly scaled from real-time.

(I actually do calculate how much of the sun disk is visible and divide the total sun disk lux by what is above the horizon). It's just 3000 / 25 is such a high ratio that the sun equals the sky in just 1/120th of ~1.5s).

(The sun rise slowdown factor I added actually works by making the sun disk bigger, so if you use the full factor of 20, it takes something like 1/6 of a second for sun to equal the sky, which IS slow enough for a human to see the blending, if still very fast.)
moo 27 Jul @ 10:56pm 
me: I'd ♥♥♥♥♥♥♥♥ it a little. Do the transition over more time. Slower planet rotation or something something...
Sjaandi  [developer] 27 Jul @ 11:28pm 
Yes a realism mod would fit well with a slower rotation. But even if I figured out how to do it - it would require rewriting a lot of game balance as pawn sleep and food consumption needs would need to be tweaked as well. A complete realism overhaul is a bit out of scope at the moment.
< >
Showing 1-13 of 13 comments
Per page: 1530 50