Search Unity

PostProcess OnRenderImage Depth doesn't work on Android

Discussion in 'Android' started by Kazs99, Jan 18, 2020.

  1. Kazs99

    Kazs99

    Joined:
    Jan 25, 2013
    Posts:
    16
    Hi guys,

    I take 2 days on this feature and I need some help :)
    I try to create a 2 colors fog on Android : first step => get the depth buffer in my shader.

    It's working well on Editor but not on my Android Device (Pixel 2).
    I am on Unity 2019.2

    Here is the script I add to my Camera :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [ExecuteInEditMode]
    5. [RequireComponent(typeof(Camera))]
    6. public class PostProcess : MonoBehaviour
    7. {
    8.     private Material material;
    9.  
    10.     // Creates a private material used to the effect
    11.     void Awake()
    12.     {
    13.         material = new Material(Shader.Find("Kcm/Depth"));
    14.  
    15.         // On active le depth
    16.         GetComponent<Camera>().depthTextureMode = GetComponent<Camera>().depthTextureMode | DepthTextureMode.Depth;
    17.     }
    18.  
    19.     // Postprocess the image
    20.     void OnRenderImage(RenderTexture source, RenderTexture destination)
    21.     {
    22.         Graphics.Blit(source, destination, material);
    23.     }
    24. }
    Then the associated Shader :
    Code (CSharp):
    1. Shader "Kcm/Depth"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex vert_img
    9.             #pragma fragment frag
    10.  
    11.             #include "UnityCG.cginc"
    12.  
    13.             uniform sampler2D _CameraDepthTexture;
    14.  
    15.             float4 frag(v2f_img i) : COLOR
    16.             {
    17.                 float depht = tex2D(_CameraDepthTexture, i.uv).r;
    18.                 return depht * 38.0;
    19.             }
    20.             ENDCG
    21.         }
    22.     }
    23. }
    The result is perfect on editor but doesn't show anything on Android!

    If you want to test, it's easy :
    Create a new 3D project on Unity
    Add the script on camera then play
     

    Attached Files: