0

Hi all I'm trying to fade between 3 distances. For example I have a grass system that all use the same shader and 3 types of LOD so I need to crossfade between 3 different distances so they don't just pop in. This is how I've been trying to do it.

float dist_near1 = 60; //farthest away from camera
float dist_far1 = 110; //farthest away from camera

float dist_near2 = 30;
float dist_far2 = 70;

float dist_near3 = 0 ; //Closest to camera
float dist_far3 = 50 ; //Closest to camera

float4 fading1 = 1-saturate((distance(Input.wpos,Input.cpos)-dist_near1)/(dist_far1-dist_near1));
float4 fading2 = fading1-saturate((distance(Input.wpos,Input.cpos)-dist_near2)/(dist_far2-dist_near2));
float4 fading3 = fading2-saturate((distance(Input.wpos,Input.cpos)-dist_near3)/(dist_far3-dist_near3));
float4 baseColour=tex2D( baseMap,Input.Texcoord);


color = lerp(fading2,fading3,fading1);

wpos = world position

cpos = camera position.

This code doesn't work and just fades from dist_near3 to dist_far3 doesn't count for the rest like its ignoring them or being overwritten. I have tried other ways which also don't work

bennygenel
  • 23,896
  • 6
  • 65
  • 78
  • try to also include the result you're getting with the code you provided. – sissy Sep 22 '17 at 12:35
  • 1
    oh yeah sorry it don't work and just fades from dist_near3 to dist_far3 doesn't count for the rest like its ignoring them or being overwritten. – RattlerCreed Sep 22 '17 at 12:46

1 Answers1

0
float3 ComputeWeights(float vMin, float vMid, float vMax, float val)
{
    float wMinMid = (val - vMin) / (vMid - vMin);
    float wMidMax = (val - vMid) / (vMax - vMid);

    float kMinMid = 1 - step(1.0f, wMinMid);
    float kMidMax = step(0.0f, wMidMax);

    wMinMid = saturate(wMinMid);
    wMidMax = saturate(wMidMax);

    return float3
    (
        ( 1 - wMinMid ) * kMinMid,
        wMinMid * kMinMid + ( 1 - wMidMax ) * kMidMax,
        wMidMax * kMidMax
    );
}

// ...

float dist_near = distance(Input.wpos,Input.cpos);

float3 weights = ComputeWeights( dist_near3, dist_near2, dist_near1, dist_near );

float dist_far = dist_far3 * weights.x + dist_far2 * weights.y + dist_far1 * weights.z;

I have no idea why do you need such complicated approach, but the code above works well for me.