- Home /
Match camera frustum's near plane to 3D plane
I've seen this question asked before, specifically this question is pretty close to mine but I couldn't find a definite answer about it and I actually need it myself now so I'll have to ask again, sorry if this was actually answered somewhere else but I couldn't find anything about it that suited my needs.
I'm trying to recreate the portals from the Portal game in Unity, and so far thanks to this blog post I've managed to create a pretty close effect for the visual part. The portal camera basically mimics the player camera's movements behind the destination portal, renders the result to a rendertexture and then a shader cuts off a portion of that texture and applies it to the portal mesh (or at least I think so, I don't quite understand shaders but this is what I got out of the blog post).
What happens though is that since portals are placed on walls, the camera will actually render the wall unless I get sufficently close to the portal. By looking around the github project I've found a way of handling the nearclip plane for the portal camera such that the camera will only start rendering from the portal's position onwards, which works. Kind of.
Because the camera's near clip plane is not always parallel to the portal's plane, getting far enough from a portal and looking at it at specific angles will start cutting out portions of the world inside the portal, as you can see here:
I can't really come up with a solution for this except for changing the orientation of the near clip plane to match it with the portal plane, basically I'm looking for a way of "cutting" the camera's frustum at the angle and position of the portal plane, kinda like this:
I've tried playing around with the camera matrix but I couldn't find any configuration that properly moved the near clip plane to the correct rotation, scale and position, but I'm sure I'm missing something here. Is there anything that can be done for this?
Answer by Lextredis · Dec 27, 2016 at 12:32 PM
Ok guys, I fixed it by looking through the MirrorReflection3 script, the functions "`Vector4 CameraSpacePlane( ... )`" and "`static void CalculateObliqueMatrix( ... )`" did it perfectly, I just had to pass the other portal's position and normal as parameters for the first function (making sure the z axis is pointing outwards), then pass the player camera's matrix and the resulting Vector4 to the second function and then apply the resulting matrix to the portal's camera, so if anyone needs it, the link to the script is here