How to associate a material with left and right cameras textures in zCore Monobehavior?

I have a stereo video feed in Unity with two videos - one for the left eye and one for the right eye. I’m trying to setup a quad with the video textures on them but only allow the left camera to render when the zCore object is rendering a left view, and the same with the right.

I know zSpace uses quad stereo buffering, but I see know way in Unity of accessing the left and right camera textures? Is this possible?

We tried using “single pass rendering” and in the frag shader checking the unity_StereoEyeIndex == 0 but there doesn’t appear to be an Index.

Please let me know if you have any suggestions as I’d really like to use Unity not the native SDK for this project - but the left and right cameras appear to be handled by the native plugin.

I looked into unity_StereoEyeIndex, and confirmed that it does work. See below for the shader I roughed out. Initially, I wasn’t sure it was working because zCore’s Preview Window wasn’t reflecting such, and would only show index 0. However, when I made a standalone build, it showed the shader working just fine. The issue you’re encountering may have to do with the fact that the Preview Window implements its own render path that doesn’t always reflect what will show up in builds. When in doubt, I recommend cutting a build and verifying whether there are any differences between it and the preview window.

Alex S.

Shader "Unlit/LeftRightEyeColors"
{
    Properties
    {
        _LeftColor ("LeftColor",  Color) = (1,0,0,1)
        _RightColor("RightColor", Color) = (0,0,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            struct appdata
            {
                float4 pos : POSITION;
            };
            struct v2f
            {
                float4 pos : POSITION;
                float4 clr : COLOR;
            };
            float4 _LeftColor;
            float4 _RightColor;
            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.pos);
                if(unity_StereoEyeIndex == 0){
                    o.clr = _LeftColor;
                }else{
                    o.clr = _RightColor;
                }
                return o;
            }
            fixed4 frag (v2f i) : COLOR
            {
                return i.clr;
            }
            ENDCG
        }
    }
}

Hi Alex et al.,

After a few days of experimentation I found a solution that will work with Unity 2018.2.

If you attach the following script to your main camera it should allow you to associate layers with game objects. In my example I have created left, right, and background layers. Background layer shows up in both eyes, left only in left and right only in the right eye. It’s a bit of a hack but I was not able to get the mimic camera script to work without the matrix transforms swimming around on the screen. (objects not fixed but moving relative to the head.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PreCullLayers : MonoBehaviour {
    private Camera zSpace_cam;
#if !UNITY_EDITOR
    private bool toggleLR;
#endif
    void Start () {
        zSpace_cam = this.GetComponent<Camera>();
#if !UNITY_EDITOR
        toggleLR = true;
#endif
}
    // To debug a standalone project use the log file:
    // Debug.Log("output");
    // see output   C:\Users\CDI_ZSpace\AppData\LocalLow\DefaultCompany\StandaloneLRCamerasZSpace\output.log
    //in standalone:
    //  zSpace_cam.stereoTargetEye is always BOTH
    //  zSpace_cam.stereoActiveEye is always LEFT  -- so left and right cameras are managed in some protected space
    //this hack works for the standalone version because precull is called first for left then right
    //the coordinate system is corect when "multi-pass rendering" is enabled in the player settings - otherwise the matrix transformations are off.
    private void OnPreCull()
    {
      //for the standalone player, toggle L/R (starting with left every time onPreCull is called).
#if !UNITY_EDITOR
        if (toggleLR)
        {
            zSpace_cam.cullingMask = (1 << LayerMask.NameToLayer("left")) | (1 << LayerMask.NameToLayer("background"));
        } else
        {
            zSpace_cam.cullingMask = (1 << LayerMask.NameToLayer("right")) | (1 << LayerMask.NameToLayer("background"));
        }
        toggleLR = !toggleLR;
#elif UNITY_EDITOR
        //the ZSpace preview window has a seperate render path defined in ZCore.preview.cs
        //I hacked the target textures in order to determine which texture is targeted before the cull layers.
        if(zSpace_cam.targetTexture != null)
        {
            if(zSpace_cam.targetTexture.name == "LeftPreviewRenderTexture")
            {
                zSpace_cam.cullingMask = (1 << LayerMask.NameToLayer("left")) | (1 << LayerMask.NameToLayer("background"));
            }
            if (zSpace_cam.targetTexture.name == "RightPreviewRenderTexture")
            {
                zSpace_cam.cullingMask = (1 << LayerMask.NameToLayer("right")) | (1 << LayerMask.NameToLayer("background"));
            }
        }
#endif
    }
}