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. Dismiss Notice

Detect VR and eye in shader

Discussion in 'Shaders' started by SunnySunshine, Jul 30, 2016.

  1. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Is there a way to tell (in a shader) which VR eye is being rendered?
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Goodness, that sounds like it should be easy but it doesn't seem to be.

    My only idea (that doesn't involve keeping track of a ton of material references) is to have 2 cameras targeted to your two separate eyes. In both eyes, render a fullscreen quad first with "blend zero one" and have each one set a different stencil value. Set up 2 passes on all your shaders that care about which eye you're using, each pass set to only render with the stencil value for the specific eye you want to see it. This may be GPU intensive, as each eye will have to run both passes and discard the unwanted pass per-pixel, so I'd limit the number of objects you use this technique on.
     
  3. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
  4. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Yeah, that seems way too heavy. I'd rather wait for Unity to implement an eye index in that case. I see they have one already actually, but it's only available if single stereo pass rendering is enabled.
     
  5. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    I suppose one way to do this would be to create two cameras, and set eye target for each one. Then, OnPreRender, set a global shader variable telling the shaders which one of them is rendering. But that seems so long winded.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    If you're using 5.4 and single pass stereo I believe there is now an index you can work off of. Not sure about non-single pass.

    If you've already downloaded the built-in shader source files for Unity 5.4 open up UnityCG.cginc and look for UnityClipSpacePos or something like that. I can't remember the exact name. It has a special form for single pass stereo that gets the object to world transform from an array with an index for the left and right eye.

    edit: That's not going to be helpful as the actual stereo index is hidden in a #define elsewhere such that by the time you get to that function (which is actually UnityObjectToClipPos) the unit_ObjectToWorld matrix has already been assigned the from the matrix array.

    The value you're really after is:
    unity_StereoEyeIndex
     
    Last edited: Jul 30, 2016