Advertisement
Guest User

Near clipping plane calculation

a guest
Mar 19th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1.     // Check original author!!!
  2.     // https://github.com/SebLague/Portals/blob/master/Assets/Scripts/Core/Portal.cs
  3.     void SetNearClipPlane () {
  4.         // Learning resource:
  5.         // http://www.terathon.com/lengyel/Lengyel-Oblique.pdf
  6.         Transform clipPlane = transform;
  7.         int dot = System.Math.Sign (Vector3.Dot (clipPlane.forward, transform.position - portalCam.transform.position));
  8.  
  9.         Vector3 camSpacePos = portalCam.worldToCameraMatrix.MultiplyPoint (clipPlane.position);
  10.         Vector3 camSpaceNormal = portalCam.worldToCameraMatrix.MultiplyVector (clipPlane.forward) * dot;
  11.         float camSpaceDst = -Vector3.Dot (camSpacePos, camSpaceNormal) + nearClipOffset;
  12.  
  13.         // Don't use oblique clip plane if very close to portal as it seems this can cause some visual artifacts
  14.         if (Mathf.Abs (camSpaceDst) > nearClipLimit) {
  15.             Vector4 clipPlaneCameraSpace = new Vector4 (camSpaceNormal.x, camSpaceNormal.y, camSpaceNormal.z, camSpaceDst);
  16.  
  17.             // Update projection based on new clip plane
  18.             // Calculate matrix with player cam so that player camera settings (fov, etc) are used
  19.             portalCam.projectionMatrix = playerCam.CalculateObliqueMatrix (clipPlaneCameraSpace);
  20.         } else {
  21.             portalCam.projectionMatrix = playerCam.projectionMatrix;
  22.         }
  23.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement