Search Unity

Question Anti-aliasing and shader graph

Discussion in 'Shader Graph' started by sivrikaya, Feb 13, 2021.

  1. sivrikaya

    sivrikaya

    Joined:
    Oct 19, 2014
    Posts:
    7
    Hi,
    I've made a basic water shader with Shader Graph, for URP.

    Should I include something maybe, enable a flag or something to make it respond to anti-aliasing settings in render pipeline?
    Because things drawn over the water plane gets these artifacts and no antialiasing as I can see.
    But there is no problem between the intersection of the objects with standard materials.
    It looks ok in scene window but not on game window.

    This is strange because shader itself handles antialiasing well if you look at the water/object intersection.
    Object object intersection is ok to. They are both default materials.

    Appreciate any help.

    shaderQuestion.png
    Unity forum scaled down the image, clicking on the image might be necessary to see the problem more clearly.

    watershader.png
    And this is my attempt to create a water shader.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The Scene Depth node is sampling the camera depth texture, which is always rendered without MSAA. It should be assumed any effect using the Scene Depth won’t work properly with MSAA.

    In this case you’ll want to check if the current fragment is further away than the scene depth, with a little bias, and then not do the “foam” in that case. The problem is with MSAA enabled there will be pixels when the water surface gets rendered further away but the depth texture is for geometry that’s close to the camera. So you’ll basically want the inverse of “foam” to fade both the foam and color back to “normal”. This will change the artifact to be a too dark edge in places where there’s a foam edge overlapping with other geometry in front.
     
    sivrikaya likes this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Pseudo code:
    Code (csharp):
    1. // what you have already
    2. float depthDiff = sceneDepth - positionDepth;
    3. float foamFactor = saturate(depthDiff * _Foam);
    4.  
    5. // numbers are arbitrary
    6. float nearFade = saturate(1.0 - depthDiff * 10);
    7.  
    8. // this is a little different
    9. half4 color = lerp(waterColor, _FoamColor, (1.0 - foamFactor) * nearFade);
     
    sivrikaya likes this.