Hello!
Currently, occlusion matrices are computed completely incorrectly by Unity. For some reason, they appear to be calculated based upon not the position/scale of the camera, but rather the position/scale of the near clipping plane. This can be seen here:
If you will notice, the near clipping plane of the occlusion matrix is equivalent in distance from the near clipping plane as the main camera is distant from the near clipping plane.
Is there a fix for this built into Unity? If not, how can I fix it with matrix multiplication? It’s seriously screwing up my performance, since I can’t use occlusion culling with any kind of custom camera.
This is an engine bug where the culling planes passed to Umbra are incorrectly extracted from the culling matrix. It’s been like this for a long time, and I’ve filed a bug for it in the past. Feel free to file a new one so it gets more weight added.
I’ve been using this as a workaround:
using UnityEngine;
[ExecuteInEditMode]
public class FixUmbraCullingMatrix : MonoBehaviour {
Camera m_Camera;
void OnEnable() { m_Camera = GetComponent<Camera>(); }
void OnDisable() { m_Camera.ResetCullingMatrix(); }
void OnPreCull () {
m_Camera.cullingMatrix = Matrix4x4.Perspective(m_Camera.fieldOfView, m_Camera.aspect, m_Camera.nearClipPlane * 0.5f, m_Camera.farClipPlane) * m_Camera.worldToCameraMatrix;
}
}
(dump it into a file called FixUmbraCullingMatrix.cs and attach the behaviour to the camera instance)
Thanks! That definitely helps me with occlusion for most cases. However, it unfortunately doesn’t help with my specific use case: Oblique near clipping planes. As the near clip becomes more rotated, the occlusion matrix scales with that rotation twice as fast as it should, which makes the occlusion matrix escape the bounds of the projection matrix and invert, exploding all over the place. Do you have any idea how I could go about trying to fix that? If not, could you explain a little bit about how an occlusion matrix even works within Unity? There’s literally no documentation, but based upon your usage of Matrix4x4.perspective I’m surprised that Unity doesn’t just let you set the occlusion matrix equal to the projection one (when you do this, the occlusion matrix is not displayed at all and it does not function whatoever
Thank you for your help! I have filed a bug report about this issue as well.
I’m surprised that Unity doesn’t just let you set the occlusion matrix equal to the projection one
It does, but it also need the camera transform, i.e. cam.cullingMatrix = cam.projectionMatrix * cam.worldToCameraMatrix;
Sorry, I haven’t tried working around this bug for oblique frusta. With a little luck, perhaps you can just halve the distance to the clipping plane the same way my workaround halves it for the near plane, but I haven’t tried this myself.
Good news! Unity got back to my bug report, and informed me that they are working on fixing it as they were able to reproduce it. How long does this typically take?
Yes, some testing revealed it, its still there, and there is also an issue filed.
This is actually quite a hefty thing, since even the standard projection culling is broken without the fix up here
Heyo, that’s my bug submisson! Thanks for giving it some weight ;p
I’m excited for this to get fixed because as soon as it does, my asset (and a ton of others) will start working correctly with mad performance boosts… Specifically for VR, which is a focus for Unity right now.
The issue is marked as duplicate now, pointing to a 404.
It might have been fixed, I couldn’t test it yet though.
A similar thread was commented/closed by staff here:
Can anyone confirm this is solved now? I didn’t find anything in any patch notes yet…
I can confirm that this is NOT solved in the newest release. The near clipping plane now matches the actual projection matrix, but oblique culling still shatters functionality. https://i.imgur.com/ZExPaDE.png
I would like to know if we’d need to post a new issue for the oblique culling matrix bug described in this thread, or if this is also already adressed?
The fix for this is in 2017.3.0b8 and staging for 2017.2 (still going through review). Could you check 2017.3 and see if it solves your issues? If not, it would be very helpful if you could submit a new bug report.
Thanks Philipp, your help is much appreciated! Could you reply with the case # please, once you get it? Then I can forward it to the team. Sorry for the inconveniences.
I have to ask, will this likely bump back the actual resolution? This new issue topic has few votes and is classified as “workaround is possible”, which isn’t true- there is currently no solution to this unfortunate issue.