Skip to main content
New Participant
November 8, 2015

Weird problem with fog image effect

Hi guys,

I have a strange issue regarding Oculus DK2 and a full-screen post image effect.

This is how it looks on a PC with the Debug Mode enabled (no device attached but OVR is running in debug mode) on and htiting play into the editor. It plays ok:



And this is what happens in another PC with the device (wrong!):



Note that the image effect runs fine on Windows and Mac. It just get distorted when Virtual Reality checkbox is enabled and during playmode.

This is the vertex shader:
   float4x4 _ClipToWorld;

struct appdata {
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};


struct v2f {
float4 pos : POSITION;
float2 uv: TEXCOORD0;
float2 depthUV : TEXCOORD1;
float3 cameraToFarPlane : TEXCOORD2;
};

v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.depthUV = MultiplyUV(UNITY_MATRIX_TEXTURE0, v.texcoord);
o.uv = o.depthUV;

#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0) {
// Depth texture is inverted WRT the main texture
o.depthUV.y = 1 - o.depthUV.y;
}
#endif

// Clip space X and Y coords
float2 clipXY = o.pos.xy / o.pos.w;

// Position of the far plane in clip space
float4 farPlaneClip = float4(clipXY, 1, 1);

// Homogeneous world position on the far plane
[B] farPlaneClip *= float4(1,-1,1,1);[/B]
float4 farPlaneWorld4 = mul(_ClipToWorld , farPlaneClip);

// World position on the far plane
float3 farPlaneWorld = farPlaneWorld4.xyz / farPlaneWorld4.w;

// Vector from the camera to the far plane
o.cameraToFarPlane = farPlaneWorld - _WorldSpaceCameraPos;
return o;
}



_ClipToWorld is set from the C# script as:

material.SetMatrix ("_ClipToWorld", Camera.main.cameraToWorldMatrix * Camera.main.projectionMatrix.inverse);


Note the line above:
farPlaneClip *= float4(1,-1,1,1);

I had to introduce this to make the fog effect work on my PC with the debug mode on. But it still doesn't work on the PC with Oculus DK2.

So, right now I'm a little lost since it works then I enable Debug but it doesn't otherwise on the other PC?

Anyone has faced a similar problem with Oculus / Virtual Reality enabled? Is this a bug?
Also I'd love to read some details about how does Virtual Reality check affect projections in Unity since at this moment I'm rather lost.

thank you very much in advance!
    This topic has been closed for replies.

    5 replies

    Inspiring
    November 8, 2015
    This has been a bug for a while. I first saw it with Global Fog. I would guess your effect works similar.

    Can you let me know what version of Unity you are using and which SDK?
    AMD Ryzen 7 1800X | MSI X370 Titanium | G.Skill 16GB DDR4 3200 | EVGA SuperNOVA 1000 | Corsair Hydro H110i Gigabyte RX Vega 64 x2 | Samsung 960 Evo M.2 500GB | Seagate FireCuda SSHD 2TB | Phanteks ENTHOO EVOLV
    kronnectAuthor
    New Participant
    November 9, 2015
    Issue occurs with Unity 5.3.0b5 on Windows 10 with Oculus 0.8.

    However it works ok on 5.2.2p3 with same SDK so I guess it's either a regression bug or something new??
    Unity has been informed of this as well.
    Up and Coming
    November 9, 2015
    I have the same issue. Using unity 5.1.3p1 with SDK 0.7. All image effects don't show up in standalone build, but are fine in the editor.
    Inspiring
    November 9, 2015
    Unity recently fixed an issue with several built-in depth-buffer-based effects (such as reflections) where the view matrix was flipped on the Y axis to deal with the texture coordinate conventions of different drivers. That was fixed in 5.2.1p3 and it may have affected fog.
    Participant
    February 23, 2016
    We also seem to suffer from the same problem.

    We have quite many effects in the game that need inverse MVP matrix in the shader (both post process, and traditional materials). It seems that the matrix is incorrect, producing incorrect effects.

    Basically the result of this function is incorrect: (cam.projectionMatrix * cam.worldToCameraMatrix).inverse

    Is there any workaround or is there a fix coming up soon?