I wrote four lines of code to do some basic interpolation.
Goes something like this,
#l = "line" or distance from a to b; p = current point between the two def iLerp(a, b, l, p): abs_diff = abs(a - b) real_diff = abs_diff/l dirn = 1 if a < b else -1 return a + real_diff*(p*dirn)
Then I figured out this can be used to transition from one color to another, which would be neat for a simple day night cycle. I had a timer class already written, so I took it out for a spin.
Given two color vectors a and b, the time of day and start/end point, we get something like,
#60*0.0168 = 1.008~ #use this to approx time as a float so e.g. 17:55 = 17.924 timeco = ( worldTime.hour + (worldTime.min*0.0168) ) #we want to start at zero, so substract start hour of transition p = (timeco - 12) if 24 > timeco >= 12 else timeco #now define length of transition (hours between a and b) #for simplicity's sake of this example, we go from 0 to 12 and 12 to 0 (24) l = 12 color = [0,0,0] color = iLerp(a, b, l, p) color = iLerp(a, b, l, p) color = iLerp(a, b, l, p)
Anyhoo, my time to float approximations are off by a few decimals, but this works good enough at making transitions smooth; the new color never “pops” in. The basic principle for having other numerical values interpolate over time is the same, you just pass in integers or floats individually rather than build a color vector; useful if you want to increase or decrease fog intensity or depth at cetain times of day.
Adding a few more steps to the transition, I got this
Phew. With all of that out of the way, with more or less the same code we could transition from one texture to another – just a matter of writing another function to do the image buffer mambo. I can take care of that no problem, but how in blazes does one grab a sky texture through python? I could just slap a regular texture on a background scene dome and manipulate that, sure, but it bugs me that I can’t figure this one out. How do you folks do it?