public unsafe void GradientLerp(nfloat *data, nfloat *outData)
{
nfloat lerpDist = *(nfloat*)data;
nint i = 0;
nint numPositions = positions.Length;
// Make sure we put the linear distance value back into the 0.0 .. 1.0 range
// depending on the wrap mode
if (wrapMode == WrapMode.Tile || wrapMode == WrapMode.TileFlipY)
{
// Repeat
lerpDist = lerpDist - (nfloat)Math.Floor(lerpDist);
}
else
{
// Reflect
lerpDist = (nfloat)Math.Abs(lerpDist) % 2.0f;
if (lerpDist > 1.0f) {
lerpDist = 2.0f - lerpDist;
}
}
for (i = 0; i < numPositions; i++)
{
if (positions[i] > lerpDist)
break;
}
nfloat prevPosition = 0;
nfloat dist = 0;
nfloat normalized = 0;
if (i == 0 || i == numPositions) {
if (i == numPositions)
--i;
// When we have multiple positions we need to interpolate the colors
// between the two positions.
// normalized will be the normalized [0,1] amount
// of the gradiant area between the two positions.
//
// The shading colors have already
// been setup with the color factors taken into account.
// Get the distance between current position and last position
dist = factors[i] - prevPosition;
// normalized value between the two shading colors
normalized = (lerpDist - prevPosition)/dist;
// Console.WriteLine("prev {0} dist {1} normal {2} i {3} t {4}",
// prevPosition, dist, normalized, i, t);
for(ushort ctr = 0; ctr < 4; ctr++) {
outData[ctr] = (nfloat)GeomUtilities.Lerp(shadingColors[0][ctr],
shadingColors[1][ctr],
(float)normalized);
}
}
else
{
// When we have multiple positions we need to interpolate the colors
// between the two positions.
// normalized will be the normalized [0,1] amount
// of the gradiant area between the two positions.
//
// The shading colors have already
// been setup with the color factors taken into account.
prevPosition = positions[i-1];
// Get the distance between current position and last position
dist = positions[i] - prevPosition;
// normalized value between the two shading colors
normalized = (lerpDist - prevPosition)/dist;
for(ushort ctr = 0; ctr < 4; ctr++) {
outData[ctr] = (nfloat)GeomUtilities.Lerp(shadingColors[i-1][ctr],
shadingColors[i][ctr],
(float)normalized);
}
}
if (gammaCorrection)
{
// * NOTE * Here I am only computing the gamma correction for RGB values not alpha
// I am really not sure if this is correct or not but from my reading on this topic
// it is really never mentioned that alpha is included.
for(ushort ctr = 0; ctr < 3; ctr++) {
outData[ctr] = (nfloat)Math.Pow(outData[ctr], gamma);
}
}
// Console.WriteLine("R: {0}, G: {1}, B: {2}, A: {3}",
// outData[0],
// outData[1],
// outData[2],
// outData[3]);
}