Given the 4 corners of a doorway in world space and a Camera, how would I go about calculating the tightest screen/viewport space rectangle I can wrap around the door? I'm implementing a modular building system where I only want to generate a room behind a door if the door is visible in the players view. The reason I need the bounds is that I can figure out whether a door on the next room can be seen through the first door (which simplifies to the intersection test between the two bounds). In edge cases it is totally fine the bounds to be too large but in no case should the bounds be smaller than the area the doorway actually takes on the screen because it can cause the player to see outside the generated area.
The approach that I tried was to convert each of the 4 corners into the viewport space of the camera (WorldToViewportPoint) and figure out the minimum and maximum values of each point on the XY plane. That worked wonderfully on most cases, but when I moved very close to the door, everything fell apart. From what it looks like the perspective projection seems to flip points behind the camera from left to right and from up to down. I could detect when the points are behind the camera by checking the Z coordinate of the projected (viewport) point but the problematic edge case is the one where only some of the points are behind the camera. For example, if I move very very close to the door and look straight up, the 2 upper corners of the door are in front of the camera and the 2 lower corners are behind it which causes a case where the returned bounds can be insufficiently small to capture the visible door.
The idea of finding a screen bounds of a rectangle in world sounds like there should be a relatively straight forward exact solution but I cannot think of one. Any ideas are appreciated.