Real-time Day/Night System in UE4

I told you about my updated Day-Night cycle in UE4 in a previous post, so I’m going to expand a bit on this subject.

First, it’s very dependent on Epic’s Ray-Traced Distance Field Soft Shadows system and therefore, quite slow on low-end graphic cards.
I optimized my system with scalability in mind, trying to do less real-time updates depending on a global setting chosen on a per-player basis.

One in-game day is equivalent to 1200 real seconds, or 20 minutes. Basically, a night of 10 minutes and a day of 10 minutes.

 

Details Panel of relevant components used in  the Time of Day setup
Details Panel of relevant components used in the Time of Day setup

I use two Directional Lights, but the trick is, only one is active.

The first one is the active one, I didn’t changed much from the default settings, except that is has the Cascaded Shadow Maps disabled and UseRayTracedDistanceShadowMap activated.

The second one is completely disabled and I’m only using its position to update the BP_SkySphere

Then a Skylight is used, the HDR doesn’t matter much as I’m using it only to cast shadows and color our Global Illumination.

And of course the UE4s default BP_SkySphere connected to my disabled directional light.

 

 

 

Level Blueprint for the Time of Day setup. Responsible for most of the effect.
Level Blueprint for the Time of Day setup. Responsible for most of the effect.

The whole system is defined and updated from the Level Blueprint, and OnBeginPlay will trigger two sets of events:

– A Timer-delayed mechanism, updating only every X seconds defined in a global “Quality Settings” struct. In High Quality settings it’s every 0.8 seconds, or every in-game minute.

– A Timeline that update every frame.

I added a debug input on the T key to set the time of day just before 5:00 am.

 

 

 

 

 

The Timeline used in the Day/Night System.
The Timeline used in the Day/Night System.

The timeline is responsible for updating the Sun location in the sky, it’s color and brightness, and trigger two events that will disable the shadow casting during the night to save in performances.

I also get the in-game time from this timeline, to display it in-game and debug my system more easily.

The Main Directional Light (the active one) rotation’s goes from -180° to 0°, an arc of 180° from midnight to midnight.

My second Directional Light (the inactive one) rotation’s goes from -270 to +90°, a full 360° rotation around earth and it’s this one that will update my sky.

 

 

 

So, why two Directional Lights and why two sets of events?

Well, I can’t update the Sky Color and BP_SkySphere every frame, it’s just too costly and destroyed my performances. And updating the Sun’s shadow every X seconds made every shadows in-game jumping around, no matter how fast I updated it. Dividing the events this way made the best compromise in terms of quality/resources!

I tried as well to remove the CubeMap from the sky and use a Recapture Scene every X seconds, but there was a noticeable hitch every time it updated it. In the end I have real-time direct shadowing and almost-realtime global illumination that does the trick!

If you have any comment or any idea to improve the system, let me know in comments just here or send me an email, I’ll update this article accordingly!

24 hours in 18 frames :

Day Night System Animation Gif
  • http://zielak.pl/ Zielak

    I hate the scrolling in this page. It’s much laggier compared to scrolling with my Magic Mouse (not apple fanboy, I have to work on Mac here).

    • praglik

      Oh wow I’m so terribly sorry! I never experienced any laggy scrolling so far, I’ll take a look into the code and see if I can do anything to help with that.

      • http://zielak.pl/ Zielak

        I would suggest to remove that scrolling “effect”. Firefox on Windows has smooth scrolling by default. Magic Mouse on Mac provides smooth scrolling by default on every browser.

        I don’t know about other’s preferences but I like how it works on Chrome Windows – no smoothing at all. It just jumps to X-lines forward without any effect and takes no time. With smoothing I have to wait until the effects stops before I can focus my eye on reading text.

        Just saying. I’m a web-dev with “small OCD” ;)

        • Praglik

          I’m totally not a web-dev, it’s kinda new to me. But as a level designer with small OCD myself, I totally understand :)
          I’ll disable this not-so-smooth-scrolling-effect as soon as possible, thanks for your input !

        • praglik

          Fixed ! Can you tell me if it changed anything for you?

          • http://zielak.pl/ Zielak

            Yup, It’s working great now! Sorry to be dick about it.

            Cheers!