I'm trying to implement a simple "lighting" system that alters a value of light between 0.0-1.0, 1.0 is midday 0.0 is total blackness.
Are there any "good" information regarding this particular subject or is it something that you have to develop yourself and adjust to your game in particular. What I'm probably looking for is an algorithm that is flexible and could work by changing values like midday point and such.
My algorithm uses 12:00 / 12PM as it's center point and at that time it's 1.0 in lighting. What I'm trying to do is perhaps "change" the midday point to any value to say 10 and it should still work the same, and perhaps also be able to alter the number of hours the midday can be. Perhaps so that the lighting rises and falls exponentially. So that between night and 4 it raises slowly, between 4 and 8 it raises much faster and at 10 it's reached it high point until 14 when it starts to fall again but in the reverse order.
My simple algorithm only calculates a value between 0.0 and 1.0 and if there is any good info or some suggestions on how to "improve" this method or any other information regarding this subject it would be greatly appreciated.
My algoritm goes as following:
If (CurrentHour < (HoursPerDay / 2))
Lighting = CurrentHour / (HoursPerDay / 2)
Lighting += (CurrentMinute / MinutesPerHour) * (1 / (HoursPerDay / 2))
Else
Lighting = 1 - ((CurrentHour / (HoursPerDay / 2)) - 1)
Lighting -= (CurrentMinute / MinutesPerHour) * (1 / (HoursPerDay/2))
MIN_VALUE = 0.3f;
Max(MIN_VALUE, Min(Lighting, 1f))
or in C# code:
public float CalculateLighting(uint hour, uint minute, uint minutesPerHour, uint hoursPerDay)
{
float lighting = 1f;
if (hour < (hoursPerDay / 2))
{
lighting = (hour / (hoursPerDay / 2)) * lightingMax;
lighting += ((minute / minutesPerHour) * (1f / (hoursPerDay / 2))) * lightingMax;
}
else
{
lighting = lightingMax - (((hour / (hoursPerDay / 2)) * lightingMax) - lightingMax);
lighting -= ((minute / minutesPerHour) * (1f / (hoursPerDay / 2))) * lightingMax;
}
lighting = MathHelper.Clamp(lighting, lightingMin, lightingMax);
}
This way when the time is 6:00 AM the value is (6/12) and the same for (12+6) and also I clamp the results so I get a value between 0.3 and 1.0 or change that to whatever in case you wanted it to be Winter season so it's darker you can raise that value. This is however a very simple implementation of this concept.
Some functions that I'd like to bring in to it is:¨
- When is the "midday" and how long is it? Say it's 4 hours between 10-14. During this time it should always be 1.0 in lighting.
- Change the midday point to anytime or remove it completely. During dark nights in a game perhaps the sun shouldn't rise fully.
- "Somehow" perhaps make it increase slow and decrease fast by using some value for each. If the sun rises slowly but falls rather quickly afterwards. Exponentially raise and exponentially fall faster.
Just some ideas I'm looking for. It doesn't have to be realistic, just a sense that the weather can change. This value should be a value between 0.0 and 1.0 or adjustable (for extra bright days or something) due to the fact that it can be used to light the entire scene. A reference point to how bright it is in the game world.
How can I best extend my algoritm and improve it and perhaps implement new features?
Any suggestions are appreciated or some information on good implementation of a Day-Cycle system.