Building Portal in VR in Single-Pass Stereo shader

Hey there! I’m having trouble writing a shader to render a Portal in VR, and I need help.

Everything I try doesn’t work. I can’t get both eyes to be aligned (I get crossed eyes when I look to the portal) and the projection of the image looks really wrong. See the gif:



To generate this image I’m just getting the same Camera.main, pointing it to a RenderTexture and sending that result to my shader. So I’m seeing my portal through the portal, both should be aligned.

Here’s my shader:

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _RenderedTexture;
			half4 _RenderedTexture_ST;

            struct v2f {
                float4 pos : SV_POSITION;
                float4 screenPos : TEXCOORD1;
            };

            v2f vert(float3 position : POSITION) {
                v2f o;

                o.pos = UnityObjectToClipPos(position);
                o.screenPos = ComputeScreenPos(o.pos);

                return o;
			}

            float4 frag(v2f i) : SV_TARGET {
#if UNITY_SINGLE_PASS_STEREO
				i.screenPos.xy = TransformStereoScreenSpaceTex(i.screenPos.xy, i.screenPos.w);
				return tex2Dproj(_RenderedTexture, i.screenPos);
#endif
				return 0.70;
            }

And here’s the component I have applied to the portal plane:

void InitRenderTexture()
{
    int width = XRSettings.eyeTextureDesc.width;
    int height = XRSettings.eyeTextureDesc.height;

    renderTexture = new RenderTexture(width, height, 24);

    renderTexture.name = "Portal-Renderer-" + GetInstanceID();
    renderTexture.hideFlags = HideFlags.DontSave;

    renderTexture.vrUsage = XRSettings.eyeTextureDesc.vrUsage;
}

void Update () 
{
    Camera main = Camera.main;

    if (!main) { return; }
    if (isRendering) { return; }
    if (!refObject) { Debug.Log("Missing refObject;"); return; }
    if (!renderTexture) { InitRenderTexture(); }

    Debug.Log("Camera aspect ratio: " + Camera.main.aspect);

    isRendering = true;

    Vector3 originalPosition = main.transform.position;
    

    main.targetTexture = renderTexture;
    main.Render();

    main.targetTexture = null;

    Material material = GetComponent<Renderer>().sharedMaterial;        
    material.SetTexture("_RenderedTexture", renderTexture);

    isRendering = false;
}

I tried using some of ther available methods discribed in Unity - Manual: Stereo rendering with no luck. I don’t know if the error is in the shader or in how I’m generating the RenderTexture (size or missing param).

Any help will be much appreciated!

Hey @celsodantas , I’m pretty new to unity and VR, and I’m also trying to build a system portal in VR ( on oculus go ). I started with the brackey tutorial on the subject, trying to adapt it.

I’m using the shader he provides in the description, you’ll may be find the ressource interesting.

Anyway, I have the same image distorsion problem.
In my case, everything aline perfectly in the game view / no distorsion, but ones in the headset the portal image is totally distorded.

Here’s a view from the game view

And here is a view from the headset

Did you found a solution?

Hey man, did you manage to figure this out? I would love to know the solution aswel! @celsodantas

I’m also looking for this solution! If anyone can guide us please.