Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
kronnect's avatar
kronnect
Explorer
10 years ago

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!

5 Replies

Replies have been turned off for this discussion
  • 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?
  • 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.
  • 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.
  • 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.
  • 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?