Search Unity

Basic depth blend [Solved]

Discussion in 'Shaders' started by D43DB33F, Mar 9, 2018.

  1. D43DB33F

    D43DB33F

    Joined:
    Apr 6, 2017
    Posts:
    43
    Hi ! I've written a water shader based on top of a Unity Editor generated surface shader. It looks ok, but it lacks shore blending. It also lacks shore foam, but I'll try to make the blending first, then I'll take care of foam later.

    I have two problems :
    1) I fail to find how to perform even a basic depth / edge / shore blending / fade (however you call it). I've checked several shaders (Low Poly Water Unity, FXWaterPro, etc) but the shore blending related code is drowned into the middle of many other complex things that I don't currently care about and, as a complete beginner with shaders, I'm having a very hard time telling what I need to extract from these and what I can just ignore.

    2) As mentionned above my shader is based on a surface shader, so Unity already handles many things with the "StandardSpecular" thing. Assuming that I find out how to write a separate basic shore blending water shader from scratch, i have no idea how to include it in my existing shader. Can I just add a fragment shader to my existing surface shader ? Aren't the modifications performed by the new shore blending shader going to overwrite or be overwritten by what Unity performs in my surface shader ?

    I'm not asking you to code anything for me of course, but if someone could just point me into the right direction it would help a lot. Thanks.

    EDIT : This shader (https://gamedev.stackexchange.com/questions/138775/simple-cartoon-water-shader-shoreline) looks like what I'm looking for, but for some reason, it renders all black when I try to use it.
     
    Last edited: Mar 9, 2018
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    is your camera rendering depth texture? otherwise it wont work
     
  3. D43DB33F

    D43DB33F

    Joined:
    Apr 6, 2017
    Posts:
    43
    I belive so. I added a component to my camera with the following code :

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
    4.     }
    EDIT : It definately does, Unity indicates it in the camera component.

    EDIT : The weird thing is that it only renders black in the game / scene views. The little "preview sphere" of the Unity editor does something that looks "less invalid".
     
    Last edited: Mar 9, 2018
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Maybe try checking your clear flags / culling?

    Your camera is definately the only active camera right? otherwise it wont be the main cam
     
  5. D43DB33F

    D43DB33F

    Joined:
    Apr 6, 2017
    Posts:
    43
    Yes yes it is the main camera. I created a clean scene before trying to make this to avoid problems.

    I found a piece of shader code that I almost got working after a few tweaking : https://lindseyreidblog.wordpress.com/2017/12/15/simple-water-shader-in-unity/

    I had to modify the fragment shader as follows :

    Code (CSharp):
    1.             float4 frag(vertexOutput input) : COLOR
    2.             {
    3.                 // apply depth texture
    4.                 float4 depthSample = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, input.screenPos);
    5.                 float depth = LinearEyeDepth(depthSample).r;
    6.  
    7.                 // create foamline
    8.                 float foamLine = saturate(_DepthFactor * (depth - input.screenPos.w));
    9.  
    10.                 float4 col = float4(_Color.rgb, foamLine);
    11.                 return col;
    12.             }

    Basically I removed the "foam ramp" stuff, cause all I was is a fade, not a texture.

    I'm getting a nice result :

    upload_2018-3-9_15-47-36.png

    However, sometimes, depending on camera orientation / position, I'm getting an ugly effect. It looks like this :

    upload_2018-3-9_15-45-57.png

    The problem is that "black" shape that appears underneath the correct looking blue fade.

    It disappears when I move / orientate the camera, and then appears again, with a delay of like one second. I don't know what this is due to, it sees a bit random.

    EDIT : Well it seems like this ugly black plane only appears while the game isn't running so it's ok. Though I'm still curious as to what could cause this.
     
    Last edited: Mar 9, 2018
  6. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Perhaps you need depth + normals mode for your depth texture? Im not super advanced at shaders so not the best at bugfixing, normally its trial and error for me until I work out why
     
  7. D43DB33F

    D43DB33F

    Joined:
    Apr 6, 2017
    Posts:
    43
    I will try this, thanks. However my biggest problem now is to find how to integrate this working shore fading water shader into my existing water surface shader. I tried simply adding a fragment shader, but unsurprisingly I'm getting the same visual render as I hadn't added anything. Now i'm trying to move some of the fragment shader code to the surface shader function, but I still havn't found how to do. And since my shader takes dozens of seconds to compile, it's becoming a pain in the a !

    EDIT : Finally go it to work ! Result :

    upload_2018-3-9_16-43-13.png

    Basically what I did was pass the "screen position" from the vertex shader to the surface shader.

    Vertex shader :
    Code (csharp):
    1.  
    2.            VertexOutput o;
    3.            // ... stuff
    4.            o.screenPos = ComputeScreenPos(o.vertex);
    5.            return o;
    6.  
    Surface shader, just assigning the transparency percentage to the Alpha field of the structure :
    Code (csharp):
    1.  
    2.        void SSMain(Input IN, inout SurfaceOutputStandardSpecular o)
    3.        {
    4.            o.Albedo = _Color.rgb;
    5.            o.Alpha = _Color.a;
    6.            o.Smoothness = _Glossiness;
    7.            o.Specular = _Specular;
    8.            o.Emission = texCUBE(_Cube, IN.worldRefl).rgb * _ReflectionIntensity;
    9.  
    10.            // apply depth texture
    11.            float4 depthSample = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, IN.screenPos);
    12.            float depth = LinearEyeDepth(depthSample).r;
    13.  
    14.            // create foamline
    15.            float foamLine = saturate(_DepthFactor * (depth - IN.screenPos.w));
    16.  
    17.            float4 col = float4(_Color.rgb, foamLine);
    18.            o.Alpha = foamLine;
    19.        }
    20. :
     
    Last edited: Mar 9, 2018
  8. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,982
    Brilliant! very glad you got it working :) sorry if I was not much help in the end!