You can achieve "infinitely smooth" fading by using something like
Interpolate
a,b(x) :=
0 | [x <= a] |
1/2 (1 + tanh((sqrt(3)/4) ((a - b) (a + b - 2 x))/((a - x) (-b + x)))) | [a < x < b] |
1 | [x >= b] |
As an interpolation function.
If you want interpolation between two functions f
1 and f
2, make it:
f
1 (1-Interpolate
a,b(x))+f
2 Interpolate
a,b(x)
The Sqrt(3) is just a choice to make the middle part of that function increase as smoothly as easily possible. The otherwise fourth order 0 accelleration at x=(a+b)/2 becomes fifth order by doing so.
Interpolate
0,1(x) simplifies to:
0 | [x<= 0] | - | here only the first function will be taken |
1 | [x >= 1] | - | here only the second function will be taken |
1/2 (1 + tanh[(sqrt(3)/4) ((1 - 2 x))/( (-1 + x) x)]) | [0 < x < 1] | - | here is a smooth, infinitely continuous blend between the two functions |
Possibly, exponential functions are slightly faster and/or more numerically stable than hyperbolic tangent functions. In that case, you might want to use:
1 - 1/(1 + e
(Sqrt(3) (a - b) (a + b - 2 x))/(2 (a - x) (-b + x)))
and for the 0-1 range,
1 - 1/(1 + e
(Sqrt(3) (1 - 2 x))/(2 (-1 + x) x))
respectively.
More generally, you can have functions of the form:
1/2 (1+tanh(p(x)/q(x))) with p and q being aribitary polynomials. (rational functions)
If you choose such a rational function that goes to -infinity at x=a and to +infinity at x=b and is monotonic in the interval in between, you can get all kinds of interpolatory behaviour that, if you don't happen to choose the infinite polynomial series of the arcus tangens hyperbolicus arctanh, you almost certainly get a function that smoothly goes 0 to 1 in the interval [a,b] but does so infinitely smoothly at the limits of a and b.
Possibly even nicer from interpolatory point of view, however slightly more timeconsuming for containing a squareroot, might be this one:
with the exponential form
1 - 1/(1 + e
(sqrt(3/2) (-1 + 2 x))/sqrt(-(-1 + x) x))
and its aribitary interval counterpart:
1/2 (1 + tanh((sqrt(3/2) (a + b - 2 x))/(2 (a - b) sqrt(((a - x) (-b + x))/(a - b)
2))])
And IT's Exponential form:
1 - 1/(1 + e
(sqrt(3/2) (a + b - 2 x))/((a - b) sqrt(((a - x) (-b + x))/(a - b)2)))
Again, the factor sqrt(3/2) is merely a choice to maximise the smoothness of the function around its interpolatory center and dropping another derivative order to 0 at that point.
Here a final comparison:
the blue and purple functions are the ones proposed in this post.
The yellowish one is what linear interpolation does and the green one is what you proposed yourself based on a sine.
And here a closeup of the last 10% of the interpolation:
In this region, the first function already is nearly flat while the others clearly are far away from it. You can see that the second function actually overtakes the sine at the edge of the 95% mark and ends up becomming just as flat as the first function, but does so much later.
The sine function lacks perfect smoothness in the endpoints (in the sense that the differentials wont go to all 0 at those points) eventhough it passes through the desired endpoint.
Just as a1 and a2 in fractower's variant, a and b in my variant determine the sharpness of the interpolation or alas the the quickness of the function to rise at the center.
Additionally, they define the location of the interpolation in case you don't want to interpolate the points 0 - 1 but rather, say, the points -3 - 1.5
by simply using this on a vector valued function, e.g.
f
1 (1-Interpolate
0,1(t))+f
2 Interpolate
0,1(t)
with
f
1 and f
2 being functions in x,y,z coordinates, you can use this without any change in 3D. Just apply per coordinate.