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.

Question Black squares in bloom effect

Discussion in 'General Graphics' started by MatheusMarkies, Nov 30, 2021.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    Good Morning!
    I'm having trouble rendering an on-screen bloom shader by post processing.
    Basically the shader works like this:


    Camera image before rendering the effect:
    dsfgdsfgdsfgdsffg.PNG


    First a filter is passed on the camera image to leave only one highlights.

    dsfgdsfgdsfgdsffg.PNG

    Code (CSharp):
    1.  
    2. float4 _BloomThreshold;
    3. float _BloomIntensity;
    4. float _BloomScattering;
    5.  
    6. float3 ApplyBloomThreshold(float3 color) {
    7.     float brightness = Max3(color.r, color.g, color.b);
    8.     float soft = brightness + _BloomThreshold.y;
    9.     soft = clamp(soft, 0.0, _BloomThreshold.z);
    10.     soft = soft * soft * _BloomThreshold.w;
    11.     float contribution = max(soft, brightness - _BloomThreshold.x);
    12.     contribution /= max(brightness, 0.00001);
    13.     return color * contribution;
    14. }
    15.  
    16. float4 PreFilter(Varyings i) : SV_TARGET{
    17.     float3 color = pow(ApplyBloomThreshold(GetSource(i.fxUV).rgb),2);
    18.     return float4(color, 1.0);
    19. }
    20.  
    After that, the image goes through a downsample:

    Code (CSharp):
    1.  
    2. float4 FragBox(Varyings i) : SV_Target
    3. {
    4.     return float4(SampleBox(i.fxUV),1);
    5. }
    6.  
    7. static const float gaussian[14] = {
    8.     0.00598,    0.060626,    0.241843,    0.383103,    0.241843,    0.060626,    0.00598,
    9.     0.02400,    0.0160125,    0.741843,    0.455653,    0.296803,    0.189060,    0.18406
    10. };
    11.  
    12. float4 FragHBlur(Varyings i) : SV_Target
    13. {
    14.     float4 color;
    15.     float2 o = float2(GetSourceTexelSize().x, 0);
    16.     for (int idx = -3; idx <= 3; idx++) {
    17.         float4 tColor = GetSource(i.fxUV + idx * o);
    18.         color += tColor * gaussian[idx + 3];
    19.     }
    20.     return float4(color.rgb, 1 / _Levels);
    21. }
    22.  
    23. float4 FragVBlur(Varyings i) : SV_Target
    24. {
    25.     float4 color;
    26.     float2 o = float2(0, GetSourceTexelSize().y);
    27.     for (int idx = -3; idx <= 3; idx++) {
    28.         float4 tColor = GetSource(i.fxUV + idx * o);
    29.         color += tColor * gaussian[idx + 3];
    30.     }
    31.     return float4(color.rgb, 1 / _Levels);
    32. }
    33.  
    This step occurs several times in the effect's rendering, with each of the images used from source having its dimensions shrunk.

    After the image is blurred, it will be merged with the image from the previous frame, which was also blurred but has larger dimensions.
    dsfgdsfgdsfgdsffg.PNG

    After merging all bloomed images, the final image is merged with the camera image before rendering the effect:



    So far so good.



    The problem is these black squares that sometimes appear when I move the camera.
    Why are they there???

    Capturarf.PNG


    The same problem happens in the lens flare shader, which uses the same downsample method:

    Capturarf.PNG
     
    Last edited: Nov 30, 2021
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,007
    Last edited: Dec 16, 2021
  3. Oniros88

    Oniros88

    Joined:
    Nov 15, 2014
    Posts:
    106
    Same here, getting NaNs everywhere after latest update. had to revert because its unusable.
     
    MatheusMarkies likes this.
  4. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,007
unityunity