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

    ccsander

    Joined:
    May 17, 2013
    Posts:
    44
    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 _WorldSpaceCameraPos.xyz - 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.

    ccs
     
  2. thep3000

    thep3000

    Unity Technologies

    Joined:
    Aug 9, 2013
    Posts:
    399
    ccsander likes this.