Search Unity

Problem in Android

Discussion in 'Shaders' started by MarcoVJ, Aug 7, 2018.

  1. MarcoVJ

    MarcoVJ

    Joined:
    Dec 7, 2016
    Posts:
    2
    Hi all.
    I'm new to shaders, so I only know basics. Probably this problem has been solved in the forums, but after trying many of the solutions that I found, I still cant solve this problem.

    I found a water shader code, fixed some of the lines to fit into what I needed and made it work fine at Unity in OpenGL ES 3.0 mode, with the Post Processing's Depth of Field enabled.



    To make the game more stable on smartphones I turned off Post Processing, but the shader stopped working.


    After search in documentation and forums, I figured out that the problem might be that depth works different in OpenGL. I tried many of the solutions that I found but none of them works, or I'm doing something wrong which is more probably.

    If I try to force an inverse depth value, the foam totally disappears.



    Here is the shader code:

    Code (CSharp):
    1. Shader "SimpleWater"
    2. {
    3.     Properties
    4.     {
    5.         _Tint("Tint", Color) = (1, 1, 1, .5)
    6.         _MainTex("Main Texture", 2D) = "white" {}
    7.         _Speed("Wave Speed", Range(0,1)) = 0.5
    8.         _Amount("Wave Amount", Range(0,1)) = 0.5
    9.         _Height("Wave Height", Range(0,1)) = 0.5
    10.         _Foam("Foamline Thickness", Range(0,3)) = 0.5
    11.  
    12.     }
    13.         SubShader
    14.     {
    15.         Tags{ "RenderType" = "Opaque"  "Queue" = "Transparent" }
    16.         LOD 100
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.      
    19.         Pass
    20.     {
    21.         CGPROGRAM
    22. #pragma vertex vert
    23. #pragma fragment frag
    24.         // make fog work
    25. #pragma multi_compile_fog
    26.  
    27. #include "UnityCG.cginc"
    28.  
    29.         struct appdata
    30.     {
    31.         float4 vertex : POSITION;
    32.         float2 uv : TEXCOORD0;
    33.     };
    34.  
    35.     struct v2f
    36.     {
    37.         float2 uv : TEXCOORD0;
    38.         UNITY_FOG_COORDS(2)
    39.         float4 vertex : SV_POSITION;
    40.         float4 scrPos : TEXCOORD1;//
    41.     };
    42.  
    43.     float4 _Tint;
    44.     uniform sampler2D _CameraDepthTexture;//Depth Texture
    45.     sampler2D _MainTex;//
    46.     float4 _MainTex_ST;
    47.     float _Speed, _Amount, _Height, _Foam;//
    48.  
    49.     v2f vert(appdata v)
    50.     {
    51.         v2f o;
    52.         v.vertex.y += sin(_Time.z * _Speed + (v.vertex.x * v.vertex.z * _Amount)) * _Height;//movement
    53.         o.vertex = UnityObjectToClipPos(v.vertex);
    54.         o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    55.         o.scrPos = ComputeScreenPos(o.vertex);// grab position on screen
    56.         UNITY_TRANSFER_FOG(o,o.vertex);
    57.  
    58.         return o;
    59.     }
    60.  
    61.     fixed4 frag(v2f i) : SV_Target
    62.     {
    63.         // sample the texture
    64.  
    65.         half4 col = tex2D(_MainTex, i.uv) * _Tint;// texture times tint;
    66.      
    67.         half depth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.scrPos))); // depth
    68.         half4 foamLine = 1 - saturate(_Foam * (depth - i.scrPos.w));// foam line by comparing depth and screenposition
    69.         col += foamLine * _Tint; // add the foam line and tint to the texture
    70.      
    71.         UNITY_APPLY_FOG(i.fogCoord, col);
    72.         return col;
    73.     }
    74.         ENDCG
    75.     }
    76.     }
    77. }
    What I'm doing wrong? I mean... I think that the problem is in depth buffer that works different in OpenGL, but can't make it work anyway, its frustrating.

    Thanks and sorry for my bad English.
     
  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    Can you add yourCameraVariable.depthTextureMode |= DepthTextureMode.Depth. The post processing probably enabled the depth buffer.
     
  3. MarcoVJ

    MarcoVJ

    Joined:
    Dec 7, 2016
    Posts:
    2
    I did it.

    Code (CSharp):
    1. public class cameraDepthEn : MonoBehaviour
    2. {
    3.     [ExecuteInEditMode]
    4.     public class RenderDepthBuffer : MonoBehaviour
    5.     {
    6.         public void Start()
    7.         {
    8.             GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
    9.         }
    10.     }
    11. }
    Tried with Post Processing disabled and removed, but it still doesn't work. I changed the platform to PC and it's doing the same: working with Post Processing and not working without it. Maybe the problem is because of forward rendering.

    Thanks for the response.
     
    Last edited: Aug 9, 2018