Camera Frustum with Custom Near Plane (Get a Projection Matrix given NearClip Plane Vertices)
As my first Question on these forums, I'm quite pleased that it is about pretty advanced stuff. Thanks in advance for any advice or help you can give!
I have a secondary camera that needs to have exactly the same frustum as the Main camera (yet it will be in a different position and rotation), but I want to render only the parts that are beyond a 3D plane in world space (similar to the near clip plane of a normal frustum, but not parallel to the far clip plane).
I would like to 'cut off' the front part of the Camera's default frustum, to create a frustum as illustrated by the black 'Result Frustum' in the image below. I would like to create a Projection Matrix for this.
I've got the camera (and its position, rotation, etc), the plane (and its normal, etc), the four blue vectors coming from the Camera's position (which are in the direction of the outgoing edges of the default frustum), and the black points where the blue vectors intersect the plane, and I know the distance from the camera to the far clip plane.
Is it possible to set up a projection matrix for this sort of effect? If so, how?
I expect I'm just at the end of figuring it out, but I can't quite get it. I do not know much about Projection or Transformation Matrices. I've tried it with the example code here, but I think that assumes the near plane is parallel to the far plane, which is not what I want.
I think you would have to write some fancy custom stuff to create a working projection matrix with a non-parallel near plane, because as far as i know there is a division of the x and y parts with the z parts of the near/far plane to compute the w parts of the corresponding matrix (i may be wrong, because its a long time ago i learned this stuff :D).
Couldnt you achive that what you want with a shader and/or layers?
Well, I am trying to make a game similar to the classic game Portal, so I want the secondary portal cameras to look exactly like the main camera (use the same clipping layers, FOV, etc), so I'm not sure how using layers would work without losing quality or having to recreate the entire scene in multiple layers. Also, I'm not very good with shaders.
I am working on the exact same problem. Did you find a solution?
Negative. The closest I have come to a good solution is this unity portal effect by Adaszewski. It uses z-index shaders ins$$anonymous$$d of changing the camera frustum, which is nice, but it does not account for skyboxes, near clipping plane, etc., so it is far from perfect (and the teleportation equations are messed up).
I will post more, if and when I find my own solution to creating Portals in Unity.
Answer by ElijahShadbolt · Nov 23, 2017 at 02:00 AM
Quick update:
Tom Hulton-Harrop successfully solved this problem in 2015, in his blog post Portal Rendering with Offscreen Render Targets. He used shaders, render textures, and oblique projection matrices.