public static Matrix4x4 GetProjectionMatrix(this Camera camera, float texelOffsetX, float texelOffsetY) { if (camera == null) { return(Matrix4x4.identity); } Vector4 extents = GetProjectionExtents(camera, texelOffsetX, texelOffsetY); float cf = camera.farClipPlane; float cn = camera.nearClipPlane; float xm = extents.z - extents.x; float xp = extents.z + extents.x; float ym = extents.w - extents.y; float yp = extents.w + extents.y; if (camera.orthographic) { return(Matrix4x4Extension.GetOrthographicProjection(xm, xp, ym, yp, cn, cf)); } else { return(Matrix4x4Extension.GetPerspectiveProjection(xm * cn, xp * cn, ym * cn, yp * cn, cn, cf)); } }
public static Matrix4x4 GetPerspectiveProjection(this Camera camera, float texelOffsetX, float texelOffsetY) { if (camera == null) { return(Matrix4x4.identity); } float oneExtentY = Mathf.Tan(0.5f * Mathf.Deg2Rad * camera.fieldOfView); float oneExtentX = oneExtentY * camera.aspect; float texelSizeX = oneExtentX / (0.5f * camera.pixelWidth); float texelSizeY = oneExtentY / (0.5f * camera.pixelHeight); float oneJitterX = texelSizeX * texelOffsetX; float oneJitterY = texelSizeY * texelOffsetY; float cf = camera.farClipPlane; float cn = camera.nearClipPlane; float xm = (oneJitterX - oneExtentX) * cn; float xp = (oneJitterX + oneExtentX) * cn; float ym = (oneJitterY - oneExtentY) * cn; float yp = (oneJitterY + oneExtentY) * cn; return(Matrix4x4Extension.GetPerspectiveProjection(xm, xp, ym, yp, cn, cf)); }