Search Unity

Screen space fog aliasing issues

Discussion in 'Image Effects' started by CGDever, Jun 25, 2018.

  1. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    Hi,
    There is a texture / mask (_CameraDigitalDeptertexture) of the depth of the entire screen, which I transmit to the shader. I do not edit this texture.
    The fog is made based on this texture, but it has smoothing artifacts on the border of objects:
    https://yadi.sk/i/VFlFpfT73YMgE3

    The picture without fog looks like this:
    https://yadi.sk/i/ADEqdi6t3YMdVy

    There is a part of image effect shader:
    Code (CSharp):
    1. // Applies one of standard fog formulas, given fog coordinate (i.e. distance)
    2. float ComputeFogFactor(float coord) {
    3.         float fog = 0.0;
    4. #if _FOG_EXP
    5.          // factor = exp(-density*z)
    6.           fog = _Density * coord;
    7.           fog = exp2(-fog);
    8. #else
    9.            #if _FOG_EXP2
    10.                         // factor = exp(-(density*z)^2)
    11.                         fog = _Density * coord;
    12.                         fog = exp2(-fog * fog);
    13.              #else // FOG_LINEAR
    14.                         fog = coord * _LinearGrad + _LinearOffs;
    15.              #endif
    16. #endif
    17.               return fog;
    18. }
    19.  
    20. // Distance-based fog
    21. float ComputeDistance(
    22.                  #if _RADIAL_DIST
    23.                  float3 ray,
    24.                  #endif
    25.                   float depth
    26.                    ) {
    27.         float dist;
    28. #if _RADIAL_DIST
    29.          dist = length(ray * depth);
    30. #else
    31.           dist = depth * _ProjectionParams.z;
    32. #endif
    33.            // Built-in fog starts at near plane, so match that by subtracting the near value.
    34.             dist -= _ProjectionParams.y;
    35.             return dist;
    36. }
    37.  
    38.  
    39. float4 frag (v2f i) : COLOR {
    40. ....
    41. float depth = Linear01Depth( SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy).r )
    42.  
    43. // DUST:
    44. // fog amount.
    45. float g = ComputeDistance(
    46. #if _RADIAL_DIST
    47. i.ray,
    48. #endif
    49. depth ) - _DistanceOffset;
    50.  
    51. float fog = ComputeFogFactor( max(0.0, g) );
    52.  
    53. // Look up the skybox color.
    54. float3 skyColor = DecodeHDR(texCUBE(_SkyCubemap, i.ray), _SkyCubemap_HDR);
    55. skyColor *= _SkyTint.rgb * _SkyExposure * unity_ColorSpaceDouble;
    56. // Lerp between source color to skybox color with fog amount.
    57. finalColor.rgb = lerp(skyColor, finalColor.rgb, fog);
    58.  
    59. return finalColor;
    60. }
    I tried to change the depth filtering modes, it did not help.
    I tried to blur the texture of the depth and this did not help, the strips only increase:
    Code (CSharp):
    1. float size = 1;
    2.                 float2 up = _CameraCustomDepthTexture_TexelSize.xy * half2(0.0, 1.0) * size;
    3.                 float2 rt = _CameraCustomDepthTexture_TexelSize.xy * half2(1.0, 0.0) * size;
    4.                 float d_up = SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy+up).r;
    5.                 float d_dn = SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy-up).r;
    6.                 float d_rt = SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy+rt).r;
    7.                 float d_lt = SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy-rt).r;
    8.                 float d_m  = SAMPLE_DEPTH_TEXTURE(_CameraCustomDepthTexture, i.uv.xy).r;*/
    I broke my head, I can not understand the reason for this artifact.
    What is the cause of the artifact and how to fix it?
     
    Last edited: Jun 25, 2018
    morepixels likes this.
  2. CGDever

    CGDever

    Joined:
    Dec 17, 2014
    Posts:
    156
    The problem is not solved yet. Nobody knows how to fix this?