Hey guys
I’ve managed to apply an oblique projection matrix to my game camera and have it clip everything behind a plane. I will be using this for a mirror effect.
Everything works fine except when I rotate the camera around X. I have hard coded the clipping plane - Vector3.zero is a point on the plane and Vector3.forward is the planes normal. Here is what it looks like - you can see me rotate around the Y axis just fine, but when I try to rotate around the X axis the clipping plane moves forwards and backwards.
Here is my script. I think maybe something is wrong with the way I calculate the the camera space plane?
using UnityEngine;
using System.Collections;
using System.Reflection;
public class ClipTest : MonoBehaviour
{
void Update()
{
Camera.main.ResetProjectionMatrix ();
Matrix4x4 obliqueProjection = Camera.main.projectionMatrix;
Vector4 cameraSpaceClipPlane = CameraSpacePlane(Camera.main, Vector3.zero, Vector3.forward, 1.0f,0);
Camera.main.projectionMatrix = Camera.main.CalculateObliqueMatrix(cameraSpaceClipPlane);
}
static Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float sideSign, float val)
{
Vector3 offsetPos = pos + normal * val;
Matrix4x4 m = cam.worldToCameraMatrix;
Vector3 cpos = m.MultiplyPoint(offsetPos);
Vector3 point = m.inverse.MultiplyPoint(new Vector3(0.0f, 0.0f, 0.0f));
cpos -= new Vector3(0.0f, point.y, 0.0f);
Vector3 cnormal = m.MultiplyVector( normal ).normalized * sideSign;
return new Vector4(cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos,cnormal));
}
}