Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Display a custom image in fullscreen

Discussion in 'VR' started by badawim-idcc, Apr 13, 2021.

  1. badawim-idcc

    badawim-idcc

    Joined:
    Dec 10, 2015
    Posts:
    10
    I have a texture filled with 2 images side by side (one for each eye) that I want to display in full screen in my VR Headset. The first step I'm having trouble with is just displaying something in the headset.

    I am using the Oculus XR Plugin, in "Single Pass Instanced" rendering mode.

    From what I've read, I need to override OnRenderImage and blit what I need from there using my custom shader.

    For now, I've just written a debug shader that returns a solid color:
    Code (csharp):
    1. Shader "DebugFullscreen"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset] _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.  
    8.         SubShader
    9.         {
    10.             Tags { "RenderType" = "Opaque" }
    11.  
    12.             Pass
    13.             {
    14.                 CGPROGRAM
    15.  
    16.                 #pragma vertex vert
    17.                 #pragma fragment frag
    18.  
    19.                 #include "UnityCG.cginc"
    20.  
    21.                 struct appdata
    22.                 {
    23.                     float4 vertex : POSITION;
    24.                     float2 uv     : TEXCOORD0;
    25.  
    26.                     UNITY_VERTEX_INPUT_INSTANCE_ID
    27.                 };
    28.  
    29.                 struct v2f
    30.                 {
    31.                     float2 uv     : TEXCOORD0;
    32.                     float4 vertex : SV_POSITION;
    33.  
    34.                     UNITY_VERTEX_OUTPUT_STEREO
    35.                 };
    36.  
    37.                 v2f vert(appdata v)
    38.                 {
    39.                     v2f o;
    40.                     UNITY_SETUP_INSTANCE_ID(v);
    41.                     UNITY_INITIALIZE_OUTPUT(v2f, o);
    42.                     UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    43.  
    44.                     o.vertex = UnityObjectToClipPos(v.vertex);
    45.                     o.uv = v.uv;
    46.  
    47.                     return o;
    48.                 }
    49.  
    50.                 UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
    51.  
    52.                 fixed4 frag(v2f i) : SV_Target
    53.                 {
    54.                     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    55.  
    56.                      return float4(0, 1, 0, 1);
    57.                 }
    58.                 ENDCG
    59.             }
    60.         }
    61. }
    I've tried two approaches in OnRenderImage.

    This one:
    Code (CSharp):
    1. Graphics.Blit(my2DTexture, destination, myMaterial);
    And this one:
    Code (CSharp):
    1. RenderTexture previous = RenderTexture.active;
    2. RenderTexture.active = myRenderTexture;
    3. Graphics.Blit(my2DTexture, myRenderTexture);
    4. RenderTexture.active = previous;
    5. Graphics.Blit(myRenderTexture, destination, ImmersiveMaterial);
    I get green color in the left eye as expected, but nothing at all in the right eye.

    I initially tried the following fragment shader:
    Code (CSharp):
    1. fixed4 frag(v2f i) : SV_Target
    2.                 {
    3.                     UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    4.  
    5.                     return  UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.uv);
    6.                 }
    With this shader I get a solid grey image in the left eye (not at all what's in my texture) and still nothing in the right eye.

    There must be something I'm missing here, but I really don't know what.

    Thanks in advance for any help you may have.
     
    Last edited: Apr 13, 2021