Skip to content

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

@Zallist

Description

@Zallist

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:

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

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions