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.
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.
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 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 :