Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Adding a stereoscopic Image within a VR Scene?

Discussion in 'VR' started by mlaubenheimer, Jul 23, 2022.

  1. mlaubenheimer

    mlaubenheimer

    Joined:
    Jun 25, 2022
    Posts:
    6
    Hi Everyone! I am creating a virtual art gallery in Unity for the Oculus Quest headset and I am trying to figure out how to add a stereoscopic image to my scene. Like add it to a room that already has photos on the walls. meaning i want the stereoscopic image to be a part of the room, like hanging on the wall. It seems to me this should be possible. maybe a custom shader? or a script that switches shaders quickly back and forth between the two sides (left right images) of the stereoscopic image.

    Any thoughts?

    Mark
     
  2. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hi,

    It depends on which render pipe line you are using, and if its single pass or multi pass rendering.
    Then you need a shader that support vr images, like side by side, or two textures, one for each eye for that environment.
     
  3. mlaubenheimer

    mlaubenheimer

    Joined:
    Jun 25, 2022
    Posts:
    6
    I'm using the URP Pipeline. with MultiPass (If I remember correctly).

    The more I think about it the more it must be a custom shader.
     
  4. TomGoethals

    TomGoethals

    Joined:
    Jan 29, 2022
    Posts:
    36
    There is a node in the shadergraph to get the current eyeIndex, use it to toggle between 2 textures in your material
     
    fuzzy3d likes this.
  5. mlaubenheimer

    mlaubenheimer

    Joined:
    Jun 25, 2022
    Posts:
    6
    here is the stereoscopic shader i created if anyone is interested:

    Code (CSharp):
    1. Shader "Custom/Stereoscopic Image" {
    2.     Properties
    3.     {
    4.         [NoScaleOffset] _MainTex("Left Eye Texture", 2D) = "white" {}
    5.     [NoScaleOffset] _MainTex2("Right Eye Texture", 2D) = "white" {}
    6.     }
    7.         SubShader
    8.     {
    9.         Pass
    10.         {
    11.             // indicate that our pass is the "base" pass in forward
    12.             // rendering pipeline. It gets ambient and main directional
    13.             // light data set up; light direction in _WorldSpaceLightPos0
    14.             // and color in _LightColor0
    15.             Tags {"LightMode" = "ForwardBase"}
    16.  
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             #include "UnityCG.cginc" // for UnityObjectToWorldNormal
    21.             #include "UnityLightingCommon.cginc" // for _LightColor0
    22.  
    23.             struct v2f
    24.             {
    25.                 float2 uv : TEXCOORD0;
    26.                 //float2 uv2 : TEXCOORD0;
    27.                 fixed4 diff : COLOR0; // diffuse lighting color
    28.                 float4 vertex : SV_POSITION;
    29.                 UNITY_VERTEX_OUTPUT_STEREO
    30.             };
    31.  
    32.             v2f vert(appdata_base v)
    33.             {
    34.                 v2f o;
    35.                 UNITY_SETUP_INSTANCE_ID(v);
    36.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.texcoord;
    39.  
    40.                 // get vertex normal in world space
    41.                 half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    42.                 // dot product between normal and light direction for
    43.                 // standard diffuse (Lambert) lighting
    44.                 half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    45.                 // factor in the light color
    46.                 o.diff = nl * _LightColor0;
    47.                 o.diff.rgb += ShadeSH9(half4(worldNormal, 1));
    48.                 return o;
    49.             }
    50.  
    51.  
    52.  
    53.             sampler2D _MainTex;
    54.             sampler2D _MainTex2;
    55.  
    56.             fixed4 frag(v2f i) : SV_Target
    57.             {
    58.  
    59.  
    60.  
    61.                 UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
    62.  
    63.                     //half4 tex;
    64.                     //half3 c;
    65.  
    66.                     if (unity_StereoEyeIndex == 0) {
    67.                         // Left Eye
    68.                         fixed4 col = tex2D(_MainTex, i.uv);
    69.                         //c = DecodeHDR(tex, _TexRight_HDR);
    70.                         col *= i.diff;
    71.                         return col;
    72.                     }
    73.          else {
    74.                         // Right Eye
    75.                         fixed4 col = tex2D(_MainTex2, i.uv);
    76.                         //tex = half4(1,0,0,1);
    77.                         //c = DecodeHDR(tex, _TexRight_HDR);
    78.                         col *= i.diff;
    79.                         return col;
    80.                     }
    81.  
    82.  
    83.  
    84.                 // sample texture
    85.                 //fixed4 col = tex2D(_MainTex2, i.uv);
    86.             // multiply by lighting
    87.  
    88.             //  col *= i.diff;
    89.             //return col;
    90.         }
    91.         ENDCG
    92.     }
    93.     }
    94. }