godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.12k stars 69 forks source link

Add angled/oblique near and far camera planes #501

Open 19PHOBOSS98 opened 4 years ago

19PHOBOSS98 commented 4 years ago

Describe the project you are working on: I'm working on an easy to use portal assets but I want to implement frustum culling.

Describe the problem or limitation you are having in your project: I like the frustum camera mode you guys added, it got me so far into making a portal asset. But if you could just tweak it a bit to let me angle the near plane that would be awesome.

I replicated JFoNS mirror demo where he got the camera to stay parallel to the mirror which in turn kept the camera's near plane parallel as well. This was effective at hiding everything that's behind it but he had to render his texture using UV instead of SCREEN_UV. As I found out (too late) that this made it really pixelated when you get up close to the mesh rendering it: https://www.youtube.com/watch?v=TxE91tWPZmU I tried enlarging the camera's viewport but it didn't even do anything to make the resolution better. It just lagged my computer. This got worse when I tried making his mirror demo bigger :(

On the other hand if I use SCREEN_UV instead, I would get a pretty clear image no mater how close I get (and for some reason, without the lag). But since anything you render in SCREEN_UV depends on where you and the mirror's camera is looking, your character's camera and mirror's camera has to be in sync with pitch and yaw (ie not being parallel to the mirror).

If I implemented this I'd end up back in square one. I can't implement frustum culling right when the near plane stays at a right angle from the camera's z-axis. This just clips thru the mirror when its camera turns far enough, as you can see: https://www.youtube.com/watch?v=UiWfSZGLuHw

The only thing that's stopping me now is the fact that I have no known means to change the angle of the near plane like how Sebastian Lague did in unity: https://www.youtube.com/watch?v=cWpFZbjtSQg&t=781s 12:49

Describe the feature / enhancement and how it helps to overcome the problem or limitation: If I had a way to change the near planes angle relative to the camera, I can keep it parallel to my portal as I move around it.

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams: Imagine this: I take a camera node and set it's near culling plane at a distance and when I go in the editor, right below the near property are two parameters: "X - angle" & "Y - angle" each would set the near(or far) plane at an angle to it's corresponding axis relative to the camera's line of sight (its Z-axis). Having control of the planes distance and angle would give full control of all possible positions within the rendering frustum.

another idea is to add the ability to align the near or far planes normals with an existing plane mesh. I'd guess this would be more efficient than calculating the angle of the plane to the cameras Z-axis in real time.

This makes it easier to implement culling even in angled shots. Plus you can do selective cross-sectional shots of scenes when you use the far plane with the near plane. like that one video about 4th dimensional toys: https://www.youtube.com/watch?v=0t4aKJuKP0Q or even selectively "load" stuff/scenes hidden outside the main scene just by adjusting the far plane

Sebastian Lague sited a paper by Eric Lengyel: http://www.terathon.com/lengyel/Lengyel-Oblique.pdf on how they did exactly that but in unity.

I just hope this actually helps cause I'm not that smart with any of this... Imma noob at game developing and that's why I love what you guys did to make it possible for me to start. I just really hope I don't need to stop now that I gotten this far.

If this enhancement will not be used often, can it be worked around with a few lines of script?: Unless there's already a feature that lets me mess with the camera's near plane angle that I'm not aware of (and without cracking NASA's 7th volume on spacial anomalies), I am stumped.

Is there a reason why this should be core and not an add-on in the asset library?: Honestly I have no idea. I mean I see that other engines support this kinda feature but they can't seem to do it with ease... might give you guys an edge if you can pull it off... plus it would make a great addition to 4.0 since its all about rendering right?

Astrono2 commented 3 years ago

I'm working on recreating Sebastian's portals in Godot as well. If you check how he angled the clipping plane, he used Oblique View Frustum Projection, which is achieved by modifying the camera's projection matrix. Adding this to the default camera would probably be bad for performance, and it messes up the depth texture if used.

Personally I'm gonna try to implement a custom camera in GDNative with an exposed CameraMatrix, but the current rendering system (both in 3.2 and master) make this pretty difficult, since the CameraMatrix used for rendering isn't stored anywhere and is instead generated on the fly by the VisualServerScene::render_camera method using the CameraMatrix::set_orthogonal, CameraMatrix::set_perspective or CameraMatrix::set_frustum methods respectively. So I'm gonna have to somehow bypass normal rendering if I want to make this work as an add-on and not a fork.

I think the best way to implement something like this in core would be to keep the Camera's CameraMatrix as an exposed variable and update it when changing the mode and other relevant variables. Or alternatively, there could be a "custom" mode besides perspective, orthogonal and frustum, that way the implementation would only add code, and wouldn't need to edit the existing logic in any way.

(The links point to the 3.2 code, but I've checked master and it's mostly the same) (also I have no idea how the ARVR rendering works but it probably makes it harder to implement in a non hacky way)

19PHOBOSS98 commented 3 years ago

@Astrono2 That's great! although it might be best to wait for Godot 4 if you're going to change rendering stuff. Saves you the trouble of rewriting anything... at least that's what I plan :)

SIsilicon commented 3 years ago

Instead of defining the oblique plane as an angle, it might be more intuitive to define it as a camera space plane; the same way Unity interprets it.

XavierH622 commented 1 year ago

I am working on a game that has portals as a Key game mechanic. I'd love to use Godot for this game but i cant until this feature is added. Just wanted to show my support.

Calinou commented 1 year ago

@XavierH622 Please don't bump issues without contributing significant new information. Use the :+1: reaction button on the first post instead.