Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Getting the depth render texture in single-pass stereo rendering.

Discussion in 'Shaders' started by Mutimir, Aug 30, 2019.

  1. Mutimir

    Mutimir

    Joined:
    Dec 6, 2018
    Posts:
    36
    I am working on an oculus quest app and am trying to get the depth texture but i get it only on the left eye. My guess is that its cause oculus quest using a screenspace-texture-array instead of a double wide render texture. Is there a way of defining the format of the depth texture using RenderTextureDescriptor? Any insight would be appreciated. Here are my example codes.
    In the .cs:

    Code (CSharp):
    1. private void Start()
    2.     {
    3.         this.gameObject.GetComponent<Camera>().depthTextureMode = DepthTextureMode.Depth;
    4.     }
    5.  
    6.     private void OnRenderImage(RenderTexture source, RenderTexture destination)
    7.     {
    8.         Graphics.Blit(source, destination, fog);
    9.     }
    And the shader:
    Code (CSharp):
    1.  
    2. CGPROGRAM
    3.             #pragma vertex vert
    4.             #pragma fragment frag
    5.  
    6.             #include "UnityCG.cginc"
    7.  
    8.             UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
    9.             uniform half4 _MainTex_ST;
    10.             UNITY_DECLARE_SCREENSPACE_TEXTURE(_CameraDepthTexture);
    11.             uniform half4 _CameraDepthTexture_ST;
    12.  
    13.             struct appdata
    14.             {
    15.                 float4 vertex : POSITION;
    16.                 float2 uv : TEXCOORD0;
    17.             };
    18.  
    19.  
    20.             struct v2f
    21.             {
    22.                 float2 uv : TEXCOORD0;
    23.                 float4 vertex : SV_POSITION;
    24.                 float2 scrPos : TEXCOORD1;        
    25.                 UNITY_VERTEX_OUTPUT_STEREO
    26.             };
    27.  
    28.             v2f vert(appdata v)
    29.             {
    30.                 v2f o;
    31.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o)
    32.                 o.vertex = UnityObjectToClipPos(v.vertex);
    33.                 o.scrPos = TRANSFORM_TEX(v.uv, _CameraDepthTexture);
    34.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    35.                 return o;
    36.             }
    37.  
    38.             float _DepthStart;
    39.             float _DepthDistance;
    40.  
    41.             fixed4 frag(v2f i) : SV_Target
    42.             {
    43.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    44.  
    45.                 fixed4 orCol = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
    46.                 float depthValue = Linear01Depth(UNITY_SAMPLE_SCREENSPACE_TEXTURE(_CameraDepthTexture, i.uv).r) * _ProjectionParams.z;
    47.                 depthValue = saturate((depthValue - _DepthStart) / _DepthDistance);
    48.                 return fixed4(depthValue, depthValue, depthValue, 1);
    49.             }
    50.             ENDCG