Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

VR WorldSpaceViewDir in shaders not working in VR (fix included in post)

Discussion in '5.4 Beta' started by ccsander, May 31, 2016.

  1. ccsander


    May 17, 2013
    For a both-eye camera and with single-pass stereo off, WorldSpaceViewDir returns the exact same vector. This results in specular/gloss not being correctly stereoscopic. All surface shaders appear to use this function when passing viewDir to the lighting function.

    In UnityCG.cginc this function is defined this way:

    inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
    return - worldPos;

    I have found it can be fixed by using the unity_CameraToWorld matrix as follows:

    inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
    float3 wpos;
    wpos.x = unity_CameraToWorld[0][3];
    wpos.y = unity_CameraToWorld[1][3];
    wpos.z = unity_CameraToWorld[2][3];
    return wpos - worldPos;

    It is kind of a pain to override UnityCG.cginc, so I'm hoping there will be an official fix for this. I haven't been able to move to single-pass stereo due to poorer performance and also broken effects.

  2. thep3000


    Unity Technologies

    Aug 9, 2013
    ccsander likes this.