Brunetons-Ocean icon indicating copy to clipboard operation
Brunetons-Ocean copied to clipboard

PlaneProjection doesn't generate correctly if the camera is pointed directly upwards or downwards

Open Zallist opened this issue 1 year ago • 0 comments

If the camera is pointed EXACTLY upwards or downwards, such that direction == Vector3(0,1,0) or Vector3(0,-1,0) then the projection will fail and the interpolation matrix is filled with NaN values.

This happens because of the following line of code: https://github.com/Scrawk/Brunetons-Ocean/blob/e764ccb6cf67c12bbbe40e170f7991332c981ddc/Assets/BrunetonsOcean/Scripts/Projection.cs#L353

This should become the following code:

            Vector3 zaxis = (position - target).normalized;
            Vector3 xaxis = Vector3.Cross(up, zaxis).normalized;

            if (Mathf.Approximately(xaxis.magnitude, 0f))
            {
                up = Vector3.right;
                xaxis = Vector3.Cross(up, zaxis).normalized;

                if (Mathf.Approximately(xaxis.magnitude, 0f))
                {
                    up = Vector3.forward;
                    xaxis = Vector3.Cross(up, zaxis).normalized;
                }
            }

            Vector3 yaxis = Vector3.Cross(zaxis, xaxis);

Basically, if the cross of zaxis and up is a zero vector, then pick right arbitrarily. If THAT is still somehow 0, then pick forward. The extra check to forward isn't really necessary but it's the standard pattern for LookAt.

Zallist avatar Mar 02 '23 18:03 Zallist