spenibus.net
2014-04-06 18:23:07 GMT

A simple day night cycle, part 1 - An introduction, phases and setup.

>**Note** The principles outlined here are fairly general but be aware that some aspects are specific to unity3d 4.3. Step 0: A boring introduction, skip at will -- Of the many ways to add atmosphere to a game, a day night cycle is an easy and effective way to give the player a sense of reality. Such a system offers visual clues analoguous to those found in reality, which presents to the player a simple idea: that the game's world is not unlike our own, rather than an alien entity with incomprehensible rules. The changes in lighting, in color, the movement of shadows, they all contribute to make the player project our reality and its rules onto the game. It is the ability to witness the passage of time and feel the game's world has existed before eyes were set on it and will continue to be after it is left. That we explore the surface of a large rock orbiting an even larger star rather than walk on a movie set. In short, we create the illusion that the game's world operates like ours on the grand scale and we let the player fill in the smaller details. But that is enough of a boring introduction, let's see the basics of that idea. We aim for something credible but not necessarily ultra realistic. We assume 24 hours per day and no seasonal changes. Step 1: Phases -- First we divide the day into 4 phases: ````txt 22:00 -> 06:00 08h Night 06:00 -> 08:00 02h Sunrise 08:00 -> 20:00 12h Day 20:00 -> 22:00 02h Sunset ```` We make the day longer than the night so the player can spend more time in a bright setting. It's basically a summer day. Then each phase is assigned a color that represents its visual identity: ````txt Night: dark blue Sunrise: orange Day: light yellow Sunset: red ```` Nothing particular here, that's pretty much what happens in reality, although we use strong colors to make the scene more visually striking at sunrise and sunset. These colors will be used for the directional light that will illuminate the scene and provide the shadows. To even out the lighting, ambient light is set at 25% of the current color. Step 2: Setup -- Within unity, we setup the following hierarchy: * dayCycle (empty gameobject) * component: dayCycle.cs (the logic of our system) * camera (not the main camera) * moon (primitive sphere) * component: directional light * material: self-illuminated / white * earthCentre (empty gameoject) * sun (primitive sphere) * material: self-illuminated / bright yellow * component: directional light All the elements of this hierarchy are assigned to the layer "dayNightCycle" which is invisible to the main camera, while the camera contained in "dayCycle" can only see this layer. While this is not the reason we do this, it makes it possible to put "dayCycle" anywhere in the scene. "earthCentre" is then counterintuitively placed above the center of "dayCycle" at position "0,4,0" to make the day longer than the night. This logic is explained in the following picture: ![](http://spenibus.net/f/g/1c) "earthCentre" is in fact not the centre of our fictional world but simply the rotation point for the sun. I should have named it better at the time. We now have more daytime than nighttime and using a negative vertical value would have the opposite effect. But this configuration now creates a problem. If we point the light at "earthCentre", there are periods where light is emitted from the ground, which makes little sense. So we have to offset the position at which the sun is looking. >Update 2014-01-07 19:50 GMT >-- I made a mistake here when saying that we simply negate the vertical value of "earthCentre". Rather we use a specific value called "earthDepth" that we can change to get better results. It is simply a point underground towards which lights are pointed. >Original text >-- We simply subtract the position of "eatchCentre" from the actual centre of our system, giving us the position "0,-4,0". This is where we will point the directional lights. We could simply point at "0,0,0" but a perfectly perpendicular light doesn't look that good. Pointing slightly below ground provides a better effect. Finally we rotate "earthCentre" along the X axis by 60 degrees to simulate a vaguely european latitude. This will give the sun a curved rotation when seen from the ground and project shadows even when the sun is at its zenith. ![](http://spenibus.net/f/g/1d) The moon is even easier to setup. We put it wherever we want and we're done. This is actually taking longer than expected to write. I will stop here and continue in [part 2](http://spenibus.net/b/p/3/A-simple-day-night-cycle-part-2-Time-initialisation-and-phases-conditions).