HoloLens Unity: Rendering different imagery to each eye

As part of my research work using the Microsoft HoloLens, I recently had to find a way to render different imagery to each eye (for example, running a shader with a different texture for geometry depending on whether I was viewing it with the left or right eye).

Initially, I had tried the approach discussed by davidlively in the Unity forums, where we take advantage of the fact that OnPreRender() is called twice every frame when rendering in stereo. In this approach, we keep track of a variable for the active eye (0 for left, 1 for right), and alternate between those values (and update a flag in a shader) on each call of OnPreRender(). However, while this is said to work for the Oculus Rift, the HoloLens is different (some pre-optimization?) and this approach had no effect.

Thanks to huntertank on the HoloLens forum for mentioning an approach that seems to work: in Unity, create two separate Camera objects, and make sure they are placed on separate layers. In the Camera settings in the Inspector, set “Target Eye” to Left for one eye, and Right for the other. Attach a script with an OnPreRender() function to each Camera, and in that OnPreRender() function set the uniform value in your shader to left or right.

The result of this is that I can, for example, render scene geometry in red in one eye and render it in blue in the other eye.

 

4 thoughts on “HoloLens Unity: Rendering different imagery to each eye”

  1. Hi Dan,

    I do not understand the “uniform value” part you are talking about in the OnPreRender() function.
    I would be very happy if you could provide a code example or further explanation. :)

    Cheers,

    Andy

  2. Certainly! By “uniform” I am referring to what Cg/HLSL seems to call “shader properties” (the equivalent is “uniform” in OpenGL, which is what I’m more familiar with). In essence, they are variables in the shader that have the same value for all vertices/fragments in a single render pass. For example, if you had a shader program in which you wanted to render things differently depending on if it was the left eye or the right eye, you might have an integer shader property “WhichEye” where 0=left and 1=right, and then use that value in the shader to change how the content is rendered.

    See https://docs.unity3d.com/Manual/SL-PropertiesInPrograms.html for more info.

  3. Hi Dan,
    I am newbie to this but I have two cameras (right and left )and I want to show the output of each camera to eye.
    Does it work in the same way you provide above?

    Thank you very much.

    1. Yes, that should work. Every time there is a new frame from each camera, you will need to set a texture uniform to point to that frame, and then render each eye with a shader that uses that texture as an input.

Leave a Reply

Your email address will not be published. Required fields are marked *