Search Unity

Problems with new Standard shaders and transparency / fog

Discussion in 'Shaders' started by Smartwater3D, Mar 22, 2015.

  1. Smartwater3D

    Smartwater3D

    Joined:
    May 16, 2014
    Posts:
    64
    I have a problem with a water shader plane I have created (surface shader) and that was working perfectly with Unity 4.

    With Unity 5 I cannot get both linear fog and transparency to work correctly together, it is either one or another.

    I explain.

    1- if I use tag=transparent for the shader and stardard = opaque for the objects, then the objects are not taking the fog in account like in this picture


    2- if I set these objects into FADE mode instead of OPAQUE mode, it works with the fog, but I get no transparency with the water because they are drawn over it.



    3- if I tweak the water priority to Transparent-120 as with Unity 4, nothing change, I tried every increment of 100, and I only get a correct result when using Transparent-1000, but in that case, there is no transparency at all. Here for example I should see what is under the water, but I get only a white flat color.



    At this point I am confused about how Unity 5 draw things. I have tried to use the LOD shader tag but it does not changes anything.

    How could this transparent surface shader display both with Fog, transparency and in correct order so that the objects are not drawn on the top of it.

    Here is how I define the shader.

    Code (CSharp):
    1.  
    2. SubShader {
    3.     Tags { "Queue"="Transparent-120" }
    4.     Blend SrcAlpha OneMinusSrcAlpha
    5.     ZWrite On
    6.  
    7. CGPROGRAM
    8.  
    9. #pragma surface surf SM3DSpecular2 vertex:vert alpha:auto
    10. #pragma target 3.0
    11. #pragma exclude_renderers flash
    12. #pragma multi_compile_fog
    13.  
    I think this problem comes from the settings in
    SetupMaterialWithBlendMode()
    Where the definition of Opaque mode is:

    Code (CSharp):
    1.         case BlendMode.Opaque:
    2.             material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);
    3.             material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);
    4.             material.SetInt("_ZWrite", 1);
    5.             material.DisableKeyword("_ALPHATEST_ON");
    6.             material.DisableKeyword("_ALPHABLEND_ON");
    7.             material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
    8.             material.renderQueue = -1;
    9.             break;
    renderQueue = -1 will certainly draw everything opaque on the top of everything else, that mean what is under the water will not be hidden as shown in the first picture.

    Also all the alpha tests are off, which will interfere with fog.

    Any idea how I can get the water working with these pre-set parameters with both Fog and a correct renderQueue for transparency?
     
    Last edited: Mar 22, 2015
  2. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    It's a known bug, linear fog in deferred rendering only works on transparent objects. Will be fixed someday...
     
  3. Smartwater3D

    Smartwater3D

    Joined:
    May 16, 2014
    Posts:
    64
    The problem is not only about the linear fog, exp fog has the same problem.
    It is a global problem of renderqueue.
    For example, I cannot get the particles (particles/alpha) to display above the water and under the water hidden.
    All the particles are showing full, even the under water part.
    I have the same problem with all the assets, rocks or anything. They are showing either fully and are working with the fogs (linear, exp or exp2), or they are showing correctly (half with that is under the water hidden by the water transparency) but no type of fog affect them.

    As I said, everything is working fine with Unity 4.6 and before, but with Unity 5... the renderqueue is messed for transparency surface like water.
     
  4. turritom

    turritom

    Joined:
    Mar 22, 2013
    Posts:
    113
    i have the same problem
     
  5. SniperED007

    SniperED007

    Joined:
    Sep 29, 2013
    Posts:
    345
    Did you ever find a solution for this?
     
  6. Phantomx

    Phantomx

    Joined:
    Oct 30, 2012
    Posts:
    202
    Unity replied to me that it won't be fixed it's a limitation of the standard shader that has too many inputs to support fog in deferred. so you'll either have to write your own shader or use fog post effect. I made one pretty cool to work with deferred. here: https://www.assetstore.unity3d.com/en/#!/content/43738