Search Unity

Screen space mapping of a texture in VR looks weird

Discussion in 'Shaders' started by Devil_Inside, May 6, 2021.

  1. Devil_Inside

    Devil_Inside

    Joined:
    Nov 19, 2012
    Posts:
    1,119
    I'm trying to create a portal through which a player can look into a different location in the scene.
    I've managed to achieve this in non-vr with a secondary camera that mimics main camera transform and renders to a RT, which is them rendered on a quad using a simple shader:
    Code (CSharp):
    1. shader "Custom/PortalVR"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Main Texture", 2D) = "white" {}
    6.         _Color ("Color", Color) = (1,1,1,1)
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags
    12.         {
    13.             "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"
    14.         }
    15.         Lighting Off
    16.        
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma fragmentoption ARB_precision_hint_fastest
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 fixed4 position : POSITION;
    29.                 fixed2 texcoord : TEXCOORD0;
    30.  
    31.                 UNITY_VERTEX_INPUT_INSTANCE_ID // Added to support VR (Single Pass Instanced)
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 fixed4 position : SV_POSITION;
    37.                 fixed4 screenPos : TEXCOORD0;
    38.  
    39.                 UNITY_VERTEX_OUTPUT_STEREO // Added to support VR (Single Pass Instanced)
    40.             };
    41.  
    42.             sampler2D _MainTex;
    43.             float4 _Color;
    44.  
    45.             v2f vert(appdata v)
    46.             {
    47.                 v2f o;
    48.  
    49.                 UNITY_SETUP_INSTANCE_ID(v); // Added to support VR (Single Pass Instanced)
    50.                 UNITY_INITIALIZE_OUTPUT(v2f, o); // Added to support VR (Single Pass Instanced)
    51.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); // Added to support VR (Single Pass Instanced)
    52.  
    53.                 o.position = UnityObjectToClipPos(v.position);
    54.                 o.screenPos = ComputeScreenPos(o.position);
    55.  
    56.                 return o;
    57.             }
    58.  
    59.             fixed3 frag(v2f i) : COLOR
    60.             {
    61.                 //UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    62.                
    63.                 //float2 screenUV = UnityStereoTransformScreenSpaceTex(i.screenPos.xy / i.screenPos.w);
    64.                 float2 screenUV = i.screenPos.xy / i.screenPos.w;
    65.                 fixed4 c = tex2D(_MainTex, screenUV) * _Color;
    66.  
    67.                 return c;
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }

    However as expected this doesn't work in VR (single pass instanced).
    I'm not talking about having a stereo effect by rendering two render-textures. I can't even get a single eye render the portal properly. For some reason the RT is projected onto the screen in some weird scaled way and I can't figure out how to fix this.
    This is how it looks in non-stereo mode and how it should look:
    Scr0.png

    This is how it looks in stereo mode left eye:
    Scr1.png

    As you can see, the RT has weird scaling and offset.
    I'm not sure what's causing this and how exactly should I calculate the screen space UV for the RT to align properly.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    When you're doing stereo rendering, the camera no longer has one projection matrix and position. It has two, one for each eye. And if you just duplicate the base camera settings you're getting the non-VR camera position and projection matrix.

    There are several threads on doing mirrors or portals in VR that discuss how to fix the problem.
     
    dyadicgames-ericzou likes this.