Skip to main content
added 24 characters in body
Source Link
return  ConvertDistToColor(asuint(distance(camposWorldPos, WorldCam) / 255), asuint(farclip / 255));
return  ConvertDistToColor(asuint(distance(campos)), asuint(farclip));
return  ConvertDistToColor(asuint(distance(WorldPos, WorldCam) / 255), asuint(farclip / 255));
Source Link

I use this in my tile based renderer to show how many lights hit a tile, I also use it for my volume ray casting to get like a heat map effect.

I work with sharpDX but its all done in HLSL so if u tweak it a bit it will work just how u want.

It takes an uint as the distance to camera and another uint as the max range and you can change the colors that match the different ranges.

static const float4 kRadarColors[14] =
{
{ 0, 0.9255, 0.9255, 1 }, // cyan
{ 0, 0.62745, 0.9647, 1 }, // light blue
{ 0, 0, 0.9647, 1 }, // blue
{ 0, 1, 0, 1 }, // bright green
{ 0, 0.7843, 0, 1 }, // green
{ 0, 0.5647, 0, 1 }, // dark green
{ 1, 1, 0, 1 }, // yellow
{ 0.90588, 0.75294, 0, 1 }, // yellow-orange
{ 1, 0.5647, 0, 1 }, // orange
{ 1, 0, 0, 1 }, // bright red
{ 0.8392, 0, 0, 1 }, // red
{ 0.75294, 0, 0, 1 }, // dark red
{ 1, 0, 1, 1 }, // magenta
{ 0.6, 0.3333, 0.7882, 1 }, // purple
};


float4 ConvertDistToColor(uint current, uint max)
{
// black for 0
if (current == 0)
    return float4(0, 0, 0, 1);
// light purple for reaching the max
else if (current == max)
    return float4(0.847, 0.745, 0.921, 1);
// white for going over the max
else if (current > max)
    return float4(1, 1, 1, 1);
// else use weather radar colors
else
{
    // use a log scale to provide more detail when the number of range is smaller

    // want to find the base b such that the logb of max is 14
    // (because we have 14 radar colors)
    float fLogBase = exp2(0.07142857f * log2((float) max));

    // change of base
    // logb(x) = log2(x) / log2(b)
    uint nColorIndex = floor(log2((float) current) / log2(fLogBase));
 
    return kRadarColors[nColorIndex];
}
}

then use it like this:

return  ConvertDistToColor(asuint(distance(campos)), asuint(farclip));