Search Unity

Shader For VR / Different color on each eye

Discussion in 'Shaders' started by atmuc, Dec 12, 2018.

  1. atmuc

    atmuc

    Joined:
    Feb 28, 2011
    Posts:
    1,166
    How can I write a simple shader to have different colour on each eye? I can use single or multiple pass. I will add a cube to the scene. I will add a material with my custom shader. I will add 2 color properties to this shader. i.e. Left eye will see this cube as red, right eye will see this cube as green.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,447
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    For single pass, use unity_StereoEyeIndex as suggested by @mgear. For multi pass you have to setup two cameras, one for each eye, and use layer masks. Annoyingly they don't set unity_StereoEyeIndex for multi pass VR, but I suspect they consider that mode deprecated internally as once single pass was added they haven't really touched it.

    Note that in a vertex fragment shader to access that variable you need to use a couple of macros to initialize that value in the fragment shader. See this page:
    https://docs.unity3d.com/Manual/SinglePassInstancing.html

    The last example on that page is in fact exactly what you're looking to do.
     
    ateti likes this.
  4. ateti

    ateti

    Joined:
    Feb 28, 2021
    Posts:
    1
    Hi there,
    I am doing a different application. I have used https://docs.unity3d.com/Manual/SinglePassInstancing.html to show different colors on each eye.

    How can I make these object transparent ? I don't know shader scripting very well.


    Shader "XR/StereoEyeIndexColor"
    {
    Properties
    {
    _LeftEyeColor("Left Eye Color", COLOR) = (0,1,0,0.5)
    _RightEyeColor("Right Eye Color", COLOR) = (1,0,0,0.5)
    }

    SubShader
    {
    Tags { "RenderType" = "Opaque" }

    Pass
    {
    CGPROGRAM

    #pragma vertex vert
    #pragma fragment frag

    float4 _LeftEyeColor;
    float4 _RightEyeColor;

    #include "UnityCG.cginc"

    struct appdata
    {
    float4 vertex : POSITION;

    UNITY_VERTEX_INPUT_INSTANCE_ID
    };

    struct v2f
    {
    float4 vertex : SV_POSITION;

    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
    };

    v2f vert (appdata v)
    {
    v2f o;

    UNITY_SETUP_INSTANCE_ID(v);
    UNITY_INITIALIZE_OUTPUT(v2f, o);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

    o.vertex = UnityObjectToClipPos(v.vertex);

    return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

    return lerp(_LeftEyeColor, _RightEyeColor, unity_StereoEyeIndex);
    }
    ENDCG
    }
    }
    }