Forum Discussion

GeniusZHUWXY's avatar
GeniusZHUWXY
Copper Contributor
Oct 02, 2024

How to make HoloLens2's eyes display different content

Hi everyone, I'm working on something. I want HoloLens 2 to display different content on the left and right eyes, when the object is not mounted under the camera.

  • haseebjadoon05's avatar
    haseebjadoon05
    Copper Contributor
    To make the HoloLens 2 display different content in each eye, you can use a technique called **stereo rendering**. This involves rendering two separate images, one for each eye, creating a sense of depth and 3D perception. Here's how to implement this in a Unity application with the Mixed Reality Toolkit (MRTK):

    ### 1. Set Up Your Project
    - **Install Unity**: Ensure you have Unity installed with the Mixed Reality Toolkit.
    - **Configure Your Project**: Create a new project and set it up for HoloLens development:
    - Go to `File > Build Settings`, select `Universal Windows Platform`, and set your target device to `HoloLens`.
    - Enable `XR Settings` in the project settings and configure the XR plugin management for the Mixed Reality Toolkit.

    ### 2. Create a Custom Shader
    - To render different content for each eye, you will typically need to write a custom shader. The shader will adjust the texture coordinates based on the eye being rendered.

    ```hlsl
    Shader "Custom/StereoContentShader"
    {
    Properties
    {
    _MainTex ("Texture", 2D) = "white" {}
    }
    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 200

    Pass
    {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

    #include "UnityCG.cginc"

    struct appdata_t
    {
    float4 vertex : POSITION;
    float2 uv : TEXCOORD0;
    };

    struct v2f
    {
    float2 uv : TEXCOORD0;
    float4 vertex : SV_POSITION;
    UNITY_FOG_COORDS(1)
    };

    sampler2D _MainTex;
    float4 _MainTex_ST;

    v2f vert (appdata_t v)
    {
    v2f o;
    o.vertex = UnityObjectToClipPos(v.vertex);
    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    UNITY_TRANSFER_FOG(o,o.vertex);
    return o;
    }

    fixed4 frag (v2f i) : SV_Target
    {
    // Use a different texture or adjust based on the eye index
    fixed4 col = tex2D(_MainTex, i.uv);
    // Logic to determine which eye is rendering and modify the content accordingly
    return col;
    }
    ENDCG
    }
    }
    }
    ```

    ### 3. Modify Content for Each Eye
    - In your Unity script, check which eye is being rendered and load different content accordingly.

    ```csharp
    using UnityEngine;
    using UnityEngine.XR;

    public class EyeContentSwitcher : MonoBehaviour
    {
    public Texture leftEyeTexture;
    public Texture rightEyeTexture;

    private Material material;

    void Start()
    {
    material = GetComponent<Renderer>().material;
    }

    void Update()
    {
    if (XRSettings.eyeTextureDesc.vrUsage == VRTextureUsage.TwoEyes)
    {
    if (XRSettings.eyeTextureDesc.eye == XRNode.LeftEye)
    {
    material.mainTexture = leftEyeTexture;
    }
    else
    {
    material.mainTexture = rightEyeTexture;
    }
    }
    }
    }
    ```

    ### 4. Test on HoloLens 2
    - Build and deploy your application to the HoloLens 2.
    - Ensure that the stereo rendering is working, with different content displayed in each eye.

    ### Additional Considerations
    - **Performance**: Rendering different content in each eye can impact performance. Optimize your assets and shaders as needed.
    - **User Experience**: Consider how different content might affect user experience and ensure it adds value to the application.

    By following these steps, you can create an engaging experience on HoloLens 2 that takes advantage of its mixed reality capabilities!

Resources