Repository: godot
Issue: unproject_position and is_position_in_frustum returns wrong results if window is resized to a different aspect ratio
Labels: bug,topic:core,topic:3d

### Godot version

4.0.3

### System information

Windows 10 - Godot v4.0.3.stable - Vulkan (Forward+) - dedicated NVIDIA GeForce GTX 1070 (Game Ready Version 528.49) - Intel Core i7-7700K CPU @ 4.20GHz (8 Threads)

### Issue description

Problem:
I get wrong results for unproject_position and is_position_in_frustum when the aspect ratio is not the same as set in the project settings' viewport size.

As you can see in my settings, I have 1:1 aspect ratio in project settings' viewport size, in order to support both landscape and portrait screens. It's a recommended setting in one of the green tip boxes at https://docs.godotengine.org/en/stable/tutorials/rendering/multiple_resolutions.html. Which also seems to be working the best for keeping relative sizes of GUI elements for different aspect ratios. But I'm using 1.77 aspect ratio in project settings' viewport override size, so the game's initial window can fit my own screen during development. And this is how I encountered the problem.

Although the 3D object is visible on screen, is_position_in_frustum returns false if the object is a bit far on left or right side of the viewport. And unproject_position for global_transform.origin can return even negative values on left parts of the viewport, and it doesn't match the correct coordinates on the right side either. It behaves as if the window is narrower on x axis.

If I change camera's "Keep Aspect" value from keep height to keep width, then the functions start working correctly for x axis, but they behave as if the viewport is higher, like, is_position_in_frustum keeps returning true till some point even when the object is vertically outside of the viewport, like very below or above and not visible.

The functions return correct results only if the current aspect ratio of the window is the same with the viewport size in the project settings.

What do I expect?
I expect the functions to return correct results, regardless of the aspect ratio being same or not with the viewport size set in the project settings.

### Steps to reproduce

My settings:

PROJECT
viewport width 1920
viewport height 1920
window width override 640
window height override 400
resizable true
mode viewport
aspect expand
scale 1
orientation sensor

SUBVIEWPORTCONTAINER
stretch true
stretch shrink 1

SUBVIEWPORT
size 1920 1920
size 2d override 1920 1920
size 2d override stretch true

CAMERA
keep aspect keep height
projection perspective


My node structure:

Control
- SubViewportContainer
- - SubViewport
- - - Camera3D
- - - MeshInstance3D
- Control (for 2D interface elements...)


My logic:

The mesh instance in subject never moves, and my camera has RTS like angle. I use _physics_process(delta), and I simply make the camera move left, right, forward and backward with W, A, S, D and print(camera.is_position_in_frustum(subject.global_transform.origin), camera.unproject_position(subject.global_transform.origin), subviewport.size, subviewport.size_2d_override, get_viewport().size, get_viewport().get_visible_rect().size, get_viewport_rect().size, get_global_rect().size). Then I just compare the values, but they don't match with anything.

Unless my window's aspect ratio is the same with the viewport size in the project settings, I keep getting wrong results on x axis. And if I change the camera's Keep Aspect value from keep height to keep width, then I keep getting wrong results on y axis.

### Minimal reproduction project

[terminal.zip](https://github.com/godotengine/godot/files/11663791/terminal.zip)