spenibus.net
2014-04-09 22:09:53 GMT

A simple day night cycle, part 3 - Some configuration, preparing the loop, night and sunrise.

Step 6: More configuration -- ```` // light color Color colorLightSunrise = new Color(0.9f, 0.6f, 0.3f); Color colorLightDay = new Color(1.0f, 0.9f, 0.8f); Color colorLightSunset = new Color(0.9f, 0.5f, 0.5f); Color colorLightNight = new Color(0.1f, 0.1f, 0.2f); // skybox tint Color colorSkyboxSunrise = new Color(0.9f, 0.6f, 0.3f); Color colorSkyboxDay = new Color(0.6f, 0.75f, 1.0f); Color colorSkyboxSunset = new Color(0.9f, 0.5f, 0.5f); Color colorSkyboxNight = new Color(0.1f, 0.1f, 0.2f); // moon tint Color colorMoonDay = new Color(0.5f, 0.5f, 0.5f); Color colorMoonNight = new Color(1.0f, 1.0f, 1.0f); // light maximum intensity float sunIntensityMax = 0.5f; float moonIntensityMax = 0.5f; // shadow type LightShadows shadowType = LightShadows.Soft; ```` Again, certain variables will only be explained later. We will never cast shadows from 2 different light sources, both for artistic and technical reasons. It seems casting from multiple lights and having overlapping shadows requires deferred lighting. This is apparently more resource intensive and also incompatible with the free version of unity, so we will enable shadows depending on which light illuminates the scene. This is why we have "shadowType". Step 7: Night phase -- In "Update()", we initialise some variables: ```` float blendFactor = 0f; // default is night Color sunColor = Color.black; Color moonColor = colorLightNight; Color skyboxColor = colorLightNight; Color ambientTint = colorLightNight; float sunIntensity = 0f; float moonIntensity = moonIntensityMax; ```` "blendFactor" is used in Lerp() functions to interpolate between values when we make the transition from one phase to another. Apart from that, nothing special happens here. Sun is black and has no intensity. Moon has night light color with maximum intensity. Ambient light is set the same as the moon light color. Skybox will be explained later, but the idea is the same. Step 8: Sunrise part 1 -- This is the transition from night to sunrise in "Update()": ```` // sunrise, part 1 if(timeLogic.hour >= 6 && timeLogic.hour < 7) { blendFactor = (timeLogic.daySecond - 6f * 3600f) / 3600f; sunColor = Color.Lerp(Color.black, colorLightSunrise, blendFactor); moonColor = Color.Lerp(colorLightNight, Color.black, blendFactor); skyboxColor = Color.Lerp(colorSkyboxNight, colorSkyboxSunrise, blendFactor); ambientTint = Color.Lerp(colorLightNight, colorLightSunrise, blendFactor); moonTint = Color.Lerp(colorMoonNight, colorMoonDay, blendFactor); if(timeLogic.minute < 30) { sunIntensity = 0f; moonIntensity = Mathf.Lerp(moonIntensityMax, 0f, blendFactor*2f); } else { sunIntensity = Mathf.Lerp(0f, sunIntensityMax, (blendFactor-0.5f)*2f); moonIntensity = 0f; } } ```` First, we calculate "blendFactor". There are many ways to do this and I won't dwelve on them, the idea being that we output a number between 0 and 1 depending on the current time. Basically: ````txt 06:00 = 0 06:15 = 0.25 06:30 = 0.5 06:45 = 0.75 07:00 = 1 ```` Here I use the variable "daySecond" from "timeLogic", which returns the number of seconds since midnight, I then subtract the starting time (06:00) in seconds (6 * 3600) and since our transition takes place over an hour, I divide this by 3600. In effect, at 06:23:00 (22980 seconds since midnight), we are at 38% of the transition between 06:00 and 07:00: ```` blendFactor = (timeLogic.daySecond - 6f * 3600f) / 3600f; blendFactor = (22980 - 21600) / 3600f; blendFactor = 1380 / 3600f; blendFactor = 0.383; // rounded ```` We then use "blendFactor" to interpolate between values for lights and colors. The sun goes from black to sunrise light color. The moon goes from night light color to black. Ambient light goes from night light color to sunrise light color. The moon changes its physical color to a darker shade during the day. It's a simple visual cue to avoid confusion with the sun. On that last point, remember we use simple sphere primitives to show the sun and the moon. We can actually depict a more realistic moon by replacing the sphere with a textured plane showing a moon picture. It doesn't really matter for the sun because its brightness makes it impossible to discern any texture. Finally, we have subconditions splitting this part in 2 more parts. The moonlight is turned down during the first half hour, while the sunlight is turned up during the second half hour. This will help with the shadows transition and also provide a "darker before dawn" effect where only ambient light is present. The same happens during the second part of the sunset. This is the end of part 3. Then comes [part 4](http://spenibus.net/b/p/5/A-simple-day-night-cycle-part-4-The-other-phases).