Creating a Dynamic Day and Night System in Unreal Engine 5
A Brighter Day, A Darker Night
In almost all of my UE5 projects I have been stuck using the same basic daylight system, sometimes I have made a night sky and a while ago I made a very rudimentary day night cycle (it looked awful). However I wanted more.
I took to the web and found this method for creating a far better day and night cycle.
The first step was to create a blueprint actor that housed all of the lighting. This was so I can adjust elements on the fly and apply code to the event graph and construction script. I made sure to add a post process as well, and disable auto exposure by setting both the EV values to 0.5.
24 Hour Clock
It's all well and good having the directional light move as time passes, but it isn't very dynamic. I added variables to the BP_DynamicSky actor I made earlier. The first variable set the time of day. The values were clamped between 0 and 24 to represent the 24 hour clock and the default value was set to 9 (9AM). Two more variables were created for dawn and dusk which were set at 6 and 18 respectively (18 being 6PM).
I made a simple construct that set the world rotation of the actor based on the variables set. This now allows for dynamic adjustment of time based on the 24 hour clock! And for added customization, a clamp range was set in the dawn and dusk variable to allow for adjustment of dawn and dusk times, as they tend to fluctuate in real life. Then, another construct was made to call this one.
Good Mooning, Everyone
Now that daytime is working well, we need night time. The process for this was quite simple. After continually messing around with the time of day slider, I was able to find slight offset values for when the moon was visible or not. All I needed to do was set the rotation for dawn and dusk and add or subtract that offset (which I determined was 0.3) to ensure the moon would be visible. In this part of the project there isn't actually a moon yet, so a cube serves as a marker for moonlight. The real issue is that the atmosphere isn't currently viewable.
I am the Night!
As it stands, night time looks, well it doesn't look good. The first step in fixing this is creating a duplicate of the map and messing around with the sky atmosphere and directional light and finding a good compromise for what I'm looking for. In the directional light, I messed around with the intensity, angles, colours and temperature and in the atmosphere I altered the Rayleigh scattering. Then I hopped into my BP_DynamicSky actor and implemented those changes via a new function to ensure I can set the new values as variables. I coped the values from the test level into these variables and made them instance editable for changes on the fly! I also took the time to set categories in the designer so things don't get too crazy, then I called the event in the construction script. This brings us to the next issue and can you spot it in the in-game image below?
Did you get it? If you guessed "Hey there's no stars in the sky!" congratulations! I'll admit, I was really excited for this part of the system because materials are something I wanted to learn more about and learn I did. After research into vectors, UVs and the HSLS coding language, I was able to fashion the beginnings of the stars. Starting with a texture co-ordinate node I multiplied that to a panner, via a parameter that held all the data I needed (Currently just tiling and panning speed). Then I divided that by a speed variable to ensure the stars panned at the right pace and applied that to the texture parameter. I then set another parameter and used it to set the brightness of the stars and multiplied both and created an output node.
I called the sky atmosphere luminance nodes in and added them all to the emissive colour pin and the result was quite good.
Comments
Post a Comment