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:
https://i.imgur.com/LxOX6Iy.mp4
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 https://docs.unity3d.com/Manual/SinglePassStereoRendering.html 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!
Answer by Untoldecay · Apr 01, 2019 at 01:57 PM
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 http://untoldecay.com/gif/GameView.gif
And here is a view from the headset http://untoldecay.com/gif/goView.gif
Did you found a solution?
Answer by avandenberg · Mar 03, 2020 at 04:52 PM
Hey man, did you manage to figure this out? I would love to know the solution aswel! @celsodantas
Answer by diegodaly · Jan 11, 2021 at 03:35 PM
I'm also looking for this solution! If anyone can guide us please.
Your answer
 
 
             Follow this Question
Related Questions
Unity 5.3.1p3 - Viewports on Render Textures are warped when Virtual Reality Supported is enabled. 0 Answers
Sandy surface for virtual reality 0 Answers
_Time in Image Effects 0 Answers
Fog not working in my own Vertext Shader 2 Answers
Why is it necessary to transform vertex coordinate in vertex shader? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                