HLSL着色距离淡入x3

时间:2017-09-22 11:56:13

标签: shader distance fade hlsl level-of-detail

大家好我想在3个距离之间褪色。例如,我有一个草系统都使用相同的着色器和3种类型的LOD,所以我需要在3个不同的距离之间交叉淡入淡出,这样它们就不会弹出。这就是我一直在努力的方法。

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.

此代码不起作用,只是从dist_near3dist_far3的淡入淡出并不计算其他代码,例如忽略它们或被覆盖。我尝试过其他方法也不起作用

1 个答案:

答案 0 :(得分: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;

我不知道为什么你需要这么复杂的方法,但上面的代码对我来说效果很好。