DayNightController

About
DayNightController Script

Implements a Day/Night cycle relative to the game world, with a World-Time clock, and optional Direcitonal Light control.

Script Version
0.0.1.0

Script Revision
5/19/2011

Description
Add this script to a new GameObject to create a Day/Night cycle for the scene. The day/night cycle effect is achieved by modifying the scene ambient light color, fog color, and skybox material. The script will also rotate, fade, and enable/disable a directional light if one is attached to the same GameObject as the DayNightController script. The length of a complete day (in seconds) and the number of hours per day are modifiable in the script fields and allow calculation of the World-time hour-of-day. Each 'phase' of the day is considered to be 1/4 of the dayCycleLength.

Note that the script will rotate the GameObject transform it is attached to, even if no directional light is attached. You will probably want to use a dedicated GameObject for this script and any attached directional light.

The GameObject with this script should be placed roughly in the center of your scene, with a height of about 2/3 the length (or width) of the terrain. If that GameObject has a light, it should be a directional light pointed straight down (x:90, y:0, z:0). This light will then be rotated around its x-axis (relative to the scene; eg. as if you used the rotation tool locked on the green x axis) and will reach its horizontal peeks during the end of dusk and beginning of dawn, turning off during the night (upside-down rotation).

The reset command will attempt to use the default skybox assets DawnDusk, Sunny2, and StarryNight if that package has been imported. The command will also choose acceptable color values and set the day cycle to two minutes. It is suggested that the directional light be a light-yellow or peach in color with a roughly 0.33f intensity. The script will not set any default values for the light, if one exists, so the light must be configured manually.

Enhancement: Skybox Material Transition
The script is fully-functional in its current state, and the default values produce a day/night cycle effect which is acceptable for many situations.

However, a better effect could be achieved through an interpolated transition of the skybox materials during each phase change. The framework code to support such an update exists in the script as comments, but lacks a worker routine which can successfully perform the interpolation. This is an enhancement which should be added.

Even without a material interpolation, it would be possible to create custom skybox materials which used the same textures set to various brightness levels. This would make the skybox change less drastic, but could still benefit from a fade effect. The existing, yet unused, skybox material change framework in the script could be adapted to implement either enhancement.

Option: Allow GameObject Translation Instead of Rotation
Another enhancement would be the option to rotate the light by moving the GameObject's position around the terrain at a radius of the current height, while rotating the GameObject to keep the light pointed at the center of the terrain. This could allow attaching a model to the GameObject and rendering a visible sun which tracks across the sky. This functionality would need to toggle with the existing rotation code in an either-or configuration.