Skip to main content
added 360 characters in body
Source Link
Nathan Reed
  • 33.7k
  • 3
  • 93
  • 116

Sure, you can do that. For instance, if you want the color to change from skyNightColor at 4:00 to skyMorningColor at 7:00, you could write some code like

if (currentHour >= 4 && currentHour < 7) color = lerp(skyNightColor, skyMorningColor, (currentHour - 4) / (7 - 4)); 

The expression (currentHour - 4) / (7 - 4) remaps currentHour linearly to zero at 4:00 and one at 7:00.

This assumes that currentHour is a float variable that varies continuously from 4 to 7 as time goes on. Right now you have it as an integer variable that changes discretely, but it'll probably be more useful as a float. (You can always cast it to an int if you need the integer hour.) Or you can also write the lerps in terms of sunY, if you prefer.

One enhancement you may want to do, depending on how fast time can run in your game, is replace the linear interpolation by cubic splines. If time runs slowly, it probably won't be noticable, but if time runs quickly (e.g. hours pass in a few seconds) then the sudden shift from one lerp to another can be noticable. Cubic splines such as Catmull-Rom splines will smooth this out. The spline control points would be the RGB values for your colors.

Sure, you can do that. For instance, if you want the color to change from skyNightColor at 4:00 to skyMorningColor at 7:00, you could write some code like

if (currentHour >= 4 && currentHour < 7) color = lerp(skyNightColor, skyMorningColor, (currentHour - 4) / (7 - 4)); 

The expression (currentHour - 4) / (7 - 4) remaps currentHour linearly to zero at 4:00 and one at 7:00.

One enhancement you may want to do, depending on how fast time can run in your game, is replace the linear interpolation by cubic splines. If time runs slowly, it probably won't be noticable, but if time runs quickly (e.g. hours pass in a few seconds) then the sudden shift from one lerp to another can be noticable. Cubic splines such as Catmull-Rom splines will smooth this out. The spline control points would be the RGB values for your colors.

Sure, you can do that. For instance, if you want the color to change from skyNightColor at 4:00 to skyMorningColor at 7:00, you could write some code like

if (currentHour >= 4 && currentHour < 7) color = lerp(skyNightColor, skyMorningColor, (currentHour - 4) / (7 - 4)); 

The expression (currentHour - 4) / (7 - 4) remaps currentHour linearly to zero at 4:00 and one at 7:00.

This assumes that currentHour is a float variable that varies continuously from 4 to 7 as time goes on. Right now you have it as an integer variable that changes discretely, but it'll probably be more useful as a float. (You can always cast it to an int if you need the integer hour.) Or you can also write the lerps in terms of sunY, if you prefer.

One enhancement you may want to do, depending on how fast time can run in your game, is replace the linear interpolation by cubic splines. If time runs slowly, it probably won't be noticable, but if time runs quickly (e.g. hours pass in a few seconds) then the sudden shift from one lerp to another can be noticable. Cubic splines such as Catmull-Rom splines will smooth this out. The spline control points would be the RGB values for your colors.

Source Link
Nathan Reed
  • 33.7k
  • 3
  • 93
  • 116

Sure, you can do that. For instance, if you want the color to change from skyNightColor at 4:00 to skyMorningColor at 7:00, you could write some code like

if (currentHour >= 4 && currentHour < 7) color = lerp(skyNightColor, skyMorningColor, (currentHour - 4) / (7 - 4)); 

The expression (currentHour - 4) / (7 - 4) remaps currentHour linearly to zero at 4:00 and one at 7:00.

One enhancement you may want to do, depending on how fast time can run in your game, is replace the linear interpolation by cubic splines. If time runs slowly, it probably won't be noticable, but if time runs quickly (e.g. hours pass in a few seconds) then the sudden shift from one lerp to another can be noticable. Cubic splines such as Catmull-Rom splines will smooth this out. The spline control points would be the RGB values for your colors.