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.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Refractive Image Effect in VR: texture misalignment between both eyes?

Discussion in 'Image Effects' started by argosy_ops, Aug 6, 2017.

  1. argosy_ops

    argosy_ops

    Joined:
    Dec 27, 2012
    Posts:
    49
    Hi everyone. I haven't found anything through search, so I hope you can help me out with the following issue: I'm trying to make a full-screen refractive effect for use in VR, using multi-pass rendering and a simple displacement or normal map. The problem I run into though is that I'm seeing the normal map twice in VR. I've seen shader functions for single-pass rendering, but it doesn't even work in multi-pass yet, so I'm guessing the problem is somewhere else. Here's the shader code:

    Code (CSharp):
    1. Shader "hidden/refract"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _DisplaceTex("Displacement Texture", 2D) = "white" {}
    7.         _Magnitude("Magnitude", Range(0,0.1)) = 1
    8.     }
    9.     SubShader
    10.     {
    11.         // No culling or depth
    12.         Cull Off ZWrite Off ZTest Always
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.            
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.            
    42.             uniform sampler2D _MainTex;
    43.             uniform sampler2D _DisplaceTex;
    44.             float _Magnitude;
    45.             half4 _MainTex_ST;
    46.  
    47.             fixed4 frag (v2f i) : SV_Target
    48.             {
    49.                 //fixed4 col = tex2D(_MainTex, i.uv);
    50.                 // just invert the colors
    51.                 //col = 1 - col;
    52.  
    53.                 float2 disp = tex2D(_DisplaceTex, i.uv).xy;
    54.                 disp = ((disp * 2) - 1) * _Magnitude;
    55.  
    56.                 float4 col = tex2D(_MainTex, i.uv + disp);
    57.                 return col;
    58.             }
    59.             ENDCG
    60.         }
    61.     }
    62. }
    63.