- Home /
Question by
mastersmit · Jan 15, 2020 at 07:33 AM ·
cameramatrix4x4
Fix camera projection matrix
Hey. I make an analogue of the game Portal. In general, drawing portals works correctly. But, when the player’s camera is very close to the portal, something like Z-fighting occurs. How can this be fixed? For several days I can’t solve this. Watch it on youtube - https://youtu.be/tVYeXTFM8Jk
I set the projection matrix by code:
Vector3 pos = targetPortal.transform.position; // position of another portal plane
Vector3 normal = targetPortal.transform.forward; // normal of another portal plane
Vector4 clipPlane = PortalUtils.CameraSpacePlane(Cams[i], pos, normal, ClipPlaneOffset, 1.0f);
Matrix4x4 projection = CameraDefault.CalculateObliqueMatrix(clipPlane);
Cams[i].projectionMatrix = projection;
CameraSpacePlane metod code:
public static Vector4 CameraSpacePlane(Camera cam, Vector3 pos, Vector3 normal, float ClipPlaneOffset, float sideSign)
{
Vector3 offsetPos = pos + (normal * ClipPlaneOffset);
Matrix4x4 m = cam.worldToCameraMatrix;
Vector3 cpos = m.MultiplyPoint(offsetPos);
Vector3 cnormal = m.MultiplyVector(-normal) * sideSign;
return new Vector4(cnormal.x, cnormal.y, cnormal.z, Vector3.Dot(-cpos, cnormal));
}
Help pls =)
Comment
Your answer

Follow this Question
Related Questions
How do I calculate a view matrix using Matrix4x4.LookAt? 2 Answers
Calculating an oblique clipping matrix / making occlusion matrix equal to projection matrix? 0 Answers
What do the values in the Matrix4x4 for Camera.projectionMatrix do? 1 Answer
Flipping projection matrix problem: It flips everything but the Skybox. How do I fix this? 1 Answer