Search Unity

Question Blending cameras without render pipeline for Oculus Quest 2

Discussion in 'Shaders' started by licaschiou, Oct 14, 2021.

  1. licaschiou

    licaschiou

    Joined:
    Jan 14, 2018
    Posts:
    1
    We are developing an app for Oculus Quest2. Our app is built with Unity's 3D template without render pipeline.

    We are trying to implement a camera blending effect inspired by Ned Makes Games's video (please search youtube "Blend Two Camera Views with a Render Texture in Unity URP")

    So far we are able to blend two camera's images but there is a visual glitch. It looks like the images of main camera and second camera have displacement.

    Our setup is as following :
    1. Use OVRCameraRig > CenterEyeAnchor's camera as main camera
    2. Create a second camera as child of main camera and set its field of view the same as main camera
    3. Create a cube and a sphere. Put them in separate layer. Set cameras' culling mask to see only one of the object.
    4. Attach the following script to main camera

    public Material blendMat;
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
    Graphics.Blit(source, destination, blendMat);
    }

    5. Assign a material with the following shader for blendMat
    Code (CSharp):
    1. Shader "CustomMobile/BlendCamera"
    2. {
    3.     Properties{
    4.         _MainTex("Main Texture", 2D) = "white" {}
    5.         _BlendTex("Blend Texture", 2D) = "white" {}
    6.         _Blend("Blend", Range(0,1)) = 0
    7.     }
    8.     SubShader{
    9.         Tags{
    10.             "RenderType" = "Opaque"
    11.             "Queue" = "Geometry"
    12.         }
    13.         ZTest Always
    14.         Cull Off
    15.         ZWrite Off
    16.         Pass{
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc"
    21.  
    22.             float _Blend;
    23.             sampler2D _BlendTex;
    24.             half4 _BlendTex_ST;
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    31.             };
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float4 vertex : SV_POSITION;
    37.                 UNITY_VERTEX_OUTPUT_STEREO
    38.             };
    39.  
    40.             v2f vert(appdata v)
    41.             {
    42.                 v2f o;
    43.                 UNITY_SETUP_INSTANCE_ID(v);
    44.                 UNITY_INITIALIZE_OUTPUT(v2f, o);
    45.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    46.                 o.vertex = UnityWorldToClipPos(v.vertex);              
    47.                 o.uv = v.uv;
    48.                 return o;
    49.             }
    50.  
    51.             UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
    52.             half4 _MainTex_ST;
    53.  
    54.             fixed4 frag(v2f i) : SV_Target
    55.             {
    56.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    57.                 fixed4 col = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);  
    58.                 fixed4 blendCol = tex2D(_BlendTex, UnityStereoTransformScreenSpaceTex(i.uv));
    59.                 col = col * (1 - _Blend) + blendCol * _Blend;
    60.                 col = saturate(col);
    61.                 return col;
    62.             }
    63.             ENDCG
    64.         }
    65.     }
    66. }
    67.  
    6. Set second camera's target texture to a render texture(1920 * 1920) and assign this render texture to the material's _BlendTex property of previous step.
    7. Use multiview for Oculus android setting.
    8. In the attached image, you can see the displacement between the sphere and cube.

    We think the problem may happen in the way we sample the render texture from second camera. We tried to replace UnityStereoTransformScreenSpaceTex with UnityStereoScreenSpaceUVAdjust, or plane
    tex2D(_BlendTex, i.uv)
    but could not find a way to fix it so far.

    Anyone has ideas how to fix this? Thank you in advance for any suggestions or tips!
     

    Attached Files: