[Edit: The errors in my assumptions were: 1. confusion between NDC and clip space;
And, 2. assuming that clip space goes from [-1,-1,0] ro [+1,+1,+1]. It goes from [-1,-1,-1] to [+1,+1,+1].
Therefore, the coordinates of the bottom-left of near clip rect in clip space are (-1,-1,-1), and so on…]
Given a projection matrix (with no assumptions about what kind of projection matrix it is, could be oblique/off-center, etc), I attempted to get the frustum corners as InverseProjectionMatrix * {corners in NDC space}, however the results seem to be incorrect. Here is the code I used:
/// This is what I need to work
void GetFrustumCornersFromCameraMatrix()
{
// The frustum rect at near clip plane is the inverse projection of a rectangle
// with bounds [-1,-1,0] to [+1,+1,0] in normalized device coordinates.
Camera cam = this.GetComponent<Camera>();
var invMat = cam.nonJitteredProjectionMatrix.inverse;
var bottomLeft = invMat * new Vector4(-1, -1, 0, 1);
var topRight = invMat * new Vector4(1, 1, 0, 1);
bottomLeft /= bottomLeft.w;
topRight /= topRight.w;
this.left = bottomLeft.x;
this.right = topRight.x;
this.bottom = bottomLeft.y;
this.top = topRight.y;
}
/// For comparison purposes only - this works perfectly
void GetFrustomCornersFromCameraSettings()
{
Camera cam = this.GetComponent<Camera>();
float frustumHeight =
2.0f * cam.nearClipPlane
* Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
var frustumWidth = frustumHeight * cam.aspect;
this.left = -frustumWidth * 0.5f;
this.right = frustumWidth * 0.5f;
this.bottom = -frustumHeight * 0.5f;
this.top = frustumHeight * 0.5f;
}
private void OnDrawGizmos()
{
var matbk = Gizmos.matrix;
Camera cam = this.GetComponent<Camera>();
float radius = 0.02f;
Gizmos.matrix = this.transform.localToWorldMatrix;
Gizmos.DrawSphere(new Vector3(left, top, cam.nearClipPlane), radius);
Gizmos.DrawSphere(new Vector3(left, bottom, cam.nearClipPlane), radius);
Gizmos.DrawSphere(new Vector3(right, top, cam.nearClipPlane), radius);
Gizmos.DrawSphere(new Vector3(right, bottom, cam.nearClipPlane), radius);
Gizmos.matrix = matbk;
}
As you can see from the Gizmos, the returned coordinates are larger than what should be.
(E.g., left should be -0.04430015, but I’m getting back -0.08857372, appears almost double but is not double)

What am I doing wrong? Thanks in advance.