Search Unity

Question Render different color depending on stereo eye index

Discussion in 'Shader Graph' started by Desoxi, Jan 11, 2021.

  1. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    Hey there,

    I'm currently fiddling around with the google cardboard SDK and am trying to find out, how to render different images to the left and right eye. A multi-camera setup did not work as it seems to be unsupported and looking into the github issues page the only way at the moment seems to use the stereo eye index in some way.

    Is it possible to use this index in shadergraph in any way to control the shader behaviour? Apparently there are a lot of keywords like UNITY_VERTEX_OUTPUT_STEREO which is the eye index (?) or allows one to use them in a normal shader. But how do I access this from inside the shadergraph? Maybe with a custom shader?

    EDIT: So to my understanding "unity_StereoEyeIndex" is the index I'm looking for. Is there a way to use this in shadergraph somehow?

    I appreciate any helpful response, thanks in advance!
     
    Last edited: Jan 11, 2021
  2. Desoxi

    Desoxi

    Joined:
    Apr 12, 2015
    Posts:
    195
    Ok everyone, i found a solution to my issue and I hope this helps anyone out there who seeks to solve this as well.
    I gave it a show and just tried out to return the unity_StereoEyeIndex with a custom function node in shadergraph. And it worked! I found a few snippets which made me think, that "unity_StereoEyeIndex" is already used internally in shadergraph and I don't need to use fancy keywords or anything like that at all.

    Using the index, some extremely simple math (just subtract or do whatever suits your needs) and setting an Vector1 by code, gave me a way to render one object in different colors (and not only colors, you can customize the whole behaviour of it with this value) on the left and right eye without the need to set up multiple cameras.

    Here is a screen of my node:

    upload_2021-1-11_23-59-17.png

    And here is the code you need to add as component to the object which you want to make different for each eye:

    Code (CSharp):
    1. [SerializeField] MeshRenderer renderer;
    2.     [SerializeField] bool right = true;
    3.    
    4.     // Start is called before the first frame update
    5.     void Start()
    6.     {
    7.         renderer = GetComponent<MeshRenderer>();
    8.         renderer.material.SetInt("_rightEye", right ? 1 : 0);
    9.     }
     
    JuanGuzmanH and SpatialEffects like this.
  3. Burgrokash

    Burgrokash

    Joined:
    Sep 12, 2018
    Posts:
    9
    Absolutely brilliant. I will try this right a way!!!
     
    Desoxi likes this.
  4. gamexpad

    gamexpad

    Joined:
    Sep 8, 2021
    Posts:
    1
    Hi Desoxi,
    I am working on a similar project to display 2 different images over 2 eyes. I am new to Unity and cannot get this work out from you steps above. Do you mind to share a simple sample on github or somewhere?
    Thanks!
     
  5. SpatialEffects

    SpatialEffects

    Joined:
    Jan 13, 2020
    Posts:
    10
    This is great! Desoxi you're a genius. It works perfectly, but only when rendering is set to MultiView and not MultiPass.

    It works both in the Unity editor, when working via Oculus Link, and on the Quest 2 as a build.
     
  6. Slaymantis

    Slaymantis

    Joined:
    May 12, 2014
    Posts:
    11
    shader-eye.png Great work! Does anyone have a unity 2022 using URP shader graph screenshot, please?
    The closest I have is this screenshot but I still see the texture in both eyes in VR
     
    Last edited: Nov 29, 2022
  7. Slaymantis

    Slaymantis

    Joined:
    May 12, 2014
    Posts:
    11
    shader-eye2.png For anyone else stuck I managed to solve this using a custom script inside the shader graph to render the stereo video textures.
    Here is the StereoEyes.cginc code I used for the custom function

    void StereoEyes_half(float3 WorldCameraPosition, float3 WorldCameraRight, float Force, float4 Left, float4 Right, out float4 Out) {
    Out = Left;
    if(unity_StereoEyeIndex == 0){
    Out = Left;
    }
    if(unity_StereoEyeIndex == 1){
    Out = Right;
    }
    }
    void StereoEyes_float(float3 WorldCameraPosition, float3 WorldCameraRight, float Force, float4 Left, float4 Right, out float4 Out) {
    Out = Left;
    if(unity_StereoEyeIndex == 0){
    Out = Left;
    }
    if(unity_StereoEyeIndex == 1){
    Out = Right;
    }
    }
     
    Last edited: Dec 1, 2022
  8. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    74
    Thanks to all for your answers.

    I want to share with the community my small modification

    upload_2023-1-20_11-24-44.png

    If you change the type of output to boolean then you will be able to use branch and there is no need to multiply or perform (1 - x).
     
    ryg81 likes this.
  9. Pepn

    Pepn

    Joined:
    Sep 27, 2017
    Posts:
    9
    Did anyone get this working in Unity 2022.2 with URP? Seems that unity_StereoEyeIndex is always 0?
     
  10. JuanGuzmanH

    JuanGuzmanH

    Joined:
    Feb 8, 2018
    Posts:
    74
    I can not tell you for sure, because still some performance issues are not solved in Untity 2021 and 2022 so I don't use other versions than 2020 when I'm targeting Oculus Quest. I believe from 2021 Shader Graph includes a specific node for that called "Eye Index Node".
     
    ryg81 likes this.
  11. ryg81

    ryg81

    Joined:
    Jun 24, 2019
    Posts:
    5
    I am using Unity 2020.3, but I am not getting Right eye without enabling Multi View rendering.
    Can you suggest what can be wrong? I am using the same graph as you shared above. I have Quest2 in which when i see get only one eye.
    Thanks in advance.
    RYG81
     
  12. EloiExperiments

    EloiExperiments

    Joined:
    Mar 25, 2013
    Posts:
    2
    November 2023. I found a solution with the help of this post:
    upload_2023-11-10_1-43-23.png

    Thank to all of you.
     
    JuanGuzmanH likes this.
  13. ethanavarro

    ethanavarro

    Joined:
    Nov 21, 2022
    Posts:
    2
    What does this exactly do? What set up are you using for the textures?
     
  14. daggasoft

    daggasoft

    Joined:
    Jun 29, 2013
    Posts:
    22
    Is it possible for this whole thing to be exported so that I can use it? This is exactly what I'm looking for. I'm struggling to reconstruct it myself because I'm new to the shader graph but if I had a working copy I could inspect it and learn from it.