🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

HLSL fade between two textures using lerp

Started by
5 comments, last by kubera 3 years, 3 months ago

Hi, I'm new to shader programming and I'm trying to make simple pixel shader that fades between two textures.

I have a variable that gets increases every frame, I normalise it, then I set the texture using lerp (code below)

	float4 TextureOne = TexOne.Sample(TexSampler, input.uv);
    float4 TextureTwo = TexTwo.Sample(TexSampler, input.uv);
    
    float n = normalize(transition);
    float3 TextureDiffuse = lerp(TextureOne.rgb, TextureTwo.rgb, n);
    float TextureSpecular = lerp(TextureOne.a, TextureTwo.a, n);

This doesn't have any impact and the texture is set to the second texture.

The other way I have tried is to do if statements

    float4 TextureOne = TexOne.Sample(TexSampler, input.uv);
    float4 TextureTwo = TexTwo.Sample(TexSampler, input.uv);
    
    float n = normalize(transition);
    float3 TextureDiffuse;
    float TextureSpecular;
    
    if (n == 0)
    {
       TextureDiffuse = TextureOne.rgb;
       TextureSpecular = TextureOne.a;
       
    }
    if (n == 1)
    {
        TextureDiffuse = TextureTwo.rgb;
        TextureSpecular = TextureTwo.a;
    }
    
   if (n > 0 && n < 1)
   {
       TextureDiffuse = lerp(TextureOne.rgb, TextureTwo.rgb, t);
       TextureSpecular = lerp(TextureOne.a, TextureTwo.a, t);
 
   }

The first two if statements on their own without the final if statement flickers between the two textures as expected but then adding the final if statement it flickers not as often as before but the texture one becomes black.

The final value that gets outputed is calculated with light otherwise there is no texture


    float3 finalTexture = diffuseLight * TextureDiffuse + specularLight * TextureSpecular ;
    
    return float4(finalTexture , 1.0f);

How can I get this to work? What am I missing?

Thank you

None

Advertisement

Looking at your code it seems strange that you would call normalize on the variable “transition” (treating it like a vector) when it is (i assume) a float.

Instead you probably want something like this:

float3 TextureDiffuse = lerp(TextureOne.rgb, TextureTwo.rgb, transition);
float TextureSpecular = lerp(TextureOne.a, TextureTwo.a, transition);

The above example assumes that “transition” is a float with a value between 0 - 1.

@Noxil I have used saturate instead of normalize and it still doesn't work, using the transition variable like you have said to do it also doesn't work. The transitionvariable is getting increased like transition+= 3 * frametime as it is being used by something else, hence why setting it to being between 1 and 0 is needed

None

Alright, well saturate is good for clamping the value between 0 and 1, ensuring the value never goes beyond the boundaries.

I assume the “transition” variable is initialized to 0, so increasing it as you mention should work just fine resulting in an increasing value.
I would suggest using a debugger or some form of logging to verify that this indeed is the case. Looking at the value before its sent to the shader you will be able to tell if the problem is in your shader or outside in your other code.

Also here is the link for the “lerp” function in HLSL (https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-lerp)

You need to tell us how you calculate ‘transition’ and what it intends to be, otherwise we can only guess. As said, ‘normalize’ calculates a unit vector, no scalar value as you seem to expect. Probably your related logic above the given code listings is wrong.

Maybe should be:
float3 TextureDiffuse = lerp(TextureOne.rgb, TextureTwo.rgb, float3(n,n,n));

This topic is closed to new replies.

Advertisement