Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Simple Fade Effect Doesn't Work For Very Small Values

Discussion in 'Shaders' started by AmazingCarpet, Aug 6, 2022.

  1. AmazingCarpet

    AmazingCarpet

    Joined:
    Aug 6, 2022
    Posts:
    2
    I'm trying to make a simple function in a compute shader which fades the trail left behind by moving agents by simply subtracting a small amount each pass. The current code is as follows:


    RWTexture2D<float4> Result;
    float fadeSpeed;
    float deltaTime;

    [numthreads(8, 8, 1)]
    void TrailFade (uint3 id: SV_DispatchThreadID)
    {
    float4 original = Result[id.xy];
    float4 newVal = max(0, original - fadeSpeed * deltaTime);

    Result[id.xy] = newVal;
    }


    Using an example fadeSpeed of 0.1, the code works perfectly fine. However, I wanted to experiment with longer trails, so I decreased the value to 0.01 and the trails don't dissapate at all. Doing some further poking around, it seems like the value needs to be >= 0.099 or else it won't work. In fact, 0.99 works properly, but 0.98 does not.

    Because of that issue, I have to assume it's some weird rounding / precision error, but I believe the float should be more than precise enough. Apologies if this is some obvious problem, I'm very new to Compute Shaders and this kind of low-level programming in general.
     
  2. AmazingCarpet

    AmazingCarpet

    Joined:
    Aug 6, 2022
    Posts:
    2
    Sorry, I found the answer! The Texture I was using simply wasn't big enough to store the precision I wanted it to. I solved it by using RenderTextureFormat.ARGBFloat