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

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

We are aware of this issue and it’s scheduled to be fixed before 5.4 is released.

I made the case public, it should show up here once the issue tracker site updates: Unity Issue Tracker - [VR] Reflections rendering in Mono

(case 754196)

1 Like