Oh.. We made a day night cycle

by | Jan 21, 2019 | Blog, Show Off, Unreal Engine 4

A long time ago me and my buddy Mons made a day night cycle, and I completely forgot to write about it or even show it. I coded everything and Mons provided the art. Its not really tuned or finished yet, but it has some great potantial and it turned out to be one hell of a system. We spent a lot of time getting it right.

We were really interested in having the moon come up a specific time of day and the sun come up a specific time of the day. We didn’t want it to be 50 percent day and 50 percent night. We wanted the moon to be up say 7 hours and the sun to be up the remaining 17 hours. So now it is set to come up and 6:00 and it goes down at 23:00.

Also you can put in how long a day lasts, 1 minute game time or 60 minutes game time. I prefer something slower, so the sun and moon don’t move to fast. This could cause a distortion on the texture, oh, and also you can clearly see the moon moving so much faster than the sun to complete its cycle. 😛 So I normally set it to 1 hour real time is 24 hours in the game.

I have added a screenshot with all the different settings. The art side of the system is controlled trough curves. Many curves. 😛 Intensity curves on sun/moon controls the intensity of the light. Others are color curves such as sky/horizon color etc. This helps give a good look and its easy to control. So if I remember correctly it is 0.0 on the curve means that is the color of the horizon when the sun gets up. Anything below 0.0 is night, and anything above 0.0 is day.

Say you want to have darker blue in the middle of the day. Just add a darker blue at 0.5 of the curve. This of course requires a little nitpicking if you want to hit the exact moment. Because the length of a day depends on when the sun gets up and goes down.

Some of the settings
The curves

Also you can control moon size and sun size and how high they are on the sky. If you look at the sun example that comes with UE4, you notice the sun is always straight above your head at 12:00. Didn’t like it, therefor I added a X rotation for both sun and moon.

I added a debugger to have more control over the system. It gave a really good clue what was happening while testing. I have included 3 screenshots above which shows it in use. It gives out the current colors, light intensity and tells you if the moon/sun is up or down. In the 3 screenshots you can also see the different curves play in. Mons tweaked them.

You can see the entire cycle in the video below. Enjoy. Currently speed up.