Search Unity

Rendertexture with stereo rendering modes (it doesn't work)

Discussion in 'AR/VR (XR) Discussion' started by mcclure111, Jul 4, 2018.

  1. mcclure111

    mcclure111

    Joined:
    Dec 10, 2016
    Posts:
    12
    I would like to be able to use RenderTextures in VR so that I can perform postprocessing effects. At this exact moment, I am attempting an effect where a shader is used to paint a surface with the contents of a RenderTexture, like a "portal" from Portal, or like a stencil buffer (I am not using a stencil buffer because I may add additional effects later). It does not work. I am getting different failure modes depending on which "Stereo Rendering Mode" I use.

    I have a minimal example project in this git repo https://bitbucket.org/runhello/srp-test-public demonstrating the problem. It has three branches, "mono", "stereo" and "stereo-instanced". The scene is named "minitvs" and the important code is in "TVShader.shader" and "ShaderResize.cs" (which creates the RenderTexture). I tested in 2018.2.0b11 and also in 2017.4.6f1, both times using an Oculus Rift.

    The scene has two cameras. One is on the layer "default", the other is on the layer "outerspace". In "outerspace" cubes continuously animate up toward the ceiling. In "default" a quad named "screen" continuously spins in a circle. Its surface is painted with the rendertexture from the "outerspace" camera, but the UVs are chosen so it looks like a window "into" outerspace rather than a TV screen displaying outerspace.

    **Case 1: branch "mono"**

    In this branch VR is turned off and everything works great.





    You cannot see that the "screen" quad is even there, except that the cubes are visible.

    **Case 2: branch "stereo", 2017.4.6f1**

    Here VR is enabled and the stereo rendering method is "single pass". This one ALMOST works:



    But there are two odd things.
    First off: When I set the RenderTexture's properties, I would like to do this:

    Code (CSharp):
    1. RenderTexture t = new RenderTexture(XRSettings.eyeTextureDesc);
    This seems consistent with the documentation. But if I do this, the aspect ratio on the outerspace rendertexture is all wrong. I have to do this instead:

    Code (CSharp):
    1.         RenderTextureDescriptor d = XRSettings.eyeTextureDesc;
    2.         d.width = d.width / 2; // THIS DOES NOT MAKE SENSE
    3.         RenderTexture t = new RenderTexture(d);
    4.  
    If I do this, I get the correct aspect ratio. This seems wrong.

    The second problem: Some of the cubes, as well as the "bar" floating in the background, are here textured with a special shader which I am using to debug eyes.

    Code (CSharp):
    1.             #if UNITY_SINGLE_PASS_STEREO
    2.             fixed4 c = unity_StereoEyeIndex == 0 ? float4(1,0,0,1) : float4(0,1,0,1);
    3.             #else
    4.             fixed4 c = float4(0,0,1,1);
    5.             #endif
    6.  
    I expect this will draw red in the left eye, green in the right eye, and blue if VR is switched off. What I see however-- as you can see in the screenshot-- is that when I render to a texture, it renders blue! Apparently UNITY_SINGLE_PASS_STEREO is false when rendering into "outerspace".

    **Case 3: branch "stereo", 2018.2.0b11**



    When I run this exact same code in the 2018.2 beta, I get the same problems, but there is an additional problem: The entire texture is offset a little bit down and to the left. It is as if the outerspace camera is not in the same place.

    **Case 3: branch "stereo-instanced", 2018.2.0b11**

    Here VR is enabled and the stereo rendering method is "single pass instanced". I have modified the shader in this branch so that the main texture is a 2D texture array instead of a single texture (I followed the instructions here and here).



    In this case, the "screen" quad is painted with a plain gray color which is not obviously present anywhere in the scene. Moreover, if I look at the RenderTexture at runtime it appears to be blank.



    ** HELP **

    What I am trying to determine is:

    - Why is the XRSettings.eyeTextureDesc width wrong in stereo mode?
    - Why is UNITY_SINGLE_PASS_STEREO false when rendering in stereo to a texture in stereo mode?
    - Why is there an "offset" when rendering in stereo mode in 2018.2b?
    - Why is the render texture empty when rendering in stereo instanced mode?

    Are any of these things bugs which I should file? (The 2018.2b behavior, at least, seems like a file-worthy bug).
     
    Last edited: Jul 4, 2018
  2. sgrein

    sgrein

    Joined:
    May 9, 2018
    Posts:
    11
    Hello mcclure111,

    we see the same issue with the offset in our project.

    Best wishes,
    SG
     
  3. nienokoue

    nienokoue

    Joined:
    Apr 19, 2017
    Posts:
    3
    I also had some issues, but using your
    Code (CSharp):
    1. RenderTextureDescriptor d = XRSettings.eyeTextureDesc;
    2. d.width = d.width / 2; // THIS DOES NOT MAKE SENSE
    3. RenderTexture t = new RenderTexture(d);
    fixed it for me! Thanks a lot.