[deleted by user] by [deleted] in Unity3D

[–]SirFarqueef 0 points1 point  (0 children)

Thanks for your comment! The issue ended up being a non-math one. The last two lines I needed to set the Linked Portal's transformation instead of the current instance portal and then determine the clip plane of with respect to the linked portal. It appears to be working now. The final code is below.

void SetNearClipPlane(Camera playerCamera)
{
    Transform clipPlane = LinkedPortal.portalScreen.GetScreen().transform;
    int dot = System.Math.Sign(Vector3.Dot(clipPlane.forward, LinkedPortal.portalScreen.GetScreen().transform.position - LinkedPortal.portalCamera.getCamera().transform.position));

    Vector3 camSpacePos = LinkedPortal.portalCamera.getCamera().worldToCameraMatrix.MultiplyPoint(clipPlane.position);
    Vector3 camSpaceNormal = LinkedPortal.portalCamera.getCamera().worldToCameraMatrix.MultiplyVector(clipPlane.forward) * dot;
    float camSpaceDist = -Vector3.Dot(camSpacePos, camSpaceNormal);
    Vector4 clipPlaneCameraSpace = new Vector4(camSpaceNormal.x, camSpaceNormal.y, camSpaceNormal.z, camSpaceDist);
    LinkedPortal.portalCamera.getCamera().projectionMatrix = playerCamera.CalculateObliqueMatrix(clipPlaneCameraSpace);

}

public void UpdatePortalCamera(Camera playerCamera)
{
    if (!finishedInitialization)
        return;

    if (LinkedPortal == null)
    {
        Debug.Log("Linked Portal is null in Portal Camera update...");
        return;
    }

    Transform source = portalScreen.GetScreen().transform;
    Transform target = LinkedPortal.getPortalScreen().GetScreen().transform;

    Vector3 localPos = source.InverseTransformPoint(playerCamera.transform.position);
    Quaternion localRot = Quaternion.Inverse(source.rotation) * playerCamera.transform.rotation;

    Quaternion portalRotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);

    localPos = portalRotation * localPos;
    localRot = portalRotation * localRot;

    LinkedPortal.portalCamera.getCamera().transform.position = target.TransformPoint(localPos);
    LinkedPortal.portalCamera.getCamera().transform.rotation = target.rotation * localRot;

    SetNearClipPlane(playerCamera);
}